@@ -126,10 +126,6 @@ func (dps *DomainProxyServer) handleConnectionRequest(domainConnection net.Conn)
126126 return
127127 }
128128 writer := & responseWriter {connection : domainConnection }
129- if err = domainConnection .SetDeadline (time .Now ().Add (sharedParams .IdleTimeout )); err != nil {
130- common .HandleSetDeadlineError (domainConnection , err )
131- return
132- }
133129 if request .Method == http .MethodConnect {
134130 dps .handleHttpsConnection (domainConnection , writer , request )
135131 } else {
@@ -173,25 +169,31 @@ func (dps *DomainProxyServer) handleHttpConnection(sourceConnection net.Conn, wr
173169 common .HandleConnectionCloseError (err )
174170 }
175171 return
172+ }
173+ if err = targetConnection .SetDeadline (time .Now ().Add (sharedParams .IdleTimeout )); err != nil {
174+ common .HandleSetDeadlineError (targetConnection , err )
175+ if err = sourceConnection .Close (); err != nil {
176+ common .HandleConnectionCloseError (err )
177+ }
178+ return
179+ }
180+ if useInternalProxy {
181+ err = request .WriteProxy (targetConnection )
176182 } else {
177- if useInternalProxy {
178- err = request .WriteProxy (targetConnection )
179- } else {
180- err = request .Write (targetConnection )
183+ err = request .Write (targetConnection )
184+ }
185+ if err != nil {
186+ dps .handleErrorResponse (writer , err , fmt .Sprintf ("Failed to send request to %s" , targetConnectionName ), false )
187+ if err = targetConnection .Close (); err != nil {
188+ common .HandleConnectionCloseError (err )
181189 }
182- if err != nil {
183- dps .handleErrorResponse (writer , err , fmt .Sprintf ("Failed to send request to %s" , targetConnectionName ), false )
184- if err = targetConnection .Close (); err != nil {
185- common .HandleConnectionCloseError (err )
186- }
187- if err = sourceConnection .Close (); err != nil {
188- common .HandleConnectionCloseError (err )
189- }
190- return
190+ if err = sourceConnection .Close (); err != nil {
191+ common .HandleConnectionCloseError (err )
191192 }
193+ return
192194 }
193195 go func () {
194- common .BiDirectionalTransfer (dps .runningContext , sourceConnection , targetConnection , sharedParams .ByteBufferSize , sharedParams . IdleTimeout , DomainSocketToHttp , connectionNo )
196+ common .BiDirectionalTransfer (dps .runningContext , sourceConnection , targetConnection , sharedParams .ByteBufferSize , DomainSocketToHttp , connectionNo )
195197 logger .Printf ("%s Connection %d ended after %d ms" , DomainSocketToHttp , connectionNo , time .Since (startTime ).Milliseconds ())
196198 }()
197199}
@@ -227,6 +229,13 @@ func (dps *DomainProxyServer) handleHttpsConnection(sourceConnection net.Conn, w
227229 }
228230 return
229231 }
232+ if err = targetConnection .SetDeadline (time .Now ().Add (sharedParams .IdleTimeout )); err != nil {
233+ common .HandleSetDeadlineError (targetConnection , err )
234+ if err = sourceConnection .Close (); err != nil {
235+ common .HandleConnectionCloseError (err )
236+ }
237+ return
238+ }
230239 if useInternalProxy {
231240 proxyConnectRequest := fmt .Sprintf ("CONNECT %s:%d HTTP/1.1\r \n Host: %s:%d\r \n Connection: close\r \n " , actualTargetHost , actualTargetPort , actualTargetHost , actualTargetPort )
232241 if dps .internalProxyUser != "" && dps .internalProxyPassword != "" {
@@ -279,7 +288,7 @@ func (dps *DomainProxyServer) handleHttpsConnection(sourceConnection net.Conn, w
279288 return
280289 }
281290 go func () {
282- common .BiDirectionalTransfer (dps .runningContext , sourceConnection , targetConnection , sharedParams .ByteBufferSize , sharedParams . IdleTimeout , DomainSocketToHttps , connectionNo )
291+ common .BiDirectionalTransfer (dps .runningContext , sourceConnection , targetConnection , sharedParams .ByteBufferSize , DomainSocketToHttps , connectionNo )
283292 logger .Printf ("%s Connection %d ended after %d ms" , DomainSocketToHttps , connectionNo , time .Since (startTime ).Milliseconds ())
284293 }()
285294}
0 commit comments