1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-04 21:15:24 +00:00
v2fly/tools/conf/router_test.go

126 lines
5.3 KiB
Go
Raw Normal View History

2016-10-17 12:35:13 +00:00
package conf_test
2016-01-24 13:40:46 +00:00
import (
2017-02-02 13:42:31 +00:00
"context"
2016-10-17 12:35:13 +00:00
"net"
2016-01-24 13:40:46 +00:00
"testing"
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
2016-10-19 10:01:11 +00:00
"v2ray.com/core/proxy"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/testing/assert"
2016-10-17 12:35:13 +00:00
. "v2ray.com/core/tools/conf"
2016-01-24 13:40:46 +00:00
)
2016-10-17 12:35:13 +00:00
func makeDestination(ip string) v2net.Destination {
return v2net.TCPDestination(v2net.IPAddress(net.ParseIP(ip)), 80)
}
func makeDomainDestination(domain string) v2net.Destination {
return v2net.TCPDestination(v2net.DomainAddress(domain), 80)
}
func TestChinaIPJson(t *testing.T) {
assert := assert.On(t)
2017-02-02 13:42:31 +00:00
rule, err := ParseRule([]byte(`{
2016-10-17 12:35:13 +00:00
"type": "chinaip",
"outboundTag": "x"
}`))
2017-02-02 13:42:31 +00:00
assert.Error(err).IsNil()
2016-10-17 12:35:13 +00:00
assert.String(rule.Tag).Equals("x")
cond, err := rule.BuildCondition()
assert.Error(err).IsNil()
2017-02-09 21:49:38 +00:00
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("121.14.1.189"), 80)))).IsTrue() // sina.com.cn
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("101.226.103.106"), 80)))).IsTrue() // qq.com
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("115.239.210.36"), 80)))).IsTrue() // image.baidu.com
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("120.135.126.1"), 80)))).IsTrue()
2017-02-09 21:49:38 +00:00
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("8.8.8.8"), 80)))).IsFalse()
2016-10-17 12:35:13 +00:00
}
func TestChinaSitesJson(t *testing.T) {
assert := assert.On(t)
2017-02-02 13:42:31 +00:00
rule, err := ParseRule([]byte(`{
2016-10-17 12:35:13 +00:00
"type": "chinasites",
"outboundTag": "y"
}`))
2017-02-02 13:42:31 +00:00
assert.Error(err).IsNil()
2016-10-17 12:35:13 +00:00
assert.String(rule.Tag).Equals("y")
cond, err := rule.BuildCondition()
assert.Error(err).IsNil()
2017-02-09 21:49:38 +00:00
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("v.qq.com"), 80)))).IsTrue()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.163.com"), 80)))).IsTrue()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("ngacn.cc"), 80)))).IsTrue()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("12306.cn"), 80)))).IsTrue()
2017-02-09 21:49:38 +00:00
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("v2ray.com"), 80)))).IsFalse()
2016-10-17 12:35:13 +00:00
}
2016-01-24 14:36:55 +00:00
func TestDomainRule(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-01-24 13:40:46 +00:00
2017-02-02 13:42:31 +00:00
rule, err := ParseRule([]byte(`{
2016-01-24 13:40:46 +00:00
"type": "field",
"domain": [
"ooxx.com",
2016-01-24 16:36:55 +00:00
"oxox.com",
"regexp:\\.cn$"
2016-01-24 13:40:46 +00:00
],
2016-01-24 14:36:55 +00:00
"network": "tcp",
2016-01-24 13:40:46 +00:00
"outboundTag": "direct"
}`))
2017-02-02 13:42:31 +00:00
assert.Error(err).IsNil()
2016-01-24 13:40:46 +00:00
assert.Pointer(rule).IsNotNil()
2016-10-11 21:02:44 +00:00
cond, err := rule.BuildCondition()
assert.Error(err).IsNil()
2017-02-09 21:49:38 +00:00
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.ooxx.com"), 80)))).IsTrue()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.aabb.com"), 80)))).IsFalse()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.12306.cn"), 80)))).IsTrue()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.ParseAddress("www.acn.com"), 80)))).IsFalse()
2016-01-24 14:36:55 +00:00
}
func TestIPRule(t *testing.T) {
2016-05-24 19:55:46 +00:00
assert := assert.On(t)
2016-01-24 14:36:55 +00:00
2017-02-02 13:42:31 +00:00
rule, err := ParseRule([]byte(`{
2016-01-24 14:36:55 +00:00
"type": "field",
"ip": [
"10.0.0.0/8",
"192.0.0.0/24"
],
"network": "tcp",
"outboundTag": "direct"
}`))
2017-02-02 13:42:31 +00:00
assert.Error(err).IsNil()
2016-01-24 14:36:55 +00:00
assert.Pointer(rule).IsNotNil()
2016-10-11 21:02:44 +00:00
cond, err := rule.BuildCondition()
assert.Error(err).IsNil()
2017-02-09 21:49:38 +00:00
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80)))).IsFalse()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)))).IsTrue()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
assert.Bool(cond.Apply(proxy.ContextWithTarget(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80)))).IsTrue()
2016-01-24 13:40:46 +00:00
}
2016-11-09 09:56:15 +00:00
func TestSourceIPRule(t *testing.T) {
assert := assert.On(t)
2017-02-02 13:42:31 +00:00
rule, err := ParseRule([]byte(`{
2016-11-09 09:56:15 +00:00
"type": "field",
"source": [
"10.0.0.0/8",
"192.0.0.0/24"
],
"outboundTag": "direct"
}`))
2017-02-02 13:42:31 +00:00
assert.Error(err).IsNil()
2016-11-09 09:56:15 +00:00
assert.Pointer(rule).IsNotNil()
cond, err := rule.BuildCondition()
assert.Error(err).IsNil()
assert.Bool(cond.Apply(proxy.ContextWithSource(context.Background(), v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80)))).IsFalse()
assert.Bool(cond.Apply(proxy.ContextWithSource(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)))).IsTrue()
assert.Bool(cond.Apply(proxy.ContextWithSource(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse()
assert.Bool(cond.Apply(proxy.ContextWithSource(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80)))).IsTrue()
2016-11-09 09:56:15 +00:00
}