-
-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(tunnel): modularize tunnel pkg (#393)
- Loading branch information
Showing
8 changed files
with
152 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package tunnel | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/xjasonlyu/tun2socks/v2/proxy" | ||
"github.com/xjasonlyu/tun2socks/v2/tunnel/statistic" | ||
) | ||
|
||
var ( | ||
_globalMu sync.RWMutex | ||
_globalT *Tunnel | ||
) | ||
|
||
func init() { | ||
ReplaceGlobal(New(&proxy.Base{}, statistic.DefaultManager)) | ||
T().ProcessAsync() | ||
} | ||
|
||
// T returns the global Tunnel, which can be reconfigured with | ||
// ReplaceGlobal. It's safe for concurrent use. | ||
func T() *Tunnel { | ||
_globalMu.RLock() | ||
t := _globalT | ||
_globalMu.RUnlock() | ||
return t | ||
} | ||
|
||
// ReplaceGlobal replaces the global Tunnel, and returns a function | ||
// to restore the original values. It's safe for concurrent use. | ||
func ReplaceGlobal(t *Tunnel) func() { | ||
_globalMu.Lock() | ||
prev := _globalT | ||
_globalT = t | ||
_globalMu.Unlock() | ||
return func() { ReplaceGlobal(prev) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,116 @@ | ||
package tunnel | ||
|
||
import ( | ||
"context" | ||
"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" | ||
) | ||
|
||
// Unbuffered TCP/UDP queues. | ||
var ( | ||
_tcpQueue = make(chan adapter.TCPConn) | ||
_udpQueue = make(chan adapter.UDPConn) | ||
const ( | ||
// tcpConnectTimeout is the default timeout for TCP handshakes. | ||
tcpConnectTimeout = 5 * time.Second | ||
// tcpWaitTimeout implements a TCP half-close timeout. | ||
tcpWaitTimeout = 60 * time.Second | ||
// udpSessionTimeout is the default timeout for UDP sessions. | ||
udpSessionTimeout = 60 * time.Second | ||
) | ||
|
||
func init() { | ||
go process() | ||
var _ adapter.TransportHandler = (*Tunnel)(nil) | ||
|
||
type Tunnel struct { | ||
// Unbuffered TCP/UDP queues. | ||
tcpQueue chan adapter.TCPConn | ||
udpQueue chan adapter.UDPConn | ||
|
||
// UDP session timeout. | ||
udpTimeout *atomic.Duration | ||
|
||
// Internal proxy.Dialer for Tunnel. | ||
dialerMu sync.RWMutex | ||
dialer proxy.Dialer | ||
|
||
// Where the Tunnel statistics are sent to. | ||
manager *statistic.Manager | ||
|
||
procOnce sync.Once | ||
procCancel context.CancelFunc | ||
} | ||
|
||
func New(dialer proxy.Dialer, manager *statistic.Manager) *Tunnel { | ||
return &Tunnel{ | ||
tcpQueue: make(chan adapter.TCPConn), | ||
udpQueue: make(chan adapter.UDPConn), | ||
udpTimeout: atomic.NewDuration(udpSessionTimeout), | ||
dialer: dialer, | ||
manager: manager, | ||
procCancel: func() { /* nop */ }, | ||
} | ||
} | ||
|
||
// TCPIn return fan-in TCP queue. | ||
func TCPIn() chan<- adapter.TCPConn { | ||
return _tcpQueue | ||
func (t *Tunnel) TCPIn() chan<- adapter.TCPConn { | ||
return t.tcpQueue | ||
} | ||
|
||
// UDPIn return fan-in UDP queue. | ||
func UDPIn() chan<- adapter.UDPConn { | ||
return _udpQueue | ||
func (t *Tunnel) UDPIn() chan<- adapter.UDPConn { | ||
return t.udpQueue | ||
} | ||
|
||
func (t *Tunnel) HandleTCP(conn adapter.TCPConn) { | ||
t.TCPIn() <- conn | ||
} | ||
|
||
func process() { | ||
func (t *Tunnel) HandleUDP(conn adapter.UDPConn) { | ||
t.UDPIn() <- conn | ||
} | ||
|
||
func (t *Tunnel) process(ctx context.Context) { | ||
for { | ||
select { | ||
case conn := <-_tcpQueue: | ||
go handleTCPConn(conn) | ||
case conn := <-_udpQueue: | ||
go handleUDPConn(conn) | ||
case conn := <-t.tcpQueue: | ||
go t.handleTCPConn(conn) | ||
case conn := <-t.udpQueue: | ||
go t.handleUDPConn(conn) | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} | ||
|
||
// ProcessAsync can be safely called multiple times, but will only be effective once. | ||
func (t *Tunnel) ProcessAsync() { | ||
t.procOnce.Do(func() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
t.procCancel = cancel | ||
go t.process(ctx) | ||
}) | ||
} | ||
|
||
// Close closes the Tunnel and releases its resources. | ||
func (t *Tunnel) Close() { | ||
t.procCancel() | ||
} | ||
|
||
func (t *Tunnel) Dialer() proxy.Dialer { | ||
t.dialerMu.RLock() | ||
d := t.dialer | ||
t.dialerMu.RUnlock() | ||
return d | ||
} | ||
|
||
func (t *Tunnel) SetDialer(dialer proxy.Dialer) { | ||
t.dialerMu.Lock() | ||
t.dialer = dialer | ||
t.dialerMu.Unlock() | ||
} | ||
|
||
func (t *Tunnel) SetUDPTimeout(timeout time.Duration) { | ||
t.udpTimeout.Store(timeout) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters