Add support for inline comments in patterns lists

Fixes #1162
This commit is contained in:
Frank Denis 2020-01-25 15:45:23 +01:00
parent 6fa865d538
commit 349320f291
11 changed files with 28 additions and 17 deletions

View File

@ -4,8 +4,8 @@ banner.*
banners.* banners.*
creatives.* creatives.*
oas.* oas.*
oascentral.* oascentral.* # test inline comment
stats.* stats.* # test inline comment with trailing spaces
tag.* tag.*
telemetry.* telemetry.*
tracker.* tracker.*

View File

@ -1,3 +1,3 @@
cloaked.* one.one.one.one cloaked.* one.one.one.one
*.cloaked2.* one.one.one.one *.cloaked2.* one.one.one.one # inline comment
=www.dnscrypt-test 192.168.100.100 =www.dnscrypt-test 192.168.100.100

View File

@ -143,6 +143,18 @@ func StringStripSpaces(str string) string {
}, str) }, 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) { func ExtractHostAndPort(str string, defaultPort int) (host string, port int) {
host, port = str, defaultPort host, port = str, defaultPort
if idx := strings.LastIndex(str, ":"); idx >= 0 && idx < len(str)-1 { if idx := strings.LastIndex(str, ":"); idx >= 0 && idx < len(str)-1 {

View File

@ -21,7 +21,7 @@ banner.*
banners.* banners.*
creatives.* creatives.*
oas.* oas.*
oascentral.* oascentral.* # inline comments are allowed after a pound sign
stats.* stats.*
tag.* tag.*
telemetry.* telemetry.*

View File

@ -13,7 +13,7 @@ www.google.* forcesafesearch.google.com
www.bing.com strict.bing.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 =duckduckgo.com safe.duckduckgo.com

View File

@ -10,4 +10,4 @@
163.5.1.4 163.5.1.4
94.46.118.* 94.46.118.*
[fe80:53:*] [fe80:53:*] # IPv6 prefix example

View File

@ -6,7 +6,6 @@ import (
"net" "net"
"strings" "strings"
"time" "time"
"unicode"
iradix "github.com/hashicorp/go-immutable-radix" iradix "github.com/hashicorp/go-immutable-radix"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
@ -38,8 +37,8 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error {
plugin.blockedPrefixes = iradix.New() plugin.blockedPrefixes = iradix.New()
plugin.blockedIPs = make(map[string]interface{}) plugin.blockedIPs = make(map[string]interface{})
for lineNo, line := range strings.Split(string(bin), "\n") { for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace) line = TrimAndStripInlineComments(line)
if len(line) == 0 || strings.HasPrefix(line, "#") { if len(line) == 0 {
continue continue
} }
ip := net.ParseIP(line) ip := net.ParseIP(line)

View File

@ -94,8 +94,8 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
patternMatcher: NewPatternPatcher(), patternMatcher: NewPatternPatcher(),
} }
for lineNo, line := range strings.Split(string(bin), "\n") { for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace) line = TrimAndStripInlineComments(line)
if len(line) == 0 || strings.HasPrefix(line, "#") { if len(line) == 0 {
continue continue
} }
parts := strings.Split(line, "@") parts := strings.Split(line, "@")

View File

@ -45,8 +45,8 @@ func (plugin *PluginCloak) Init(proxy *Proxy) error {
plugin.patternMatcher = NewPatternPatcher() plugin.patternMatcher = NewPatternPatcher()
cloakedNames := make(map[string]*CloakedName) cloakedNames := make(map[string]*CloakedName)
for lineNo, line := range strings.Split(string(bin), "\n") { for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace) line = TrimAndStripInlineComments(line)
if len(line) == 0 || strings.HasPrefix(line, "#") { if len(line) == 0 {
continue continue
} }
var target string var target string

View File

@ -35,8 +35,8 @@ func (plugin *PluginForward) Init(proxy *Proxy) error {
return err return err
} }
for lineNo, line := range strings.Split(string(bin), "\n") { for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace) line = TrimAndStripInlineComments(line)
if len(line) == 0 || strings.HasPrefix(line, "#") { if len(line) == 0 {
continue continue
} }
domain, serversStr, ok := StringTwoFields(line) domain, serversStr, ok := StringTwoFields(line)

View File

@ -37,8 +37,8 @@ func (plugin *PluginWhitelistName) Init(proxy *Proxy) error {
plugin.allWeeklyRanges = proxy.allWeeklyRanges plugin.allWeeklyRanges = proxy.allWeeklyRanges
plugin.patternMatcher = NewPatternPatcher() plugin.patternMatcher = NewPatternPatcher()
for lineNo, line := range strings.Split(string(bin), "\n") { for lineNo, line := range strings.Split(string(bin), "\n") {
line = strings.TrimFunc(line, unicode.IsSpace) line = TrimAndStripInlineComments(line)
if len(line) == 0 || strings.HasPrefix(line, "#") { if len(line) == 0 {
continue continue
} }
parts := strings.Split(line, "@") parts := strings.Split(line, "@")