Repository navigation
Expand file tree
/
Copy pathinit_upstreams.go
More file actions
197 lines (171 loc) · 5.84 KB
/
Copy pathinit_upstreams.go
File metadata and controls
197 lines (171 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
File: init_upstreams.go
Version: 1.9.0
Last Updated: 10-Nov-2026 11:45 CET
Description:
Parses upstream definitions and builds group strategies for sdproxy.
Handles bootstrap resolution and ECH/ALPN discovery for outbound encrypted DNS.
Changes:
1.9.0 - [FEAT] Propagated `DoH3FallbackThreshold` (inheriting from
`cfg.Server.DoH3FallbackThreshold` when nil on the group) into each
`Upstream` and `UpstreamGroup`.
1.8.0 - [SECURITY/FEAT] Propagated `DNS0x20`, `DNSCookies`, and `RequireDNSSECAD`
(inheriting from `cfg.Server` defaults when nil on the group) into each
`Upstream` and `UpstreamGroup`.
1.7.0 - [PERF/SECURITY] Bounded concurrent upstream bootstrap and DDR/ECH discovery
goroutines in `initUpstreams` with a 16-slot semaphore (`upstreamInitSem`)
to prevent socket/goroutine exhaustion on configurations with large numbers
of upstream endpoints.
1.6.0 - [FEAT] Propagated `cfg.Server.MaxSecureUpstreams` into each constructed
`UpstreamGroup`.
1.5.0 - [CLEANUP] Pruned obsolete `hasClientNameUpstream` variable assignment securely.
1.4.0 - [PERF] Parallelized upstream parsing and DDR execution natively using `sync.WaitGroup`.
1.3.0 - [SECURITY/FIX] Dynamically asserted `HasClientName` bounds natively
during Upstream Group instantiation.
*/
package main
import (
"log"
"net"
"strings"
"sync"
)
const maxConcurrentUpstreamInit = 16
// initUpstreams constructs connection pools and selection strategies for outbound DNS.
func initUpstreams() {
routeUpstreams = make(map[string]*UpstreamGroup, len(cfg.Upstreams))
globalStrategy := cfg.Server.UpstreamSelection
if globalStrategy == "" {
globalStrategy = "stagger"
}
maxSecureUpstreams := cfg.Server.MaxSecureUpstreams
if maxSecureUpstreams <= 0 {
maxSecureUpstreams = 5
}
upstreamInitSem := make(chan struct{}, maxConcurrentUpstreamInit)
var wg sync.WaitGroup
var mu sync.Mutex
for groupName, ugc := range cfg.Upstreams {
wg.Add(1)
go func(gName string, gConf UpstreamGroupConfig) {
defer wg.Done()
var serverWg sync.WaitGroup
parsedServers := make([]*Upstream, len(gConf.Servers))
grpECSAction := strings.ToLower(gConf.ECS.Action)
grpECSV4 := gConf.ECS.IPv4Mask
grpECSV6 := gConf.ECS.IPv6Mask
if grpECSAction == "" {
grpECSAction = cfg.Server.ECS.Action
}
if grpECSV4 <= 0 || grpECSV4 > 32 {
grpECSV4 = cfg.Server.ECS.IPv4Mask
}
if grpECSV6 <= 0 || grpECSV6 > 128 {
grpECSV6 = cfg.Server.ECS.IPv6Mask
}
grpDNS0x20 := cfg.Server.DNS0x20
if gConf.DNS0x20 != nil {
grpDNS0x20 = *gConf.DNS0x20
}
grpDNSCookies := cfg.Server.DNSCookies
if gConf.DNSCookies != nil {
grpDNSCookies = *gConf.DNSCookies
}
grpRequireAD := cfg.Server.RequireDNSSECAD
if gConf.RequireDNSSECAD != nil {
grpRequireAD = *gConf.RequireDNSSECAD
}
grpDoH3Thresh := cfg.Server.DoH3FallbackThreshold
if gConf.DoH3FallbackThreshold != nil {
grpDoH3Thresh = *gConf.DoH3FallbackThreshold
}
if grpDoH3Thresh < 0 {
grpDoH3Thresh = 0
}
for i, rawURL := range gConf.Servers {
serverWg.Add(1)
go func(idx int, url string) {
defer serverWg.Done()
upstreamInitSem <- struct{}{}
defer func() { <-upstreamInitSem }()
u, err := ParseUpstream(url, globalBootstrapServers)
if err != nil {
if logSystem {
log.Printf("[WARN] Failed to parse upstream %q in group %q: %v", url, gName, err)
}
return
}
u.ECSAction, u.ECSV4Mask, u.ECSV6Mask = grpECSAction, grpECSV4, grpECSV6
u.DNS0x20 = grpDNS0x20
u.DNSCookies = grpDNSCookies
u.RequireDNSSECAD = grpRequireAD
u.DoH3FallbackThreshold = int32(grpDoH3Thresh)
parsedServers[idx] = u
}(i, rawURL)
}
serverWg.Wait()
var ups []*Upstream
uniqueEndpoints := make(map[string]struct{})
hasGrpClientName := false
for i, u := range parsedServers {
if u == nil {
continue
}
ups = append(ups, u)
rawURL := gConf.Servers[i]
if strings.Contains(rawURL, "{client-name}") {
hasGrpClientName = true
}
for _, ip := range u.BootstrapIPs {
uniqueEndpoints[ip] = struct{}{}
}
for _, dialAddr := range u.dialAddrs {
if host, _, err := net.SplitHostPort(dialAddr); err == nil {
uniqueEndpoints[host] = struct{}{}
} else {
uniqueEndpoints[dialAddr] = struct{}{}
}
}
}
strategy := strings.ToLower(gConf.Strategy)
if strategy == "" {
strategy = globalStrategy
}
if len(uniqueEndpoints) <= 1 && strategy != "stagger" {
strategy = "stagger"
}
pref := strings.ToLower(gConf.Preference)
if pref == "" {
pref = "fastest"
}
mode := strings.ToLower(gConf.Mode)
if mode == "" {
mode = "loose"
}
mu.Lock()
routeUpstreams[gName] = &UpstreamGroup{
Name: gName,
Strategy: strategy,
Preference: pref,
Mode: mode,
IgnoreQnameLabels: gConf.IgnoreQnameLabels,
DNS0x20: grpDNS0x20,
DNSCookies: grpDNSCookies,
RequireDNSSECAD: grpRequireAD,
DoH3FallbackThreshold: grpDoH3Thresh,
ECSAction: grpECSAction,
ECSV4Mask: grpECSV4,
ECSV6Mask: grpECSV6,
HasClientName: hasGrpClientName,
MaxSecureUpstreams: maxSecureUpstreams,
Servers: ups,
}
if logSystem {
log.Printf("[INIT] Upstream group %q: %d server(s), strategy: %s (pref: %s, mode: %s, ignore_labels: %v, ecs: %s, isolated_cache: %v, max_secure_upstreams: %d, 0x20: %v, cookies: %v, require_ad: %v, doh3_fallback_threshold: %d)",
gName, len(ups), strategy, pref, mode, gConf.IgnoreQnameLabels, grpECSAction, hasGrpClientName, maxSecureUpstreams, grpDNS0x20, grpDNSCookies, grpRequireAD, grpDoH3Thresh)
}
mu.Unlock()
}(groupName, ugc)
}
wg.Wait()
}