-
-
Notifications
You must be signed in to change notification settings - Fork 467
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
- Loading branch information
Showing
8 changed files
with
137 additions
and
74 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
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,50 @@ | ||
package tunnel | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"sync" | ||
|
||
M "github.com/xjasonlyu/tun2socks/v2/metadata" | ||
"github.com/xjasonlyu/tun2socks/v2/proxy" | ||
"github.com/xjasonlyu/tun2socks/v2/tunnel/statistic" | ||
) | ||
|
||
var ( | ||
_globalMu sync.RWMutex | ||
_globalT *Tunnel | ||
) | ||
|
||
func init() { | ||
ReplaceGlobals(New(wrapper{}, statistic.DefaultManager)) | ||
go T().Process() | ||
} | ||
|
||
type wrapper struct{} | ||
|
||
func (wrapper) DialContext(ctx context.Context, metadata *M.Metadata) (net.Conn, error) { | ||
return proxy.DialContext(ctx, metadata) | ||
} | ||
|
||
func (wrapper) DialUDP(metadata *M.Metadata) (net.PacketConn, error) { | ||
return proxy.DialUDP(metadata) | ||
} | ||
|
||
// T returns the global Tunnel, which can be reconfigured with | ||
// ReplaceGlobals. It's safe for concurrent use. | ||
func T() *Tunnel { | ||
_globalMu.RLock() | ||
t := _globalT | ||
_globalMu.RUnlock() | ||
return t | ||
} | ||
|
||
// ReplaceGlobals replaces the global Tunnel, and returns a function | ||
// to restore the original values. It's safe for concurrent use. | ||
func ReplaceGlobals(t *Tunnel) func() { | ||
_globalMu.Lock() | ||
prev := _globalT | ||
_globalT = t | ||
_globalMu.Unlock() | ||
return func() { ReplaceGlobals(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,84 @@ | ||
package tunnel | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"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 udpSessionTimeout = 60 * time.Second | ||
|
||
var _ adapter.TransportHandler = (*Tunnel)(nil) | ||
|
||
type Tunnel struct { | ||
// Unbuffered TCP/UDP queues. | ||
tcpQueue chan adapter.TCPConn | ||
udpQueue chan adapter.UDPConn | ||
|
||
// UDP session timeout. | ||
udpTimeout time.Duration | ||
|
||
func init() { | ||
go process() | ||
dialer proxy.Dialer | ||
manager *statistic.Manager | ||
|
||
once sync.Once | ||
cancel context.CancelFunc | ||
} | ||
|
||
func New(dialer proxy.Dialer, manager *statistic.Manager) *Tunnel { | ||
return &Tunnel{ | ||
tcpQueue: make(chan adapter.TCPConn), | ||
udpQueue: make(chan adapter.UDPConn), | ||
udpTimeout: udpSessionTimeout, | ||
dialer: dialer, | ||
manager: manager, | ||
cancel: func() {}, | ||
} | ||
} | ||
|
||
// 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 process() { | ||
func (t *Tunnel) HandleTCP(conn adapter.TCPConn) { | ||
t.TCPIn() <- conn | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} | ||
|
||
func (t *Tunnel) Process() { | ||
t.once.Do(func() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
t.cancel = cancel | ||
t.process(ctx) | ||
}) | ||
} | ||
|
||
func (t *Tunnel) Close() { | ||
t.cancel() | ||
} |
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