| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- package conf_test
- import (
- "encoding/json"
- "fmt"
- "os"
- "path/filepath"
- "testing"
- "time"
- _ "unsafe"
- "github.com/xtls/xray-core/app/router"
- "github.com/xtls/xray-core/common"
- "github.com/xtls/xray-core/common/net"
- "github.com/xtls/xray-core/common/platform"
- "github.com/xtls/xray-core/common/platform/filesystem"
- "github.com/xtls/xray-core/common/serial"
- . "github.com/xtls/xray-core/infra/conf"
- "google.golang.org/protobuf/proto"
- )
- func getAssetPath(file string) (string, error) {
- path := platform.GetAssetLocation(file)
- _, err := os.Stat(path)
- if os.IsNotExist(err) {
- path := filepath.Join("..", "..", "resources", file)
- _, err := os.Stat(path)
- if os.IsNotExist(err) {
- return "", fmt.Errorf("can't find %s in standard asset locations or {project_root}/resources", file)
- }
- if err != nil {
- return "", fmt.Errorf("can't stat %s: %v", path, err)
- }
- return path, nil
- }
- if err != nil {
- return "", fmt.Errorf("can't stat %s: %v", path, err)
- }
- return path, nil
- }
- func TestToCidrList(t *testing.T) {
- tempDir, err := os.MkdirTemp("", "test-")
- if err != nil {
- t.Fatalf("can't create temp dir: %v", err)
- }
- defer os.RemoveAll(tempDir)
- geoipPath, err := getAssetPath("geoip.dat")
- if err != nil {
- t.Fatal(err)
- }
- common.Must(filesystem.CopyFile(filepath.Join(tempDir, "geoip.dat"), geoipPath))
- common.Must(filesystem.CopyFile(filepath.Join(tempDir, "geoiptestrouter.dat"), geoipPath))
- os.Setenv("xray.location.asset", tempDir)
- defer os.Unsetenv("xray.location.asset")
- 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)
- }
- }
- 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: `{
- "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"],
- "fallbackTag": "fall"
- },
- {
- "tag": "b2",
- "selector": ["test"],
- "strategy": {
- "type": "leastload",
- "settings": {
- "healthCheck": {
- "interval": "5m0s",
- "sampling": 2,
- "timeout": "5s",
- "destination": "dest",
- "connectivity": "conn"
- },
- "costs": [
- {
- "regexp": true,
- "match": "\\d+(\\.\\d+)",
- "value": 5
- }
- ],
- "baselines": ["400ms", "600ms"],
- "expected": 6,
- "maxRTT": "1000ms",
- "tolerance": 0.5
- }
- },
- "fallbackTag": "fall"
- }
- ]
- }`,
- Parser: createParser(),
- Output: &router.Config{
- DomainStrategy: router.Config_AsIs,
- BalancingRule: []*router.BalancingRule{
- {
- Tag: "b1",
- OutboundSelector: []string{"test"},
- Strategy: "random",
- FallbackTag: "fall",
- },
- {
- Tag: "b2",
- OutboundSelector: []string{"test"},
- Strategy: "leastload",
- StrategySettings: serial.ToTypedMessage(&router.StrategyLeastLoadConfig{
- Costs: []*router.StrategyWeight{
- {
- Regexp: true,
- Match: "\\d+(\\.\\d+)",
- Value: 5,
- },
- },
- Baselines: []int64{
- int64(time.Duration(400) * time.Millisecond),
- int64(time.Duration(600) * time.Millisecond),
- },
- Expected: 6,
- MaxRTT: int64(time.Duration(1000) * time.Millisecond),
- Tolerance: 0.5,
- }),
- FallbackTag: "fall",
- },
- },
- 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",
- },
- },
- {
- 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: `{
- "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",
- },
- },
- },
- },
- },
- })
- }
|