2017-05-08 06:18:13 -04:00
|
|
|
package router_test
|
|
|
|
|
|
|
|
import (
|
2021-04-06 15:08:03 -04:00
|
|
|
"errors"
|
2021-09-07 08:43:28 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/router/routercommon"
|
2021-04-11 22:01:30 -04:00
|
|
|
"io/fs"
|
2017-11-06 16:30:56 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2021-05-03 16:03:45 -04:00
|
|
|
"strings"
|
2017-05-08 06:18:13 -04:00
|
|
|
"testing"
|
2018-09-18 17:09:54 -04:00
|
|
|
|
2021-05-03 16:03:45 -04:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v4/app/router"
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/platform/filesystem"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/protocol"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/protocol/http"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/session"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/routing"
|
|
|
|
routing_session "github.com/v2fly/v2ray-core/v4/features/routing/session"
|
2017-05-08 06:18:13 -04:00
|
|
|
)
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
func init() {
|
2021-05-04 09:51:38 -04:00
|
|
|
const (
|
|
|
|
geoipURL = "https://raw.githubusercontent.com/v2fly/geoip/release/geoip.dat"
|
|
|
|
geositeURL = "https://raw.githubusercontent.com/v2fly/domain-list-community/release/dlc.dat"
|
|
|
|
)
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
tempPath := filepath.Join(wd, "..", "..", "testing", "temp")
|
2021-05-04 09:51:38 -04:00
|
|
|
geoipPath := filepath.Join(tempPath, "geoip.dat")
|
|
|
|
geositePath := filepath.Join(tempPath, "geosite.dat")
|
2021-04-06 15:08:03 -04:00
|
|
|
|
2021-05-04 09:51:38 -04:00
|
|
|
os.Setenv("v2ray.location.asset", tempPath)
|
2021-05-04 14:07:35 -04:00
|
|
|
|
2021-05-04 14:13:52 -04:00
|
|
|
if _, err := os.Stat(geoipPath); err != nil && errors.Is(err, fs.ErrNotExist) {
|
2021-05-19 17:28:52 -04:00
|
|
|
common.Must(os.MkdirAll(tempPath, 0o755))
|
2021-05-04 14:13:52 -04:00
|
|
|
geoipBytes, err := common.FetchHTTPContent(geoipURL)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must(filesystem.WriteFile(geoipPath, geoipBytes))
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(geositePath); err != nil && errors.Is(err, fs.ErrNotExist) {
|
2021-05-19 17:28:52 -04:00
|
|
|
common.Must(os.MkdirAll(tempPath, 0o755))
|
2021-05-04 14:13:52 -04:00
|
|
|
geositeBytes, err := common.FetchHTTPContent(geositeURL)
|
|
|
|
common.Must(err)
|
|
|
|
common.Must(filesystem.WriteFile(geositePath, geositeBytes))
|
2020-09-03 04:00:24 -04:00
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2020-09-03 23:32:19 -04:00
|
|
|
func withBackground() routing.Context {
|
|
|
|
return &routing_session.Context{}
|
2018-09-18 17:09:54 -04:00
|
|
|
}
|
|
|
|
|
2020-09-03 23:32:19 -04:00
|
|
|
func withOutbound(outbound *session.Outbound) routing.Context {
|
|
|
|
return &routing_session.Context{Outbound: outbound}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withInbound(inbound *session.Inbound) routing.Context {
|
|
|
|
return &routing_session.Context{Inbound: inbound}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withContent(content *session.Content) routing.Context {
|
|
|
|
return &routing_session.Context{Content: content}
|
2018-10-15 02:36:50 -04:00
|
|
|
}
|
|
|
|
|
2017-05-17 07:24:53 -04:00
|
|
|
func TestRoutingRule(t *testing.T) {
|
|
|
|
type ruleTest struct {
|
2020-09-03 23:32:19 -04:00
|
|
|
input routing.Context
|
2017-05-17 07:24:53 -04:00
|
|
|
output bool
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
2021-04-06 15:08:03 -04:00
|
|
|
rule *router.RoutingRule
|
2017-05-17 07:24:53 -04:00
|
|
|
test []ruleTest
|
|
|
|
}{
|
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2021-09-07 08:43:28 -04:00
|
|
|
Domain: []*routercommon.Domain{
|
2017-05-17 07:24:53 -04:00
|
|
|
{
|
2021-02-16 15:31:50 -05:00
|
|
|
Value: "v2fly.org",
|
2021-09-07 08:43:28 -04:00
|
|
|
Type: routercommon.Domain_Plain,
|
2017-05-17 07:24:53 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Value: "google.com",
|
2021-09-07 08:43:28 -04:00
|
|
|
Type: routercommon.Domain_RootDomain,
|
2017-05-17 07:24:53 -04:00
|
|
|
},
|
2017-05-17 15:46:57 -04:00
|
|
|
{
|
|
|
|
Value: "^facebook\\.com$",
|
2021-09-07 08:43:28 -04:00
|
|
|
Type: routercommon.Domain_Regex,
|
2017-05-17 15:46:57 -04:00
|
|
|
},
|
2017-05-17 07:24:53 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2021-02-16 15:31:50 -05:00
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2fly.org"), 80)}),
|
2017-05-17 07:24:53 -04:00
|
|
|
output: true,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2021-02-16 15:31:50 -05:00
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.v2fly.org.www"), 80)}),
|
2017-05-17 07:24:53 -04:00
|
|
|
output: true,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2018-09-18 17:09:54 -04:00
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.co"), 80)}),
|
2017-05-17 07:24:53 -04:00
|
|
|
output: false,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2018-09-18 17:09:54 -04:00
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.google.com"), 80)}),
|
2017-05-17 07:24:53 -04:00
|
|
|
output: true,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2018-09-18 17:09:54 -04:00
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("facebook.com"), 80)}),
|
2017-05-17 15:46:57 -04:00
|
|
|
output: true,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2018-09-18 17:09:54 -04:00
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.DomainAddress("www.facebook.com"), 80)}),
|
2017-05-17 15:46:57 -04:00
|
|
|
output: false,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2020-09-03 23:32:19 -04:00
|
|
|
input: withBackground(),
|
2017-05-17 15:46:57 -04:00
|
|
|
output: false,
|
|
|
|
},
|
2017-05-17 07:24:53 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2021-09-07 08:43:28 -04:00
|
|
|
Cidr: []*routercommon.CIDR{
|
2017-05-17 07:24:53 -04:00
|
|
|
{
|
|
|
|
Ip: []byte{8, 8, 8, 8},
|
|
|
|
Prefix: 32,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Ip: []byte{8, 8, 8, 8},
|
|
|
|
Prefix: 32,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
|
|
|
|
Prefix: 128,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2018-09-18 17:09:54 -04:00
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
|
2017-05-17 07:24:53 -04:00
|
|
|
output: true,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2018-09-18 17:09:54 -04:00
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
|
2017-05-17 07:24:53 -04:00
|
|
|
output: false,
|
|
|
|
},
|
2018-11-01 04:10:41 -04:00
|
|
|
{
|
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
2020-09-03 23:32:19 -04:00
|
|
|
input: withBackground(),
|
2018-11-01 04:10:41 -04:00
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2021-09-07 08:43:28 -04:00
|
|
|
Geoip: []*routercommon.GeoIP{
|
2018-11-01 04:10:41 -04:00
|
|
|
{
|
2021-09-07 08:43:28 -04:00
|
|
|
Cidr: []*routercommon.CIDR{
|
2018-11-01 04:10:41 -04:00
|
|
|
{
|
|
|
|
Ip: []byte{8, 8, 8, 8},
|
|
|
|
Prefix: 32,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Ip: []byte{8, 8, 8, 8},
|
|
|
|
Prefix: 32,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Ip: net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334").IP(),
|
|
|
|
Prefix: 128,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
|
|
|
{
|
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.4.4"), 80)}),
|
|
|
|
output: false,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2018-09-18 17:09:54 -04:00
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 80)}),
|
2017-05-17 07:24:53 -04:00
|
|
|
output: true,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2020-09-03 23:32:19 -04:00
|
|
|
input: withBackground(),
|
2017-05-17 15:46:57 -04:00
|
|
|
output: false,
|
|
|
|
},
|
2017-05-17 07:24:53 -04:00
|
|
|
},
|
|
|
|
},
|
2018-10-22 14:59:01 -04:00
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2021-09-07 08:43:28 -04:00
|
|
|
SourceCidr: []*routercommon.CIDR{
|
2018-10-22 14:59:01 -04:00
|
|
|
{
|
|
|
|
Ip: []byte{192, 168, 0, 0},
|
|
|
|
Prefix: 16,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
|
|
|
{
|
|
|
|
input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("192.168.0.1"), 80)}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: withInbound(&session.Inbound{Source: net.TCPDestination(net.ParseAddress("10.0.0.1"), 80)}),
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-05-17 07:28:22 -04:00
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2017-05-17 07:28:22 -04:00
|
|
|
UserEmail: []string{
|
2021-02-16 15:31:50 -05:00
|
|
|
"admin@v2fly.org",
|
2017-05-17 07:28:22 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2021-02-16 15:31:50 -05:00
|
|
|
input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "admin@v2fly.org"}}),
|
2017-05-17 07:28:22 -04:00
|
|
|
output: true,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2021-02-16 15:31:50 -05:00
|
|
|
input: withInbound(&session.Inbound{User: &protocol.MemoryUser{Email: "love@v2fly.org"}}),
|
2017-05-17 07:28:22 -04:00
|
|
|
output: false,
|
|
|
|
},
|
2017-12-02 19:04:38 -05:00
|
|
|
{
|
2020-09-03 23:32:19 -04:00
|
|
|
input: withBackground(),
|
2017-05-17 15:46:57 -04:00
|
|
|
output: false,
|
|
|
|
},
|
2017-05-17 07:28:22 -04:00
|
|
|
},
|
|
|
|
},
|
2018-08-02 07:28:13 -04:00
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2018-08-02 07:28:13 -04:00
|
|
|
Protocol: []string{"http"},
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
|
|
|
{
|
2020-09-03 23:32:19 -04:00
|
|
|
input: withContent(&session.Content{Protocol: (&http.SniffHeader{}).Protocol()}),
|
2018-08-02 07:28:13 -04:00
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-22 14:59:01 -04:00
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2018-10-22 14:59:01 -04:00
|
|
|
InboundTag: []string{"test", "test1"},
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
|
|
|
{
|
|
|
|
input: withInbound(&session.Inbound{Tag: "test"}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: withInbound(&session.Inbound{Tag: "test2"}),
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-02-24 17:43:00 -05:00
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2019-02-24 17:43:00 -05:00
|
|
|
PortList: &net.PortList{
|
|
|
|
Range: []*net.PortRange{
|
|
|
|
{From: 443, To: 443},
|
|
|
|
{From: 1000, To: 1100},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
|
|
|
{
|
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 443)}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 1100)}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 1005)}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: withOutbound(&session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 53)}),
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-08-09 04:53:45 -04:00
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2020-08-09 04:53:45 -04:00
|
|
|
SourcePortList: &net.PortList{
|
|
|
|
Range: []*net.PortRange{
|
|
|
|
{From: 123, To: 123},
|
|
|
|
{From: 9993, To: 9999},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
|
|
|
{
|
|
|
|
input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 123)}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 9999)}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 9994)}),
|
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: withInbound(&session.Inbound{Source: net.UDPDestination(net.LocalHostIP, 53)}),
|
|
|
|
output: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-02-28 08:04:43 -05:00
|
|
|
{
|
2021-04-06 15:08:03 -04:00
|
|
|
rule: &router.RoutingRule{
|
2019-02-28 08:04:43 -05:00
|
|
|
Protocol: []string{"http"},
|
|
|
|
Attributes: "attrs[':path'].startswith('/test')",
|
|
|
|
},
|
|
|
|
test: []ruleTest{
|
|
|
|
{
|
2020-09-12 12:34:35 -04:00
|
|
|
input: withContent(&session.Content{Protocol: "http/1.1", Attributes: map[string]string{":path": "/test/1"}}),
|
2019-02-28 08:04:43 -05:00
|
|
|
output: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-05-17 07:24:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cases {
|
|
|
|
cond, err := test.rule.BuildCondition()
|
2018-10-22 14:59:01 -04:00
|
|
|
common.Must(err)
|
2017-05-17 07:24:53 -04:00
|
|
|
|
2018-10-22 14:59:01 -04:00
|
|
|
for _, subtest := range test.test {
|
|
|
|
actual := cond.Apply(subtest.input)
|
|
|
|
if actual != subtest.output {
|
|
|
|
t.Error("test case failed: ", subtest.input, " expected ", subtest.output, " but got ", actual)
|
|
|
|
}
|
2017-05-17 07:24:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 16:30:56 -05:00
|
|
|
|
2021-09-07 08:43:28 -04:00
|
|
|
func loadGeoSite(country string) ([]*routercommon.Domain, error) {
|
2021-05-03 16:03:45 -04:00
|
|
|
geositeBytes, err := filesystem.ReadAsset("geosite.dat")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-07 08:43:28 -04:00
|
|
|
var geositeList routercommon.GeoSiteList
|
2021-05-03 16:03:45 -04:00
|
|
|
if err := proto.Unmarshal(geositeBytes, &geositeList); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, site := range geositeList.Entry {
|
|
|
|
if strings.EqualFold(site.CountryCode, country) {
|
|
|
|
return site.Domain, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("country not found: " + country)
|
|
|
|
}
|
2021-05-19 17:28:52 -04:00
|
|
|
|
2017-11-06 16:30:56 -05:00
|
|
|
func TestChinaSites(t *testing.T) {
|
2021-05-03 16:03:45 -04:00
|
|
|
domains, err := loadGeoSite("CN")
|
2019-01-31 14:57:01 -05:00
|
|
|
common.Must(err)
|
2017-11-06 16:30:56 -05:00
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
matcher, err := router.NewDomainMatcher(domains)
|
2018-08-19 15:04:15 -04:00
|
|
|
common.Must(err)
|
2021-04-06 15:08:03 -04:00
|
|
|
acMatcher, err := router.NewMphMatcherGroup(domains)
|
2021-01-20 02:53:07 -05:00
|
|
|
common.Must(err)
|
2017-11-06 16:30:56 -05:00
|
|
|
|
2019-01-31 14:57:01 -05:00
|
|
|
type TestCase struct {
|
|
|
|
Domain string
|
|
|
|
Output bool
|
|
|
|
}
|
|
|
|
testCases := []TestCase{
|
|
|
|
{
|
|
|
|
Domain: "163.com",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "163.com",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "164.com",
|
|
|
|
Output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "164.com",
|
|
|
|
Output: false,
|
|
|
|
},
|
|
|
|
}
|
2017-11-06 16:30:56 -05:00
|
|
|
|
|
|
|
for i := 0; i < 1024; i++ {
|
2019-01-31 14:57:01 -05:00
|
|
|
testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
2021-01-20 02:53:07 -05:00
|
|
|
r1 := matcher.ApplyDomain(testCase.Domain)
|
|
|
|
r2 := acMatcher.ApplyDomain(testCase.Domain)
|
|
|
|
if r1 != testCase.Output {
|
|
|
|
t.Error("DomainMatcher expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r1)
|
|
|
|
} else if r2 != testCase.Output {
|
|
|
|
t.Error("ACDomainMatcher expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 03:21:38 -04:00
|
|
|
func BenchmarkMphDomainMatcher(b *testing.B) {
|
2021-05-03 16:03:45 -04:00
|
|
|
domains, err := loadGeoSite("CN")
|
2021-01-20 02:53:07 -05:00
|
|
|
common.Must(err)
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
matcher, err := router.NewMphMatcherGroup(domains)
|
2021-01-20 02:53:07 -05:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
type TestCase struct {
|
|
|
|
Domain string
|
|
|
|
Output bool
|
|
|
|
}
|
|
|
|
testCases := []TestCase{
|
|
|
|
{
|
|
|
|
Domain: "163.com",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "163.com",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "164.com",
|
|
|
|
Output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "164.com",
|
|
|
|
Output: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 1024; i++ {
|
|
|
|
testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
_ = matcher.ApplyDomain(testCase.Domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDomainMatcher(b *testing.B) {
|
2021-05-03 16:03:45 -04:00
|
|
|
domains, err := loadGeoSite("CN")
|
2021-01-20 02:53:07 -05:00
|
|
|
common.Must(err)
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
matcher, err := router.NewDomainMatcher(domains)
|
2021-01-20 02:53:07 -05:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
type TestCase struct {
|
|
|
|
Domain string
|
|
|
|
Output bool
|
|
|
|
}
|
|
|
|
testCases := []TestCase{
|
|
|
|
{
|
|
|
|
Domain: "163.com",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "163.com",
|
|
|
|
Output: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "164.com",
|
|
|
|
Output: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Domain: "164.com",
|
|
|
|
Output: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 1024; i++ {
|
|
|
|
testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
_ = matcher.ApplyDomain(testCase.Domain)
|
2019-01-31 14:57:01 -05:00
|
|
|
}
|
2017-11-06 16:30:56 -05:00
|
|
|
}
|
|
|
|
}
|
2018-11-07 17:57:06 -05:00
|
|
|
|
|
|
|
func BenchmarkMultiGeoIPMatcher(b *testing.B) {
|
2021-09-07 08:43:28 -04:00
|
|
|
var geoips []*routercommon.GeoIP
|
2018-11-07 17:57:06 -05:00
|
|
|
|
|
|
|
{
|
2021-05-03 16:03:45 -04:00
|
|
|
ips, err := loadGeoIP("CN")
|
2018-11-07 17:57:06 -05:00
|
|
|
common.Must(err)
|
2021-09-07 08:43:28 -04:00
|
|
|
geoips = append(geoips, &routercommon.GeoIP{
|
2018-11-07 17:57:06 -05:00
|
|
|
CountryCode: "CN",
|
|
|
|
Cidr: ips,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-05-03 16:03:45 -04:00
|
|
|
ips, err := loadGeoIP("JP")
|
2018-11-07 17:57:06 -05:00
|
|
|
common.Must(err)
|
2021-09-07 08:43:28 -04:00
|
|
|
geoips = append(geoips, &routercommon.GeoIP{
|
2018-11-07 17:57:06 -05:00
|
|
|
CountryCode: "JP",
|
|
|
|
Cidr: ips,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-05-03 16:03:45 -04:00
|
|
|
ips, err := loadGeoIP("CA")
|
2018-11-07 17:57:06 -05:00
|
|
|
common.Must(err)
|
2021-09-07 08:43:28 -04:00
|
|
|
geoips = append(geoips, &routercommon.GeoIP{
|
2018-11-07 17:57:06 -05:00
|
|
|
CountryCode: "CA",
|
|
|
|
Cidr: ips,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2021-05-03 16:03:45 -04:00
|
|
|
ips, err := loadGeoIP("US")
|
2018-11-07 17:57:06 -05:00
|
|
|
common.Must(err)
|
2021-09-07 08:43:28 -04:00
|
|
|
geoips = append(geoips, &routercommon.GeoIP{
|
2018-11-07 17:57:06 -05:00
|
|
|
CountryCode: "US",
|
|
|
|
Cidr: ips,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-06 15:08:03 -04:00
|
|
|
matcher, err := router.NewMultiGeoIPMatcher(geoips, false)
|
2018-11-07 17:57:06 -05:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
ctx := withOutbound(&session.Outbound{Target: net.TCPDestination(net.ParseAddress("8.8.8.8"), 80)})
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_ = matcher.Apply(ctx)
|
|
|
|
}
|
|
|
|
}
|