Fix leak in server acceptLoop

Refactor out a separate handleStream function and ensure that all
connections are closed and the references are out of scope.
This commit is contained in:
Cecylia Bocovich 2021-06-24 09:33:19 -04:00
parent 10b6075eaa
commit 53a2365696

View file

@ -41,7 +41,7 @@ additional HTTP listener on port 80 to work with ACME.
flag.PrintDefaults() flag.PrintDefaults()
} }
// Copy from one stream to another. //proxy copies data bidirectionally from one connection to another.
func proxy(local *net.TCPConn, conn net.Conn) { func proxy(local *net.TCPConn, conn net.Conn) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Add(2)
@ -66,6 +66,20 @@ func proxy(local *net.TCPConn, conn net.Conn) {
wg.Wait() wg.Wait()
} }
//handleConn bidirectionally connects a client snowflake connection with an ORPort.
func handleConn(conn net.Conn) error {
addr := conn.RemoteAddr().String()
statsChannel <- addr != ""
or, err := pt.DialOr(&ptInfo, addr, ptMethodName)
if err != nil {
return fmt.Errorf("failed to connect to ORPort: %s", err)
}
defer or.Close()
proxy(or, conn)
return nil
}
//acceptLoop accepts incoming client snowflake connection and passes them to a handler function.
func acceptLoop(ln net.Listener) { func acceptLoop(ln net.Listener) {
for { for {
conn, err := ln.Accept() conn, err := ln.Accept()
@ -76,17 +90,13 @@ func acceptLoop(ln net.Listener) {
log.Printf("Snowflake accept error: %s", err) log.Printf("Snowflake accept error: %s", err)
break break
} }
go func() {
defer conn.Close() defer conn.Close()
err := handleConn(conn)
addr := conn.RemoteAddr().String()
statsChannel <- addr != ""
or, err := pt.DialOr(&ptInfo, addr, ptMethodName)
if err != nil { if err != nil {
log.Printf("failed to connect to ORPort: %s", err) log.Printf("handleConn: %v", err)
continue
} }
defer or.Close() }()
go proxy(or, conn)
} }
} }