mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-08 10:10:12 +08:00
Improve: with gvisor internal log
This commit is contained in:
@@ -23,10 +23,6 @@ type Config struct {
|
||||
// stack to set transport handlers.
|
||||
TransportHandler adapter.TransportHandler
|
||||
|
||||
// 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.
|
||||
Options []option.Option
|
||||
@@ -34,10 +30,6 @@ type Config struct {
|
||||
|
||||
// CreateStack creates *stack.Stack with given config.
|
||||
func CreateStack(cfg *Config) (*stack.Stack, error) {
|
||||
if cfg.PrintFunc == nil {
|
||||
cfg.PrintFunc = func(string, ...any) {}
|
||||
}
|
||||
|
||||
opts := []option.Option{option.WithDefault()}
|
||||
if len(opts) > 0 {
|
||||
opts = append(opts, cfg.Options...)
|
||||
@@ -64,8 +56,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.PrintFunc),
|
||||
withUDPHandler(cfg.TransportHandler.HandleUDP, cfg.PrintFunc),
|
||||
withTCPHandler(cfg.TransportHandler.HandleTCP),
|
||||
withUDPHandler(cfg.TransportHandler.HandleUDP),
|
||||
|
||||
// Create stack NIC and then bind link endpoint to it.
|
||||
withCreatingNIC(nicID, cfg.LinkEndpoint),
|
||||
|
@@ -3,6 +3,7 @@ package core
|
||||
import (
|
||||
"time"
|
||||
|
||||
glog "gvisor.dev/gvisor/pkg/log"
|
||||
"gvisor.dev/gvisor/pkg/tcpip"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/header"
|
||||
@@ -40,7 +41,7 @@ const (
|
||||
tcpKeepaliveInterval = 30 * time.Second
|
||||
)
|
||||
|
||||
func withTCPHandler(handle func(adapter.TCPConn), printf func(string, ...any)) option.Option {
|
||||
func withTCPHandler(handle func(adapter.TCPConn)) option.Option {
|
||||
return func(s *stack.Stack) error {
|
||||
tcpForwarder := tcp.NewForwarder(s, defaultWndSize, maxConnAttempts, func(r *tcp.ForwarderRequest) {
|
||||
var (
|
||||
@@ -52,7 +53,7 @@ func withTCPHandler(handle func(adapter.TCPConn), printf func(string, ...any)) o
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
printf("forward tcp request %s:%d->%s:%d: %s",
|
||||
glog.Warningf("forward tcp request: %s:%d->%s:%d: %s",
|
||||
id.RemoteAddress, id.RemotePort, id.LocalAddress, id.LocalPort, err)
|
||||
}
|
||||
}()
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
glog "gvisor.dev/gvisor/pkg/log"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/stack"
|
||||
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
|
||||
@@ -10,7 +11,7 @@ import (
|
||||
"github.com/xjasonlyu/tun2socks/v2/core/option"
|
||||
)
|
||||
|
||||
func withUDPHandler(handle func(adapter.UDPConn), printf func(string, ...any)) option.Option {
|
||||
func withUDPHandler(handle func(adapter.UDPConn)) option.Option {
|
||||
return func(s *stack.Stack) error {
|
||||
udpForwarder := udp.NewForwarder(s, func(r *udp.ForwarderRequest) {
|
||||
var (
|
||||
@@ -19,7 +20,7 @@ func withUDPHandler(handle func(adapter.UDPConn), printf func(string, ...any)) o
|
||||
)
|
||||
ep, err := r.CreateEndpoint(&wq)
|
||||
if err != nil {
|
||||
printf("udp forwarder request %s:%d->%s:%d: %s",
|
||||
glog.Warningf("forward udp request: %s:%d->%s:%d: %s",
|
||||
id.RemoteAddress, id.RemotePort, id.LocalAddress, id.LocalPort, err)
|
||||
return
|
||||
}
|
||||
|
@@ -2,7 +2,6 @@ package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -192,9 +191,6 @@ func netstack(k *Key) (err error) {
|
||||
if _defaultStack, err = core.CreateStack(&core.Config{
|
||||
LinkEndpoint: _defaultDevice,
|
||||
TransportHandler: &mirror.Tunnel{},
|
||||
PrintFunc: func(format string, v ...any) {
|
||||
log.Warnf("[STACK] %s", fmt.Sprintf(format, v...))
|
||||
},
|
||||
Options: opts,
|
||||
}); err != nil {
|
||||
return
|
||||
|
Reference in New Issue
Block a user