mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-08-26 06:36:26 +00:00
* fix:observatory not supported by multi-json * Fix: observatory starts with empty config & fails to close (#957) * Update strategy_leastping.go (#1019) * add custom probe URL support for observatory * add custom probe interval for observer * apply coding style * Fix: observatory log & JSON config(#1211) Co-authored-by: ihotte <ihotte@yeah.net> * Change default probe url from api.v2fly.org to www.google.com * Cherry-pick missing code from branch 'dev-advloadblancer-2' Co-authored-by: Shelikhoo <xiaokangwang@outlook.com> Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Co-authored-by: fanyiguan <52657276+fanyiguang@users.noreply.github.com> Co-authored-by: ihotte <3087168217@qq.com> Co-authored-by: ihotte <ihotte@yeah.net>
304 lines
5.9 KiB
Go
304 lines
5.9 KiB
Go
package conf_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
_ "unsafe"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"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/infra/conf"
|
|
)
|
|
|
|
func init() {
|
|
wd, err := os.Getwd()
|
|
common.Must(err)
|
|
|
|
if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
|
|
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "resources", "geoip.dat")))
|
|
}
|
|
|
|
os.Setenv("xray.location.asset", wd)
|
|
}
|
|
|
|
func TestToCidrList(t *testing.T) {
|
|
t.Log(os.Getenv("xray.location.asset"))
|
|
|
|
common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), "geoip.dat"))
|
|
|
|
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: `{
|
|
"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"},
|
|
Strategy: "random",
|
|
},
|
|
},
|
|
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: `{
|
|
"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",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
}
|