2026-09-30 09:26:03 +03:30
|
|
|
package conf_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"runtime"
|
2026-10-11 19:44:19 -07:00
|
|
|
"slices"
|
2026-09-30 09:26:03 +03:30
|
|
|
"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),
|
2026-10-31 16:46:31 +03:30
|
|
|
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{"udp/53", "tcp/53", "windnsclient", "tcp/853", "udp/853", "misconfigtun"}},
|
2026-09-30 09:26:03 +03:30
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: `{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["DNS"]}`,
|
|
|
|
|
Parser: loadJSON(creator),
|
2026-10-31 16:46:31 +03:30
|
|
|
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{"udp/53", "tcp/53", "windnsclient", "tcp/853", "udp/853"}},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
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"}},
|
2026-09-30 09:26:03 +03:30
|
|
|
},
|
2026-11-02 10:58:54 +03:30
|
|
|
{
|
2026-11-02 12:49:56 +03:30
|
|
|
Input: `{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["All", "UDP", "tcp/8000-9000", "icmp"]}`,
|
2026-11-02 10:58:54 +03:30
|
|
|
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"}},
|
|
|
|
|
},
|
2026-11-02 11:50:18 +03:30
|
|
|
{
|
|
|
|
|
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", "udp/53", "tcp/53", "windnsclient", "tcp/853", "udp/853"}},
|
|
|
|
|
},
|
2026-11-02 13:09:55 +03:30
|
|
|
{
|
2026-11-02 17:52:23 +03:30
|
|
|
Input: `{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["IPv4:TCP/853", "ipv6:udp/10-20", "IPv6:ICMP"]}`,
|
2026-11-02 13:09:55 +03:30
|
|
|
Parser: loadJSON(creator),
|
2026-11-02 17:52:23 +03:30
|
|
|
Output: &tun.Config{Name: "xray0", Desc: "Wintun", MTU: 1500, AutoSystemRoutingTable: []string{"0.0.0.0/0"}, AutoOutboundsInterface: "auto", AutoSystemWfpBlockLeak: []string{"ipv4:tcp/853", "ipv6:udp/10-20", "ipv6:icmp"}},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: `{"name": "xray0", "dns": ["1.1.1.1"], "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["ipv4:dns", "IPv6:All", "ipv6:udp/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{"udp/53", "ipv4:tcp/53", "ipv4:windnsclient", "ipv4:tcp/853", "ipv4:udp/853", "ipv6:all"}},
|
2026-11-02 13:09:55 +03:30
|
|
|
},
|
2026-09-30 09:26:03 +03:30
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
2026-10-11 19:44:19 -07:00
|
|
|
goos []string // where it is rejected
|
2026-09-30 09:26:03 +03:30
|
|
|
}{
|
2026-10-11 19:44:19 -07:00
|
|
|
{`{"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"}},
|
2026-10-31 16:46:31 +03:30
|
|
|
{`{"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},
|
2026-11-02 10:58:54 +03:30
|
|
|
{`{"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},
|
2026-11-02 17:52:23 +03:30
|
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["ipv6:udp/53"]}`, []string{"windows"}},
|
|
|
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["ipv4:dns"]}`, []string{"windows"}},
|
|
|
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["ipv6:all"]}`, []string{"windows"}},
|
|
|
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["ipv4:windnsclient"]}`, []string{"windows"}},
|
|
|
|
|
{`{"name": "xray0", "autoSystemRoutingTable": ["0.0.0.0/0"], "autoSystemWfpBlockLeak": ["ipv4:tcp/853", "ipv4:icmp", "ipv6:misconfigtun"]}`, nil},
|
|
|
|
|
{`{"name": "xray0", "autoSystemWfpBlockLeak": ["ipv6:misconfigtun"]}`, []string{"windows"}},
|
2026-11-02 10:58:54 +03:30
|
|
|
{`{"name": "xray0", "autoSystemWfpBlockLeak": ["all"]}`, []string{"windows"}},
|
2026-11-02 11:50:18 +03:30
|
|
|
{`{"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},
|
2026-10-11 19:44:19 -07:00
|
|
|
{`{"name": "xray0", "autoSystemDnsToGateway": true}`, []string{"linux", "freebsd"}},
|
2026-09-30 09:26:03 +03:30
|
|
|
} {
|
|
|
|
|
config := new(TunConfig)
|
|
|
|
|
if err := json.Unmarshal([]byte(c.input), config); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-10-11 19:44:19 -07:00
|
|
|
if _, err := config.Build(); (err != nil) != slices.Contains(c.goos, runtime.GOOS) {
|
2026-09-30 09:26:03 +03:30
|
|
|
t.Errorf("%s on %s: error = %v", c.input, runtime.GOOS, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTunConfigAutoSystemWfpBlockLeakUnknown(t *testing.T) {
|
2026-11-02 17:52:23 +03:30
|
|
|
for _, value := range []string{"ip", "tcp/0", "tcp/0-10", "icmp/1", "udp/20-10", "tcp4", "tcpv4", "ipv4", "ipv5:udp", "ipv4:icmp/1"} {
|
2026-10-31 16:46:31 +03:30
|
|
|
config := new(TunConfig)
|
|
|
|
|
if err := json.Unmarshal([]byte(`{"name": "xray0", "autoSystemWfpBlockLeak": ["dns", "`+value+`"]}`), config); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := config.Build(); err == nil {
|
|
|
|
|
t.Errorf("autoSystemWfpBlockLeak value %q was accepted", value)
|
|
|
|
|
}
|
2026-09-30 09:26:03 +03:30
|
|
|
}
|
|
|
|
|
}
|