Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: GeoSite & GeoIP #643

Open
wants to merge 27 commits into
base: dns
Choose a base branch
from
Open
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
96 changes: 93 additions & 3 deletions app/router/condition_test.go
Original file line number Diff line number Diff line change
@@ -362,6 +362,9 @@ func TestChinaSites(t *testing.T) {
matcher, err := domain.NewDomainMatcher(domains)
common.Must(err)

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

type TestCase struct {
Domain string
Output bool
@@ -390,9 +393,96 @@ func TestChinaSites(t *testing.T) {
}

for _, testCase := range testCases {
r := matcher.ApplyDomain(testCase.Domain)
if r != testCase.Output {
t.Error("expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r)
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)
}
}
}

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

matcher, err := domain.NewMphMatcherGroup(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 BenchmarkDomainMatcher(b *testing.B) {
domains, err := loadGeoSite("CN")
common.Must(err)

matcher, err := domain.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)
}
}
}
21 changes: 17 additions & 4 deletions app/router/config.go
Original file line number Diff line number Diff line change
@@ -30,11 +30,24 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
conds := NewConditionChan()

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

}

if len(rr.UserEmail) > 0 {
82 changes: 44 additions & 38 deletions app/router/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/router/config.proto
Original file line number Diff line number Diff line change
@@ -61,6 +61,8 @@ message RoutingRule {
repeated string protocol = 9;

string attributes = 15;

string domain_matcher = 17;
}

message BalancingRule {
22 changes: 20 additions & 2 deletions common/matcher/domain/matcher.go
Original file line number Diff line number Diff line change
@@ -32,6 +32,24 @@ type DomainMatcher struct {
matchers str.IndexMatcher
}

func NewMphMatcherGroup(domains []*Domain) (*DomainMatcher, error) {
g := str.NewMphMatcherGroup()
for _, d := range domains {
matcherType, f := matcherTypeMap[d.Type]
if !f {
return nil, newError("unsupported domain type", d.Type)
}
_, err := g.AddPattern(d.Value, matcherType)
if err != nil {
return nil, err
}
}
g.Build()
return &DomainMatcher{
matchers: g,
}, nil
}

func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
g := new(str.MatcherGroup)
for _, d := range domains {
@@ -48,7 +66,7 @@ func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
}

func (m *DomainMatcher) ApplyDomain(domain string) bool {
return len(m.matchers.Match(domain)) > 0
return len(m.matchers.Match(strings.ToLower(domain))) > 0
}

// Apply implements Condition.
@@ -57,5 +75,5 @@ func (m *DomainMatcher) Apply(ctx routing.Context) bool {
if len(domain) == 0 {
return false
}
return m.ApplyDomain(strings.ToLower(domain))
return m.ApplyDomain(domain)
}
Loading
Oops, something went wrong.