Skip to content

Commit c3ba78f

Browse files
committed
Simplify
1 parent 0acc6a5 commit c3ba78f

11 files changed

Lines changed: 27 additions & 316 deletions

dnscrypt-proxy/config.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,16 +530,6 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
530530
return nil
531531
}
532532

533-
// GetRefusedFlag - Returns whether the config has defined refused_code_in_responses
534-
func (config *Config) GetRefusedFlag(configFile string) (bool, bool) {
535-
var refused bool
536-
md, err := toml.DecodeFile(configFile, &refused)
537-
if err != nil {
538-
return false, false
539-
}
540-
return refused, md.IsDefined("refused_code_in_responses")
541-
}
542-
543533
// configureBrokenImplementations - Helper function for IsDefined check
544534
func configureBrokenImplementations(proxy *Proxy, config *Config) {
545535
// Backwards compatibility

dnscrypt-proxy/logger.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ func Logger(logMaxSize int, logMaxAge int, logMaxBackups int, fileName string) i
2525
}
2626
return fp
2727
}
28-
if fp, err := os.OpenFile(fileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644); err == nil {
29-
fp.Close()
30-
} else {
31-
dlog.Errorf("Unable to create [%v]: [%v]", fileName, err)
32-
}
3328
logger := &lumberjack.Logger{
3429
LocalTime: true,
3530
MaxSize: logMaxSize,

dnscrypt-proxy/monitoring_ui.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,10 @@ func (ui *MonitoringUI) UpdateMetrics(pluginsState PluginsState, msg *dns.Msg) {
326326
// Update query types - separate lock
327327
if msg != nil && len(msg.Question) > 0 {
328328
question := msg.Question[0]
329-
qType, ok := dns.TypeToString[dns.RRToType(question)]
329+
rrType := dns.RRToType(question)
330+
qType, ok := dns.TypeToString[rrType]
330331
if !ok {
331-
qType = fmt.Sprintf("%d", dns.RRToType(question))
332+
qType = fmt.Sprintf("%d", rrType)
332333
}
333334
mc.queryTypesMutex.Lock()
334335
mc.queryTypes[qType]++

dnscrypt-proxy/plugin_allow_ip.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (plugin *PluginAllowedIP) Init(proxy *Proxy) error {
5050
plugin.allowedIPs = make(map[string]any)
5151
plugin.allowedNetworks = critbitgo.NewNet()
5252

53-
plugin.allowedPrefixes, err = plugin.loadRules(lines, plugin.allowedPrefixes, plugin.allowedIPs, plugin.allowedNetworks)
53+
plugin.allowedPrefixes, err = LoadIPRules(lines, plugin.allowedPrefixes, plugin.allowedIPs, plugin.allowedNetworks)
5454
if err != nil {
5555
return err
5656
}
@@ -61,11 +61,6 @@ func (plugin *PluginAllowedIP) Init(proxy *Proxy) error {
6161
return nil
6262
}
6363

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)
67-
}
68-
6964
func (plugin *PluginAllowedIP) Drop() error {
7065
if plugin.configWatcher != nil {
7166
plugin.configWatcher.RemoveFile(plugin.configFile)
@@ -83,7 +78,7 @@ func (plugin *PluginAllowedIP) PrepareReload() error {
8378

8479
// Load rules into staging structures
8580
var err error
86-
plugin.stagingPrefixes, err = plugin.loadRules(lines, plugin.stagingPrefixes, plugin.stagingIPs, plugin.stagingNetworks)
81+
plugin.stagingPrefixes, err = LoadIPRules(lines, plugin.stagingPrefixes, plugin.stagingIPs, plugin.stagingNetworks)
8782
return err
8883
})
8984
}

dnscrypt-proxy/plugin_block_ip.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error {
5050
plugin.blockedIPs = make(map[string]any)
5151
plugin.blockedNetworks = critbitgo.NewNet()
5252

53-
plugin.blockedPrefixes, err = plugin.loadRules(lines, plugin.blockedPrefixes, plugin.blockedIPs, plugin.blockedNetworks)
53+
plugin.blockedPrefixes, err = LoadIPRules(lines, plugin.blockedPrefixes, plugin.blockedIPs, plugin.blockedNetworks)
5454
if err != nil {
5555
return err
5656
}
@@ -61,11 +61,6 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error {
6161
return nil
6262
}
6363

64-
// loadRules parses and loads IP rules into the provided tree, map, and network table
65-
func (plugin *PluginBlockIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]any, networks *critbitgo.Net) (*iradix.Tree, error) {
66-
return LoadIPRules(lines, prefixes, ips, networks)
67-
}
68-
6964
func (plugin *PluginBlockIP) Drop() error {
7065
if plugin.configWatcher != nil {
7166
plugin.configWatcher.RemoveFile(plugin.configFile)
@@ -83,7 +78,7 @@ func (plugin *PluginBlockIP) PrepareReload() error {
8378

8479
// Load rules into staging structures
8580
var err error
86-
plugin.stagingPrefixes, err = plugin.loadRules(lines, plugin.stagingPrefixes, plugin.stagingIPs, plugin.stagingNetworks)
81+
plugin.stagingPrefixes, err = LoadIPRules(lines, plugin.stagingPrefixes, plugin.stagingIPs, plugin.stagingNetworks)
8782
return err
8883
})
8984
}

dnscrypt-proxy/plugin_cloak.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,13 @@ func (plugin *PluginCloak) CancelReload() {
264264

265265
// Reload implements hot-reloading for the plugin
266266
func (plugin *PluginCloak) Reload() error {
267-
dlog.Noticef("Reloading configuration for plugin [%s]", plugin.Name())
268-
269-
// Prepare the new configuration
270-
if err := plugin.PrepareReload(); err != nil {
271-
plugin.CancelReload()
272-
return err
273-
}
274-
275-
// Apply the new configuration
276-
return plugin.ApplyReload()
267+
return StandardReloadPattern(plugin.Name(), func() error {
268+
if err := plugin.PrepareReload(); err != nil {
269+
plugin.CancelReload()
270+
return err
271+
}
272+
return plugin.ApplyReload()
273+
})
277274
}
278275

279276
// GetConfigPath returns the path to the plugin's configuration file

dnscrypt-proxy/plugin_forward.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,13 @@ func (plugin *PluginForward) CancelReload() {
293293

294294
// Reload implements hot-reloading for the plugin
295295
func (plugin *PluginForward) Reload() error {
296-
dlog.Noticef("Reloading configuration for plugin [%s]", plugin.Name())
297-
298-
// Prepare the new configuration
299-
if err := plugin.PrepareReload(); err != nil {
300-
plugin.CancelReload()
301-
return err
302-
}
303-
304-
// Apply the new configuration
305-
return plugin.ApplyReload()
296+
return StandardReloadPattern(plugin.Name(), func() error {
297+
if err := plugin.PrepareReload(); err != nil {
298+
plugin.CancelReload()
299+
return err
300+
}
301+
return plugin.ApplyReload()
302+
})
306303
}
307304

308305
// GetConfigPath returns the path to the plugin's configuration file

dnscrypt-proxy/plugin_nx_log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ func (plugin *PluginNxLog) Eval(pluginsState *PluginsState, msg *dns.Msg) error
5050
return nil
5151
}
5252
question := msg.Question[0]
53-
qType, ok := dns.TypeToString[dns.RRToType(question)]
53+
rrType := dns.RRToType(question)
54+
qType, ok := dns.TypeToString[rrType]
5455
if !ok {
55-
qType = fmt.Sprintf("%d", dns.RRToType(question))
56+
qType = fmt.Sprintf("%d", rrType)
5657
}
5758
qName := pluginsState.qName
5859

dnscrypt-proxy/plugin_query_log.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) err
5050
return nil
5151
}
5252
question := msg.Question[0]
53-
qType, ok := dns.TypeToString[dns.RRToType(question)]
53+
rrType := dns.RRToType(question)
54+
qType, ok := dns.TypeToString[rrType]
5455
if !ok {
55-
qType = fmt.Sprintf("%d", dns.RRToType(question))
56+
qType = fmt.Sprintf("%d", rrType)
5657
}
5758
if len(plugin.ignoredQtypes) > 0 {
5859
for _, ignoredQtype := range plugin.ignoredQtypes {

dnscrypt-proxy/reload_utils.go

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package main
33
import (
44
"errors"
55
"fmt"
6-
"sync"
7-
"sync/atomic"
86
"time"
97

108
"codeberg.org/miekg/dns"
@@ -29,115 +27,6 @@ type ReloadablePlugin interface {
2927
SetConfigWatcher(watcher *ConfigWatcher) // Set the config watcher
3028
}
3129

32-
// ReloadSafeguard provides atomic operations to ensure configuration reloading is safe
33-
type ReloadSafeguard struct {
34-
isReloading int32 // Flag indicating if a reload is in progress (0=false, 1=true)
35-
reloadMutex sync.Mutex // Mutex for reload operations
36-
activeConfigMu sync.RWMutex // Read-write mutex for accessing active configuration
37-
activeTimestamp time.Time // Timestamp when the active config was loaded
38-
}
39-
40-
// NewReloadSafeguard creates a new reload safeguard
41-
func NewReloadSafeguard() *ReloadSafeguard {
42-
return &ReloadSafeguard{
43-
activeTimestamp: time.Now(),
44-
}
45-
}
46-
47-
// StartReload attempts to start a reload operation
48-
// Returns true if reload can proceed, false if another reload is in progress
49-
func (rs *ReloadSafeguard) StartReload() bool {
50-
// Try to set isReloading atomically, only succeeds if it was previously false
51-
if atomic.CompareAndSwapInt32(&rs.isReloading, 0, 1) {
52-
rs.reloadMutex.Lock()
53-
return true
54-
}
55-
return false
56-
}
57-
58-
// FinishReload completes a reload operation, releasing locks
59-
func (rs *ReloadSafeguard) FinishReload() {
60-
atomic.StoreInt32(&rs.isReloading, 0)
61-
rs.reloadMutex.Unlock()
62-
}
63-
64-
// AcquireConfigRead acquires a read lock on the active configuration
65-
func (rs *ReloadSafeguard) AcquireConfigRead() {
66-
rs.activeConfigMu.RLock()
67-
}
68-
69-
// ReleaseConfigRead releases a read lock on the active configuration
70-
func (rs *ReloadSafeguard) ReleaseConfigRead() {
71-
rs.activeConfigMu.RUnlock()
72-
}
73-
74-
// AcquireConfigWrite acquires a write lock on the active configuration
75-
func (rs *ReloadSafeguard) AcquireConfigWrite() {
76-
rs.activeConfigMu.Lock()
77-
}
78-
79-
// ReleaseConfigWrite releases a write lock on the active configuration
80-
func (rs *ReloadSafeguard) ReleaseConfigWrite() {
81-
rs.activeConfigMu.Unlock()
82-
rs.activeTimestamp = time.Now()
83-
}
84-
85-
// SafeReload handles the entire reload process with proper locking
86-
// The provided function is executed while holding the write lock
87-
func (rs *ReloadSafeguard) SafeReload(reloadFunc func() error) error {
88-
if !rs.StartReload() {
89-
return errors.New("another reload operation is already in progress")
90-
}
91-
defer rs.FinishReload()
92-
93-
// Acquire write lock for configuration update
94-
rs.AcquireConfigWrite()
95-
defer rs.ReleaseConfigWrite()
96-
97-
// Execute the provided reload function
98-
return reloadFunc()
99-
}
100-
101-
// RegisterPluginForReload adds a plugin to the config watcher for automatic reloading
102-
func RegisterPluginForReload(plugin ReloadablePlugin, watcher *ConfigWatcher) error {
103-
configPath := plugin.GetConfigPath()
104-
if configPath == "" {
105-
return errors.New("empty configuration path for plugin: " + plugin.Name())
106-
}
107-
108-
// Create a reload function closure that handles the complete reload process
109-
reloadFunc := func() error {
110-
dlog.Noticef("Reloading configuration for plugin [%s]", plugin.Name())
111-
112-
// Prepare stage: Load and validate new configuration without applying it
113-
if err := plugin.PrepareReload(); err != nil {
114-
dlog.Errorf("Failed to prepare reload for plugin [%s]: %v", plugin.Name(), err)
115-
plugin.CancelReload() // Ensure cleanup of any temporary resources
116-
return err
117-
}
118-
119-
// Apply stage: Switch to new configuration
120-
if err := plugin.ApplyReload(); err != nil {
121-
dlog.Errorf("Failed to apply reload for plugin [%s]: %v", plugin.Name(), err)
122-
plugin.CancelReload() // Ensure cleanup of any temporary resources
123-
return err
124-
}
125-
126-
dlog.Noticef("Successfully reloaded plugin [%s]", plugin.Name())
127-
return nil
128-
}
129-
130-
// Add the plugin's config file to the watcher
131-
if err := watcher.AddFile(configPath, reloadFunc); err != nil {
132-
return err
133-
}
134-
135-
// Set the config watcher in the plugin
136-
plugin.SetConfigWatcher(watcher)
137-
138-
return nil
139-
}
140-
14130
// SafeReadTextFile is similar to ReadTextFile but with additional safeguards
14231
// to prevent reading partially written files
14332
func SafeReadTextFile(filePath string) (string, error) {

0 commit comments

Comments
 (0)