Skip to content

Commit

Permalink
Add support for inline comments in patterns lists
Browse files Browse the repository at this point in the history
Fixes #1162
  • Loading branch information
jedisct1 committed Jan 25, 2020
1 parent 6fa865d commit 349320f
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .ci/blacklist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ banner.*
banners.*
creatives.*
oas.*
oascentral.*
stats.*
oascentral.* # test inline comment
stats.* # test inline comment with trailing spaces
tag.*
telemetry.*
tracker.*
Expand Down
2 changes: 1 addition & 1 deletion .ci/cloaking-rules.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cloaked.* one.one.one.one
*.cloaked2.* one.one.one.one
*.cloaked2.* one.one.one.one # inline comment
=www.dnscrypt-test 192.168.100.100
12 changes: 12 additions & 0 deletions dnscrypt-proxy/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ func StringStripSpaces(str string) string {
}, str)
}

func TrimAndStripInlineComments(str string) string {
if idx := strings.LastIndexByte(str, '#'); idx >= 0 {
if idx == 0 {
return ""
}
if prev := str[idx-1]; prev == ' ' || prev == '\t' {
str = str[:idx-1]
}
}
return strings.TrimFunc(str, unicode.IsSpace)
}

func ExtractHostAndPort(str string, defaultPort int) (host string, port int) {
host, port = str, defaultPort
if idx := strings.LastIndex(str, ":"); idx >= 0 && idx < len(str)-1 {
Expand Down
2 changes: 1 addition & 1 deletion dnscrypt-proxy/example-blacklist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ banner.*
banners.*
creatives.*
oas.*
oascentral.*
oascentral.* # inline comments are allowed after a pound sign
stats.*
tag.*
telemetry.*
Expand Down
2 changes: 1 addition & 1 deletion dnscrypt-proxy/example-cloaking-rules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ www.google.* forcesafesearch.google.com

www.bing.com strict.bing.com

yandex.ru familysearch.yandex.ru
yandex.ru familysearch.yandex.ru # inline comments are allowed after a pound sign

=duckduckgo.com safe.duckduckgo.com

Expand Down
2 changes: 1 addition & 1 deletion dnscrypt-proxy/example-ip-blacklist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

163.5.1.4
94.46.118.*
[fe80:53:*]
[fe80:53:*] # IPv6 prefix example
5 changes: 2 additions & 3 deletions dnscrypt-proxy/plugin_block_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"
"strings"
"time"
"unicode"

iradix "github.com/hashicorp/go-immutable-radix"
"github.com/jedisct1/dlog"
Expand Down Expand Up @@ -38,8 +37,8 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error {
plugin.blockedPrefixes = iradix.New()
plugin.blockedIPs = make(map[string]interface{})
for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace)
if len(line) == 0 || strings.HasPrefix(line, "#") {
line = TrimAndStripInlineComments(line)
if len(line) == 0 {
continue
}
ip := net.ParseIP(line)
Expand Down
4 changes: 2 additions & 2 deletions dnscrypt-proxy/plugin_block_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
patternMatcher: NewPatternPatcher(),
}
for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace)
if len(line) == 0 || strings.HasPrefix(line, "#") {
line = TrimAndStripInlineComments(line)
if len(line) == 0 {
continue
}
parts := strings.Split(line, "@")
Expand Down
4 changes: 2 additions & 2 deletions dnscrypt-proxy/plugin_cloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
plugin.patternMatcher = NewPatternPatcher()
cloakedNames := make(map[string]*CloakedName)
for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace)
if len(line) == 0 || strings.HasPrefix(line, "#") {
line = TrimAndStripInlineComments(line)
if len(line) == 0 {
continue
}
var target string
Expand Down
4 changes: 2 additions & 2 deletions dnscrypt-proxy/plugin_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (plugin *PluginForward) Init(proxy *Proxy) error {
return err
}
for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace)
if len(line) == 0 || strings.HasPrefix(line, "#") {
line = TrimAndStripInlineComments(line)
if len(line) == 0 {
continue
}
domain, serversStr, ok := StringTwoFields(line)
Expand Down
4 changes: 2 additions & 2 deletions dnscrypt-proxy/plugin_whitelist_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (plugin *PluginWhitelistName) Init(proxy *Proxy) error {
plugin.allWeeklyRanges = proxy.allWeeklyRanges
plugin.patternMatcher = NewPatternPatcher()
for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace)
if len(line) == 0 || strings.HasPrefix(line, "#") {
line = TrimAndStripInlineComments(line)
if len(line) == 0 {
continue
}
parts := strings.Split(line, "@")
Expand Down

0 comments on commit 349320f

Please sign in to comment.