1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/app/router/condition_test.go

405 lines
8.8 KiB
Go
Raw Normal View History

2017-05-08 06:18:13 -04:00
package router_test
import (
2017-11-06 16:30:56 -05:00
"os"
"path/filepath"
"strconv"
2017-05-08 06:18:13 -04:00
"testing"
2017-11-06 16:30:56 -05:00
proto "github.com/golang/protobuf/proto"
2018-10-22 10:14:00 -04:00
2017-05-08 06:18:13 -04:00
. "v2ray.com/core/app/router"
2017-11-06 16:30:56 -05:00
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
2017-05-08 06:18:13 -04:00
"v2ray.com/core/common/net"
2017-11-06 16:30:56 -05:00
"v2ray.com/core/common/platform"
2019-02-10 13:04:11 -05:00
"v2ray.com/core/common/platform/filesystem"
2017-05-17 07:28:22 -04:00
"v2ray.com/core/common/protocol"
2018-08-02 07:28:13 -04:00
"v2ray.com/core/common/protocol/http"
2018-10-22 10:14:00 -04:00
"v2ray.com/core/common/session"
2017-05-08 06:18:13 -04:00
)
2019-02-10 13:04:11 -05:00
func init() {
wd, err := os.Getwd()
common.Must(err)
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
}
func withOutbound(outbound *session.Outbound) *Context {
return &Context{Outbound: outbound}
}
func withInbound(inbound *session.Inbound) *Context {
return &Context{Inbound: inbound}
2018-10-15 02:36:50 -04:00
}
2017-05-17 07:24:53 -04:00
func TestRoutingRule(t *testing.T) {
type ruleTest struct {
input *Context
2017-05-17 07:24:53 -04:00
output bool
}
cases := []struct {
rule *RoutingRule
test []ruleTest
}{
{
rule: &RoutingRule{
Domain: []*Domain{
{
Value: "v2ray.com",
Type: Domain_Plain,
},
{
Value: "google.com",
Type: Domain_Domain,
},
2017-05-17 15:46:57 -04:00
{
Value: "^facebook\\.com$",
Type: Domain_Regex,
},
2017-05-17 07:24:53 -04:00
},
},
test: []ruleTest{
2017-12-02 19:04:38 -05:00
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)}),
2017-05-17 07:24:53 -04:00
output: true,
},
2017-12-02 19:04:38 -05:00
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.v2ray.com.www"), 80)}),
2017-05-17 07:24:53 -04:00
output: true,
},
2017-12-02 19:04:38 -05:00
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.co"), 80)}),
2017-05-17 07:24:53 -04:00
output: false,
},
2017-12-02 19:04:38 -05:00
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.google.com"), 80)}),
2017-05-17 07:24:53 -04:00
output: true,
},
2017-12-02 19:04:38 -05:00
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("facebook.com"), 80)}),
2017-05-17 15:46:57 -04:00
output: true,
},
2017-12-02 19:04:38 -05:00
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.facebook.com"), 80)}),
2017-05-17 15:46:57 -04:00
output: false,
},
2017-12-02 19:04:38 -05:00
{
input: &Context{},
2017-05-17 15:46:57 -04:00
output: false,
},
2017-05-17 07:24:53 -04:00
},
},
{
rule: &RoutingRule{
Cidr: []*CIDR{
{
Ip: []byte{8, 8, 8, 8},
Prefix: 32,
},
{
Ip: []byte{8, 8, 8, 8},
Prefix: 32,
},
{
Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
Prefix: 128,
},
},
},
test: []ruleTest{
2017-12-02 19:04:38 -05:00
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
2017-05-17 07:24:53 -04:00
output: true,
},
2017-12-02 19:04:38 -05:00
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
2017-05-17 07:24:53 -04:00
output: false,
},
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
output: true,
},
{
input: &Context{},
output: false,
},
},
},
{
rule: &RoutingRule{
Geoip: []*GeoIP{
{
Cidr: []*CIDR{
{
Ip: []byte{8, 8, 8, 8},
Prefix: 32,
},
{
Ip: []byte{8, 8, 8, 8},
Prefix: 32,
},
{
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,
},
2017-12-02 19:04:38 -05:00
{
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
2017-05-17 07:24:53 -04:00
output: true,
},
2017-12-02 19:04:38 -05:00
{
input: &Context{},
2017-05-17 15:46:57 -04:00
output: false,
},
2017-05-17 07:24:53 -04:00
},
},
2018-10-22 14:59:01 -04:00
{
rule: &RoutingRule{
SourceCidr: []*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,
},
},
},
2017-05-17 07:28:22 -04:00
{
rule: &RoutingRule{
UserEmail: []string{
"admin@v2ray.com",
},
},
test: []ruleTest{
2017-12-02 19:04:38 -05:00
{
2018-10-15 02:36:50 -04:00
input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "admin@v2ray.com"}}),
2017-05-17 07:28:22 -04:00
output: true,
},
2017-12-02 19:04:38 -05:00
{
2018-10-15 02:36:50 -04:00
input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "love@v2ray.com"}}),
2017-05-17 07:28:22 -04:00
output: false,
},
2017-12-02 19:04:38 -05:00
{
input: &Context{},
2017-05-17 15:46:57 -04:00
output: false,
},
2017-05-17 07:28:22 -04:00
},
},
2018-08-02 07:28:13 -04:00
{
rule: &RoutingRule{
Protocol: []string{"http"},
},
test: []ruleTest{
{
input: &Context{Content: &session.Content{Protocol: (&http.SniffHeader{}).Protocol()}},
2018-08-02 07:28:13 -04:00
output: true,
},
},
},
2018-10-22 14:59:01 -04:00
{
rule: &RoutingRule{
InboundTag: []string{"test", "test1"},
},
test: []ruleTest{
{
input: withInbound(&session.Inbound{Tag: "test"}),
output: true,
},
{
input: withInbound(&session.Inbound{Tag: "test2"}),
output: false,
},
},
},
2019-02-24 17:43:00 -05:00
{
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{
Protocol: []string{"http"},
Attributes: "attrs[':path'].startswith('/test')",
},
test: []ruleTest{
{
input: &Context{Content: &session.Content{Protocol: "http/1.1", Attributes: map[string]interface{}{":path": "/test/1"}}},
output: true,
},
},
},
2017-05-17 07:24:53 -04:00
}
for _, test := range cases {
cond, err := test.rule.BuildCondition()
2018-10-22 14:59:01 -04:00
common.Must(err)
2017-05-17 07:24:53 -04:00
2018-10-22 14:59:01 -04:00
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)
}
2017-05-17 07:24:53 -04:00
}
}
}
2017-11-06 16:30:56 -05:00
func loadGeoSite(country string) ([]*Domain, error) {
2019-02-10 13:04:11 -05:00
geositeBytes, err := filesystem.ReadAsset("geosite.dat")
2017-11-06 16:30:56 -05:00
if err != nil {
return nil, err
}
var geositeList GeoSiteList
if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
return nil, err
}
for _, site := range geositeList.Entry {
if site.CountryCode == country {
return site.Domain, nil
}
}
return nil, errors.New("country not found: " + country)
}
func TestChinaSites(t *testing.T) {
domains, err := loadGeoSite("CN")
2019-01-31 14:57:01 -05:00
common.Must(err)
2017-11-06 16:30:56 -05:00
2018-08-19 16:28:02 -04:00
matcher, err := NewDomainMatcher(domains)
2018-08-19 15:04:15 -04:00
common.Must(err)
2017-11-06 16:30:56 -05:00
2019-01-31 14:57:01 -05:00
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,
},
}
2017-11-06 16:30:56 -05:00
for i := 0; i < 1024; i++ {
2019-01-31 14:57:01 -05:00
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("expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r)
}
2017-11-06 16:30:56 -05:00
}
}
2018-11-07 17:57:06 -05:00
func BenchmarkMultiGeoIPMatcher(b *testing.B) {
var geoips []*GeoIP
{
ips, err := loadGeoIP("CN")
common.Must(err)
geoips = append(geoips, &GeoIP{
CountryCode: "CN",
Cidr: ips,
})
}
{
ips, err := loadGeoIP("JP")
common.Must(err)
geoips = append(geoips, &GeoIP{
CountryCode: "JP",
Cidr: ips,
})
}
{
ips, err := loadGeoIP("CA")
common.Must(err)
geoips = append(geoips, &GeoIP{
CountryCode: "CA",
Cidr: ips,
})
}
{
ips, err := loadGeoIP("US")
common.Must(err)
geoips = append(geoips, &GeoIP{
CountryCode: "US",
Cidr: ips,
})
}
matcher, err := NewMultiGeoIPMatcher(geoips, false)
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)
}
}