TUN inbound: Add gateway, dns, autoSystemRoutingTable, autoOutboundsInterface for Windows (#5887)

And refactor `mtu` to support setting IPv4/v6 separately

Example: https://github.com/XTLS/Xray-core/pull/5887#issue-4198837696
This commit is contained in:
LjhAUMEM
2026-04-13 21:38:10 +08:00
committed by GitHub
parent f27edc3172
commit 806b8dc27d
16 changed files with 544 additions and 109 deletions
+13 -8
View File
@@ -2,6 +2,7 @@ package internet
import (
"context"
"sync"
"syscall"
"time"
@@ -11,6 +12,8 @@ import (
"github.com/xtls/xray-core/features/outbound"
)
var Controllers []func(network, address string, c syscall.RawConn) error
var ControllersLock sync.Mutex
var effectiveSystemDialer SystemDialer = &DefaultSystemDialer{}
type SystemDialer interface {
@@ -19,9 +22,8 @@ type SystemDialer interface {
}
type DefaultSystemDialer struct {
controllers []func(network, address string, c syscall.RawConn) error
dns dns.Client
obm outbound.Manager
dns dns.Client
obm outbound.Manager
}
func resolveSrcAddr(network net.Network, src net.Address) net.Addr {
@@ -63,7 +65,7 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
return nil, err
}
lc.Control = func(network, address string, c syscall.RawConn) error {
for _, ctl := range d.controllers {
for _, ctl := range Controllers {
if err := ctl(network, address, c); err != nil {
errors.LogInfoInner(ctx, err, "failed to apply external controller")
}
@@ -115,12 +117,12 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
KeepAliveConfig: keepAliveConfig,
}
if sockopt != nil || len(d.controllers) > 0 {
if sockopt != nil || len(Controllers) > 0 {
if sockopt != nil && sockopt.TcpMptcp {
dialer.SetMultipathTCP(true)
}
dialer.Control = func(network, address string, c syscall.RawConn) error {
for _, ctl := range d.controllers {
for _, ctl := range Controllers {
if err := ctl(network, address, c); err != nil {
errors.LogInfoInner(ctx, err, "failed to apply external controller")
}
@@ -208,12 +210,15 @@ func RegisterDialerController(ctl func(network, address string, c syscall.RawCon
return errors.New("nil listener controller")
}
dialer, ok := effectiveSystemDialer.(*DefaultSystemDialer)
ControllersLock.Lock()
Controllers = append(Controllers, ctl)
ControllersLock.Unlock()
_, ok := effectiveSystemDialer.(*DefaultSystemDialer)
if !ok {
return errors.New("RegisterListenerController not supported in custom dialer")
}
dialer.controllers = append(dialer.controllers, ctl)
return nil
}