Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"errors"
"fmt"
"net"
"os/exec"
"sync"
Expand Down Expand Up @@ -178,9 +179,6 @@ func restAPI(k *Key) error {
}

func netstack(k *Key) (err error) {
if k.Proxy == "" {
return errors.New("empty proxy")
}
if k.Device == "" {
return errors.New("empty device")
}
Expand All @@ -207,7 +205,7 @@ func netstack(k *Key) (err error) {
return err
}

if _defaultProxy, err = parseProxy(k.Proxy); err != nil {
if _defaultProxy, err = buildProxy(k); err != nil {
return err
}
tunnel.T().SetProxy(_defaultProxy)
Expand Down Expand Up @@ -247,6 +245,23 @@ func netstack(k *Key) (err error) {
return err
}

log.Infof("[STACK] %s <-> %s", k.Device, k.Proxy)
log.Infof("[STACK] %s <-> %s", k.Device, proxyDescription(k))
return nil
}

// proxyDescription renders a short, log-friendly description of the
// proxy configuration, accounting for the split case where tcp-proxy
// and/or udp-proxy override --proxy.
func proxyDescription(k *Key) string {
if k.TCPProxy == "" && k.UDPProxy == "" {
return k.Proxy
}
tcp, udp := k.TCPProxy, k.UDPProxy
if tcp == "" {
tcp = k.Proxy
}
if udp == "" {
udp = k.Proxy
}
return fmt.Sprintf("tcp=%s udp=%s", tcp, udp)
}
2 changes: 2 additions & 0 deletions engine/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type Key struct {
MTU int `yaml:"mtu"`
Mark int `yaml:"fwmark"`
Proxy string `yaml:"proxy"`
TCPProxy string `yaml:"tcp-proxy"`
UDPProxy string `yaml:"udp-proxy"`
RestAPI string `yaml:"restapi"`
Device string `yaml:"device"`
LogLevel string `yaml:"loglevel"`
Expand Down
49 changes: 49 additions & 0 deletions engine/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package engine

import (
"errors"
"fmt"
"net"
"net/netip"
Expand Down Expand Up @@ -85,6 +86,54 @@ func parseProxy(s string) (proxy.Proxy, error) {
return proxy.Parse(u)
}

// buildProxy resolves the (Proxy, TCPProxy, UDPProxy) triple into a
// single proxy.Proxy. When TCPProxy or UDPProxy is set, the result is a
// proxy.Split wrapper with the Proxy value (if any) used as the default
// for the unspecified side.
func buildProxy(k *Key) (proxy.Proxy, error) {
parseOne := func(s string) (proxy.Proxy, error) {
if s == "" {
return nil, nil
}
return parseProxy(s)
}

base, err := parseOne(k.Proxy)
if err != nil {
return nil, err
}
tcp, err := parseOne(k.TCPProxy)
if err != nil {
return nil, err
}
udp, err := parseOne(k.UDPProxy)
if err != nil {
return nil, err
}

if tcp == nil && udp == nil {
if base == nil {
return nil, errors.New("no proxy configured: set proxy or tcp-proxy/udp-proxy")
}
return base, nil
}

tcpSide, udpSide := tcp, udp
if tcpSide == nil {
tcpSide = base
}
if udpSide == nil {
udpSide = base
}
if tcpSide == nil {
return nil, errors.New("udp-proxy is set but no TCP proxy configured (set proxy or tcp-proxy)")
}
if udpSide == nil {
return nil, errors.New("tcp-proxy is set but no UDP proxy configured (set proxy or udp-proxy)")
}
return &proxy.Split{TCP: tcpSide, UDP: udpSide}, nil
}

func parseMulticastGroups(v []string) ([]netip.Addr, error) {
groups := make([]netip.Addr, 0, len(v))
for _, ip := range v {
Expand Down
50 changes: 50 additions & 0 deletions engine/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package engine

import (
"strings"
"testing"

"github.com/xjasonlyu/tun2socks/v2/proxy"
)

// TestBuildProxySplit guards against regression of the netstack()
// guard that used to reject tcp-proxy/udp-proxy configurations when
// --proxy was empty. buildProxy should accept that pairing and return
// a *proxy.Split.
func TestBuildProxySplit(t *testing.T) {
p, err := buildProxy(&Key{TCPProxy: "direct://", UDPProxy: "direct://"})
if err != nil {
t.Fatalf("buildProxy split: %v", err)
}
if _, ok := p.(*proxy.Split); !ok {
t.Fatalf("buildProxy split: got %T, want *proxy.Split", p)
}
}

func TestBuildProxyEmpty(t *testing.T) {
_, err := buildProxy(&Key{})
if err == nil {
t.Fatal("buildProxy empty: expected error, got nil")
}
if !strings.Contains(err.Error(), "no proxy configured") {
t.Fatalf("buildProxy empty: unexpected error: %v", err)
}
}

func TestBuildProxyBaseOnly(t *testing.T) {
p, err := buildProxy(&Key{Proxy: "direct://"})
if err != nil {
t.Fatalf("buildProxy base-only: %v", err)
}
if _, ok := p.(*proxy.Split); ok {
t.Fatalf("buildProxy base-only: got *proxy.Split, want direct proxy")
}
}

func TestBuildProxyHalfSplit(t *testing.T) {
// udp-proxy set, tcp-proxy empty, no base -> error
_, err := buildProxy(&Key{UDPProxy: "direct://"})
if err == nil {
t.Fatal("buildProxy half-split: expected error, got nil")
}
}
1 change: 1 addition & 0 deletions engine/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
_ "github.com/xjasonlyu/tun2socks/v2/proxy/direct"
_ "github.com/xjasonlyu/tun2socks/v2/proxy/http"
_ "github.com/xjasonlyu/tun2socks/v2/proxy/masque"
_ "github.com/xjasonlyu/tun2socks/v2/proxy/reject"
_ "github.com/xjasonlyu/tun2socks/v2/proxy/relay"
_ "github.com/xjasonlyu/tun2socks/v2/proxy/shadowsocks"
Expand Down
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/schema v1.4.1
github.com/gorilla/websocket v1.5.3
github.com/quic-go/masque-go v0.3.0
github.com/quic-go/quic-go v0.59.0
github.com/spf13/pflag v1.0.10
github.com/stretchr/testify v1.11.1
github.com/yosida95/uritemplate/v3 v3.0.2
go.uber.org/atomic v1.11.0
go.uber.org/zap v1.27.1
golang.org/x/crypto v0.49.0
Expand All @@ -27,13 +30,18 @@ require (
require (
github.com/ajg/form v1.7.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dunglas/httpsfv v1.0.2 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/qpack v0.6.0 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
go.uber.org/mock v0.5.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20260312153236-7ab1446f8b90 // indirect
golang.org/x/mod v0.34.0 // indirect
golang.org/x/net v0.52.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/text v0.35.0 // indirect
golang.org/x/tools v0.43.0 // indirect
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
28 changes: 22 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/ajg/form v1.7.1 h1:OsnBDzTkrWdrxvEnO68I72ZVGJGNaMwPhoAm0V+llgc=
github.com/ajg/form v1.7.1/go.mod h1:HL757PzLyNkj5AIfptT6L+iGNeXTlnrr/oDePGc/y7Q=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dunglas/httpsfv v1.0.2 h1:iERDp/YAfnojSDJ7PW3dj1AReJz4MrwbECSSE59JWL0=
github.com/dunglas/httpsfv v1.0.2/go.mod h1:zID2mqw9mFsnt7YC3vYQ9/cjq30q41W+1AnDwH8TiMg=
github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug=
github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0=
github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE=
Expand All @@ -16,6 +17,8 @@ github.com/go-gost/relay v0.5.0 h1:JG1tgy/KWiVXS0ukuVXvbM0kbYuJTWxYpJ5JwzsCf/c=
github.com/go-gost/relay v0.5.0/go.mod h1:lcX+23LCQ3khIeASBo+tJ/WbwXFO32/N5YN6ucuYTG8=
github.com/google/btree v1.1.3 h1:CVpQJjYgC4VbzxeGVHfvZrv1ctoYCAI8vbl07Fcxlyg=
github.com/google/btree v1.1.3/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand All @@ -24,27 +27,32 @@ github.com/gorilla/schema v1.4.1 h1:jUg5hUjCSDZpNGLuXQOgIWGdlgrIdYvgQ0wZtdK1M3E=
github.com/gorilla/schema v1.4.1/go.mod h1:Dg5SSm5PV60mhF2NFaTV1xuYYj8tV8NOPRo4FggUMnM=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/quic-go/masque-go v0.3.0 h1:7dfKbv/fSWPr0/d+bwOfOuNPwdDtiEHqyG39CFSUPNw=
github.com/quic-go/masque-go v0.3.0/go.mod h1:5jAgw26NNKsyVHJy5QIJbN6i/ijSUSq2OhW+V5mt+6w=
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw=
github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko=
go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.1 h1:08RqriUEv8+ArZRYSTXy1LeBScaMpVSTBhCeaZYfMYc=
Expand All @@ -53,14 +61,22 @@ golang.org/x/crypto v0.49.0 h1:+Ng2ULVvLHnJ/ZFEq4KdcDd/cfjrrjjNSXNzxg0Y4U4=
golang.org/x/crypto v0.49.0/go.mod h1:ErX4dUh2UM+CFYiXZRTcMpEcN8b/1gxEuv3nODoYtCA=
golang.org/x/exp v0.0.0-20260312153236-7ab1446f8b90 h1:jiDhWWeC7jfWqR9c/uplMOqJ0sbNlNWv0UkzE0vX1MA=
golang.org/x/exp v0.0.0-20260312153236-7ab1446f8b90/go.mod h1:xE1HEv6b+1SCZ5/uscMRjUBKtIxworgEcEi+/n9NQDQ=
golang.org/x/mod v0.34.0 h1:xIHgNUUnW6sYkcM5Jleh05DvLOtwc6RitGHbDk4akRI=
golang.org/x/mod v0.34.0/go.mod h1:ykgH52iCZe79kzLLMhyCUzhMci+nQj+0XkbXpNYtVjY=
golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0=
golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU=
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=
golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8=
golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA=
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
golang.org/x/tools v0.43.0 h1:12BdW9CeB3Z+J/I/wj34VMl8X+fEXBxVR90JeMX5E7s=
golang.org/x/tools v0.43.0/go.mod h1:uHkMso649BX2cZK6+RpuIPXS3ho2hZo4FVwfoy1vIk0=
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 h1:B82qJJgjvYKsXS9jeunTOisW56dUokqW/FOteYJJ/yg=
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2/go.mod h1:deeaetjYA+DHMHg+sMSMI58GrEteJUUzzw7en6TJQcI=
golang.zx2c4.com/wireguard v0.0.0-20250521234502-f333402bd9cb h1:whnFRlWMcXI9d+ZbWg+4sHnLp52d5yiIPUxMBSt4X9A=
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func init() {
flag.StringVarP(&configFile, "config", "c", "", "YAML format configuration file")
flag.StringVarP(&key.Device, "device", "d", "", "Use this device [driver://]name")
flag.StringVarP(&key.Proxy, "proxy", "p", "", "Use this proxy [protocol://]host[:port]")
flag.StringVar(&key.TCPProxy, "tcp-proxy", "", "Use this proxy for TCP only (overrides proxy for TCP)")
flag.StringVar(&key.UDPProxy, "udp-proxy", "", "Use this proxy for UDP only (overrides proxy for UDP; required for MASQUE)")
flag.StringVar(&key.Interface, "interface", "", "Use network INTERFACE (Linux/MacOS/Windows)")
flag.StringVar(&key.LogLevel, "loglevel", "info", "Log level [debug|info|warn|error|silent]")
flag.StringVar(&key.RestAPI, "restapi", "", "HTTP statistic server listen address")
Expand Down
43 changes: 43 additions & 0 deletions proxy/masque/capsule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package masque

import (
"errors"
"io"

"github.com/quic-go/quic-go/http3"
"github.com/quic-go/quic-go/quicvarint"

"github.com/xjasonlyu/tun2socks/v2/log"
)

// drainCapsules consumes HTTP/3 capsules from the stream's byte channel.
// This is mandatory: the RequestStream multiplexes DATA-frame bytes on
// the underlying QUIC stream, and if no one reads them, QUIC stream-level
// flow control eventually stalls the whole connection. RFC 9298 permits
// unrecognised capsule types and requires skipping them.
func drainCapsules(pc *h3DatagramConn) {
r := quicvarint.NewReader(pc.rs)
for {
ct, body, err := http3.ParseCapsule(r)
if err != nil {
if !errors.Is(err, io.EOF) && !isClosed(pc) {
log.Debugf("[MASQUE] capsule drain ended: %v", err)
}
return
}
if _, err := io.Copy(io.Discard, body); err != nil {
log.Debugf("[MASQUE] capsule 0x%x body discard: %v", uint64(ct), err)
return
}
log.Debugf("[MASQUE] drained capsule type=0x%x", uint64(ct))
}
}

func isClosed(pc *h3DatagramConn) bool {
select {
case <-pc.done:
return true
default:
return false
}
}
Loading
Loading