From 20499c6432a896b0d88cb26491136aaf10231b4f Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 3 Apr 2023 17:57:13 +0800 Subject: [PATCH] Revert: cli tcp-wait-timeout option This reverts commit 2c51a656858b264b6968e3591c4d227725df7d99. --- engine/engine.go | 4 ---- engine/key.go | 3 +-- main.go | 1 - tunnel/tcp.go | 14 ++++++-------- 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/engine/engine.go b/engine/engine.go index 52ff976..e7cb863 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -123,10 +123,6 @@ func general(k *Key) error { log.Infof("[DIALER] set fwmark: %#x", k.Mark) } - if k.TCPWaitTimeout > 0 { - tunnel.SetTCPWaitTimeout(k.TCPWaitTimeout) - } - if k.UDPTimeout > 0 { if k.UDPTimeout < time.Second { return errors.New("invalid udp timeout value") diff --git a/engine/key.go b/engine/key.go index d829ba7..32e39dc 100644 --- a/engine/key.go +++ b/engine/key.go @@ -13,8 +13,7 @@ type Key struct { TCPModerateReceiveBuffer bool `yaml:"tcp-moderate-receive-buffer"` TCPSendBufferSize string `yaml:"tcp-send-buffer-size"` TCPReceiveBufferSize string `yaml:"tcp-receive-buffer-size"` - TCPWaitTimeout time.Duration `yaml:"tcp-wait-timeout"` - UDPTimeout time.Duration `yaml:"udp-timeout"` TUNPreUp string `yaml:"tun-pre-up"` TUNPostUp string `yaml:"tun-post-up"` + UDPTimeout time.Duration `yaml:"udp-timeout"` } diff --git a/main.go b/main.go index 33f17ab..7cc4b1f 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,6 @@ func init() { flag.StringVar(&key.TCPSendBufferSize, "tcp-sndbuf", "", "Set TCP send buffer size for netstack") flag.StringVar(&key.TCPReceiveBufferSize, "tcp-rcvbuf", "", "Set TCP receive buffer size for netstack") flag.BoolVar(&key.TCPModerateReceiveBuffer, "tcp-auto-tuning", false, "Enable TCP receive buffer auto-tuning") - flag.DurationVar(&key.TCPWaitTimeout, "tcp-wait-timeout", 0, "Set timeout before closing each TCP connection") flag.StringVar(&key.TUNPreUp, "tun-pre-up", "", "Execute a command before TUN device setup") flag.StringVar(&key.TUNPostUp, "tun-post-up", "", "Execute a command after TUN device setup") flag.BoolVar(&versionFlag, "version", false, "Show version and then quit") diff --git a/tunnel/tcp.go b/tunnel/tcp.go index 4502804..a45de8b 100644 --- a/tunnel/tcp.go +++ b/tunnel/tcp.go @@ -15,12 +15,10 @@ import ( "github.com/xjasonlyu/tun2socks/v2/tunnel/statistic" ) -// _tcpWaitTimeout is the default timeout to wait after closing each TCP connection. -var _tcpWaitTimeout = 5 * time.Second - -func SetTCPWaitTimeout(t time.Duration) { - _tcpWaitTimeout = t -} +const ( + // tcpWaitTimeout implements a TCP half-close timeout. + tcpWaitTimeout = 60 * time.Second +) func handleTCPConn(originConn adapter.TCPConn) { defer originConn.Close() @@ -62,7 +60,7 @@ func pipe(origin, remote net.Conn) error { if err := copyBuffer(remote, origin); err != nil { leftErr = errors.Join(leftErr, err) } - remote.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) + remote.SetReadDeadline(time.Now().Add(tcpWaitTimeout)) }() go func() { @@ -70,7 +68,7 @@ func pipe(origin, remote net.Conn) error { if err := copyBuffer(origin, remote); err != nil { rightErr = errors.Join(rightErr, err) } - origin.SetReadDeadline(time.Now().Add(_tcpWaitTimeout)) + origin.SetReadDeadline(time.Now().Add(tcpWaitTimeout)) }() wg.Wait()