Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions app/router/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,6 @@ func NewMphMatcherGroup(domains []*Domain) (*DomainMatcher, error) {
}, nil
}

func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
g := new(strmatcher.MatcherGroup)
for _, d := range domains {
m, err := domainToMatcher(d)
if err != nil {
return nil, err
}
g.Add(m)
}

return &DomainMatcher{
matchers: g,
}, nil
}

func (m *DomainMatcher) ApplyDomain(domain string) bool {
return len(m.matchers.Match(strings.ToLower(domain))) > 0
}
Expand Down
54 changes: 3 additions & 51 deletions app/router/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,6 @@ func TestChinaSites(t *testing.T) {
domains, err := loadGeoSite("CN")
common.Must(err)

matcher, err := NewDomainMatcher(domains)
common.Must(err)

acMatcher, err := NewMphMatcherGroup(domains)
common.Must(err)

Expand Down Expand Up @@ -362,12 +359,9 @@ func TestChinaSites(t *testing.T) {
}

for _, testCase := range testCases {
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)
r := acMatcher.ApplyDomain(testCase.Domain)
if r != testCase.Output {
t.Error("ACDomainMatcher expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r)
}
}
}
Expand Down Expand Up @@ -414,48 +408,6 @@ func BenchmarkMphDomainMatcher(b *testing.B) {
}
}

func BenchmarkDomainMatcher(b *testing.B) {
domains, err := loadGeoSite("CN")
common.Must(err)

matcher, err := NewDomainMatcher(domains)
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 BenchmarkMultiGeoIPMatcher(b *testing.B) {
var geoips []*GeoIP

Expand Down
21 changes: 5 additions & 16 deletions app/router/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,12 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
conds := NewConditionChan()

if len(rr.Domain) > 0 {
switch rr.DomainMatcher {
case "linear":
matcher, err := NewDomainMatcher(rr.Domain)
if err != nil {
return nil, errors.New("failed to build domain condition").Base(err)
}
conds.Add(matcher)
case "mph", "hybrid":
fallthrough
default:
matcher, err := NewMphMatcherGroup(rr.Domain)
if err != nil {
return nil, errors.New("failed to build domain condition with MphDomainMatcher").Base(err)
}
errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)")
conds.Add(matcher)
matcher, err := NewMphMatcherGroup(rr.Domain)
if err != nil {
return nil, errors.New("failed to build domain condition with MphDomainMatcher").Base(err)
}
errors.LogDebug(context.Background(), "MphDomainMatcher is enabled for ", len(rr.Domain), " domain rule(s)")
conds.Add(matcher)
}

if len(rr.UserEmail) > 0 {
Expand Down
157 changes: 73 additions & 84 deletions app/router/config.pb.go

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions app/router/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ message RoutingRule {
// Tag of routing balancer.
string balancing_tag = 12;
}
string rule_tag = 20;
string rule_tag = 19;

// List of domains for target domain matching.
repeated Domain domain = 2;
Expand Down Expand Up @@ -109,10 +109,8 @@ message RoutingRule {

map<string, string> attributes = 15;

string domain_matcher = 17;

repeated GeoIP local_geoip = 18;
xray.common.net.PortList local_port_list = 19;
repeated GeoIP local_geoip = 17;
xray.common.net.PortList local_port_list = 18;
}

message BalancingRule {
Expand Down
3 changes: 1 addition & 2 deletions common/reflect/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ func getConfig() string {
"inboundTag": [
"api-in"
],
"outboundTag": "api",
"type": "field"
"outboundTag": "api"
}
],
"domainStrategy": "AsIs"
Expand Down
9 changes: 7 additions & 2 deletions infra/conf/freedom.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

type FreedomConfig struct {
TargetStrategy string `json:"targetStrategy"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按惯例,别名放原名上面

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

DomainStrategy string `json:"domainStrategy"`
Redirect string `json:"redirect"`
UserLevel uint32 `json:"userLevel"`
Expand All @@ -40,7 +41,11 @@ type Noise struct {
// Build implements Buildable
func (c *FreedomConfig) Build() (proto.Message, error) {
config := new(freedom.Config)
switch strings.ToLower(c.DomainStrategy) {
targetStrategy := c.TargetStrategy
if targetStrategy == "" {
targetStrategy = c.DomainStrategy
}
switch strings.ToLower(targetStrategy) {
case "asis", "":
config.DomainStrategy = freedom.Config_AS_IS
case "useip":
Expand All @@ -64,7 +69,7 @@ func (c *FreedomConfig) Build() (proto.Message, error) {
case "forceipv6v4":
config.DomainStrategy = freedom.Config_FORCE_IP64
default:
return nil, errors.New("unsupported domain strategy: ", c.DomainStrategy)
return nil, errors.New("unsupported domain strategy: ", targetStrategy)
}

if c.Fragment != nil {
Expand Down
25 changes: 5 additions & 20 deletions infra/conf/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ type RouterConfig struct {
RuleList []json.RawMessage `json:"rules"`
DomainStrategy *string `json:"domainStrategy"`
Balancers []*BalancingRule `json:"balancers"`

DomainMatcher string `json:"domainMatcher"`
}

func (c *RouterConfig) getDomainStrategy() router.Config_DomainStrategy {
Expand Down Expand Up @@ -111,10 +109,6 @@ func (c *RouterConfig) Build() (*router.Config, error) {
return nil, err
}

if rule.DomainMatcher == "" {
rule.DomainMatcher = c.DomainMatcher
}

config.Rule = append(config.Rule, rule)
}
for _, rawBalancer := range c.Balancers {
Expand All @@ -129,11 +123,8 @@ func (c *RouterConfig) Build() (*router.Config, error) {

type RouterRule struct {
RuleTag string `json:"ruleTag"`
Type string `json:"type"`
OutboundTag string `json:"outboundTag"`
BalancerTag string `json:"balancerTag"`

DomainMatcher string `json:"domainMatcher"`
}

func ParseIP(s string) (*router.CIDR, error) {
Expand Down Expand Up @@ -567,10 +558,6 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
return nil, errors.New("neither outboundTag nor balancerTag is specified in routing rule")
}

if rawFieldRule.DomainMatcher != "" {
rule.DomainMatcher = rawFieldRule.DomainMatcher
}

if rawFieldRule.Domain != nil {
for _, domain := range *rawFieldRule.Domain {
rules, err := parseDomainRule(domain)
Expand Down Expand Up @@ -666,12 +653,10 @@ func ParseRule(msg json.RawMessage) (*router.RoutingRule, error) {
if err != nil {
return nil, errors.New("invalid router rule").Base(err)
}
if rawRule.Type == "" || strings.EqualFold(rawRule.Type, "field") {
fieldrule, err := parseFieldRule(msg)
if err != nil {
return nil, errors.New("invalid field rule").Base(err)
}
return fieldrule, nil

fieldrule, err := parseFieldRule(msg)
if err != nil {
return nil, errors.New("invalid field rule").Base(err)
}
return nil, errors.New("unknown router rule type: ", rawRule.Type)
return fieldrule, nil
}
6 changes: 0 additions & 6 deletions infra/conf/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,22 @@ func TestRouterConfig(t *testing.T) {
"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"
}
Expand Down Expand Up @@ -249,15 +245,13 @@ func TestRouterConfig(t *testing.T) {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"type": "field",
"domain": [
"baidu.com",
"qq.com"
],
"outboundTag": "direct"
},
{
"type": "field",
"ip": [
"10.0.0.0/8",
"::1/128"
Expand Down
1 change: 0 additions & 1 deletion infra/conf/xray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func TestXrayConfig(t *testing.T) {
"ip": [
"10.0.0.0/8"
],
"type": "field",
"outboundTag": "blocked"
}
]
Expand Down
1 change: 0 additions & 1 deletion main/commands/all/api/source_ip_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func executeSourceIpBlock(cmd *base.Command, args []string) {
"ruleTag" : "%s",
"inboundTag": %s,
"outboundTag": "%s",
"type": "field",
"source": %s
}
]
Expand Down
Loading