func performConnect(backend string, frontconn io.ReadWriter) { log.Printf("trying to connect to %s...\n", backend) backconn, err := net.Dial("tcp", backend) if err != nil { log.Printf("failed to connect to %s: %s\n", backend, err) frontconn.Write(errorReplyConnect(0x05)) return } backaddr := backconn.RemoteAddr().String() log.Println("CONNECTED backend", backconn, backaddr) defer func() { backconn.Close() log.Println("DISCONNECTED backend", backconn, backaddr) }() // reply to the CONNECT command buf := make([]byte, 10) copy(buf, []byte{0x05, 0x00, 0x00, 0x01}) packNetAddr(backconn.RemoteAddr(), buf[4:]) frontconn.Write(buf) // bridge connection shutdown := make(chan bool, 2) go common.IOBridge(frontconn, backconn, shutdown) go common.IOBridge(backconn, frontconn, shutdown) <-shutdown }
func performConnect(frontconn, backconn io.ReadWriter) { shutdown := make(chan bool, 2) go common.IOBridge(frontconn, backconn, shutdown) go common.IOBridge(backconn, frontconn, shutdown) // wait for either side to close <-shutdown }