@@ -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) {
404404func 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 ()
0 commit comments