mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-10 03:00:15 +08:00
Refactor: use core/adapter
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package core
|
||||
package adapter
|
||||
|
||||
import (
|
||||
"net"
|
@@ -1,4 +1,4 @@
|
||||
package core
|
||||
package adapter
|
||||
|
||||
// Handler is a TCP/UDP connection handler that implements
|
||||
// HandleTCPConn and HandleUDPConn methods.
|
@@ -2,7 +2,7 @@
|
||||
package stack
|
||||
|
||||
import (
|
||||
"github.com/xjasonlyu/tun2socks/v2/core"
|
||||
"github.com/xjasonlyu/tun2socks/v2/core/adapter"
|
||||
|
||||
"gvisor.dev/gvisor/pkg/tcpip"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
|
||||
@@ -16,12 +16,12 @@ import (
|
||||
type Stack struct {
|
||||
*stack.Stack
|
||||
|
||||
handler core.Handler
|
||||
handler adapter.Handler
|
||||
nicID tcpip.NICID
|
||||
}
|
||||
|
||||
// New allocates a new *Stack with given options.
|
||||
func New(ep stack.LinkEndpoint, handler core.Handler, opts ...Option) (*Stack, error) {
|
||||
func New(ep stack.LinkEndpoint, handler adapter.Handler, opts ...Option) (*Stack, error) {
|
||||
s := &Stack{
|
||||
Stack: stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocolFactory{
|
||||
|
@@ -1,18 +1,18 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"github.com/xjasonlyu/tun2socks/v2/core"
|
||||
"github.com/xjasonlyu/tun2socks/v2/core/adapter"
|
||||
"github.com/xjasonlyu/tun2socks/v2/tunnel"
|
||||
)
|
||||
|
||||
var _ core.Handler = (*fakeTunnel)(nil)
|
||||
var _ adapter.Handler = (*fakeTunnel)(nil)
|
||||
|
||||
type fakeTunnel struct{}
|
||||
|
||||
func (*fakeTunnel) HandleTCPConn(conn core.TCPConn) {
|
||||
func (*fakeTunnel) HandleTCPConn(conn adapter.TCPConn) {
|
||||
tunnel.TCPIn() <- conn
|
||||
}
|
||||
|
||||
func (*fakeTunnel) HandleUDPConn(conn core.UDPConn) {
|
||||
func (*fakeTunnel) HandleUDPConn(conn adapter.UDPConn) {
|
||||
tunnel.UDPIn() <- conn
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/xjasonlyu/tun2socks/v2/common/pool"
|
||||
"github.com/xjasonlyu/tun2socks/v2/core"
|
||||
"github.com/xjasonlyu/tun2socks/v2/core/adapter"
|
||||
"github.com/xjasonlyu/tun2socks/v2/log"
|
||||
M "github.com/xjasonlyu/tun2socks/v2/metadata"
|
||||
"github.com/xjasonlyu/tun2socks/v2/proxy"
|
||||
@@ -22,7 +22,7 @@ func newTCPTracker(conn net.Conn, metadata *M.Metadata) net.Conn {
|
||||
return statistic.NewTCPTracker(conn, metadata, statistic.DefaultManager)
|
||||
}
|
||||
|
||||
func handleTCPConn(localConn core.TCPConn) {
|
||||
func handleTCPConn(localConn adapter.TCPConn) {
|
||||
defer localConn.Close()
|
||||
|
||||
var (
|
||||
|
@@ -1,13 +1,13 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"github.com/xjasonlyu/tun2socks/v2/core"
|
||||
"github.com/xjasonlyu/tun2socks/v2/core/adapter"
|
||||
)
|
||||
|
||||
// Unbuffered TCP/UDP queues.
|
||||
var (
|
||||
_tcpQueue = make(chan core.TCPConn)
|
||||
_udpQueue = make(chan core.UDPConn)
|
||||
_tcpQueue = make(chan adapter.TCPConn)
|
||||
_udpQueue = make(chan adapter.UDPConn)
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -15,12 +15,12 @@ func init() {
|
||||
}
|
||||
|
||||
// TCPIn return fan-in TCP queue.
|
||||
func TCPIn() chan<- core.TCPConn {
|
||||
func TCPIn() chan<- adapter.TCPConn {
|
||||
return _tcpQueue
|
||||
}
|
||||
|
||||
// UDPIn return fan-in UDP queue.
|
||||
func UDPIn() chan<- core.UDPConn {
|
||||
func UDPIn() chan<- adapter.UDPConn {
|
||||
return _udpQueue
|
||||
}
|
||||
|
||||
|
@@ -7,7 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/xjasonlyu/tun2socks/v2/common/pool"
|
||||
"github.com/xjasonlyu/tun2socks/v2/core"
|
||||
"github.com/xjasonlyu/tun2socks/v2/core/adapter"
|
||||
"github.com/xjasonlyu/tun2socks/v2/log"
|
||||
M "github.com/xjasonlyu/tun2socks/v2/metadata"
|
||||
"github.com/xjasonlyu/tun2socks/v2/proxy"
|
||||
@@ -25,7 +25,7 @@ func newUDPTracker(conn net.PacketConn, metadata *M.Metadata) net.PacketConn {
|
||||
return statistic.NewUDPTracker(conn, metadata, statistic.DefaultManager)
|
||||
}
|
||||
|
||||
func handleUDPConn(uc core.UDPConn) {
|
||||
func handleUDPConn(uc adapter.UDPConn) {
|
||||
defer uc.Close()
|
||||
|
||||
var (
|
||||
@@ -55,7 +55,7 @@ func handleUDPConn(uc core.UDPConn) {
|
||||
handleUDPToLocal(uc, pc, remote)
|
||||
}
|
||||
|
||||
func handleUDPToRemote(uc core.UDPConn, pc net.PacketConn, remote net.Addr) {
|
||||
func handleUDPToRemote(uc adapter.UDPConn, pc net.PacketConn, remote net.Addr) {
|
||||
buf := pool.Get(pool.MaxSegmentSize)
|
||||
defer pool.Put(buf)
|
||||
|
||||
@@ -74,7 +74,7 @@ func handleUDPToRemote(uc core.UDPConn, pc net.PacketConn, remote net.Addr) {
|
||||
}
|
||||
}
|
||||
|
||||
func handleUDPToLocal(uc core.UDPConn, pc net.PacketConn, remote net.Addr) {
|
||||
func handleUDPToLocal(uc adapter.UDPConn, pc net.PacketConn, remote net.Addr) {
|
||||
buf := pool.Get(pool.MaxSegmentSize)
|
||||
defer pool.Put(buf)
|
||||
|
||||
|
Reference in New Issue
Block a user