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.
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),

View File

@@ -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)
}
}()

View File

@@ -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
}

View File

@@ -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