diff --git a/socks5.go b/socks5.go index a17be68..a8269d8 100644 --- a/socks5.go +++ b/socks5.go @@ -50,7 +50,7 @@ type Config struct { Dial func(ctx context.Context, network, addr string) (net.Conn, error) } -// Server is reponsible for accepting connections and handling +// Server is responsible for accepting connections and handling // the details of the SOCKS5 protocol type Server struct { config *Config @@ -107,14 +107,21 @@ func (s *Server) ListenAndServe(network, addr string) error { // Serve is used to serve connections from a listener func (s *Server) Serve(l net.Listener) error { + errChan := make(chan error) for { conn, err := l.Accept() if err != nil { return err } - go s.ServeConn(conn) + go func(net.Conn) { + if err := s.ServeConn(conn); err != nil { + errChan <- err + } else { + errChan <- nil + } + }(conn) + return <-errChan } - return nil } // ServeConn is used to serve a single connection.