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