Skip to content

Commit c4c0c78

Browse files
authored
Add use-sni configuration option (#53)
1 parent e4970ac commit c4c0c78

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎aproxy.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func RelayHTTP(conn io.ReadWriter, proxyConn io.ReadWriteCloser, logger *slog.Lo
341341
}
342342

343343
// HandleConn manages the incoming connections.
344-
func HandleConn(conn net.Conn, proxy string) {
344+
func HandleConn(conn net.Conn, proxy string, useSNI bool) {
345345
defer conn.Close()
346346
logger := slog.With("src", conn.RemoteAddr())
347347
dst, err := GetOriginalDst(conn.(*net.TCPConn))
@@ -351,8 +351,8 @@ func HandleConn(conn net.Conn, proxy string) {
351351
}
352352
logger = logger.With("original_dst", dst)
353353
consigned := NewPrereadConn(conn)
354-
switch dst.Port {
355-
case 443:
354+
switch {
355+
case dst.Port == 443 && useSNI:
356356
sni, err := PrereadSNI(consigned)
357357
if err != nil {
358358
logger.Error("failed to preread SNI from connection", "error", err)
@@ -371,7 +371,7 @@ func HandleConn(conn net.Conn, proxy string) {
371371
}
372372
logger.Info("relay TLS connection to proxy")
373373
RelayTCP(consigned, proxyConn, logger)
374-
case 80, 11371:
374+
case dst.Port == 80 || dst.Port == 11371:
375375
host, err := PrereadHttpHost(consigned)
376376
if err != nil {
377377
logger.Error("failed to preread HTTP host from connection", "error", err)
@@ -404,6 +404,7 @@ func HandleConn(conn net.Conn, proxy string) {
404404
func main() {
405405
proxyFlag := flag.String("proxy", "", "upstream proxy address in the 'host:port' format")
406406
listenFlag := flag.String("listen", ":8443", "the address and port on which the server will listen")
407+
useSNIFlag := flag.Bool("use-sni", true, "preread TLS SNI on port 443; if false, CONNECT to the original destination IP:port")
407408
flag.Parse()
408409
listenAddr := *listenFlag
409410
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
@@ -426,7 +427,7 @@ func main() {
426427
slog.Error("failed to accept connection", "error", err)
427428
continue
428429
}
429-
go HandleConn(conn, proxy)
430+
go HandleConn(conn, proxy, *useSNIFlag)
430431
}
431432
}()
432433
<-ctx.Done()

‎snap/hooks/configure‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,18 @@ fi
3434

3535
validate_proxy "$proxy"
3636

37-
echo "--proxy $proxy --listen $listen" > $SNAP_DATA/args
37+
args="--proxy $proxy --listen $listen"
38+
use_sni="$(snapctl get use-sni)"
39+
case "$use_sni" in
40+
"") ;;
41+
true|false) args="$args --use-sni=$use_sni" ;;
42+
*)
43+
echo "invalid use-sni: '$use_sni' (must be true or false)"
44+
exit 1
45+
;;
46+
esac
47+
48+
echo "$args" > $SNAP_DATA/args
3849

3950
snapctl stop ${SNAP_NAME}.aproxy
4051
snapctl start ${SNAP_NAME}.aproxy --enable

0 commit comments

Comments
 (0)