Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu committed Aug 31, 2024
1 parent ff7ac24 commit 394c21a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 15 deletions.
1 change: 0 additions & 1 deletion tunnel/statistic/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func init() {
uploadTotal: atomic.NewInt64(0),
downloadTotal: atomic.NewInt64(0),
}

go DefaultManager.handle()
}

Expand Down
18 changes: 5 additions & 13 deletions tunnel/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sync"
"time"

"go.uber.org/atomic"

"github.com/xjasonlyu/tun2socks/v2/core/adapter"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/tunnel/statistic"
Expand All @@ -27,8 +29,7 @@ type Tunnel struct {
udpQueue chan adapter.UDPConn

// UDP session timeout.
udpTimeoutMu sync.RWMutex
udpTimeout time.Duration
udpTimeout *atomic.Duration

// Internal proxy.Dialer for Tunnel.
dialerMu sync.RWMutex
Expand All @@ -45,7 +46,7 @@ func New(dialer proxy.Dialer, manager *statistic.Manager) *Tunnel {
return &Tunnel{
tcpQueue: make(chan adapter.TCPConn),
udpQueue: make(chan adapter.UDPConn),
udpTimeout: udpSessionTimeout,
udpTimeout: atomic.NewDuration(udpSessionTimeout),
dialer: dialer,
manager: manager,
procCancel: func() { /* nop */ },
Expand Down Expand Up @@ -110,15 +111,6 @@ func (t *Tunnel) SetDialer(dialer proxy.Dialer) {
t.dialerMu.Unlock()
}

func (t *Tunnel) UDPTimeout() time.Duration {
t.udpTimeoutMu.RLock()
timeout := t.udpTimeout
t.udpTimeoutMu.RUnlock()
return timeout
}

func (t *Tunnel) SetUDPTimeout(timeout time.Duration) {
t.udpTimeoutMu.Lock()
t.udpTimeout = timeout
t.udpTimeoutMu.Unlock()
t.udpTimeout.Store(timeout)
}
2 changes: 1 addition & 1 deletion tunnel/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (t *Tunnel) handleUDPConn(uc adapter.UDPConn) {
pc = newSymmetricNATPacketConn(pc, metadata)

log.Infof("[UDP] %s <-> %s", metadata.SourceAddress(), metadata.DestinationAddress())
pipePacket(uc, pc, remote, t.UDPTimeout())
pipePacket(uc, pc, remote, t.udpTimeout.Load())
}

func pipePacket(origin, remote net.PacketConn, to net.Addr, timeout time.Duration) {
Expand Down

0 comments on commit 394c21a

Please sign in to comment.