Skip to content

Commit f602e68

Browse files
committed
Factor more plugin redundant code
1 parent 687cc2e commit f602e68

5 files changed

Lines changed: 60 additions & 82 deletions

File tree

dnscrypt-proxy/common.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
"sync"
1414
"time"
1515
"unicode"
16+
17+
iradix "github.com/hashicorp/go-immutable-radix"
18+
"github.com/jedisct1/dlog"
1619
)
1720

1821
type CryptoConstruction uint16
@@ -281,3 +284,44 @@ func ParseIPRule(line string, lineNo int) (cleanLine string, trailingStar bool,
281284

282285
return strings.ToLower(cleanLine), trailingStar, nil
283286
}
287+
288+
// ProcessConfigLines processes configuration file lines, calling the processor function for each non-empty line
289+
func ProcessConfigLines(lines string, processor func(line string, lineNo int) error) error {
290+
for lineNo, line := range strings.Split(lines, "\n") {
291+
line = TrimAndStripInlineComments(line)
292+
if len(line) == 0 {
293+
continue
294+
}
295+
if err := processor(line, lineNo); err != nil {
296+
return err
297+
}
298+
}
299+
return nil
300+
}
301+
302+
// LoadIPRules loads IP rules from text lines into radix tree and map structures
303+
func LoadIPRules(lines string, prefixes *iradix.Tree, ips map[string]interface{}) (*iradix.Tree, error) {
304+
err := ProcessConfigLines(lines, func(line string, lineNo int) error {
305+
cleanLine, trailingStar, lineErr := ParseIPRule(line, lineNo)
306+
if lineErr != nil {
307+
dlog.Error(lineErr)
308+
return nil // Continue processing (matching existing behavior)
309+
}
310+
311+
if trailingStar {
312+
prefixes, _, _ = prefixes.Insert([]byte(cleanLine), 0)
313+
} else {
314+
ips[cleanLine] = true
315+
}
316+
return nil
317+
})
318+
return prefixes, err
319+
}
320+
321+
// InitializePluginLogger initializes a logger for a plugin if the log file is configured
322+
func InitializePluginLogger(logFile, format string, maxSize, maxAge, maxBackups int) (io.Writer, string) {
323+
if len(logFile) > 0 {
324+
return Logger(maxSize, maxAge, maxBackups, logFile), format
325+
}
326+
return nil, ""
327+
}

dnscrypt-proxy/plugin_allow_ip.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"errors"
55
"io"
6-
"strings"
76
"sync"
87

98
iradix "github.com/hashicorp/go-immutable-radix"
@@ -50,36 +49,14 @@ func (plugin *PluginAllowedIP) Init(proxy *Proxy) error {
5049
return err
5150
}
5251

53-
if len(proxy.allowedIPLogFile) > 0 {
54-
plugin.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.allowedIPLogFile)
55-
plugin.format = proxy.allowedIPFormat
56-
}
52+
plugin.logger, plugin.format = InitializePluginLogger(proxy.allowedIPLogFile, proxy.allowedIPFormat, proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups)
5753

5854
return nil
5955
}
6056

6157
// loadRules parses and loads IP rules into the provided tree and map
6258
func (plugin *PluginAllowedIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]interface{}) (*iradix.Tree, error) {
63-
for lineNo, line := range strings.Split(lines, "\n") {
64-
line = TrimAndStripInlineComments(line)
65-
if len(line) == 0 {
66-
continue
67-
}
68-
69-
cleanLine, trailingStar, err := ParseIPRule(line, lineNo)
70-
if err != nil {
71-
dlog.Error(err)
72-
continue
73-
}
74-
75-
if trailingStar {
76-
prefixes, _, _ = prefixes.Insert([]byte(cleanLine), 0)
77-
} else {
78-
ips[cleanLine] = true
79-
}
80-
}
81-
82-
return prefixes, nil
59+
return LoadIPRules(lines, prefixes, ips)
8360
}
8461

8562
func (plugin *PluginAllowedIP) Drop() error {

dnscrypt-proxy/plugin_allow_name.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"errors"
55
"io"
6-
"strings"
76
"sync"
87

98
"github.com/jedisct1/dlog"
@@ -47,35 +46,26 @@ func (plugin *PluginAllowName) Init(proxy *Proxy) error {
4746
return err
4847
}
4948

50-
if len(proxy.allowNameLogFile) > 0 {
51-
plugin.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.allowNameLogFile)
52-
plugin.format = proxy.allowNameFormat
53-
}
49+
plugin.logger, plugin.format = InitializePluginLogger(proxy.allowNameLogFile, proxy.allowNameFormat, proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups)
5450

5551
return nil
5652
}
5753

5854
// loadPatterns parses and loads patterns into the provided pattern matcher
5955
func (plugin *PluginAllowName) loadPatterns(lines string, patternMatcher *PatternMatcher) error {
60-
for lineNo, line := range strings.Split(lines, "\n") {
61-
line = TrimAndStripInlineComments(line)
62-
if len(line) == 0 {
63-
continue
64-
}
65-
56+
return ProcessConfigLines(lines, func(line string, lineNo int) error {
6657
rulePart, weeklyRanges, err := ParseTimeBasedRule(line, lineNo, plugin.allWeeklyRanges)
6758
if err != nil {
6859
dlog.Error(err)
69-
continue
60+
return nil
7061
}
7162

7263
if err := patternMatcher.Add(rulePart, weeklyRanges, lineNo+1); err != nil {
7364
dlog.Error(err)
74-
continue
65+
return nil
7566
}
76-
}
77-
78-
return nil
67+
return nil
68+
})
7969
}
8070

8171
func (plugin *PluginAllowName) Drop() error {

dnscrypt-proxy/plugin_block_ip.go

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"errors"
55
"io"
6-
"strings"
76
"sync"
87

98
iradix "github.com/hashicorp/go-immutable-radix"
@@ -50,36 +49,14 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error {
5049
return err
5150
}
5251

53-
if len(proxy.blockIPLogFile) > 0 {
54-
plugin.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.blockIPLogFile)
55-
plugin.format = proxy.blockIPFormat
56-
}
52+
plugin.logger, plugin.format = InitializePluginLogger(proxy.blockIPLogFile, proxy.blockIPFormat, proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups)
5753

5854
return nil
5955
}
6056

6157
// loadRules parses and loads IP rules into the provided tree and map
6258
func (plugin *PluginBlockIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]interface{}) (*iradix.Tree, error) {
63-
for lineNo, line := range strings.Split(lines, "\n") {
64-
line = TrimAndStripInlineComments(line)
65-
if len(line) == 0 {
66-
continue
67-
}
68-
69-
cleanLine, trailingStar, err := ParseIPRule(line, lineNo)
70-
if err != nil {
71-
dlog.Error(err)
72-
continue
73-
}
74-
75-
if trailingStar {
76-
prefixes, _, _ = prefixes.Insert([]byte(cleanLine), 0)
77-
} else {
78-
ips[cleanLine] = true
79-
}
80-
}
81-
82-
return prefixes, nil
59+
return LoadIPRules(lines, prefixes, ips)
8360
}
8461

8562
func (plugin *PluginBlockIP) Drop() error {

dnscrypt-proxy/plugin_block_name.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"errors"
55
"io"
6-
"strings"
76
"sync"
87

98
"github.com/jedisct1/dlog"
@@ -93,10 +92,7 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
9392
return err
9493
}
9594

96-
if len(proxy.blockNameLogFile) > 0 {
97-
xBlockedNames.logger = Logger(proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups, proxy.blockNameLogFile)
98-
xBlockedNames.format = proxy.blockNameFormat
99-
}
95+
xBlockedNames.logger, xBlockedNames.format = InitializePluginLogger(proxy.blockNameLogFile, proxy.blockNameFormat, proxy.logMaxSize, proxy.logMaxAge, proxy.logMaxBackups)
10096

10197
blockedNamesLock.Lock()
10298
blockedNames = &xBlockedNames
@@ -107,25 +103,19 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
107103

108104
// loadRules parses and loads name patterns into the BlockedNames
109105
func (plugin *PluginBlockName) loadRules(lines string, blockedNamesObj *BlockedNames) error {
110-
for lineNo, line := range strings.Split(lines, "\n") {
111-
line = TrimAndStripInlineComments(line)
112-
if len(line) == 0 {
113-
continue
114-
}
115-
106+
return ProcessConfigLines(lines, func(line string, lineNo int) error {
116107
rulePart, weeklyRanges, err := ParseTimeBasedRule(line, lineNo, blockedNamesObj.allWeeklyRanges)
117108
if err != nil {
118109
dlog.Error(err)
119-
continue
110+
return nil
120111
}
121112

122113
if err := blockedNamesObj.patternMatcher.Add(rulePart, weeklyRanges, lineNo+1); err != nil {
123114
dlog.Error(err)
124-
continue
115+
return nil
125116
}
126-
}
127-
128-
return nil
117+
return nil
118+
})
129119
}
130120

131121
func (plugin *PluginBlockName) Drop() error {

0 commit comments

Comments
 (0)