TUN inbound: Support configurable Wintun adapter guid on Windows (#7460)

Closes https://github.com/XTLS/Xray-core/issues/7458
This commit is contained in:
Plangto
2026-11-24 12:09:36 +00:00
committed by GitHub
parent 8c7447fd2d
commit d9131d381c
9 changed files with 225 additions and 7 deletions
+9
View File
@@ -7,6 +7,7 @@ import (
"net"
"runtime"
"strconv"
"strings"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/proxy/tun"
@@ -16,6 +17,7 @@ import (
type TunConfig struct {
Name string `json:"name"`
Desc string `json:"desc"`
GUID string `json:"guid"`
MTU uint32 `json:"mtu"`
Gateway []string `json:"gateway"`
DNS []string `json:"dns"`
@@ -37,6 +39,13 @@ func (v *TunConfig) Build() (proto.Message, error) {
AutoSystemRoutingTable: v.AutoSystemRoutingTable,
AutoSystemDnsToGateway: v.AutoSystemDnsToGateway,
}
if v.GUID != "" {
// only Windows gives the TUN a GUID, but a config is checked wherever it is loaded
if _, err := tun.ParseGUID(v.GUID); err != nil {
return nil, err
}
config.GUID = strings.ToLower(v.GUID)
}
leaks, err := tun.ExpandWfpBlockLeak(v.AutoSystemWfpBlockLeak)
if err != nil {
return nil, err
+45
View File
@@ -124,3 +124,48 @@ func TestTunConfigAutoSystemWfpBlockLeakUnknown(t *testing.T) {
}
}
}
// TestTunConfigGUID checks that a guid is read, written back in the canonical
// form, and rejected unless it is one, on every system, even though only
// Windows gives the TUN a GUID.
func TestTunConfigGUID(t *testing.T) {
build := func(guid string) (*tun.Config, error) {
config := new(TunConfig)
if err := json.Unmarshal([]byte(`{"name": "xray0", "guid": "`+guid+`"}`), config); err != nil {
t.Fatal(err)
}
message, err := config.Build()
if err != nil {
return nil, err
}
return message.(*tun.Config), nil
}
if config, err := build("6ba7b810-9dad-11d1-80b4-00c04fd430c8"); err != nil {
t.Fatal(err)
} else if config.GUID != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
t.Errorf("guid = %q, want it kept", config.GUID)
}
if config, err := build("6BA7B810-9DAD-11D1-80B4-00C04FD430C8"); err != nil {
t.Fatal(err)
} else if config.GUID != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
t.Errorf("guid = %q, want it lowercased", config.GUID)
}
if config, err := build(""); err != nil {
t.Fatal(err)
} else if config.GUID != "" {
t.Errorf("guid = %q, want it empty, as the adapter GUID is derived from the name then", config.GUID)
}
for _, guid := range []string{
"xray0",
"6ba7b810-9dad-11d1-80b4-00c04fd430c",
"6ba7b8109dad11d180b400c04fd430c8",
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
"urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"6ba7b810-9dad-11d1-80b4-00c04fd430cg",
} {
if _, err := build(guid); err == nil {
t.Errorf("guid %q was accepted", guid)
}
}
}
+2
View File
@@ -223,6 +223,8 @@ To make it start, wintun.dll specific for your Windows/arch must be present next
After the start network adapter with the name you chose in the config will be created in the system, and exist while Xray is running. A Wintun adapter that already has that name is reused.
`guid` sets the GUID of the adapter Xray creates, as in `"guid": "6ba7b810-9dad-11d1-80b4-00c04fd430c8"`, for applications that identify the adapter by something other than its name, e.g. to keep the same identity across renames. It is used only when Xray creates the adapter: a Wintun adapter that already has the name is opened as it is and keeps its own GUID, so this setting cannot change it. Without it the GUID is derived from the name with MD5, which keeps it the same for as long as the name does. The GUID must not be one that another adapter uses, and Wintun holds one session per adapter, so an adapter is in use by one engine at a time. The config is rejected, on Windows and elsewhere, if the value is not a canonical GUID; other systems ignore it.
With IPv6 turned off by the `DisabledComponents` registry value (0x10 set, as in 0xFF), errors setting up IPv6 for `gateway`, `dns` and `autoSystemRoutingTable`, clearing the IPv6 DNS servers when `dns` has none included, are only logged. Otherwise, they keep the TUN from starting.
When `dns` is set, those servers are applied to the adapter. Windows is kept from registering the TUN's addresses in DNS, and its DNS cache is flushed when the TUN starts and stops. Xray's own lookups through the system's DNS servers, like those of the `localhost` DNS server with `autoOutboundsInterface`, skip them, unless another interface uses them as well, as they would lead back into the TUN.
+11 -2
View File
@@ -33,6 +33,7 @@ type Config struct {
Desc string `protobuf:"bytes,8,opt,name=desc,proto3" json:"desc,omitempty"`
AutoSystemDnsToGateway bool `protobuf:"varint,9,opt,name=auto_system_dns_to_gateway,json=autoSystemDnsToGateway,proto3" json:"auto_system_dns_to_gateway,omitempty"`
AutoSystemWfpBlockLeak []string `protobuf:"bytes,10,rep,name=auto_system_wfp_block_leak,json=autoSystemWfpBlockLeak,proto3" json:"auto_system_wfp_block_leak,omitempty"`
GUID string `protobuf:"bytes,11,opt,name=GUID,proto3" json:"GUID,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -137,11 +138,18 @@ func (x *Config) GetAutoSystemWfpBlockLeak() []string {
return nil
}
func (x *Config) GetGUID() string {
if x != nil {
return x.GUID
}
return ""
}
var File_proxy_tun_config_proto protoreflect.FileDescriptor
const file_proxy_tun_config_proto_rawDesc = "" +
"\n" +
"\x16proxy/tun/config.proto\x12\x0exray.proxy.tun\"\xfa\x02\n" +
"\x16proxy/tun/config.proto\x12\x0exray.proxy.tun\"\x8e\x03\n" +
"\x06Config\x12\x12\n" +
"\x04name\x18\x01 \x01(\tR\x04name\x12\x10\n" +
"\x03MTU\x18\x02 \x01(\rR\x03MTU\x12\x18\n" +
@@ -154,7 +162,8 @@ const file_proxy_tun_config_proto_rawDesc = "" +
"\x04desc\x18\b \x01(\tR\x04desc\x12:\n" +
"\x1aauto_system_dns_to_gateway\x18\t \x01(\bR\x16autoSystemDnsToGateway\x12:\n" +
"\x1aauto_system_wfp_block_leak\x18\n" +
" \x03(\tR\x16autoSystemWfpBlockLeakBL\n" +
" \x03(\tR\x16autoSystemWfpBlockLeak\x12\x12\n" +
"\x04GUID\x18\v \x01(\tR\x04GUIDBL\n" +
"\x12com.xray.proxy.tunP\x01Z#github.com/xtls/xray-core/proxy/tun\xaa\x02\x0eXray.Proxy.Tunb\x06proto3"
var (
+1
View File
@@ -17,4 +17,5 @@ message Config {
string desc = 8;
bool auto_system_dns_to_gateway = 9;
repeated string auto_system_wfp_block_leak = 10;
string GUID = 11;
}
+21
View File
@@ -0,0 +1,21 @@
package tun
import (
"github.com/google/uuid"
"github.com/xtls/xray-core/common/errors"
)
// ParseGUID parses a canonical textual GUID, as in
// "00000000-0000-0000-0000-000000000000", into the 16 bytes it denotes.
// The form is all that is accepted, not the other ones uuid.Parse takes,
// such as one without hyphens; the length check is what rules those out.
func ParseGUID(s string) ([16]byte, error) {
if len(s) != 36 {
return [16]byte{}, errors.New("invalid guid: ", s, ", want xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
}
id, err := uuid.Parse(s)
if err != nil {
return [16]byte{}, errors.New("invalid guid: ", s, ", want xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").Base(err)
}
return id, nil
}
+54
View File
@@ -0,0 +1,54 @@
package tun_test
import (
"testing"
"github.com/xtls/xray-core/proxy/tun"
)
func TestParseGUID(t *testing.T) {
for _, c := range []struct {
guid string
raw [16]byte
}{
{"00000000-0000-0000-0000-000000000000", [16]byte{}},
{"00010203-0405-0607-0809-0a0b0c0d0e0f", [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
{"6ba7b810-9dad-11d1-80b4-00c04fd430c8", [16]byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}},
{"6BA7B810-9DAD-11D1-80B4-00C04FD430C8", [16]byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}},
{"ffffffff-ffff-ffff-ffff-ffffffffffff", [16]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
} {
raw, err := tun.ParseGUID(c.guid)
if err != nil {
t.Errorf("ParseGUID(%q) failed: %v", c.guid, err)
continue
}
if raw != c.raw {
t.Errorf("ParseGUID(%q) = %x, want %x", c.guid, raw, c.raw)
}
}
}
func TestParseGUIDInvalid(t *testing.T) {
for _, guid := range []string{
"",
" ",
"xray0",
// right characters, wrong places of the hyphens
"6ba7b8109-dad-11d1-80b4-00c04fd430c8",
"6ba7b810-9dad-11d1-80b4-00c04fd430c",
"6ba7b810-9dad-11d1-80b4-00c04fd430c88",
"6ba7b810-9dad-11d1-80b4_00c04fd430c8",
// not hexadecimal
"6ba7b81g-9dad-11d1-80b4-00c04fd430c8",
"6ba7b810-9dad-11d1-80b4-00c04fd430cg",
// forms other than the canonical one
"6ba7b8109dad11d180b400c04fd430c8",
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
"urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"6ba7b810-9dad-11d1-80b4-00c04fd430c8 ",
} {
if raw, err := tun.ParseGUID(guid); err == nil {
t.Errorf("ParseGUID(%q) = %x, want an error", guid, raw)
}
}
}
+54
View File
@@ -0,0 +1,54 @@
//go:build windows
package tun
import (
"crypto/md5"
"testing"
"unsafe"
"golang.org/x/sys/windows"
)
func TestAdapterGUID(t *testing.T) {
// a configured GUID has to be the adapter GUID the text names: the groups
// are field values, not the bytes of the text in order
guid, err := adapterGUID("xray0", "01020304-0506-0708-090a-0b0c0d0e0f10")
if err != nil {
t.Fatal(err)
}
want := windows.GUID{
Data1: 0x01020304,
Data2: 0x0506,
Data3: 0x0708,
Data4: [8]byte{0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10},
}
if *guid != want {
t.Errorf("adapterGUID = %+v, want %+v", *guid, want)
}
// the same struct Windows builds from that text, and the text Windows shows
// for it
if fromText, err := windows.GUIDFromString("{01020304-0506-0708-090A-0B0C0D0E0F10}"); err != nil {
t.Fatal(err)
} else if *guid != fromText {
t.Errorf("adapterGUID = %s, want %s", guid.String(), fromText.String())
}
if got, want := guid.String(), "{01020304-0506-0708-090A-0B0C0D0E0F10}"; got != want {
t.Errorf("adapterGUID = %s, want %s", got, want)
}
// without one, the bytes of the MD5 of the name, as they always were; that
// GUID is never read back as text, so its byte order does not matter
guid, err = adapterGUID("xray0", "")
if err != nil {
t.Fatal(err)
}
sum := md5.Sum([]byte("xray0"))
if got := unsafe.Slice((*byte)(unsafe.Pointer(guid)), 16); string(got) != string(sum[:]) {
t.Errorf("adapterGUID = %x, want %x", got, sum)
}
if _, err := adapterGUID("xray0", "xray0"); err == nil {
t.Error("an invalid guid was accepted")
}
}
+28 -5
View File
@@ -59,7 +59,7 @@ var _ Tun = (*WindowsTun)(nil)
// interface with the same name exist, it tried to be reused.
func NewTun(options *Config) (Tun, error) {
// instantiate wintun adapter
adapter, err := open(options.Name, options.Desc)
adapter, err := open(options.Name, options.Desc, options.GUID)
if err != nil {
return nil, err
}
@@ -82,10 +82,11 @@ func NewTun(options *Config) (Tun, error) {
return tun, nil
}
func open(name, desc string) (*wintun.Adapter, error) {
// generate a deterministic GUID from the adapter name
id := md5.Sum([]byte(name))
guid := (*windows.GUID)(unsafe.Pointer(&id[0]))
func open(name, desc, id string) (*wintun.Adapter, error) {
guid, err := adapterGUID(name, id)
if err != nil {
return nil, err
}
// try to open existing adapter by name
adapter, err := wintun.OpenAdapter(name)
if err == nil {
@@ -99,6 +100,28 @@ func open(name, desc string) (*wintun.Adapter, error) {
return nil, err
}
// adapterGUID returns the GUID that an adapter created with the given name is
// to have: the configured one, if there is one, or else one derived from the name.
func adapterGUID(name, id string) (*windows.GUID, error) {
if id == "" {
// generate a deterministic GUID from the adapter name
sum := md5.Sum([]byte(name))
return (*windows.GUID)(unsafe.Pointer(&sum[0])), nil
}
raw, err := ParseGUID(id)
if err != nil {
return nil, err
}
// the groups are the field values, big-endian in the canonical bytes; Windows
// holds them little-endian in memory, which the fields take care of
return &windows.GUID{
Data1: binary.BigEndian.Uint32(raw[0:4]),
Data2: binary.BigEndian.Uint16(raw[4:6]),
Data3: binary.BigEndian.Uint16(raw[6:8]),
Data4: [8]byte(raw[8:16]),
}, nil
}
// ipv6Off tells whether DisabledComponents turns IPv6 off on the non-tunnel
// interfaces (0x10, as 0xFF does), the TUN among them. Windows applies it at
// boot only.