1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 01:17:30 -05:00
v2fly/app/router/condition_test.go

237 lines
5.3 KiB
Go
Raw Normal View History

2017-05-08 06:18:13 -04:00
package router_test
import (
"context"
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
"time"
2017-05-08 06:18:13 -04:00
2017-11-06 16:30:56 -05:00
proto "github.com/golang/protobuf/proto"
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"
2017-05-17 07:28:22 -04:00
"v2ray.com/core/common/protocol"
2017-05-08 06:18:13 -04:00
"v2ray.com/core/proxy"
2017-10-24 10:15:35 -04:00
. "v2ray.com/ext/assert"
2017-11-06 16:30:56 -05:00
"v2ray.com/ext/sysio"
2017-05-08 06:18:13 -04:00
)
func TestSubDomainMatcher(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-05-08 06:18:13 -04:00
cases := []struct {
pattern string
2017-11-06 15:12:28 -05:00
input string
2017-05-08 06:18:13 -04:00
output bool
}{
{
pattern: "v2ray.com",
2017-11-06 15:12:28 -05:00
input: "www.v2ray.com",
2017-05-08 06:18:13 -04:00
output: true,
},
{
pattern: "v2ray.com",
2017-11-06 15:12:28 -05:00
input: "v2ray.com",
2017-05-08 06:18:13 -04:00
output: true,
},
{
pattern: "v2ray.com",
2017-11-06 15:12:28 -05:00
input: "www.v3ray.com",
2017-05-08 06:18:13 -04:00
output: false,
},
{
pattern: "v2ray.com",
2017-11-06 15:12:28 -05:00
input: "2ray.com",
2017-05-08 06:18:13 -04:00
output: false,
},
2017-05-09 07:39:09 -04:00
{
pattern: "v2ray.com",
2017-11-06 15:12:28 -05:00
input: "xv2ray.com",
2017-05-09 07:39:09 -04:00
output: false,
},
2017-05-08 06:18:13 -04:00
}
for _, test := range cases {
matcher := NewSubDomainMatcher(test.pattern)
2017-10-24 10:15:35 -04:00
assert(matcher.Apply(test.input) == test.output, IsTrue)
2017-05-08 06:18:13 -04:00
}
}
2017-05-17 07:24:53 -04:00
func TestRoutingRule(t *testing.T) {
2017-10-24 10:15:35 -04:00
assert := With(t)
2017-05-17 07:24:53 -04:00
type ruleTest struct {
input context.Context
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{
ruleTest{
input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.com"), 80)),
output: true,
},
ruleTest{
input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.v2ray.com.www"), 80)),
output: true,
},
ruleTest{
input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("v2ray.co"), 80)),
output: false,
},
ruleTest{
input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.google.com"), 80)),
output: true,
},
2017-05-17 15:46:57 -04:00
ruleTest{
input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("facebook.com"), 80)),
output: true,
},
ruleTest{
input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.DomainAddress("www.facebook.com"), 80)),
output: false,
},
ruleTest{
input: context.Background(),
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{
ruleTest{
input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)),
output: true,
},
ruleTest{
input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)),
output: false,
},
ruleTest{
input: proxy.ContextWithTarget(context.Background(), net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)),
output: true,
},
2017-05-17 15:46:57 -04:00
ruleTest{
input: context.Background(),
output: false,
},
2017-05-17 07:24:53 -04:00
},
},
2017-05-17 07:28:22 -04:00
{
rule: &RoutingRule{
UserEmail: []string{
"admin@v2ray.com",
},
},
test: []ruleTest{
ruleTest{
input: protocol.ContextWithUser(context.Background(), &protocol.User{Email: "admin@v2ray.com"}),
output: true,
},
ruleTest{
input: protocol.ContextWithUser(context.Background(), &protocol.User{Email: "love@v2ray.com"}),
output: false,
},
2017-05-17 15:46:57 -04:00
ruleTest{
input: context.Background(),
output: false,
},
2017-05-17 07:28:22 -04:00
},
},
2017-05-17 07:24:53 -04:00
}
for _, test := range cases {
cond, err := test.rule.BuildCondition()
2017-10-24 10:15:35 -04:00
assert(err, IsNil)
2017-05-17 07:24:53 -04:00
for _, t := range test.test {
2017-10-24 10:15:35 -04:00
assert(cond.Apply(t.input), Equals, t.output)
2017-05-17 07:24:53 -04:00
}
}
}
2017-11-06 16:30:56 -05:00
func loadGeoSite(country string) ([]*Domain, error) {
geositeBytes, err := sysio.ReadAsset("geosite.dat")
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) {
assert := With(t)
common.Must(sysio.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "tools", "release", "config", "geosite.dat")))
domains, err := loadGeoSite("CN")
assert(err, IsNil)
matcher := NewCachableDomainMatcher()
for _, d := range domains {
assert(matcher.Add(d), IsNil)
}
assert(matcher.ApplyDomain("163.com"), IsTrue)
assert(matcher.ApplyDomain("163.com"), IsTrue)
assert(matcher.ApplyDomain("164.com"), IsFalse)
assert(matcher.ApplyDomain("164.com"), IsFalse)
for i := 0; i < 1024; i++ {
assert(matcher.ApplyDomain(strconv.Itoa(i)+".not-exists.com"), IsFalse)
}
time.Sleep(time.Second * 10)
for i := 0; i < 1024; i++ {
assert(matcher.ApplyDomain(strconv.Itoa(i)+".not-exists2.com"), IsFalse)
}
}