Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions aproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func RelayHTTP(conn io.ReadWriter, proxyConn io.ReadWriteCloser, logger *slog.Lo
}

// HandleConn manages the incoming connections.
func HandleConn(conn net.Conn, proxy string) {
func HandleConn(conn net.Conn, proxy string, useSNI bool) {
defer conn.Close()
logger := slog.With("src", conn.RemoteAddr())
dst, err := GetOriginalDst(conn.(*net.TCPConn))
Expand All @@ -351,8 +351,8 @@ func HandleConn(conn net.Conn, proxy string) {
}
logger = logger.With("original_dst", dst)
consigned := NewPrereadConn(conn)
switch dst.Port {
case 443:
switch {
case dst.Port == 443 && useSNI:
sni, err := PrereadSNI(consigned)
Comment thread
weiiwang01 marked this conversation as resolved.
if err != nil {
logger.Error("failed to preread SNI from connection", "error", err)
Expand All @@ -371,7 +371,7 @@ func HandleConn(conn net.Conn, proxy string) {
}
logger.Info("relay TLS connection to proxy")
RelayTCP(consigned, proxyConn, logger)
case 80, 11371:
case dst.Port == 80 || dst.Port == 11371:
host, err := PrereadHttpHost(consigned)
if err != nil {
logger.Error("failed to preread HTTP host from connection", "error", err)
Expand Down Expand Up @@ -404,6 +404,7 @@ func HandleConn(conn net.Conn, proxy string) {
func main() {
proxyFlag := flag.String("proxy", "", "upstream proxy address in the 'host:port' format")
listenFlag := flag.String("listen", ":8443", "the address and port on which the server will listen")
useSNIFlag := flag.Bool("use-sni", true, "preread TLS SNI on port 443; if false, CONNECT to the original destination IP:port")
flag.Parse()
listenAddr := *listenFlag
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
Expand All @@ -426,7 +427,7 @@ func main() {
slog.Error("failed to accept connection", "error", err)
continue
}
go HandleConn(conn, proxy)
go HandleConn(conn, proxy, *useSNIFlag)
}
}()
<-ctx.Done()
Expand Down
13 changes: 12 additions & 1 deletion snap/hooks/configure
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ fi

validate_proxy "$proxy"

echo "--proxy $proxy --listen $listen" > $SNAP_DATA/args
args="--proxy $proxy --listen $listen"
use_sni="$(snapctl get use-sni)"
case "$use_sni" in
"") ;;
true|false) args="$args --use-sni=$use_sni" ;;
Comment thread
weiiwang01 marked this conversation as resolved.
*)
echo "invalid use-sni: '$use_sni' (must be true or false)"
exit 1
;;
esac

echo "$args" > $SNAP_DATA/args

snapctl stop ${SNAP_NAME}.aproxy
snapctl start ${SNAP_NAME}.aproxy --enable
Loading