Skip to content

Commit 45715fd

Browse files
committed
go fix
1 parent 52681d9 commit 45715fd

19 files changed

Lines changed: 83 additions & 92 deletions

dnscrypt-proxy/coldstart.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,11 @@ func addColdStartListener(
138138
if err != nil {
139139
return err
140140
}
141-
captivePortalHandler.wg.Add(1)
142-
go func() {
141+
captivePortalHandler.wg.Go(func() {
143142
for !handleColdStartClient(clientPc, captivePortalHandler.cancelChannel, ipsMap) {
144143
}
145144
clientPc.Close()
146-
captivePortalHandler.wg.Done()
147-
}()
145+
})
148146
return nil
149147
}
150148

@@ -181,7 +179,7 @@ func ColdStart(proxy *Proxy) (*CaptivePortalHandler, error) {
181179
)
182180
}
183181
var ips []net.IP
184-
for _, ip := range strings.Split(ipsStr, ",") {
182+
for ip := range strings.SplitSeq(ipsStr, ",") {
185183
ipStr := strings.TrimSpace(ip)
186184
if ip := net.ParseIP(ipStr); ip != nil {
187185
ips = append(ips, ip)

dnscrypt-proxy/common.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,25 @@ func FormatLogLine(format, clientIP, qName, reason string, additionalFields ...s
210210
hour, minute, second := now.Clock()
211211
tsStr := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d]", year, int(month), day, hour, minute, second)
212212

213-
line := fmt.Sprintf("%s\t%s\t%s\t%s", tsStr, clientIP, StringQuote(qName), StringQuote(reason))
213+
var line strings.Builder
214+
line.WriteString(fmt.Sprintf("%s\t%s\t%s\t%s", tsStr, clientIP, StringQuote(qName), StringQuote(reason)))
214215
for _, field := range additionalFields {
215-
line += fmt.Sprintf("\t%s", StringQuote(field))
216+
line.WriteString(fmt.Sprintf("\t%s", StringQuote(field)))
216217
}
217-
return line + "\n", nil
218+
return line.String() + "\n", nil
218219
} else if format == "ltsv" {
219-
line := fmt.Sprintf("time:%d\thost:%s\tqname:%s\tmessage:%s", time.Now().Unix(), clientIP, StringQuote(qName), StringQuote(reason))
220+
var line strings.Builder
221+
line.WriteString(fmt.Sprintf("time:%d\thost:%s\tqname:%s\tmessage:%s", time.Now().Unix(), clientIP, StringQuote(qName), StringQuote(reason)))
220222

221223
// For LTSV format, additional fields are added with specific labels
222224
for i, field := range additionalFields {
223225
if i == 0 {
224-
line += fmt.Sprintf("\tip:%s", StringQuote(field))
226+
line.WriteString(fmt.Sprintf("\tip:%s", StringQuote(field)))
225227
} else {
226-
line += fmt.Sprintf("\tfield%d:%s", i, StringQuote(field))
228+
line.WriteString(fmt.Sprintf("\tfield%d:%s", i, StringQuote(field)))
227229
}
228230
}
229-
return line + "\n", nil
231+
return line.String() + "\n", nil
230232
}
231233
return "", fmt.Errorf("unexpected log format: [%s]", format)
232234
}
@@ -312,7 +314,7 @@ func ProcessConfigLines(lines string, processor func(line string, lineNo int) er
312314
}
313315

314316
// LoadIPRules loads IP rules from text lines into radix tree and map structures
315-
func LoadIPRules(lines string, prefixes *iradix.Tree, ips map[string]interface{}) (*iradix.Tree, error) {
317+
func LoadIPRules(lines string, prefixes *iradix.Tree, ips map[string]any) (*iradix.Tree, error) {
316318
err := ProcessConfigLines(lines, func(line string, lineNo int) error {
317319
cleanLine, trailingStar, lineErr := ParseIPRule(line, lineNo)
318320
if lineErr != nil {

dnscrypt-proxy/config_loader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ func configureLoadBalancing(proxy *Proxy, config *Config) {
207207
case "wp2":
208208
lbStrategy = LBStrategyWP2{}
209209
default:
210-
if strings.HasPrefix(lbStrategyStr, "p") {
211-
n, err := strconv.ParseInt(strings.TrimPrefix(lbStrategyStr, "p"), 10, 32)
210+
if after, ok := strings.CutPrefix(lbStrategyStr, "p"); ok {
211+
n, err := strconv.ParseInt(after, 10, 32)
212212
if err != nil || n <= 0 {
213213
dlog.Warnf("Invalid load balancing strategy: [%s]", config.LBStrategy)
214214
} else {

dnscrypt-proxy/config_watcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func TestConfigWatcher(t *testing.T) {
7575
}
7676

7777
// Test that rapid changes are debounced
78-
for i := 0; i < 5; i++ {
79-
content := []byte(fmt.Sprintf("%d content", i))
78+
for i := range 5 {
79+
content := fmt.Appendf(nil, "%d content", i)
8080
if err := os.WriteFile(tempFile, content, 0o644); err != nil {
8181
t.Fatalf("Failed to update test file in loop: %v", err)
8282
}

dnscrypt-proxy/crypto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func ComputeSharedKey(
5959
} else {
6060
box.Precompute(&sharedKey, serverPk, secretKey)
6161
c := byte(0)
62-
for i := 0; i < 32; i++ {
62+
for i := range 32 {
6363
c |= sharedKey[i]
6464
}
6565
if c == 0 {

dnscrypt-proxy/dnsutils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func NormalizeQName(str string) (string, error) {
142142
hasUpper := false
143143
str = strings.TrimSuffix(str, ".")
144144
strLen := len(str)
145-
for i := 0; i < strLen; i++ {
145+
for i := range strLen {
146146
c := str[i]
147147
if c >= utf8.RuneSelf {
148148
return str, errors.New("Query name is not an ASCII string")
@@ -154,7 +154,7 @@ func NormalizeQName(str string) (string, error) {
154154
}
155155
var b strings.Builder
156156
b.Grow(len(str))
157-
for i := 0; i < strLen; i++ {
157+
for i := range strLen {
158158
c := str[i]
159159
if 'A' <= c && c <= 'Z' {
160160
c += 'a' - 'A'
@@ -326,7 +326,7 @@ func DNSExchange(
326326
var err error
327327
options := 0
328328

329-
for tries := 0; tries < maxTries; tries++ {
329+
for tries := range maxTries {
330330
if tryFragmentsSupport {
331331
queryCopy := query.Copy()
332332
queryCopy.ID += uint16(options)

dnscrypt-proxy/monitoring_ui.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type MetricsCollector struct {
6666

6767
// Caching for expensive calculations
6868
cacheMutex sync.RWMutex
69-
cachedMetrics map[string]interface{}
69+
cachedMetrics map[string]any
7070
cacheLastUpdate time.Time
7171
cacheTTL time.Duration
7272

@@ -170,7 +170,7 @@ func NewMonitoringUI(proxy *Proxy) *MonitoringUI {
170170
privacyLevel: proxy.monitoringUI.PrivacyLevel,
171171
// Initialize caching with 1 second TTL
172172
cacheTTL: time.Second,
173-
cachedMetrics: make(map[string]interface{}),
173+
cachedMetrics: make(map[string]any),
174174
// Initialize Prometheus
175175
prometheusEnabled: proxy.monitoringUI.PrometheusEnabled,
176176
proxy: proxy,
@@ -688,8 +688,8 @@ func (mc *MetricsCollector) collectResolverSnapshots() ([]resolverSnapshot, map[
688688
return snapshots, index
689689
}
690690

691-
func (mc *MetricsCollector) collectCacheStats(cacheHitRatio float64, cacheHits, cacheMisses uint64) map[string]interface{} {
692-
stats := map[string]interface{}{
691+
func (mc *MetricsCollector) collectCacheStats(cacheHitRatio float64, cacheHits, cacheMisses uint64) map[string]any {
692+
stats := map[string]any{
693693
"enabled": false,
694694
"configured_size": 0,
695695
"entries": 0,
@@ -718,12 +718,12 @@ func (mc *MetricsCollector) collectCacheStats(cacheHitRatio float64, cacheHits,
718718
return stats
719719
}
720720

721-
func (mc *MetricsCollector) collectSourceRefresh() []map[string]interface{} {
721+
func (mc *MetricsCollector) collectSourceRefresh() []map[string]any {
722722
if mc.proxy == nil || len(mc.proxy.sources) == 0 {
723723
return nil
724724
}
725725

726-
results := make([]map[string]interface{}, 0, len(mc.proxy.sources))
726+
results := make([]map[string]any, 0, len(mc.proxy.sources))
727727
now := time.Now()
728728

729729
for _, source := range mc.proxy.sources {
@@ -765,7 +765,7 @@ func (mc *MetricsCollector) collectSourceRefresh() []map[string]interface{} {
765765
status = "stale"
766766
}
767767

768-
entry := map[string]interface{}{
768+
entry := map[string]any{
769769
"name": name,
770770
"cache_file": cacheFile,
771771
"age_seconds": ageSeconds,
@@ -799,7 +799,7 @@ func (mc *MetricsCollector) invalidateCache() {
799799
}
800800

801801
// GetMetrics - Returns the current metrics
802-
func (mc *MetricsCollector) GetMetrics() map[string]interface{} {
802+
func (mc *MetricsCollector) GetMetrics() map[string]any {
803803
// Check cache first
804804
mc.cacheMutex.RLock()
805805
if time.Since(mc.cacheLastUpdate) < mc.cacheTTL && mc.cachedMetrics != nil {
@@ -858,7 +858,7 @@ func (mc *MetricsCollector) GetMetrics() map[string]interface{} {
858858
}
859859

860860
// Get top domains (limited to 20) sorted by decreasing count
861-
topDomainsList := make([]map[string]interface{}, 0)
861+
topDomainsList := make([]map[string]any, 0)
862862
if mc.privacyLevel < 2 {
863863
// Create a slice of domain-count pairs
864864
type domainCount struct {
@@ -884,7 +884,7 @@ func (mc *MetricsCollector) GetMetrics() map[string]interface{} {
884884
// Take top 20
885885
count := 0
886886
for _, dc := range domainCounts {
887-
topDomainsList = append(topDomainsList, map[string]interface{}{
887+
topDomainsList = append(topDomainsList, map[string]any{
888888
"domain": html.EscapeString(dc.domain),
889889
"count": dc.count,
890890
})
@@ -896,7 +896,7 @@ func (mc *MetricsCollector) GetMetrics() map[string]interface{} {
896896
}
897897

898898
// Get query type distribution sorted by decreasing count and limited to 10
899-
queryTypesList := make([]map[string]interface{}, 0)
899+
queryTypesList := make([]map[string]any, 0)
900900

901901
// Create a slice of query type-count pairs
902902
type queryTypeCount struct {
@@ -922,7 +922,7 @@ func (mc *MetricsCollector) GetMetrics() map[string]interface{} {
922922
// Take top 10
923923
count := 0
924924
for _, qtc := range queryTypeCounts {
925-
queryTypesList = append(queryTypesList, map[string]interface{}{
925+
queryTypesList = append(queryTypesList, map[string]any{
926926
"type": qtc.qtype,
927927
"count": qtc.count,
928928
})
@@ -938,9 +938,9 @@ func (mc *MetricsCollector) GetMetrics() map[string]interface{} {
938938
copy(recentQueries, mc.recentQueries)
939939
mc.queryLogMutex.RUnlock()
940940

941-
resolverHealth := make([]map[string]interface{}, 0, len(resolverSnapshots))
941+
resolverHealth := make([]map[string]any, 0, len(resolverSnapshots))
942942
for _, snapshot := range resolverSnapshots {
943-
entry := map[string]interface{}{
943+
entry := map[string]any{
944944
"name": snapshot.name,
945945
"proto": snapshot.proto,
946946
"status": snapshot.status,
@@ -968,7 +968,7 @@ func (mc *MetricsCollector) GetMetrics() map[string]interface{} {
968968
generatedAt := time.Now().UTC()
969969

970970
// Return all metrics and cache the result
971-
metrics := map[string]interface{}{
971+
metrics := map[string]any{
972972
"total_queries": totalQueries,
973973
"queries_per_second": queriesPerSecond,
974974
"uptime_seconds": time.Since(startTime).Seconds(),
@@ -1168,7 +1168,7 @@ func (ui *MonitoringUI) handleWebSocket(w http.ResponseWriter, r *http.Request)
11681168

11691169
for {
11701170
// Read message from client
1171-
var msg map[string]interface{}
1171+
var msg map[string]any
11721172
err := conn.ReadJSON(&msg)
11731173
if err != nil {
11741174
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {

dnscrypt-proxy/pattern_matcher.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ type PatternMatcher struct {
2626
suffixes *critbitgo.Trie
2727
substrings []string
2828
patterns []string
29-
exact map[string]interface{}
30-
indirectVals map[string]interface{}
29+
exact map[string]any
30+
indirectVals map[string]any
3131
}
3232

3333
func NewPatternMatcher() *PatternMatcher {
3434
patternMatcher := PatternMatcher{
3535
prefixes: critbitgo.NewTrie(),
3636
suffixes: critbitgo.NewTrie(),
37-
exact: make(map[string]interface{}),
38-
indirectVals: make(map[string]interface{}),
37+
exact: make(map[string]any),
38+
indirectVals: make(map[string]any),
3939
}
4040
return &patternMatcher
4141
}
@@ -51,7 +51,7 @@ func isGlobCandidate(str string) bool {
5151
return false
5252
}
5353

54-
func (patternMatcher *PatternMatcher) Add(pattern string, val interface{}, position int) error {
54+
func (patternMatcher *PatternMatcher) Add(pattern string, val any, position int) error {
5555
// Determine pattern type based on wildcards and special characters
5656
leadingStar := strings.HasPrefix(pattern, "*")
5757
trailingStar := strings.HasSuffix(pattern, "*")
@@ -122,7 +122,7 @@ func (patternMatcher *PatternMatcher) Add(pattern string, val interface{}, posit
122122
return nil
123123
}
124124

125-
func (patternMatcher *PatternMatcher) Eval(qName string) (reject bool, reason string, val interface{}) {
125+
func (patternMatcher *PatternMatcher) Eval(qName string) (reject bool, reason string, val any) {
126126
if len(qName) < 2 {
127127
return false, "", nil
128128
}

dnscrypt-proxy/plugin_allow_ip.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
type PluginAllowedIP struct {
1414
allowedPrefixes *iradix.Tree
15-
allowedIPs map[string]interface{}
15+
allowedIPs map[string]any
1616
logger io.Writer
1717
format string
1818
ipCryptConfig *IPCryptConfig
@@ -22,7 +22,7 @@ type PluginAllowedIP struct {
2222
configFile string
2323
configWatcher *ConfigWatcher
2424
stagingPrefixes *iradix.Tree
25-
stagingIPs map[string]interface{}
25+
stagingIPs map[string]any
2626
}
2727

2828
func (plugin *PluginAllowedIP) Name() string {
@@ -43,7 +43,7 @@ func (plugin *PluginAllowedIP) Init(proxy *Proxy) error {
4343
}
4444

4545
plugin.allowedPrefixes = iradix.New()
46-
plugin.allowedIPs = make(map[string]interface{})
46+
plugin.allowedIPs = make(map[string]any)
4747

4848
plugin.allowedPrefixes, err = plugin.loadRules(lines, plugin.allowedPrefixes, plugin.allowedIPs)
4949
if err != nil {
@@ -57,7 +57,7 @@ func (plugin *PluginAllowedIP) Init(proxy *Proxy) error {
5757
}
5858

5959
// 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]interface{}) (*iradix.Tree, error) {
60+
func (plugin *PluginAllowedIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]any) (*iradix.Tree, error) {
6161
return LoadIPRules(lines, prefixes, ips)
6262
}
6363

@@ -73,7 +73,7 @@ func (plugin *PluginAllowedIP) PrepareReload() error {
7373
return StandardPrepareReloadPattern(plugin.Name(), plugin.configFile, func(lines string) error {
7474
// Create staging structures
7575
plugin.stagingPrefixes = iradix.New()
76-
plugin.stagingIPs = make(map[string]interface{})
76+
plugin.stagingIPs = make(map[string]any)
7777

7878
// Load rules into staging structures
7979
var err error

dnscrypt-proxy/plugin_block_ip.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
type PluginBlockIP struct {
1414
blockedPrefixes *iradix.Tree
15-
blockedIPs map[string]interface{}
15+
blockedIPs map[string]any
1616
logger io.Writer
1717
format string
1818
ipCryptConfig *IPCryptConfig
@@ -22,7 +22,7 @@ type PluginBlockIP struct {
2222
configFile string
2323
configWatcher *ConfigWatcher
2424
stagingPrefixes *iradix.Tree
25-
stagingIPs map[string]interface{}
25+
stagingIPs map[string]any
2626
}
2727

2828
func (plugin *PluginBlockIP) Name() string {
@@ -43,7 +43,7 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error {
4343
}
4444

4545
plugin.blockedPrefixes = iradix.New()
46-
plugin.blockedIPs = make(map[string]interface{})
46+
plugin.blockedIPs = make(map[string]any)
4747

4848
plugin.blockedPrefixes, err = plugin.loadRules(lines, plugin.blockedPrefixes, plugin.blockedIPs)
4949
if err != nil {
@@ -57,7 +57,7 @@ func (plugin *PluginBlockIP) Init(proxy *Proxy) error {
5757
}
5858

5959
// loadRules parses and loads IP rules into the provided tree and map
60-
func (plugin *PluginBlockIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]interface{}) (*iradix.Tree, error) {
60+
func (plugin *PluginBlockIP) loadRules(lines string, prefixes *iradix.Tree, ips map[string]any) (*iradix.Tree, error) {
6161
return LoadIPRules(lines, prefixes, ips)
6262
}
6363

@@ -73,7 +73,7 @@ func (plugin *PluginBlockIP) PrepareReload() error {
7373
return StandardPrepareReloadPattern(plugin.Name(), plugin.configFile, func(lines string) error {
7474
// Create staging structures
7575
plugin.stagingPrefixes = iradix.New()
76-
plugin.stagingIPs = make(map[string]interface{})
76+
plugin.stagingIPs = make(map[string]any)
7777

7878
// Load rules into staging structures
7979
var err error

0 commit comments

Comments
 (0)