Chore(core): use config.PrintFunc

This commit is contained in:
xjasonlyu
2022-03-31 17:48:52 +08:00
parent ccf53dcb88
commit 21232703af
4 changed files with 14 additions and 14 deletions

View File

@@ -23,9 +23,9 @@ type Config struct {
// stack to set transport handlers. // stack to set transport handlers.
TransportHandler adapter.TransportHandler TransportHandler adapter.TransportHandler
// ErrorFunc is the function that will be called // PrintFunc is the function that will be called
// when internal stack encounters errors. // to print internal stack events.
ErrorFunc func(tcpip.Error) PrintFunc func(string, ...any)
// Options are supplement options to apply settings // Options are supplement options to apply settings
// for the internal stack. // for the internal stack.
@@ -34,8 +34,8 @@ type Config struct {
// CreateStack creates *stack.Stack with given config. // CreateStack creates *stack.Stack with given config.
func CreateStack(cfg *Config) (*stack.Stack, error) { func CreateStack(cfg *Config) (*stack.Stack, error) {
if cfg.ErrorFunc == nil { if cfg.PrintFunc == nil {
cfg.ErrorFunc = func(tcpip.Error) {} cfg.PrintFunc = func(string, ...any) {}
} }
opts := cfg.Options opts := cfg.Options
@@ -64,8 +64,8 @@ func CreateStack(cfg *Config) (*stack.Stack, error) {
// before creating NIC, otherwise NIC would dispatch packets // before creating NIC, otherwise NIC would dispatch packets
// to stack and cause race condition. // to stack and cause race condition.
// Initiate transport protocol (TCP/UDP) with given handler. // Initiate transport protocol (TCP/UDP) with given handler.
withTCPHandler(cfg.TransportHandler.HandleTCP, cfg.ErrorFunc), withTCPHandler(cfg.TransportHandler.HandleTCP, cfg.PrintFunc),
withUDPHandler(cfg.TransportHandler.HandleUDP, cfg.ErrorFunc), withUDPHandler(cfg.TransportHandler.HandleUDP, cfg.PrintFunc),
// Create stack NIC and then bind link endpoint to it. // Create stack NIC and then bind link endpoint to it.
withCreatingNIC(nicID, cfg.LinkEndpoint), withCreatingNIC(nicID, cfg.LinkEndpoint),

View File

@@ -39,7 +39,7 @@ const (
tcpKeepaliveInterval = 30 * time.Second 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 { return func(s *stack.Stack) error {
tcpForwarder := tcp.NewForwarder(s, defaultWndSize, maxConnAttempts, func(r *tcp.ForwarderRequest) { tcpForwarder := tcp.NewForwarder(s, defaultWndSize, maxConnAttempts, func(r *tcp.ForwarderRequest) {
var ( var (
@@ -50,7 +50,7 @@ func withTCPHandler(handle func(adapter.TCPConn), callback func(tcpip.Error)) op
defer func() { defer func() {
if err != nil { if err != nil {
callback(err) printf("forward tcp request: %s", err)
} }
}() }()

View File

@@ -4,20 +4,19 @@ import (
"github.com/xjasonlyu/tun2socks/v2/core/adapter" "github.com/xjasonlyu/tun2socks/v2/core/adapter"
"github.com/xjasonlyu/tun2socks/v2/core/option" "github.com/xjasonlyu/tun2socks/v2/core/option"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/stack" "gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp" "gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"gvisor.dev/gvisor/pkg/waiter" "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 { return func(s *stack.Stack) error {
udpForwarder := udp.NewForwarder(s, func(r *udp.ForwarderRequest) { udpForwarder := udp.NewForwarder(s, func(r *udp.ForwarderRequest) {
var wq waiter.Queue var wq waiter.Queue
ep, err := r.CreateEndpoint(&wq) ep, err := r.CreateEndpoint(&wq)
if err != nil { if err != nil {
callback(err) printf("forward udp request: %s", err)
return return
} }

View File

@@ -2,6 +2,7 @@ package engine
import ( import (
"errors" "errors"
"fmt"
"sync" "sync"
"github.com/xjasonlyu/tun2socks/v2/component/dialer" "github.com/xjasonlyu/tun2socks/v2/component/dialer"
@@ -158,8 +159,8 @@ func netstack(k *Key) (err error) {
if _defaultStack, err = core.CreateStack(&core.Config{ if _defaultStack, err = core.CreateStack(&core.Config{
LinkEndpoint: _defaultDevice, LinkEndpoint: _defaultDevice,
TransportHandler: &mirror.Tunnel{}, TransportHandler: &mirror.Tunnel{},
ErrorFunc: func(err tcpip.Error) { PrintFunc: func(format string, v ...any) {
log.Warnf("[STACK] %s", err) log.Warnf("[STACK] %s", fmt.Sprintf(format, v...))
}, },
}); err != nil { }); err != nil {
return return