Skip to content

Commit 496f8e9

Browse files
fixed more race conditions, improved logging
1 parent 0dcb616 commit 496f8e9

File tree

6 files changed

+242
-128
lines changed

6 files changed

+242
-128
lines changed

colorize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818

1919
var (
2020
ipPortPattern = regexp.MustCompile(
21-
`\b(?:\d{1,3}\.){3}\d{1,3}(?::(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]?\d{1,4}))?\b`,
21+
`(?:\[(?:[0-9a-fA-F:.]+)\]|(?:\d{1,3}\.){3}\d{1,3})(?::(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]?\d{1,4}))?`,
2222
)
2323
domainPattern = regexp.MustCompile(
2424
`\b(?:[a-zA-Z0-9-]{1,63}\.)+(?:com|net|org|io|co|uk|ru|de|edu|gov|info|biz|dev|app|ai)(?::(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]?\d{1,4}))?\b`,

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.10
10+
github.com/shadowy-pycoder/mshark v0.0.12
1111
github.com/wzshiming/socks5 v0.5.2
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.10 h1:pLMIsgfvnO0oKeBNdy0fTGQsx//6scCPT52g93CqyT4=
34-
github.com/shadowy-pycoder/mshark v0.0.10/go.mod h1:FqbHFdsx0zMnrZZH0+oPzaFcleP4O+tUWv8i5gxo87k=
33+
github.com/shadowy-pycoder/mshark v0.0.12 h1:1zPAQLhKu1pxAZTm5oqUahywJ7SIpx9ds9/ytz5TwaU=
34+
github.com/shadowy-pycoder/mshark v0.0.12/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
github.com/wzshiming/socks5 v0.5.2 h1:LtoowVNwAmkIQSkP1r1Wg435xUmC+tfRxorNW30KtnM=

helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func createSysctlOptCmd(opt, value, setex string, opts map[string]string, debug
199199
`, setex, opt, value))
200200
cmd.Stdout = os.Stdout
201201
cmd.Stderr = os.Stderr
202-
if debug {
202+
if !debug {
203203
cmd.Stdout = nil
204204
}
205205
return cmd

tproxy_linux.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"os/exec"
1414
"sync"
15+
"sync/atomic"
1516
"syscall"
1617
"time"
1718
"unsafe"
@@ -22,10 +23,11 @@ import (
2223
)
2324

2425
type tproxyServer struct {
25-
listener net.Listener
26-
quit chan struct{}
27-
wg sync.WaitGroup
28-
p *proxyapp
26+
listener net.Listener
27+
quit chan struct{}
28+
wg sync.WaitGroup
29+
p *proxyapp
30+
startingFlag atomic.Bool
2931
}
3032

3133
func newTproxyServer(p *proxyapp) *tproxyServer {
@@ -63,8 +65,10 @@ func newTproxyServer(p *proxyapp) *tproxyServer {
6365
}
6466

6567
func (ts *tproxyServer) ListenAndServe() {
68+
ts.startingFlag.Store(true)
6669
ts.wg.Add(1)
6770
go ts.serve()
71+
ts.startingFlag.Store(false)
6872
}
6973

7074
func (ts *tproxyServer) serve() {
@@ -146,10 +150,8 @@ func (ts *tproxyServer) handleConnection(srcConn net.Conn) {
146150
ts.p.logger.Error().Err(err).Msgf("[tcp %s] Failed to get destination address", ts.p.tproxyMode)
147151
return
148152
}
149-
ts.p.logger.Debug().Msgf("[tcp %s] getsockopt SO_ORIGINAL_DST %s", ts.p.tproxyMode, dst)
150153
case "tproxy":
151154
dst = srcConn.LocalAddr().String()
152-
ts.p.logger.Debug().Msgf("[tcp %s] IP_TRANSPARENT %s", ts.p.tproxyMode, dst)
153155
default:
154156
ts.p.logger.Fatal().Msg("Unknown tproxyMode")
155157
}
@@ -220,6 +222,9 @@ func (ts *tproxyServer) handleConnection(srcConn net.Conn) {
220222
}
221223

222224
func (ts *tproxyServer) Shutdown() {
225+
for ts.startingFlag.Load() {
226+
time.Sleep(50 * time.Millisecond)
227+
}
223228
close(ts.quit)
224229
ts.listener.Close()
225230
done := make(chan struct{})

0 commit comments

Comments
 (0)