diff --git a/README.md b/README.md index 96b77b8..63f8740 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ 1. 默认配置(`ts-dns.toml`),开箱即用 ```toml listen = ":53" + gfwlist = "gfwlist.txt" cnip = "cnip.txt" [groups] @@ -49,7 +50,6 @@ [groups.dirty] dns = [""] # 省略 - rules = ["google.com"] ``` 2. 指定hosts文件和自定义hosts @@ -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) diff --git a/go.mod b/go.mod index 9133a56..0d4aa6c 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/matcher/adblock.go b/matcher/adblock.go index 1f48f66..4accec2 100644 --- a/matcher/adblock.go +++ b/matcher/adblock.go @@ -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) diff --git a/matcher/adblock_test.go b/matcher/adblock_test.go index b2c5223..15ce83f 100644 --- a/matcher/adblock_test.go +++ b/matcher/adblock_test.go @@ -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) { @@ -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.") @@ -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) } diff --git a/ts-dns-full.toml b/ts-dns-full.toml index fedb91f..65aab22 100644 --- a/ts-dns-full.toml +++ b/ts-dns-full.toml @@ -26,7 +26,6 @@ max_ttl = 86400 # 最大ttl,单位为秒 dot = ["1.0.0.1:853@cloudflare-dns.com"] # 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中 diff --git a/ts-dns.toml b/ts-dns.toml index 5f72910..33c0db1 100644 --- a/ts-dns.toml +++ b/ts-dns.toml @@ -11,4 +11,3 @@ cnip = "cnip.txt" [groups.dirty] dns = ["208.67.222.222:5353", "176.103.130.130:5353"] - rules = ["google.com"]