Skip to content

Commit

Permalink
feat(matcher): support google regex in gfwlist
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf-joe committed Mar 15, 2020
1 parent 112d768 commit abcfb6f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
1. 默认配置(`ts-dns.toml`),开箱即用
```toml
listen = ":53"
gfwlist = "gfwlist.txt"
cnip = "cnip.txt"
[groups]
Expand All @@ -49,7 +50,6 @@
[groups.dirty]
dns = [""] # 省略
rules = ["google.com"]
```

2. 指定hosts文件和自定义hosts
Expand Down Expand Up @@ -94,12 +94,6 @@
* 默认使用ECS转发DNS请求
* DNS并发响应

## 依赖
* [github.com/miekg/dns](https://github.com/miekg/dns)
* [github.com/coreos/go-semver/semver](https://github.com/coreos/go-semver/semver)
* [github.com/BurntSushi/toml](https://github.com/BurntSushi/toml)

## 特别鸣谢
* [github.com/janeczku/go-ipset](https://github.com/janeczku/go-ipset)
* [github.com/arloan/prdns](https://github.com/arloan/prdns)
* [github.com/gfwlist/gfwlist](https://github.com/gfwlist/gfwlist)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/janeczku/go-ipset v0.0.0-20170206212442-499ed3217c4b
github.com/miekg/dns v1.1.28
github.com/stretchr/testify v1.2.2
golang.org/x/net v0.0.0-20200301022130-244492dfa37a
)
10 changes: 8 additions & 2 deletions matcher/adblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ func NewABPByText(text string) (matcher *ABPlus) {
}
matcher = &ABPlus{isBlocked: map[string]bool{}}
for _, line := range strings.Split(text, "\n") {
if line == "" || line[0] == '!' || line[0] == '/' || line[0] == '[' {
continue // 忽略空行、注释行、path类规则、类型声明
if line == "" || line[0] == '!' || line[0] == '[' {
continue // 忽略空行、注释行、类型声明
} else if line[0] == '/' { // path类规则
if line[:13] == "/^https?:\\/\\/" && line[len(line)-5:] == "\\/.*/" { // google正则补丁
reg := regexp.MustCompile(line[13 : len(line)-5])
matcher.blockedRegs = append(matcher.blockedRegs, reg)
}
continue
}
line = strings.Replace(line, "%2F", "/", -1)

Expand Down
16 changes: 11 additions & 5 deletions matcher/adblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ var text = `[AutoProxy]
!comment
|http://1.1.1.1
unknown
|https://*.youtube.com/path
.google.com
.abc.com
@@||cip.cc
@@||*.cn
/^https?:\/\/([^\/]+\.)*google\.(ac|ad|ae|af|al|am|as|at|az|ba|be|bf|bg|bi|bj|bs|bt|by|ca|cat|cd|cf|cg|ch|ci|cl|cm|co.ao|co.bw|co.ck|co.cr|co.id|co.il|co.in|co.jp|co.ke|co.kr|co.ls|co.ma|com|com.af|com.ag|com.ai|com.ar|com.au|com.bd|com.bh|com.bn|com.bo|com.br|com.bz|com.co|com.cu|com.cy|com.do|com.ec|com.eg|com.et|com.fj|com.gh|com.gi|com.gt|com.hk|com.jm|com.kh|com.kw|com.lb|com.ly|com.mm|com.mt|com.mx|com.my|com.na|com.nf|com.ng|com.ni|com.np|com.om|com.pa|com.pe|com.pg|com.ph|com.pk|com.pr|com.py|com.qa|com.sa|com.sb|com.sg|com.sl|com.sv|com.tj|com.tr|com.tw|com.ua|com.uy|com.vc|com.vn|co.mz|co.nz|co.th|co.tz|co.ug|co.uk|co.uz|co.ve|co.vi|co.za|co.zm|co.zw|cv|cz|de|dj|dk|dm|dz|ee|es|eu|fi|fm|fr|ga|ge|gg|gl|gm|gp|gr|gy|hk|hn|hr|ht|hu|ie|im|iq|is|it|it.ao|je|jo|kg|ki|kz|la|li|lk|lt|lu|lv|md|me|mg|mk|ml|mn|ms|mu|mv|mw|mx|ne|nl|no|nr|nu|org|pl|pn|ps|pt|ro|rs|ru|rw|sc|se|sh|si|sk|sm|sn|so|sr|st|td|tg|tk|tl|tm|tn|to|tt|us|vg|vn|vu|ws)\/.*/
`

func TestNewChecker(t *testing.T) {
Expand All @@ -42,11 +44,11 @@ func TestNewChecker(t *testing.T) {
// 判断空串
matched, ok := matcher.Match("")
assert.Equal(t, ok, false)
// 规则.google.com不匹配google.com
matched, ok = matcher.Match("google.com")
// 规则.abc.com不匹配abc.com
matched, ok = matcher.Match("abc.com")
assert.Equal(t, ok, false)
// 但匹配test.google.com
matched, ok = matcher.Match("test.google.com")
// 但匹配test.abc.com
matched, ok = matcher.Match("test.abc.com")
assert.Equal(t, ok, true)
// 匹配白名单@@||cip.cc
matched, ok = matcher.Match("cip.cc.")
Expand All @@ -60,4 +62,8 @@ func TestNewChecker(t *testing.T) {
matched, ok = matcher.Match("www.youtube.com")
assert.Equal(t, ok, true)
assert.Equal(t, matched, true)
// 匹配google正则
matched, ok = matcher.Match("google.com")
assert.Equal(t, matched, true)
assert.Equal(t, ok, true)
}
1 change: 0 additions & 1 deletion ts-dns-full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ max_ttl = 86400 # 最大ttl,单位为秒
dot = ["1.0.0.1:[email protected]"] # dns over tls服务器
# 警告:如果本机的dns指向ts-dns自身,且DoH地址中的域名被归类到该组,则会出现递归解析的情况,此时需要在上面的hosts中指定对应IP
doh = ["https://cloudflare-dns.com/dns-query"] # dns over https服务器
rules = ["google.com"] # 官方gfwlist里只有".google.com"规则,无法匹配"google.com",所以手动加上

# 警告:进程启动时会覆盖已有同名IPSet
ipset = "blocked" # 目标IPSet名称,该组所有域名的ipv4解析结果将加入到该IPSet中
Expand Down
1 change: 0 additions & 1 deletion ts-dns.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ cnip = "cnip.txt"

[groups.dirty]
dns = ["208.67.222.222:5353", "176.103.130.130:5353"]
rules = ["google.com"]

0 comments on commit abcfb6f

Please sign in to comment.