Skip to content

Commit e1a0e5b

Browse files
fixed connections leaks, now debug mode of arpspoffing is correctly separated from main app
1 parent 8ba1d3c commit e1a0e5b

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ You can download the binary for your platform from [Releases](https://github.com
101101
Example:
102102

103103
```shell
104-
HPTS_RELEASE=v1.9.0; wget -v https://github.com/shadowy-pycoder/go-http-proxy-to-socks/releases/download/$HPTS_RELEASE/gohpts-$HPTS_RELEASE-linux-amd64.tar.gz -O gohpts && tar xvzf gohpts && mv -f gohpts-$HPTS_RELEASE-linux-amd64 gohpts && ./gohpts -h
104+
HPTS_RELEASE=v1.9.1; wget -v https://github.com/shadowy-pycoder/go-http-proxy-to-socks/releases/download/$HPTS_RELEASE/gohpts-$HPTS_RELEASE-linux-amd64.tar.gz -O gohpts && tar xvzf gohpts && mv -f gohpts-$HPTS_RELEASE-linux-amd64 gohpts && ./gohpts -h
105105
```
106106

107107
Alternatively, you can install it using `go install` command (requires Go [1.24](https://go.dev/doc/install) or later):

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/google/uuid v1.6.0
88
github.com/rs/zerolog v1.34.0
99
github.com/shadowy-pycoder/colors v0.0.1
10-
github.com/shadowy-pycoder/mshark v0.0.7
10+
github.com/shadowy-pycoder/mshark v0.0.8
1111
golang.org/x/net v0.40.0
1212
golang.org/x/sys v0.33.0
1313
golang.org/x/term v0.32.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
3030
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
3131
github.com/shadowy-pycoder/colors v0.0.1 h1:weCj/YIOupqy4BSP8KuVzr20fC+cuAv/tArz7bhhkP4=
3232
github.com/shadowy-pycoder/colors v0.0.1/go.mod h1:lkrJS1PY2oVigNLTT6pkbF7B/v0YcU2LD5PZnss1Q4U=
33-
github.com/shadowy-pycoder/mshark v0.0.7 h1:iuCLxKXh0HhukrZOQIbXjGneTTOZSPIS0mkIBSJm/4U=
34-
github.com/shadowy-pycoder/mshark v0.0.7/go.mod h1:FqbHFdsx0zMnrZZH0+oPzaFcleP4O+tUWv8i5gxo87k=
33+
github.com/shadowy-pycoder/mshark v0.0.8 h1:7kuVgX9Qp4Q9nGl9Gi7UOaNFUnOF0I2Vpfmc4X3GLug=
34+
github.com/shadowy-pycoder/mshark v0.0.8/go.mod h1:FqbHFdsx0zMnrZZH0+oPzaFcleP4O+tUWv8i5gxo87k=
3535
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
3636
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3737
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=

gohpts.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ import (
4040
)
4141

4242
const (
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

11641186
func 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)

tproxy_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (ts *tproxyServer) serve() {
7979
case <-ts.quit:
8080
return
8181
default:
82-
ts.pa.logger.Error().Err(err).Msg("")
82+
ts.pa.logger.Error().Err(err).Msg("Failed accepting connection")
8383
}
8484
} else {
8585
ts.wg.Add(1)
@@ -237,7 +237,7 @@ func (ts *tproxyServer) Shutdown() {
237237
case <-done:
238238
ts.pa.logger.Info().Msgf("[%s] Server gracefully shutdown", ts.pa.tproxyMode)
239239
return
240-
case <-time.After(timeout):
240+
case <-time.After(shutdownTimeout):
241241
ts.pa.logger.Error().Msgf("[%s] Server timed out waiting for connections to finish", ts.pa.tproxyMode)
242242
return
243243
}

0 commit comments

Comments
 (0)