mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-27 03:50:08 +00:00
As requested in https://github.com/XTLS/Xray-core/pull/7460#issuecomment-7131171930: proxy/tun goes from 36 files to 29. No behaviour change: the merged code is moved as it is. Merged: - guid.go into config.go - tun_windows_outbound.go into tun_windows.go - tun_windows_wfp_test.go and guid_windows_test.go into tun_windows_test.go - tun_freebsd_dns_test.go into tun_freebsd_test.go - tun_linux_dns_test.go and tun_linux_dns_route_test.go into tun_linux_test.go Renamed wfp_leak.go and wfp_leak_test.go to wfp_block.go and wfp_block_test.go. Removed: - guid_test.go: TestTunConfigGUID in infra/conf covers what ParseGUID accepts, and TestAdapterGUID the bytes it returns. The one case they lacked, a hyphen out of place, is now in TestTunConfigGUID. - TestLeakCovers: covers is only ever asked about port 53, which TestExpandWfpBlockLeak checks through BlocksSystemDNS. - udp_queue_bench_test.go and BenchmarkUDPNewSession: benchmarks, which CI does not run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
173 lines
9.2 KiB
Go
173 lines
9.2 KiB
Go
package conf_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"runtime"
|
|
"slices"
|
|
"testing"
|
|
|
|
. "github.com/xtls/xray-core/infra/conf"
|
|
"github.com/xtls/xray-core/proxy/tun"
|
|
)
|
|
|
|
func TestTunConfigAutoSystem(t *testing.T) {
|
|
creator := func() Buildable {
|
|
return new(TunConfig)
|
|
}
|
|
|
|
runMultiTestCase(t, []TestCase{
|
|
{
|
|
Input: `{"name": "xray0"}`,
|
|
Parser: loadJSON(creator),
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500},
|
|
},
|
|
{
|
|
Input: `{"name": "xray0", "gateway": ["10.0.0.1/24"], "autoSystemDnsToGateway": true}`,
|
|
Parser: loadJSON(creator),
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500, Gateway: []string{"10.0.0.1/24"}, AutoSystemDnsToGateway: true},
|
|
},
|
|
{
|
|
Input: `{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["dns", "misconfigtun"]}`,
|
|
Parser: loadJSON(creator),
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500, DNS: []string{"1.1.1.1"}, AutoSystemRoutingTable: []string{"0.0.0.0/0"}, AutoOutboundsInterface: "auto", AutoSystemWfpBlockLeak: []string{"53", "853", "windnsclient", "misconfigtun"}},
|
|
},
|
|
{
|
|
Input: `{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["DNS"]}`,
|
|
Parser: loadJSON(creator),
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500, DNS: []string{"1.1.1.1"}, AutoSystemRoutingTable: []string{"0.0.0.0/0"}, AutoOutboundsInterface: "auto", AutoSystemWfpBlockLeak: []string{"53", "853", "windnsclient"}},
|
|
},
|
|
{
|
|
Input: `{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["TCP/853", "udp/853", "tcp/0853"]}`,
|
|
Parser: loadJSON(creator),
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500, AutoSystemRoutingTable: []string{"0.0.0.0/0"}, AutoOutboundsInterface: "auto", AutoSystemWfpBlockLeak: []string{"tcp/853", "udp/853"}},
|
|
},
|
|
{
|
|
Input: `{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["All", "UDP", "tcp/8000-9000", "icmp"]}`,
|
|
Parser: loadJSON(creator),
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500, DNS: []string{"1.1.1.1"}, AutoSystemRoutingTable: []string{"0.0.0.0/0"}, AutoOutboundsInterface: "auto", AutoSystemWfpBlockLeak: []string{"all", "udp", "tcp/8000-9000", "icmp"}},
|
|
},
|
|
{
|
|
Input: `{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["AllExceptLAN"]}`,
|
|
Parser: loadJSON(creator),
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500, DNS: []string{"1.1.1.1"}, AutoSystemRoutingTable: []string{"0.0.0.0/0"}, AutoOutboundsInterface: "auto", AutoSystemWfpBlockLeak: []string{"allexceptlan", "53", "853", "windnsclient"}},
|
|
},
|
|
{
|
|
Input: `{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["1.1.1.1/24:TCP/853", "[2001:DB8::/32]:udp/10-20", "[::/0]:ICMP", "853"]}`,
|
|
Parser: loadJSON(creator),
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500, AutoSystemRoutingTable: []string{"0.0.0.0/0"}, AutoOutboundsInterface: "auto", AutoSystemWfpBlockLeak: []string{"1.1.1.0/24:tcp/853", "[2001:db8::/32]:udp/10-20", "[::/0]:icmp", "853"}},
|
|
},
|
|
{
|
|
Input: `{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["0.0.0.0/0:dns", "[::/0]:All", "53"]}`,
|
|
Parser: loadJSON(creator),
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500, DNS: []string{"1.1.1.1"}, AutoSystemRoutingTable: []string{"0.0.0.0/0"}, AutoOutboundsInterface: "auto", AutoSystemWfpBlockLeak: []string{"0.0.0.0/0:53", "0.0.0.0/0:853", "0.0.0.0/0:windnsclient", "[::/0]:all", "53"}},
|
|
},
|
|
})
|
|
}
|
|
|
|
// TestTunConfigAutoSystemNeeds checks that an option is rejected without the
|
|
// setting it needs, only on the system it takes effect on.
|
|
func TestTunConfigAutoSystemNeeds(t *testing.T) {
|
|
for _, c := range []struct {
|
|
input string
|
|
goos []string // where it is rejected
|
|
}{
|
|
{`{"name": "xray0", "autoSystemWfpBlockLeak": ["misconfigtun"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["misconfigtun"]}`, nil},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["dns"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["udp/53"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["windnsclient"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["tcp/853", "udp/853"]}`, nil},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["all"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["udp"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["tcp/50-60"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["tcp/54-60", "icmp"]}`, nil},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["53"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["50-60"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["[::/0]:udp/53"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["1.1.1.1/32:dns"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["[2001:db8::/32]:all"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["0.0.0.0/0:windnsclient"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["0.0.0.0/0:tcp/853", "1.1.1.0/24:icmp", "[::/0]:misconfigtun", "54-60"]}`, nil},
|
|
{`{"name": "xray0", "autoSystemWfpBlockLeak": ["[::/0]:misconfigtun"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemWfpBlockLeak": ["all"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["allexceptlan"]}`, []string{"windows"}},
|
|
{`{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["allexceptlan"]}`, nil},
|
|
{`{"name": "xray0", "autoSystemDnsToGateway": true}`, []string{"linux", "freebsd"}},
|
|
} {
|
|
config := new(TunConfig)
|
|
if err := json.Unmarshal([]byte(c.input), config); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := config.Build(); (err != nil) != slices.Contains(c.goos, runtime.GOOS) {
|
|
t.Errorf("%s on %s: error = %v", c.input, runtime.GOOS, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTunConfigAutoSystemWfpBlockLeakUnknown(t *testing.T) {
|
|
// With the settings the values need on Windows, so that only an unknown
|
|
// one fails.
|
|
build := func(value string) error {
|
|
config := new(TunConfig)
|
|
if err := json.Unmarshal([]byte(`{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["dns", "`+value+`"]}`), config); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := config.Build()
|
|
return err
|
|
}
|
|
if err := build("1.1.1.0/24:53"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, value := range []string{"ip", "tcp/0", "tcp/0-10", "icmp/1", "udp/20-10", "0", "20-10", "53/tcp", "tcpv4", "ipv4:udp", "1.1.1.1:53", "2001:db8::/32:53", "[1.1.1.0/24]:53", "[::ffff:1.1.1.0/120]:53", "1.1.1.0/24"} {
|
|
if build(value) == nil {
|
|
t.Errorf("autoSystemWfpBlockLeak value %q was accepted", value)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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",
|
|
"6ba7b8109-dad-11d1-80b4-00c04fd430c8", // the right length, a hyphen out of place
|
|
"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)
|
|
}
|
|
}
|
|
}
|