Skip to content

Commit e0ec961

Browse files
committed
Fix HTTP server leak
1 parent e727641 commit e0ec961

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

protocol/http/handshake.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
type Handler = N.TCPConnectionHandler
2222

2323
func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Reader, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error {
24-
var httpClient *http.Client
2524
for {
2625
request, err := ReadRequest(reader)
2726
if err != nil {
@@ -95,28 +94,26 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
9594
}
9695

9796
var innerErr error
98-
if httpClient == nil {
99-
httpClient = &http.Client{
100-
Transport: &http.Transport{
101-
DisableCompression: true,
102-
DialContext: func(context context.Context, network, address string) (net.Conn, error) {
103-
metadata.Destination = M.ParseSocksaddr(address)
104-
metadata.Protocol = "http"
105-
input, output := net.Pipe()
106-
go func() {
107-
hErr := handler.NewConnection(ctx, output, metadata)
108-
if hErr != nil {
109-
innerErr = hErr
110-
common.Close(input, output)
111-
}
112-
}()
113-
return input, nil
114-
},
97+
httpClient := &http.Client{
98+
Transport: &http.Transport{
99+
DisableCompression: true,
100+
DialContext: func(context context.Context, network, address string) (net.Conn, error) {
101+
metadata.Destination = M.ParseSocksaddr(address)
102+
metadata.Protocol = "http"
103+
input, output := net.Pipe()
104+
go func() {
105+
hErr := handler.NewConnection(ctx, output, metadata)
106+
if hErr != nil {
107+
innerErr = hErr
108+
common.Close(input, output)
109+
}
110+
}()
111+
return input, nil
115112
},
116-
CheckRedirect: func(req *http.Request, via []*http.Request) error {
117-
return http.ErrUseLastResponse
118-
},
119-
}
113+
},
114+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
115+
return http.ErrUseLastResponse
116+
},
120117
}
121118

122119
response, err := httpClient.Do(request)
@@ -139,6 +136,8 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
139136
return E.Errors(innerErr, err)
140137
}
141138

139+
httpClient.CloseIdleConnections()
140+
142141
if !keepAlive {
143142
return conn.Close()
144143
}

0 commit comments

Comments
 (0)