@@ -3,16 +3,19 @@ package main
33import (
44 "errors"
55 "io"
6+ "net"
67 "sync"
78
89 "codeberg.org/miekg/dns"
910 iradix "github.com/hashicorp/go-immutable-radix"
1011 "github.com/jedisct1/dlog"
12+ "github.com/k-sone/critbitgo"
1113)
1214
1315type PluginAllowedIP struct {
1416 allowedPrefixes * iradix.Tree
1517 allowedIPs map [string ]any
18+ allowedNetworks * critbitgo.Net
1619 logger io.Writer
1720 format string
1821 ipCryptConfig * IPCryptConfig
@@ -23,6 +26,7 @@ type PluginAllowedIP struct {
2326 configWatcher * ConfigWatcher
2427 stagingPrefixes * iradix.Tree
2528 stagingIPs map [string ]any
29+ stagingNetworks * critbitgo.Net
2630}
2731
2832func (plugin * PluginAllowedIP ) Name () string {
@@ -44,8 +48,9 @@ func (plugin *PluginAllowedIP) Init(proxy *Proxy) error {
4448
4549 plugin .allowedPrefixes = iradix .New ()
4650 plugin .allowedIPs = make (map [string ]any )
51+ plugin .allowedNetworks = critbitgo .NewNet ()
4752
48- plugin .allowedPrefixes , err = plugin .loadRules (lines , plugin .allowedPrefixes , plugin .allowedIPs )
53+ plugin .allowedPrefixes , err = plugin .loadRules (lines , plugin .allowedPrefixes , plugin .allowedIPs , plugin . allowedNetworks )
4954 if err != nil {
5055 return err
5156 }
@@ -56,9 +61,9 @@ func (plugin *PluginAllowedIP) Init(proxy *Proxy) error {
5661 return nil
5762}
5863
59- // loadRules parses and loads IP rules into the provided tree and map
60- func (plugin * PluginAllowedIP ) loadRules (lines string , prefixes * iradix.Tree , ips map [string ]any ) (* iradix.Tree , error ) {
61- return LoadIPRules (lines , prefixes , ips )
64+ // loadRules parses and loads IP rules into the provided tree, map, and network table
65+ func (plugin * PluginAllowedIP ) loadRules (lines string , prefixes * iradix.Tree , ips map [string ]any , networks * critbitgo. Net ) (* iradix.Tree , error ) {
66+ return LoadIPRules (lines , prefixes , ips , networks )
6267}
6368
6469func (plugin * PluginAllowedIP ) Drop () error {
@@ -74,27 +79,30 @@ func (plugin *PluginAllowedIP) PrepareReload() error {
7479 // Create staging structures
7580 plugin .stagingPrefixes = iradix .New ()
7681 plugin .stagingIPs = make (map [string ]any )
82+ plugin .stagingNetworks = critbitgo .NewNet ()
7783
7884 // Load rules into staging structures
7985 var err error
80- plugin .stagingPrefixes , err = plugin .loadRules (lines , plugin .stagingPrefixes , plugin .stagingIPs )
86+ plugin .stagingPrefixes , err = plugin .loadRules (lines , plugin .stagingPrefixes , plugin .stagingIPs , plugin . stagingNetworks )
8187 return err
8288 })
8389}
8490
8591// ApplyReload atomically replaces the active rules with the staging ones
8692func (plugin * PluginAllowedIP ) ApplyReload () error {
8793 return StandardApplyReloadPattern (plugin .Name (), func () error {
88- if plugin .stagingPrefixes == nil || plugin .stagingIPs == nil {
94+ if plugin .stagingPrefixes == nil || plugin .stagingIPs == nil || plugin . stagingNetworks == nil {
8995 return errors .New ("no staged configuration to apply" )
9096 }
9197
9298 // Use write lock to swap rule structures
9399 plugin .rwLock .Lock ()
94100 plugin .allowedPrefixes = plugin .stagingPrefixes
95101 plugin .allowedIPs = plugin .stagingIPs
102+ plugin .allowedNetworks = plugin .stagingNetworks
96103 plugin .stagingPrefixes = nil
97104 plugin .stagingIPs = nil
105+ plugin .stagingNetworks = nil
98106 plugin .rwLock .Unlock ()
99107
100108 return nil
@@ -105,6 +113,7 @@ func (plugin *PluginAllowedIP) ApplyReload() error {
105113func (plugin * PluginAllowedIP ) CancelReload () {
106114 plugin .stagingPrefixes = nil
107115 plugin .stagingIPs = nil
116+ plugin .stagingNetworks = nil
108117}
109118
110119// Reload implements hot-reloading for the plugin
@@ -165,6 +174,14 @@ func (plugin *PluginAllowedIP) Eval(pluginsState *PluginsState, msg *dns.Msg) er
165174 break
166175 }
167176 }
177+ if plugin .allowedNetworks .Size () > 0 {
178+ if ip := net .ParseIP (ipStr ); ip != nil {
179+ if route , _ , _ := plugin .allowedNetworks .MatchIP (ip ); route != nil {
180+ allowed , reason = true , route .String ()
181+ break
182+ }
183+ }
184+ }
168185 }
169186
170187 if allowed {
0 commit comments