@@ -40,9 +40,10 @@ import (
4040)
4141
4242const (
43- readTimeout time.Duration = 3 * time .Second
44- writeTimeout time.Duration = 3 * time .Second
43+ readTimeout time.Duration = 30 * time .Second
44+ writeTimeout time.Duration = 30 * time .Second
4545 timeout time.Duration = 10 * time .Second
46+ shutdownTimeout time.Duration = 30 * time .Second
4647 hopTimeout time.Duration = 3 * time .Second
4748 flushTimeout time.Duration = 10 * time .Millisecond
4849 availProxyUpdateInterval time.Duration = 30 * time .Second
@@ -589,12 +590,15 @@ func (p *proxyapp) updateSocksList() {
589590 if err != nil && ! errors .Is (err , io .EOF ) { // check for EOF to include localhost SOCKS5 in the chain
590591 p .logger .Error ().Err (err ).Msgf ("%s Unable to connect to %s" , ctl , pr .Address )
591592 failed ++
593+ if conn != nil {
594+ conn .Close ()
595+ }
592596 continue
593597 } else {
598+ p .availProxyList = append (p .availProxyList , proxyEntry {Address : pr .Address , Username : pr .Username , Password : pr .Password })
594599 if conn != nil {
595600 conn .Close ()
596601 }
597- p .availProxyList = append (p .availProxyList , proxyEntry {Address : pr .Address , Username : pr .Username , Password : pr .Password })
598602 break
599603 }
600604 }
@@ -619,6 +623,9 @@ func (p *proxyapp) updateSocksList() {
619623 conn , err := dialer .(proxy.ContextDialer ).DialContext (ctx , "tcp" , pr .Address )
620624 if err != nil {
621625 p .logger .Error ().Err (err ).Msgf ("%s Unable to connect to %s" , ctl , pr .Address )
626+ if conn != nil {
627+ conn .Close ()
628+ }
622629 continue
623630 }
624631 conn .Close ()
@@ -1094,13 +1101,19 @@ readLoop:
10941101 default :
10951102 er := src .SetReadDeadline (time .Now ().Add (readTimeout ))
10961103 if er != nil {
1104+ if errors .Is (er , net .ErrClosed ) {
1105+ break readLoop
1106+ }
10971107 err = er
10981108 break readLoop
10991109 }
11001110 nr , er := src .Read (buf )
11011111 if nr > 0 {
11021112 er := dst .SetWriteDeadline (time .Now ().Add (writeTimeout ))
11031113 if er != nil {
1114+ if errors .Is (er , net .ErrClosed ) {
1115+ break readLoop
1116+ }
11041117 err = er
11051118 break readLoop
11061119 }
@@ -1120,7 +1133,9 @@ readLoop:
11201133 written += int64 (nw )
11211134 if ew != nil {
11221135 if ne , ok := ew .(net.Error ); ok && ne .Timeout () {
1123- err = ne
1136+ break readLoop
1137+ }
1138+ if errors .Is (ew , net .ErrClosed ) {
11241139 break readLoop
11251140 }
11261141 }
@@ -1130,13 +1145,17 @@ readLoop:
11301145 }
11311146 }
11321147 if er != nil {
1133- if ne , ok := err .(net.Error ); ok && ne .Timeout () {
1134- err = er
1148+ if ne , ok := er .(net.Error ); ok && ne .Timeout () {
1149+ break readLoop
1150+ }
1151+ if errors .Is (er , net .ErrClosed ) {
11351152 break readLoop
11361153 }
11371154 if er == io .EOF {
11381155 break readLoop
11391156 }
1157+ err = er
1158+ break readLoop
11401159 }
11411160 }
11421161 }
@@ -1158,7 +1177,10 @@ func (p *proxyapp) transfer(
11581177 if err != nil {
11591178 p .logger .Error ().Err (err ).Msgf ("Error during copy from %s to %s: %v" , srcName , destName , err )
11601179 }
1161- p .logger .Debug ().Msgf ("copied %s from %s to %s" , prettifyBytes (n ), srcName , destName )
1180+ if n > 0 {
1181+ p .logger .Debug ().Msgf ("copied %s from %s to %s" , prettifyBytes (n ), srcName , destName )
1182+ }
1183+ src .Close ()
11621184}
11631185
11641186func parseProxyAuth (auth string ) (username , password string , ok bool ) {
@@ -1527,7 +1549,7 @@ func (p *proxyapp) Run() {
15271549 tproxyServer .Shutdown ()
15281550 }
15291551 p .logger .Info ().Msg ("Server is shutting down..." )
1530- ctx , cancel := context .WithTimeout (context .Background (), timeout )
1552+ ctx , cancel := context .WithTimeout (context .Background (), shutdownTimeout )
15311553
15321554 defer cancel ()
15331555 p .httpServer .SetKeepAlivesEnabled (false )
0 commit comments