Skip to content

Commit 96f285e

Browse files
authored
Merge pull request #2930 from lifenjoiner/rules
Rename PatternMatcher's field names and improve pattern rule examples
2 parents 9b75d0c + d8b8bf5 commit 96f285e

3 files changed

Lines changed: 43 additions & 41 deletions

File tree

dnscrypt-proxy/example-allowed-names.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
##
88
## Example of valid patterns:
99
##
10-
## ads.* | matches anything with an "ads." prefix
11-
## *.example.com | matches example.com and all names within that zone such as www.example.com
12-
## example.com | identical to the above
13-
## =example.com | allows example.com but not *.example.com
14-
## *sex* | matches any name containing that substring
15-
## ads[0-9]* | matches "ads" followed by one or more digits
16-
## ads*.example* | *, ? and [] can be used anywhere, but prefixes/suffixes are faster
10+
## ads.* | matches anything with an "ads." prefix
11+
## *.example.com | matches example.com and all names within that zone such as www.example.com
12+
## example.com | identical to the above
13+
## =example.com | allows example.com but not *.example.com
14+
## [a-z0-9\-_]*.example.com | allows *.example.com but not example.com
15+
## *sex* | matches any name containing that substring
16+
## ads[0-9]* | matches "ads" followed by one or more digits
17+
## ads*.example* | *, ? and [] can be used anywhere, but prefixes/suffixes are faster
1718

1819

1920
# That one may be blocked due to 'tracker' being in the name.

dnscrypt-proxy/example-blocked-names.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
##
88
## Example of valid patterns:
99
##
10-
## ads.* | matches anything with an "ads." prefix
11-
## *.example.com | matches example.com and all names within that zone such as www.example.com
12-
## example.com | identical to the above
13-
## =example.com | block example.com but not *.example.com
14-
## *sex* | matches any name containing that substring
15-
## ads[0-9]* | matches "ads" followed by one or more digits
16-
## ads*.example* | *, ? and [] can be used anywhere, but prefixes/suffixes are faster
10+
## ads.* | matches anything with an "ads." prefix
11+
## *.example.com | matches example.com and all names within that zone such as www.example.com
12+
## example.com | identical to the above
13+
## =example.com | blocks example.com but not *.example.com
14+
## [a-z0-9\-_]*.example.com | blocks *.example.com but not example.com
15+
## *sex* | matches any name containing that substring
16+
## ads[0-9]* | matches "ads" followed by one or more digits
17+
## ads*.example* | *, ? and [] can be used anywhere, but prefixes/suffixes are faster
1718

1819
ad.*
1920
ads.*

dnscrypt-proxy/pattern_matcher.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ const (
2222
)
2323

2424
type PatternMatcher struct {
25-
blockedPrefixes *critbitgo.Trie
26-
blockedSuffixes *critbitgo.Trie
27-
blockedSubstrings []string
28-
blockedPatterns []string
29-
blockedExact map[string]interface{}
30-
indirectVals map[string]interface{}
25+
prefixes *critbitgo.Trie
26+
suffixes *critbitgo.Trie
27+
substrings []string
28+
patterns []string
29+
exact map[string]interface{}
30+
indirectVals map[string]interface{}
3131
}
3232

3333
func NewPatternMatcher() *PatternMatcher {
3434
patternMatcher := PatternMatcher{
35-
blockedPrefixes: critbitgo.NewTrie(),
36-
blockedSuffixes: critbitgo.NewTrie(),
37-
blockedExact: make(map[string]interface{}),
38-
indirectVals: make(map[string]interface{}),
35+
prefixes: critbitgo.NewTrie(),
36+
suffixes: critbitgo.NewTrie(),
37+
exact: make(map[string]interface{}),
38+
indirectVals: make(map[string]interface{}),
3939
}
4040
return &patternMatcher
4141
}
@@ -63,27 +63,27 @@ func (patternMatcher *PatternMatcher) Add(pattern string, val interface{}, posit
6363
patternType = PatternTypePattern
6464
_, err := filepath.Match(pattern, "example.com") // Validate pattern syntax
6565
if len(pattern) < 2 || err != nil {
66-
return fmt.Errorf("Syntax error in block rules at pattern %d", position)
66+
return fmt.Errorf("Syntax error in the rule file at line %d", position)
6767
}
6868
} else if leadingStar && trailingStar {
6969
// Substring match (*contains*)
7070
patternType = PatternTypeSubstring
7171
if len(pattern) < 3 {
72-
return fmt.Errorf("Syntax error in block rules at pattern %d", position)
72+
return fmt.Errorf("Syntax error in the rule file at line %d", position)
7373
}
7474
pattern = pattern[1 : len(pattern)-1] // Remove stars
7575
} else if trailingStar {
7676
// Prefix match (starts*)
7777
patternType = PatternTypePrefix
7878
if len(pattern) < 2 {
79-
return fmt.Errorf("Syntax error in block rules at pattern %d", position)
79+
return fmt.Errorf("Syntax error in the rule file at line %d", position)
8080
}
8181
pattern = pattern[:len(pattern)-1] // Remove trailing star
8282
} else if exact {
8383
// Exact match (=example.com)
8484
patternType = PatternTypeExact
8585
if len(pattern) < 2 {
86-
return fmt.Errorf("Syntax error in block rules at pattern %d", position)
86+
return fmt.Errorf("Syntax error in the rule file at line %d", position)
8787
}
8888
pattern = pattern[1:] // Remove = prefix
8989
} else {
@@ -95,29 +95,29 @@ func (patternMatcher *PatternMatcher) Add(pattern string, val interface{}, posit
9595
pattern = strings.TrimPrefix(pattern, ".") // Remove leading dot if present
9696
}
9797
if len(pattern) == 0 {
98-
dlog.Errorf("Syntax error in block rule at line %d", position)
98+
dlog.Errorf("Syntax error in the rule file at line %d", position)
9999
}
100100

101101
pattern = strings.ToLower(pattern)
102102
switch patternType {
103103
case PatternTypeSubstring:
104-
patternMatcher.blockedSubstrings = append(patternMatcher.blockedSubstrings, pattern)
104+
patternMatcher.substrings = append(patternMatcher.substrings, pattern)
105105
if val != nil {
106106
patternMatcher.indirectVals[pattern] = val
107107
}
108108
case PatternTypePattern:
109-
patternMatcher.blockedPatterns = append(patternMatcher.blockedPatterns, pattern)
109+
patternMatcher.patterns = append(patternMatcher.patterns, pattern)
110110
if val != nil {
111111
patternMatcher.indirectVals[pattern] = val
112112
}
113113
case PatternTypePrefix:
114-
patternMatcher.blockedPrefixes.Insert([]byte(pattern), val)
114+
patternMatcher.prefixes.Insert([]byte(pattern), val)
115115
case PatternTypeSuffix:
116-
patternMatcher.blockedSuffixes.Insert([]byte(StringReverse(pattern)), val)
116+
patternMatcher.suffixes.Insert([]byte(StringReverse(pattern)), val)
117117
case PatternTypeExact:
118-
patternMatcher.blockedExact[pattern] = val
118+
patternMatcher.exact[pattern] = val
119119
default:
120-
dlog.Fatal("Unexpected block type")
120+
dlog.Fatal("Unexpected rule pattern type")
121121
}
122122
return nil
123123
}
@@ -127,19 +127,19 @@ func (patternMatcher *PatternMatcher) Eval(qName string) (reject bool, reason st
127127
return false, "", nil
128128
}
129129

130-
if xval := patternMatcher.blockedExact[qName]; xval != nil {
130+
if xval := patternMatcher.exact[qName]; xval != nil {
131131
return true, qName, xval
132132
}
133133

134134
revQname := StringReverse(qName)
135-
if match, xval, found := patternMatcher.blockedSuffixes.LongestPrefix([]byte(revQname)); found {
135+
if match, xval, found := patternMatcher.suffixes.LongestPrefix([]byte(revQname)); found {
136136
if len(match) == len(revQname) || revQname[len(match)] == '.' {
137137
return true, "*." + StringReverse(string(match)), xval
138138
}
139139
if len(match) < len(revQname) && len(revQname) > 0 {
140140
if i := strings.LastIndex(revQname, "."); i > 0 {
141141
pName := revQname[:i]
142-
if match, _, found := patternMatcher.blockedSuffixes.LongestPrefix([]byte(pName)); found {
142+
if match, _, found := patternMatcher.suffixes.LongestPrefix([]byte(pName)); found {
143143
if len(match) == len(pName) || pName[len(match)] == '.' {
144144
return true, "*." + StringReverse(string(match)), xval
145145
}
@@ -148,17 +148,17 @@ func (patternMatcher *PatternMatcher) Eval(qName string) (reject bool, reason st
148148
}
149149
}
150150

151-
if match, xval, found := patternMatcher.blockedPrefixes.LongestPrefix([]byte(qName)); found {
151+
if match, xval, found := patternMatcher.prefixes.LongestPrefix([]byte(qName)); found {
152152
return true, string(match) + "*", xval
153153
}
154154

155-
for _, substring := range patternMatcher.blockedSubstrings {
155+
for _, substring := range patternMatcher.substrings {
156156
if strings.Contains(qName, substring) {
157157
return true, "*" + substring + "*", patternMatcher.indirectVals[substring]
158158
}
159159
}
160160

161-
for _, pattern := range patternMatcher.blockedPatterns {
161+
for _, pattern := range patternMatcher.patterns {
162162
if found, _ := filepath.Match(pattern, qName); found {
163163
return true, pattern, patternMatcher.indirectVals[pattern]
164164
}

0 commit comments

Comments
 (0)