2020-11-25 11:01:53 +00:00
|
|
|
package conf_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/app/router"
|
2021-03-24 15:01:20 +00:00
|
|
|
"github.com/xtls/xray-core/common/matcher/domain"
|
|
|
|
"github.com/xtls/xray-core/common/matcher/geoip"
|
2020-12-04 01:36:16 +00:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
. "github.com/xtls/xray-core/infra/conf"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
|
|
|
},{
|
|
|
|
"type": "field",
|
|
|
|
"port": "53, 443, 1000-2000",
|
|
|
|
"outboundTag": "test"
|
|
|
|
},{
|
|
|
|
"type": "field",
|
|
|
|
"port": 123,
|
|
|
|
"outboundTag": "test"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"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{
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Domain: []*domain.Domain{
|
2020-11-25 11:01:53 +00:00
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Type: domain.MatchingType_Keyword,
|
2020-11-25 11:01:53 +00:00
|
|
|
Value: "baidu.com",
|
|
|
|
},
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Type: domain.MatchingType_Keyword,
|
2020-11-25 11:01:53 +00:00
|
|
|
Value: "qq.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Geoip: []*geoip.GeoIP{
|
2020-11-25 11:01:53 +00:00
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Cidr: []*geoip.CIDR{
|
2020-11-25 11:01:53 +00:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PortList: &net.PortList{
|
|
|
|
Range: []*net.PortRange{
|
|
|
|
{From: 53, To: 53},
|
|
|
|
{From: 443, To: 443},
|
|
|
|
{From: 1000, To: 2000},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
PortList: &net.PortList{
|
|
|
|
Range: []*net.PortRange{
|
|
|
|
{From: 123, To: 123},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "test",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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{
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Domain: []*domain.Domain{
|
2020-11-25 11:01:53 +00:00
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Type: domain.MatchingType_Keyword,
|
2020-11-25 11:01:53 +00:00
|
|
|
Value: "baidu.com",
|
|
|
|
},
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Type: domain.MatchingType_Keyword,
|
2020-11-25 11:01:53 +00:00
|
|
|
Value: "qq.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Geoip: []*geoip.GeoIP{
|
2020-11-25 11:01:53 +00:00
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Cidr: []*geoip.CIDR{
|
2020-11-25 11:01:53 +00:00
|
|
|
{
|
|
|
|
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{
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Domain: []*domain.Domain{
|
2020-11-25 11:01:53 +00:00
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Type: domain.MatchingType_Keyword,
|
2020-11-25 11:01:53 +00:00
|
|
|
Value: "baidu.com",
|
|
|
|
},
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Type: domain.MatchingType_Keyword,
|
2020-11-25 11:01:53 +00:00
|
|
|
Value: "qq.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Geoip: []*geoip.GeoIP{
|
2020-11-25 11:01:53 +00:00
|
|
|
{
|
2021-03-24 15:01:20 +00:00
|
|
|
Cidr: []*geoip.CIDR{
|
2020-11-25 11:01:53 +00:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|