mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-05-08 14:13:22 +00:00
https://github.com/XTLS/Xray-core/pull/5947#issuecomment-4258063215 https://github.com/XTLS/Xray-core/pull/5951#issuecomment-4260093653
405 lines
9.6 KiB
Go
405 lines
9.6 KiB
Go
package router_test
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
|
|
. "github.com/xtls/xray-core/app/router"
|
|
"github.com/xtls/xray-core/common"
|
|
"github.com/xtls/xray-core/common/geodata"
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
"github.com/xtls/xray-core/common/protocol/http"
|
|
"github.com/xtls/xray-core/common/session"
|
|
"github.com/xtls/xray-core/features/routing"
|
|
routing_session "github.com/xtls/xray-core/features/routing/session"
|
|
)
|
|
|
|
func withBackground() routing.Context {
|
|
return &routing_session.Context{}
|
|
}
|
|
|
|
func withOutbound(outbound *session.Outbound) routing.Context {
|
|
return &routing_session.Context{Outbound: outbound}
|
|
}
|
|
|
|
func withInbound(inbound *session.Inbound) routing.Context {
|
|
return &routing_session.Context{Inbound: inbound}
|
|
}
|
|
|
|
func withContent(content *session.Content) routing.Context {
|
|
return &routing_session.Context{Content: content}
|
|
}
|
|
|
|
func TestRoutingRule(t *testing.T) {
|
|
type ruleTest struct {
|
|
input routing.Context
|
|
output bool
|
|
}
|
|
|
|
cases := []struct {
|
|
rule *RoutingRule
|
|
test []ruleTest
|
|
}{
|
|
{
|
|
rule: &RoutingRule{
|
|
Domain: []*geodata.DomainRule{
|
|
{
|
|
Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Substr, Value: "example.com"}},
|
|
},
|
|
{
|
|
Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Domain, Value: "google.com"}},
|
|
},
|
|
{
|
|
Value: &geodata.DomainRule_Custom{Custom: &geodata.Domain{Type: geodata.Domain_Regex, Value: "^facebook\\.com$"}},
|
|
},
|
|
},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("example.com"), 80)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.example.com.www"), 80)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("example.co"), 80)}),
|
|
output: false,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.google.com"), 80)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("facebook.com"), 80)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.facebook.com"), 80)}),
|
|
output: false,
|
|
},
|
|
{
|
|
input: withBackground(),
|
|
output: false,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
rule: &RoutingRule{
|
|
Ip: []*geodata.IPRule{
|
|
{
|
|
Value: &geodata.IPRule_Custom{
|
|
Custom: &geodata.CIDRRule{
|
|
Cidr: &geodata.CIDR{Ip: []byte{8, 8, 8, 8}, Prefix: 32},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Value: &geodata.IPRule_Custom{
|
|
Custom: &geodata.CIDRRule{
|
|
Cidr: &geodata.CIDR{Ip: []byte{8, 8, 8, 8}, Prefix: 32},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Value: &geodata.IPRule_Custom{
|
|
Custom: &geodata.CIDRRule{
|
|
Cidr: &geodata.CIDR{Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(), Prefix: 128},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
|
|
output: false,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withBackground(),
|
|
output: false,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
rule: &RoutingRule{
|
|
SourceIp: []*geodata.IPRule{
|
|
{
|
|
Value: &geodata.IPRule_Custom{
|
|
Custom: &geodata.CIDRRule{
|
|
Cidr: &geodata.CIDR{Ip: []byte{192, 168, 0, 0}, Prefix: 16},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("192.168.0.1"), 80)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("10.0.0.1"), 80)}),
|
|
output: false,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
rule: &RoutingRule{
|
|
UserEmail: []string{
|
|
"admin@example.com",
|
|
},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "admin@example.com"}}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "love@example.com"}}),
|
|
output: false,
|
|
},
|
|
{
|
|
input: withBackground(),
|
|
output: false,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
rule: &RoutingRule{
|
|
Protocol: []string{"http"},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withContent(&session.Content{Protocol: (&http.SniffHeader{}).Protocol()}),
|
|
output: true,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
rule: &RoutingRule{
|
|
InboundTag: []string{"test", "test1"},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withInbound(&session.Inbound{Tag: "test"}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withInbound(&session.Inbound{Tag: "test2"}),
|
|
output: false,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
rule: &RoutingRule{
|
|
PortList: &net.PortList{
|
|
Range: []*net.PortRange{
|
|
{From: 443, To: 443},
|
|
{From: 1000, To: 1100},
|
|
},
|
|
},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 443)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 1100)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 1005)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 53)}),
|
|
output: false,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
rule: &RoutingRule{
|
|
SourcePortList: &net.PortList{
|
|
Range: []*net.PortRange{
|
|
{From: 123, To: 123},
|
|
{From: 9993, To: 9999},
|
|
},
|
|
},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 123)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 9999)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 9994)}),
|
|
output: true,
|
|
},
|
|
{
|
|
input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 53)}),
|
|
output: false,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
rule: &RoutingRule{
|
|
Protocol: []string{"http"},
|
|
Attributes: map[string]string{
|
|
":path": "/test",
|
|
},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withContent(&session.Content{Protocol: "http/1.1", Attributes: map[string]string{":path": "/test/1"}}),
|
|
output: true,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
rule: &RoutingRule{
|
|
Attributes: map[string]string{
|
|
"Custom": "p([a-z]+)ch",
|
|
},
|
|
},
|
|
test: []ruleTest{
|
|
{
|
|
input: withContent(&session.Content{Attributes: map[string]string{"custom": "peach"}}),
|
|
output: true,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range cases {
|
|
cond, err := test.rule.BuildCondition()
|
|
common.Must(err)
|
|
|
|
for _, subtest := range test.test {
|
|
actual := cond.Apply(subtest.input)
|
|
if actual != subtest.output {
|
|
t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestChinaSites(t *testing.T) {
|
|
t.Setenv("xray.location.asset", filepath.Join("..", "..", "resources"))
|
|
rules, err := geodata.ParseDomainRules([]string{"geosite:cn"}, geodata.Domain_Substr)
|
|
common.Must(err)
|
|
|
|
matcher, err := NewDomainMatcher(rules)
|
|
common.Must(err)
|
|
|
|
type TestCase struct {
|
|
Domain string
|
|
Output bool
|
|
}
|
|
testCases := []TestCase{
|
|
{
|
|
Domain: "163.com",
|
|
Output: true,
|
|
},
|
|
{
|
|
Domain: "163.com",
|
|
Output: true,
|
|
},
|
|
{
|
|
Domain: "164.com",
|
|
Output: false,
|
|
},
|
|
{
|
|
Domain: "164.com",
|
|
Output: false,
|
|
},
|
|
}
|
|
|
|
for i := 0; i < 1024; i++ {
|
|
testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
r := matcher.ApplyDomain(testCase.Domain)
|
|
if r != testCase.Output {
|
|
t.Error("DomainMatcher expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkMphDomainMatcher(b *testing.B) {
|
|
b.Setenv("xray.location.asset", filepath.Join("..", "..", "resources"))
|
|
rules, err := geodata.ParseDomainRules([]string{"geosite:cn"}, geodata.Domain_Substr)
|
|
common.Must(err)
|
|
|
|
matcher, err := NewDomainMatcher(rules)
|
|
common.Must(err)
|
|
|
|
type TestCase struct {
|
|
Domain string
|
|
Output bool
|
|
}
|
|
testCases := []TestCase{
|
|
{
|
|
Domain: "163.com",
|
|
Output: true,
|
|
},
|
|
{
|
|
Domain: "163.com",
|
|
Output: true,
|
|
},
|
|
{
|
|
Domain: "164.com",
|
|
Output: false,
|
|
},
|
|
{
|
|
Domain: "164.com",
|
|
Output: false,
|
|
},
|
|
}
|
|
|
|
for i := 0; i < 1024; i++ {
|
|
testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
|
|
}
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
for _, testCase := range testCases {
|
|
_ = matcher.ApplyDomain(testCase.Domain)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkMultiGeoIPMatcher(b *testing.B) {
|
|
b.Setenv("xray.location.asset", filepath.Join("..", "..", "resources"))
|
|
rules, err := geodata.ParseIPRules([]string{"geoip:cn", "geoip:jp", "geoip:ca", "geoip:us"})
|
|
common.Must(err)
|
|
|
|
matcher, err := NewIPMatcher(rules, MatcherAsType_Target)
|
|
common.Must(err)
|
|
|
|
ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)})
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
_ = matcher.Apply(ctx)
|
|
}
|
|
}
|