2020-11-25 13:01:53 +02:00
|
|
|
package conf_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-11-14 18:04:17 +02:00
|
|
|
"fmt"
|
2021-04-08 08:19:25 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-11-25 13:01:53 +02:00
|
|
|
"testing"
|
2024-02-18 05:51:37 +02:00
|
|
|
"time"
|
2021-04-08 08:19:25 +03:00
|
|
|
_ "unsafe"
|
2020-11-25 13:01:53 +02:00
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/app/router"
|
2021-04-08 08:19:25 +03:00
|
|
|
"github.com/xtls/xray-core/common"
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
2021-04-08 08:19:25 +03:00
|
|
|
"github.com/xtls/xray-core/common/platform"
|
|
|
|
"github.com/xtls/xray-core/common/platform/filesystem"
|
2024-02-18 05:51:37 +02:00
|
|
|
"github.com/xtls/xray-core/common/serial"
|
2020-12-04 03:36:16 +02:00
|
|
|
. "github.com/xtls/xray-core/infra/conf"
|
2023-08-10 07:43:34 +03:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
2024-11-14 18:04:17 +02:00
|
|
|
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)
|
2021-04-08 08:19:25 +03:00
|
|
|
}
|
|
|
|
|
2024-11-14 18:04:17 +02:00
|
|
|
return path, nil
|
2021-04-08 08:19:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestToCidrList(t *testing.T) {
|
2024-11-14 18:04:17 +02:00
|
|
|
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))
|
2021-04-08 08:19:25 +03:00
|
|
|
|
2024-11-14 18:04:17 +02:00
|
|
|
os.Setenv("xray.location.asset", tempDir)
|
|
|
|
defer os.Unsetenv("xray.location.asset")
|
2021-04-08 08:19:25 +03: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",
|
|
|
|
})
|
|
|
|
|
2024-11-14 18:04:17 +02:00
|
|
|
_, err = ToCidrList(ips)
|
2021-04-08 08:19:25 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse geoip list, got %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:01:53 +02: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: `{
|
2024-09-11 17:37:25 +03:00
|
|
|
"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"
|
|
|
|
}
|
|
|
|
],
|
2020-11-25 13:01:53 +02:00
|
|
|
"balancers": [
|
|
|
|
{
|
|
|
|
"tag": "b1",
|
2024-05-05 17:07:40 +03:00
|
|
|
"selector": ["test"],
|
|
|
|
"fallbackTag": "fall"
|
2024-02-18 05:51:37 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"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"
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}`,
|
|
|
|
Parser: createParser(),
|
|
|
|
Output: &router.Config{
|
|
|
|
DomainStrategy: router.Config_AsIs,
|
|
|
|
BalancingRule: []*router.BalancingRule{
|
|
|
|
{
|
|
|
|
Tag: "b1",
|
|
|
|
OutboundSelector: []string{"test"},
|
2021-10-26 08:00:31 +03:00
|
|
|
Strategy: "random",
|
2024-05-05 17:07:40 +03:00
|
|
|
FallbackTag: "fall",
|
2020-11-25 13:01:53 +02:00
|
|
|
},
|
2024-02-18 05:51:37 +02:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
2020-11-25 13:01:53 +02: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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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: `{
|
2024-09-11 17:37:25 +03:00
|
|
|
"domainStrategy": "IPIfNonMatch",
|
2020-11-25 13:01:53 +02:00
|
|
|
"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{
|
2024-09-11 17:37:25 +03:00
|
|
|
DomainStrategy: router.Config_IpIfNonMatch,
|
2020-11-25 13:01:53 +02: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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|