2019-02-10 13:04:11 -05:00
|
|
|
package conf_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-04-06 15:08:03 -04:00
|
|
|
"errors"
|
2021-04-11 22:01:30 -04:00
|
|
|
"io/fs"
|
2021-04-06 00:29:12 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-02-10 13:04:11 -05:00
|
|
|
"testing"
|
2021-04-06 00:29:12 -04:00
|
|
|
_ "unsafe"
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/router"
|
2021-04-06 00:29:12 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
2021-04-06 00:29:12 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
|
2021-02-16 15:31:50 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v4/infra/conf"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
2021-04-06 00:29:12 -04:00
|
|
|
func init() {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
|
|
|
|
geoipPath := filepath.Join(tempPath, "geoip.dat")
|
|
|
|
|
|
|
|
os.Setenv("v2ray.location.asset", tempPath)
|
2021-04-06 00:29:12 -04:00
|
|
|
|
2021-04-13 13:15:06 -04:00
|
|
|
common.Must(os.MkdirAll(tempPath, 0755))
|
|
|
|
|
2021-04-11 22:01:30 -04:00
|
|
|
if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && errors.Is(err, fs.ErrNotExist) {
|
|
|
|
if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
|
2021-04-06 15:08:03 -04:00
|
|
|
geoipBytes, err := common.FetchHTTPContent(geoipURL)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 00:29:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//go:linkname toCidrList github.com/v2fly/v2ray-core/v4/infra/conf.toCidrList
|
|
|
|
func toCidrList(ips StringList) ([]*router.GeoIP, error)
|
|
|
|
|
|
|
|
func TestToCidrList(t *testing.T) {
|
|
|
|
t.Log(os.Getenv("v2ray.location.asset"))
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), platform.GetAssetLocation("geoip.dat")))
|
2021-04-06 00:29:12 -04:00
|
|
|
|
|
|
|
ips := StringList([]string{
|
|
|
|
"geoip:us",
|
|
|
|
"geoip:cn",
|
|
|
|
"geoip:!cn",
|
|
|
|
"ext:geoiptestrouter.dat:!cn",
|
|
|
|
"ext:geoiptestrouter.dat:ca",
|
|
|
|
"ext-ip:geoiptestrouter.dat:!cn",
|
|
|
|
"ext-ip:geoiptestrouter.dat:!ca",
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err := toCidrList(ips)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse geoip list, got %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05: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"
|
|
|
|
},
|
2020-12-11 08:16:52 -05:00
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"domains": [
|
|
|
|
"v2fly.org",
|
|
|
|
"github.com"
|
|
|
|
],
|
|
|
|
"outboundTag": "direct"
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
{
|
|
|
|
"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"},
|
2021-04-08 17:04:47 -04:00
|
|
|
Strategy: "random",
|
2019-02-10 13:04:11 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 08:16:52 -05:00
|
|
|
{
|
|
|
|
Domain: []*router.Domain{
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "v2fly.org",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "github.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
{
|
|
|
|
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"
|
|
|
|
},
|
2020-12-11 08:16:52 -05:00
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"domains": [
|
|
|
|
"v2fly.org",
|
|
|
|
"github.com"
|
|
|
|
],
|
|
|
|
"outboundTag": "direct"
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
{
|
|
|
|
"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",
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 08:16:52 -05:00
|
|
|
{
|
|
|
|
Domain: []*router.Domain{
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "v2fly.org",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "github.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
{
|
|
|
|
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"
|
|
|
|
},
|
2020-12-11 08:16:52 -05:00
|
|
|
{
|
|
|
|
"type": "field",
|
|
|
|
"domains": [
|
|
|
|
"v2fly.org",
|
|
|
|
"github.com"
|
|
|
|
],
|
|
|
|
"outboundTag": "direct"
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
{
|
|
|
|
"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",
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 08:16:52 -05:00
|
|
|
{
|
|
|
|
Domain: []*router.Domain{
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "v2fly.org",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: router.Domain_Plain,
|
|
|
|
Value: "github.com",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
|
|
|
},
|
2019-02-10 13:04:11 -05:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|