@@ -21,7 +21,6 @@ import (
21
21
type Handler = N.TCPConnectionHandler
22
22
23
23
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
25
24
for {
26
25
request , err := ReadRequest (reader )
27
26
if err != nil {
@@ -95,28 +94,26 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
95
94
}
96
95
97
96
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
115
112
},
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
+ },
120
117
}
121
118
122
119
response , err := httpClient .Do (request )
@@ -139,6 +136,8 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
139
136
return E .Errors (innerErr , err )
140
137
}
141
138
139
+ httpClient .CloseIdleConnections ()
140
+
142
141
if ! keepAlive {
143
142
return conn .Close ()
144
143
}
0 commit comments