diff --git a/core/stack.go b/core/stack.go index 94178da..7edc757 100644 --- a/core/stack.go +++ b/core/stack.go @@ -23,9 +23,9 @@ type Config struct { // stack to set transport handlers. TransportHandler adapter.TransportHandler - // ErrorFunc is the function that will be called - // when internal stack encounters errors. - ErrorFunc func(tcpip.Error) + // PrintFunc is the function that will be called + // to print internal stack events. + PrintFunc func(string, ...any) // Options are supplement options to apply settings // for the internal stack. @@ -34,8 +34,8 @@ type Config struct { // CreateStack creates *stack.Stack with given config. func CreateStack(cfg *Config) (*stack.Stack, error) { - if cfg.ErrorFunc == nil { - cfg.ErrorFunc = func(tcpip.Error) {} + if cfg.PrintFunc == nil { + cfg.PrintFunc = func(string, ...any) {} } opts := cfg.Options @@ -64,8 +64,8 @@ func CreateStack(cfg *Config) (*stack.Stack, error) { // before creating NIC, otherwise NIC would dispatch packets // to stack and cause race condition. // Initiate transport protocol (TCP/UDP) with given handler. - withTCPHandler(cfg.TransportHandler.HandleTCP, cfg.ErrorFunc), - withUDPHandler(cfg.TransportHandler.HandleUDP, cfg.ErrorFunc), + withTCPHandler(cfg.TransportHandler.HandleTCP, cfg.PrintFunc), + withUDPHandler(cfg.TransportHandler.HandleUDP, cfg.PrintFunc), // Create stack NIC and then bind link endpoint to it. withCreatingNIC(nicID, cfg.LinkEndpoint), diff --git a/core/tcp.go b/core/tcp.go index 305d759..22a14a7 100644 --- a/core/tcp.go +++ b/core/tcp.go @@ -39,7 +39,7 @@ const ( tcpKeepaliveInterval = 30 * time.Second ) -func withTCPHandler(handle func(adapter.TCPConn), callback func(tcpip.Error)) option.Option { +func withTCPHandler(handle func(adapter.TCPConn), printf func(string, ...any)) option.Option { return func(s *stack.Stack) error { tcpForwarder := tcp.NewForwarder(s, defaultWndSize, maxConnAttempts, func(r *tcp.ForwarderRequest) { var ( @@ -50,7 +50,7 @@ func withTCPHandler(handle func(adapter.TCPConn), callback func(tcpip.Error)) op defer func() { if err != nil { - callback(err) + printf("forward tcp request: %s", err) } }() diff --git a/core/udp.go b/core/udp.go index 3c2436b..d90d33a 100644 --- a/core/udp.go +++ b/core/udp.go @@ -4,20 +4,19 @@ import ( "github.com/xjasonlyu/tun2socks/v2/core/adapter" "github.com/xjasonlyu/tun2socks/v2/core/option" - "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" "gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/transport/udp" "gvisor.dev/gvisor/pkg/waiter" ) -func withUDPHandler(handle func(adapter.UDPConn), callback func(tcpip.Error)) option.Option { +func withUDPHandler(handle func(adapter.UDPConn), printf func(string, ...any)) option.Option { return func(s *stack.Stack) error { udpForwarder := udp.NewForwarder(s, func(r *udp.ForwarderRequest) { var wq waiter.Queue ep, err := r.CreateEndpoint(&wq) if err != nil { - callback(err) + printf("forward udp request: %s", err) return } diff --git a/engine/engine.go b/engine/engine.go index 92bb12c..cc3a1ab 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -2,6 +2,7 @@ package engine import ( "errors" + "fmt" "sync" "github.com/xjasonlyu/tun2socks/v2/component/dialer" @@ -158,8 +159,8 @@ func netstack(k *Key) (err error) { if _defaultStack, err = core.CreateStack(&core.Config{ LinkEndpoint: _defaultDevice, TransportHandler: &mirror.Tunnel{}, - ErrorFunc: func(err tcpip.Error) { - log.Warnf("[STACK] %s", err) + PrintFunc: func(format string, v ...any) { + log.Warnf("[STACK] %s", fmt.Sprintf(format, v...)) }, }); err != nil { return