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
+29 -10
View File
@@ -2,6 +2,7 @@ package tun
import (
"context"
"syscall"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
@@ -15,6 +16,7 @@ import (
"github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/transport"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat"
)
@@ -38,11 +40,6 @@ type ConnectionHandler interface {
// Handler implements ConnectionHandler
var _ ConnectionHandler = (*Handler)(nil)
func (t *Handler) policy() policy.Session {
p := t.policyManager.ForLevel(t.config.UserLevel)
return p
}
// Init the Handler instance with necessary parameters
func (t *Handler) Init(ctx context.Context, pm policy.Manager, dispatcher routing.Dispatcher) error {
var err error
@@ -60,15 +57,37 @@ func (t *Handler) Init(ctx context.Context, pm policy.Manager, dispatcher routin
t.dispatcher = dispatcher
tunName := t.config.Name
tunOptions := TunOptions{
Name: tunName,
MTU: t.config.MTU,
}
tunInterface, err := NewTun(tunOptions)
tunInterface, err := NewTun(t.config)
if err != nil {
return err
}
if t.config.AutoOutboundsInterface != "" {
tunIndex, err := tunInterface.Index()
if err != nil {
_ = tunInterface.Close()
return err
}
if t.config.AutoOutboundsInterface == "auto" {
t.config.AutoOutboundsInterface = ""
}
updater = &InterfaceUpdater{tunIndex: tunIndex, fixedName: t.config.AutoOutboundsInterface}
updater.Update()
internet.RegisterDialerController(func(network, address string, c syscall.RawConn) error {
iface := updater.Get()
if iface == nil {
errors.LogInfo(context.Background(), "[tun] falied to set interface > iface == nil")
return nil
}
return c.Control(func(fd uintptr) {
err := setinterface(network, address, fd, iface)
if err != nil {
errors.LogInfoInner(context.Background(), err, "[tun] falied to set interface")
}
})
})
}
errors.LogInfo(t.ctx, tunName, " created")
tunStackOptions := StackOptions{