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

Amend domain matcher with returning values of all matched subdomains #95

Merged
merged 1 commit into from
Aug 13, 2020

Conversation

Vigilans
Copy link
Contributor

@Vigilans Vigilans commented Aug 13, 2020

Comment on lines +943 to +953
{ // Will match server 4,3,1,2 and server 4 returns expected one
ips, err := client.LookupIP("v2.api.google.com")
if err != nil {
t.Fatal("unexpected error: ", err)
}

if r := cmp.Diff(ips, []net.IP{{8, 8, 7, 8}}); r != "" {
t.Fatal(r)
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

现在DNS的测试使用如下四个用例:

  • 查询google.com,匹配DNS [1, 2],DNS 1返回的IP是Expected的,直接返回;
  • 查询ipv6.google.com,匹配DNS [1, 2],DNS 1返回的IP不是Expected的,这时并不直接Fallback到默认轮询,而是继续查询DNS 2,收到Expected IP并返回。
  • 查询api.google.com,匹配DNS [3, 1, 2],这是因为domain:api.google.com的优先级更高。从DNS 3收到Expected IP并返回。
  • 查询v2.api.google.com,匹配DNS [4, 3, 1, 2],这是因为Full Matcher的优先级最高。从DNS 4收到Expected IP并返回。

Comment on lines +86 to +96
switch len(matches) {
case 0:
return nil
case 1:
return matches[0]
default:
result := []uint32{}
for idx := range matches {
// Insert reversely, the subdomain that matches further ranks higher
result = append(result, matches[len(matches)-1-idx]...)
}
return result
Copy link
Contributor Author

Choose a reason for hiding this comment

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

当没有match时,直接返回nil;当只有一个match时,返回matches里保存的数组引用,从而避免构造一个新数组。

Comment on lines +36 to +38
{ // Matches [c.a.b.com, a.b.com]
Domain: "c.a.b.com",
Result: []uint32{4},
Result: []uint32{5, 4},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个测试用例似乎说明原作者是特意设计了级数最少的优先匹配这个规则的(c.a.b.com添加时为5,最后匹配到了4)。不知最初的用意为何……

@Vigilans
Copy link
Contributor Author

在本机上,原版、Amend PR 1、当前PR的Benchmark

原版实现

$ go test -bench=.
goos: linux
goarch: amd64
pkg: v2ray.com/core/common/strmatcher
BenchmarkDomainMatcherGroup-6           29919849                40.1 ns/op
BenchmarkFullMatcherGroup-6             100000000               11.5 ns/op
BenchmarkMarchGroup-6                   28158232                42.6 ns/op
PASS
ok      v2ray.com/core/common/strmatcher        3.658s

$ go test -bench=.
goos: linux
goarch: amd64
pkg: v2ray.com/core/common/strmatcher
BenchmarkDomainMatcherGroup-6           27993582                40.1 ns/op
BenchmarkFullMatcherGroup-6             100000000               11.5 ns/op
BenchmarkMarchGroup-6                   27865580                42.7 ns/op
PASS
ok      v2ray.com/core/common/strmatcher        3.572s

$ go test -bench=.
goos: linux
goarch: amd64
pkg: v2ray.com/core/common/strmatcher
BenchmarkDomainMatcherGroup-6           29901494                40.1 ns/op
BenchmarkFullMatcherGroup-6             100000000               11.5 ns/op
BenchmarkMarchGroup-6                   28147051                42.9 ns/op
PASS
ok      v2ray.com/core/common/strmatcher        3.666s

Amend PR 1 实现

$ go test -bench=.
goos: linux
goarch: amd64
pkg: v2ray.com/core/common/strmatcher
BenchmarkDomainMatcherGroup-6           29018882                41.3 ns/op
BenchmarkFullMatcherGroup-6             100000000               11.7 ns/op
BenchmarkMarchGroup-6                   26082274                46.0 ns/op
PASS
ok      v2ray.com/core/common/strmatcher        3.678s

$ go test -bench=.
goos: linux
goarch: amd64
pkg: v2ray.com/core/common/strmatcher
BenchmarkDomainMatcherGroup-6           29016004                41.3 ns/op
BenchmarkFullMatcherGroup-6             100000000               11.7 ns/op
BenchmarkMarchGroup-6                   26079870                46.0 ns/op
PASS
ok      v2ray.com/core/common/strmatcher        3.673s

$ go test -bench=.
goos: linux
goarch: amd64
pkg: v2ray.com/core/common/strmatcher
BenchmarkDomainMatcherGroup-6           28999465                41.3 ns/op
BenchmarkFullMatcherGroup-6             100000000               11.6 ns/op
BenchmarkMarchGroup-6                   25960336                46.0 ns/op
PASS
ok      v2ray.com/core/common/strmatcher        3.665s

Amend PR 2 实现

$ go test -bench=.
goos: linux
goarch: amd64
pkg: v2ray.com/core/common/strmatcher
BenchmarkDomainMatcherGroup-6           28199155                41.7 ns/op
BenchmarkFullMatcherGroup-6             100000000               11.5 ns/op
BenchmarkMarchGroup-6                   25007541                47.3 ns/op
PASS
ok      v2ray.com/core/common/strmatcher        3.626s

$ go test -bench=.
goos: linux
goarch: amd64
pkg: v2ray.com/core/common/strmatcher
BenchmarkDomainMatcherGroup-6           28081856                41.7 ns/op
BenchmarkFullMatcherGroup-6             100000000               11.5 ns/op
BenchmarkMarchGroup-6                   25373059                47.3 ns/op
PASS
ok      v2ray.com/core/common/strmatcher        3.637s

$ go test -bench=.
goos: linux
goarch: amd64
pkg: v2ray.com/core/common/strmatcher
BenchmarkDomainMatcherGroup-6           28081856                41.7 ns/op
BenchmarkFullMatcherGroup-6             100000000               11.5 ns/op
BenchmarkMarchGroup-6                   25373059                47.3 ns/op
PASS
ok      v2ray.com/core/common/strmatcher        3.637s

@Vigilans Vigilans marked this pull request as draft August 13, 2020 06:12
@Vigilans Vigilans force-pushed the vigilans/dns-subdomain-multimatch branch from 1e743f5 to 1e22242 Compare August 13, 2020 07:27
@Vigilans Vigilans closed this Aug 13, 2020
@Vigilans Vigilans reopened this Aug 13, 2020
@Vigilans Vigilans marked this pull request as ready for review August 13, 2020 08:35
@kslr kslr merged commit aa80035 into v2fly:master Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal for amending domain matching logic
2 participants