Skip to content

Commit 94323d5

Browse files
committed
plugin_{allow,block}_ip: fix prefix matching regression
Introduced with hot-reloading. The hot-reloading refactoring extracted IP loading logic into separate loadRules functions. However, Go's iradix.Tree is an immutable data strcture, so calling Insert() returns a new tree that must be assigned back. The refactored code was updating a local parameter variable instead of the original data structure.
1 parent cc4c28c commit 94323d5

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

dnscrypt-proxy/plugin_allow_ip.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func (plugin *PluginAllowedIP) Init(proxy *Proxy) error {
4848
plugin.allowedPrefixes = iradix.New()
4949
plugin.allowedIPs = make(map[string]interface{})
5050

51-
if err := plugin.loadRules(lines, plugin.allowedPrefixes, plugin.allowedIPs); err != nil {
51+
plugin.allowedPrefixes, err = plugin.loadRules(lines, plugin.allowedPrefixes, plugin.allowedIPs)
52+
if err != nil {
5253
return err
5354
}
5455

@@ -61,7 +62,7 @@ func (plugin *PluginAllowedIP) Init(proxy *Proxy) error {
6162
}
6263

6364
// loadRules parses and loads IP rules into the provided tree and map
64-
func (plugin *PluginAllowedIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]interface{}) error {
65+
func (plugin *PluginAllowedIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]interface{}) (*iradix.Tree, error) {
6566
for lineNo, line := range strings.Split(lines, "\n") {
6667
line = TrimAndStripInlineComments(line)
6768
if len(line) == 0 {
@@ -92,15 +93,13 @@ func (plugin *PluginAllowedIP) loadRules(lines string, prefixes *iradix.Tree, ip
9293

9394
line = strings.ToLower(line)
9495
if trailingStar {
95-
var updated *iradix.Tree
96-
updated, _, _ = prefixes.Insert([]byte(line), 0)
97-
prefixes = updated
96+
prefixes, _, _ = prefixes.Insert([]byte(line), 0)
9897
} else {
9998
ips[line] = true
10099
}
101100
}
102101

103-
return nil
102+
return prefixes, nil
104103
}
105104

106105
func (plugin *PluginAllowedIP) Drop() error {
@@ -123,7 +122,8 @@ func (plugin *PluginAllowedIP) PrepareReload() error {
123122
plugin.stagingIPs = make(map[string]interface{})
124123

125124
// Load rules into staging structures
126-
if err := plugin.loadRules(lines, plugin.stagingPrefixes, plugin.stagingIPs); err != nil {
125+
plugin.stagingPrefixes, err = plugin.loadRules(lines, plugin.stagingPrefixes, plugin.stagingIPs)
126+
if err != nil {
127127
return fmt.Errorf("error parsing config during reload preparation: %w", err)
128128
}
129129

dnscrypt-proxy/plugin_block_ip.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error {
4848
plugin.blockedPrefixes = iradix.New()
4949
plugin.blockedIPs = make(map[string]interface{})
5050

51-
if err := plugin.loadRules(lines, plugin.blockedPrefixes, plugin.blockedIPs); err != nil {
51+
plugin.blockedPrefixes, err = plugin.loadRules(lines, plugin.blockedPrefixes, plugin.blockedIPs)
52+
if err != nil {
5253
return err
5354
}
5455

@@ -61,7 +62,7 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error {
6162
}
6263

6364
// loadRules parses and loads IP rules into the provided tree and map
64-
func (plugin *PluginBlockIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]interface{}) error {
65+
func (plugin *PluginBlockIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]interface{}) (*iradix.Tree, error) {
6566
for lineNo, line := range strings.Split(lines, "\n") {
6667
line = TrimAndStripInlineComments(line)
6768
if len(line) == 0 {
@@ -92,15 +93,13 @@ func (plugin *PluginBlockIP) loadRules(lines string, prefixes *iradix.Tree, ips
9293

9394
line = strings.ToLower(line)
9495
if trailingStar {
95-
var updated *iradix.Tree
96-
updated, _, _ = prefixes.Insert([]byte(line), 0)
97-
prefixes = updated
96+
prefixes, _, _ = prefixes.Insert([]byte(line), 0)
9897
} else {
9998
ips[line] = true
10099
}
101100
}
102101

103-
return nil
102+
return prefixes, nil
104103
}
105104

106105
func (plugin *PluginBlockIP) Drop() error {
@@ -123,7 +122,8 @@ func (plugin *PluginBlockIP) PrepareReload() error {
123122
plugin.stagingIPs = make(map[string]interface{})
124123

125124
// Load rules into staging structures
126-
if err := plugin.loadRules(lines, plugin.stagingPrefixes, plugin.stagingIPs); err != nil {
125+
plugin.stagingPrefixes, err = plugin.loadRules(lines, plugin.stagingPrefixes, plugin.stagingIPs)
126+
if err != nil {
127127
return fmt.Errorf("error parsing config during reload preparation: %w", err)
128128
}
129129

0 commit comments

Comments
 (0)