2019-02-10 13:04:11 -05:00
|
|
|
package conf_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
|
|
|
"v2ray.com/core/app/router"
|
2019-02-24 17:43:00 -05:00
|
|
|
"v2ray.com/core/common/net"
|
2019-02-10 13:04:11 -05:00
|
|
|
. "v2ray.com/core/infra/conf"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouterConfig(t *testing.T) {
|
|
|
|
createParser := func() func(string) (proto.Message, error) {
|
|
|
|
return func(s string) (proto.Message, error) {
|
|
|
|
config := new(RouterConfig)
|
|
|
|
if err := json.Unmarshal([]byte(s), config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return config.Build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runMultiTestCase(t, []TestCase{
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"strategy": "rules",
|
|
|
|
"settings": {
|
|
|
|
"domainStrategy": "AsIs",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"domain": [
|
|
|
|
"baidu.com",
|
|
|
|
"qq.com"
|
|
|
|
],
|
|
|
|
"outboundTag": "direct"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"ip": [
|
|
|
|
"10.0.0.0/8",
|
|
|
|
"::1/128"
|
|
|
|
],
|
|
|
|
"outboundTag": "test"
|
2019-02-24 17:43:00 -05:00
|
|
|
},{
|
|
|
|
"type": "field",
|
|
|
|
"port": "53, 443, 1000-2000",
|
|
|
|
"outboundTag": "test"
|
2019-05-11 11:14:38 -04:00
|
|
|
},{
|
|
|
|
"type": "field",
|
|
|
|
"port": 123,
|
|
|
|
"outboundTag": "test"
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"balancers": [
|
|
|
|
{
|
|
|
|
"tag": "b1",
|
|
|
|
"selector": ["test"]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
|
|
|
Output: &router.Config{
|
|
|
|
DomainStrategy: router.Config_AsIs,
|
|
|
|
BalancingRule: []*router.BalancingRule{
|
|
|
|
{
|
|
|
|
Tag: "b1",
|
|
|
|
OutboundSelector: []string{"test"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Rule: []*router.RoutingRule{
|
|
|
|
{
|
|
|
|
Domain: []*router.Domain{
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "baidu.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "qq.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Geoip: []*router.GeoIP{
|
|
|
|
{
|
|
|
|
Cidr: []*router.CIDR{
|
|
|
|
{
|
|
|
|
Ip: []byte{10, 0, 0, 0},
|
|
|
|
Prefix: 8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
|
|
|
Prefix: 128,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "test",
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 17:43:00 -05:00
|
|
|
{
|
|
|
|
PortList: &net.PortList{
|
|
|
|
Range: []*net.PortRange{
|
|
|
|
{From: 53, To: 53},
|
|
|
|
{From: 443, To: 443},
|
|
|
|
{From: 1000, To: 2000},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "test",
|
|
|
|
},
|
|
|
|
},
|
2019-05-11 11:14:38 -04:00
|
|
|
{
|
|
|
|
PortList: &net.PortList{
|
|
|
|
Range: []*net.PortRange{
|
|
|
|
{From: 123, To: 123},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "test",
|
|
|
|
},
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"strategy": "rules",
|
|
|
|
"settings": {
|
|
|
|
"domainStrategy": "IPIfNonMatch",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"domain": [
|
|
|
|
"baidu.com",
|
|
|
|
"qq.com"
|
|
|
|
],
|
|
|
|
"outboundTag": "direct"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"ip": [
|
|
|
|
"10.0.0.0/8",
|
|
|
|
"::1/128"
|
|
|
|
],
|
|
|
|
"outboundTag": "test"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
|
|
|
Output: &router.Config{
|
|
|
|
DomainStrategy: router.Config_IpIfNonMatch,
|
|
|
|
Rule: []*router.RoutingRule{
|
|
|
|
{
|
|
|
|
Domain: []*router.Domain{
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "baidu.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "qq.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Geoip: []*router.GeoIP{
|
|
|
|
{
|
|
|
|
Cidr: []*router.CIDR{
|
|
|
|
{
|
|
|
|
Ip: []byte{10, 0, 0, 0},
|
|
|
|
Prefix: 8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
|
|
|
Prefix: 128,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: `{
|
|
|
|
"domainStrategy": "AsIs",
|
|
|
|
"rules": [
|
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"domain": [
|
|
|
|
"baidu.com",
|
|
|
|
"qq.com"
|
|
|
|
],
|
|
|
|
"outboundTag": "direct"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"ip": [
|
|
|
|
"10.0.0.0/8",
|
|
|
|
"::1/128"
|
|
|
|
],
|
|
|
|
"outboundTag": "test"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
|
|
|
Output: &router.Config{
|
|
|
|
DomainStrategy: router.Config_AsIs,
|
|
|
|
Rule: []*router.RoutingRule{
|
|
|
|
{
|
|
|
|
Domain: []*router.Domain{
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "baidu.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "qq.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Geoip: []*router.GeoIP{
|
|
|
|
{
|
|
|
|
Cidr: []*router.CIDR{
|
|
|
|
{
|
|
|
|
Ip: []byte{10, 0, 0, 0},
|
|
|
|
Prefix: 8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
|
|
|
|
Prefix: 128,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|