-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupstream_parser.go
More file actions
512 lines (464 loc) · 17.3 KB
/
Copy pathupstream_parser.go
File metadata and controls
512 lines (464 loc) · 17.3 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
File: upstream_parser.go
Version: 2.47.0
Updated: 07-Jul-2026 16:59 CEST
Description:
Configuration parsing, initialization, and client allocation for sdproxy upstreams.
Extracted from upstream.go to cleanly separate boot-time instantiation from
the hot-path routing execution pipeline.
Changes:
2.47.0 - [FEAT] Implemented robust extraction bounds for the newly deployed
`?echdomain=` upstream configuration parameter. Organically triggers
isolated DDR queries for ECH keys explicitly traversing customized domain bounds.
2.46.0 - [SECURITY/FIX] Eradicated a Bootstrap Resolution Bypass vulnerability.
Enforced strict `netip.ParseAddr()` structural validation on ALL parsed
bootstrap components within the `#ip1,ip2` configuration blocks. Prevents
misconfigured hostnames from bypassing the isolated bootstrap constraints
and invoking the host OS resolver, which previously triggered severe
startup deadlocks on constrained networks natively.
2.45.0 - [SECURITY/FIX] Addressed a severe initialization SIGSEGV. `ParseUpstream`
now dynamically receives `bootstrapNodes` as an argument natively, completely
decoupling it from the `globalBootstrapServers` array. Prevents concurrent
data races and infinite loops when bootstrap servers execute DDR discoveries.
2.44.0 - [SECURITY/FIX] Resolved manual ECH base64 decoding failure for standard
unpadded payloads by integrating base64.RawStdEncoding into the fallback chain.
Prevents silent startup decoding failures, fallback bootstrap lookups, and
unencrypted connection leakage on strict networks.
*/
package main
import (
"context"
"crypto/tls"
"encoding/base64"
"fmt"
"log"
"net"
"net/http"
"net/netip"
"strings"
"time"
"github.com/miekg/dns"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
)
// ---------------------------------------------------------------------------
// Initialization & Parsing
// ---------------------------------------------------------------------------
// stripMethodModifier checks whether a DoH/DoH3 URL path (before any # fragment)
// ends with +get or +post (case-insensitive). Returns the stripped URL and whether
// GET was requested. Default (no modifier) → POST (false).
func stripMethodModifier(url string) (stripped string, useGET bool) {
lower := strings.ToLower(url)
switch {
// [SECURITY/FIX] Validate suffix rather than prefix to securely evaluate
// query directives appended AFTER the schema and FQDN.
case strings.HasSuffix(lower, "+get"):
return url[:len(url)-4], true
case strings.HasSuffix(lower, "+post"):
return url[:len(url)-5], false
default:
return url, false
}
}
// stripDefaultHTTPSPort removes the redundant :443 port from the host part of an HTTPS URL.
// In HTTP/2 and HTTP/3, sending the default port in the :authority pseudo-header
// can cause strict servers to reject the request stream natively.
func stripDefaultHTTPSPort(rawURL string) string {
if !strings.HasPrefix(rawURL, "https://") {
return rawURL
}
pathIdx := strings.IndexByte(rawURL[8:], '/')
if pathIdx >= 0 {
hostPart := rawURL[8 : 8+pathIdx]
if strings.HasSuffix(hostPart, ":443") {
hostPart = strings.TrimSuffix(hostPart, ":443")
return "https://" + hostPart + rawURL[8+pathIdx:]
}
} else {
if strings.HasSuffix(rawURL, ":443") {
return strings.TrimSuffix(rawURL, ":443")
}
}
return rawURL
}
// ParseUpstream parses a raw upstream URL string into an initialised Upstream.
func ParseUpstream(raw string, bootstrapNodes []*Upstream) (*Upstream, error) {
u := &Upstream{}
parts := strings.Split(raw, "#")
urlPart := parts[0]
// Extract manual Encrypted Client Hello (ECH) parameters from the URL
var echBase64, echDomain string
// [FEAT] Safely extract the optional ?echdomain= target bounds
// Check ?echdomain= first to avoid string overlap with ?ech= mappings organically.
if idx := strings.Index(urlPart, "echdomain="); idx >= 0 {
end := strings.IndexAny(urlPart[idx:], "&")
if end < 0 {
echDomain = urlPart[idx+10:]
urlPart = urlPart[:idx]
} else {
echDomain = urlPart[idx+10 : idx+end]
urlPart = urlPart[:idx] + urlPart[idx+end+1:]
}
urlPart = strings.TrimRight(urlPart, "?&")
}
// Extract standard ?ech= base64 payloads
if idx := strings.Index(urlPart, "ech="); idx >= 0 {
end := strings.IndexAny(urlPart[idx:], "&")
if end < 0 {
echBase64 = urlPart[idx+4:]
urlPart = urlPart[:idx]
} else {
echBase64 = urlPart[idx+4 : idx+end]
urlPart = urlPart[:idx] + urlPart[idx+end+1:]
}
urlPart = strings.TrimRight(urlPart, "?&")
}
u.echDomain = echDomain
useECH := cfg.Server.UseUpstreamECH
if useECH == "" {
useECH = "disable"
}
if echBase64 != "" {
if useECH == "disable" {
if logTLS {
log.Printf("[TLS] [ECH] Upstream %s: ECH is explicitly disabled globally ('use_upstream_ech: disable'), ignoring manual URL config", urlPart)
}
} else {
// [FIX] Implement multi-encoding checks to seamlessly parse both padded and
// unpadded ECH payloads natively, ensuring broad upstream compatibility.
decoded, err := base64.RawURLEncoding.DecodeString(echBase64)
if err != nil {
decoded, err = base64.URLEncoding.DecodeString(echBase64)
if err != nil {
decoded, err = base64.StdEncoding.DecodeString(echBase64)
if err != nil {
decoded, err = base64.RawStdEncoding.DecodeString(echBase64)
}
}
}
if err == nil {
u.ECHConfigList = decoded
if logTLS {
log.Printf("[TLS] [ECH] Upstream %s: Loaded manual ECH config via URL parameter (%d bytes)", urlPart, len(decoded))
}
} else {
if logTLS {
log.Printf("[TLS] [ECH] Upstream %s: WARNING - Invalid manual ECH base64 string: %v", urlPart, err)
}
}
}
}
if len(parts) > 1 && parts[1] != "" {
rawIPs := strings.Split(parts[1], ",")
// [SECURITY/FIX] IP Version Support Enforcement and strict Boundary Validation.
// Exclusively accept mathematically verified IP payloads natively to thwart
// URL misconfigurations from invoking un-sandboxed OS host resolution sequences.
for _, rip := range rawIPs {
rip = strings.TrimSpace(rip)
if addr, err := netip.ParseAddr(rip); err == nil {
if ipVersionSupport == "ipv4" && !addr.Is4() { continue }
if ipVersionSupport == "ipv6" && !addr.Is6() { continue }
u.BootstrapIPs = append(u.BootstrapIPs, rip)
} else {
if logSystem {
log.Printf("[WARN] Invalid bootstrap IP %q ignored for upstream %s", rip, urlPart)
}
}
}
}
u.hasClientNameTemplate = strings.Contains(urlPart, "{client-name}")
switch {
case strings.HasPrefix(urlPart, "udp://"):
u.Proto = "udp"
u.RawURL = strings.TrimPrefix(urlPart, "udp://")
u.udpClient = &dns.Client{Net: "udp", Timeout: 3 * time.Second}
case strings.HasPrefix(urlPart, "tcp://"):
u.Proto = "tcp"
u.RawURL = strings.TrimPrefix(urlPart, "tcp://")
u.streamConns = make(map[string][]*streamConnEntry)
case strings.HasPrefix(urlPart, "dot://"), strings.HasPrefix(urlPart, "tls://"):
u.Proto = "dot"
u.RawURL = strings.TrimPrefix(strings.TrimPrefix(urlPart, "dot://"), "tls://")
u.baseTLSConf = getHardenedTLSConfig()
u.baseTLSConf.NextProtos = []string{"dot"}
u.streamConns = make(map[string][]*streamConnEntry)
case strings.HasPrefix(urlPart, "doh://"), strings.HasPrefix(urlPart, "https://"):
u.Proto = "doh"
urlPart, u.useGET = stripMethodModifier(urlPart)
u.RawURL = "https://" + strings.TrimPrefix(strings.TrimPrefix(urlPart, "doh://"), "https://")
u.RawURL = stripDefaultHTTPSPort(u.RawURL) // [SECURITY/FIX] Normalize :authority
dialer := &net.Dialer{
Timeout: 3 * time.Second,
KeepAlive: 30 * time.Second,
}
// [PERF/FIX] Removed rigid 5-second `Timeout` definitions across HTTP clients.
// Execution deadlines are now natively supervised by the dynamic `context.Context`
// passed down during active queries, faithfully enforcing `upstream_timeout_ms`
// limits without artificial severances.
u.h2Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: getHardenedTLSConfig(),
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 5 * time.Second,
TLSHandshakeTimeout: 3 * time.Second,
ResponseHeaderTimeout: 3 * time.Second,
DisableCompression: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
if len(u.BootstrapIPs) > 0 {
_, port, _ := net.SplitHostPort(addr)
for _, ip := range u.BootstrapIPs {
conn, err := dialer.DialContext(ctx, network, net.JoinHostPort(ip, port))
if err == nil {
return conn, nil
}
}
}
return dialer.DialContext(ctx, network, addr)
},
},
}
if cfg.Server.UpgradeDoH3 {
u.h3Client = &http.Client{
Transport: &http3.Transport{
TLSClientConfig: getHardenedTLSConfig(),
DisableCompression: true,
QUICConfig: &quic.Config{
HandshakeIdleTimeout: 3 * time.Second,
MaxIdleTimeout: 30 * time.Second,
KeepAlivePeriod: 15 * time.Second,
},
Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, qCfg *quic.Config) (*quic.Conn, error) {
if len(u.BootstrapIPs) > 0 {
_, port, _ := net.SplitHostPort(addr)
for _, ip := range u.BootstrapIPs {
conn, err := quic.DialAddrEarly(ctx, net.JoinHostPort(ip, port), tlsCfg, qCfg)
if err == nil {
return conn, nil
}
}
}
return quic.DialAddrEarly(ctx, addr, tlsCfg, qCfg)
},
},
}
}
case strings.HasPrefix(urlPart, "doh3://"):
u.Proto = "doh3"
urlPart, u.useGET = stripMethodModifier(urlPart)
u.RawURL = "https://" + strings.TrimPrefix(urlPart, "doh3://")
u.RawURL = stripDefaultHTTPSPort(u.RawURL) // [SECURITY/FIX] Normalize :authority
// [PERF/FIX] Removed rigid 5-second `Timeout` definitions.
// Context natively enforces boundaries based on the unified pipeline thresholds.
u.h3Client = &http.Client{
Transport: &http3.Transport{
TLSClientConfig: getHardenedTLSConfig(),
DisableCompression: true,
QUICConfig: &quic.Config{
HandshakeIdleTimeout: 3 * time.Second,
MaxIdleTimeout: 30 * time.Second,
KeepAlivePeriod: 15 * time.Second,
},
Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, qCfg *quic.Config) (*quic.Conn, error) {
if len(u.BootstrapIPs) > 0 {
_, port, _ := net.SplitHostPort(addr)
for _, ip := range u.BootstrapIPs {
conn, err := quic.DialAddrEarly(ctx, net.JoinHostPort(ip, port), tlsCfg, qCfg)
if err == nil {
return conn, nil
}
}
}
return quic.DialAddrEarly(ctx, addr, tlsCfg, qCfg)
},
},
}
case strings.HasPrefix(urlPart, "doq://"):
u.Proto = "doq"
u.RawURL = strings.TrimPrefix(urlPart, "doq://")
u.doqConns = make(map[string]*doqConnEntry)
u.baseTLSConf = getHardenedTLSConfig()
u.baseTLSConf.NextProtos = []string{"doq"}
default:
return nil, fmt.Errorf("unsupported upstream scheme in %q", raw)
}
// Resolve dialHost / dialAddrs for address-based protocols.
switch u.Proto {
case "udp", "tcp", "dot", "doq":
host, port := parseHostPort(u.RawURL, u.Proto)
if net.ParseIP(host) == nil {
u.dialHost = host
}
if addr, err := netip.ParseAddr(host); err == nil {
if ipVersionSupport == "ipv4" && !addr.Is4() {
return nil, fmt.Errorf("upstream %s is IPv6 but support_ip_version is 'ipv4'", host)
}
if ipVersionSupport == "ipv6" && !addr.Is6() {
return nil, fmt.Errorf("upstream %s is IPv4 but support_ip_version is 'ipv6'", host)
}
}
u.dialAddrs = []string{net.JoinHostPort(host, port)}
}
// Evaluate the core hostname for DNS bootstrapping (IPs and ECH configurations)
var resolveHost string
switch u.Proto {
case "udp", "tcp", "dot", "doq":
if net.ParseIP(u.dialHost) == nil {
resolveHost = strings.ReplaceAll(u.dialHost, "{client-name}", "bootstrap")
}
case "doh", "doh3":
rest := strings.TrimPrefix(u.RawURL, "https://")
if idx := strings.IndexByte(rest, '/'); idx >= 0 {
rest = rest[:idx]
}
if h, _, err := net.SplitHostPort(rest); err == nil {
rest = h
}
if net.ParseIP(rest) == nil {
resolveHost = strings.ReplaceAll(rest, "{client-name}", "bootstrap")
}
}
var hintedIPs []string
var nativeH3 bool
needsDDR := false
if (useECH == "try" || useECH == "strict") && len(u.ECHConfigList) == 0 {
needsDDR = true
}
if u.Proto == "doh" && cfg.Server.UpgradeDoH3 && !u.h3Upgraded.Load() {
needsDDR = true
}
// Bootstrap DDR Params implicitly if not manually defined by the user
if needsDDR && (u.Proto == "dot" || u.Proto == "doh" || u.Proto == "doh3" || u.Proto == "doq") {
if logDDR {
log.Printf("[DDR] Upstream %s: Attempting discovery for ECH, IP hints, and ALPN...", resolveHost)
}
if resolveHost != "" && len(bootstrapNodes) > 0 {
echConfig, hIPs, h3Supported := bootstrapResolveECH(resolveHost, u.echDomain, bootstrapNodes)
if len(echConfig) > 0 && len(u.ECHConfigList) == 0 && useECH != "disable" {
u.ECHConfigList = echConfig
if logDDR {
log.Printf("[DDR] Upstream %s: Extracted ECH config (%d bytes) via native UDP HTTPS record", resolveHost, len(echConfig))
}
}
if len(u.BootstrapIPs) == 0 && len(hIPs) > 0 {
hintedIPs = hIPs
if logDDR {
log.Printf("[DDR] Upstream %s: Discovered %d IP hint(s) via HTTPS/SVCB record: %v", resolveHost, len(hIPs), hIPs)
}
}
if h3Supported {
nativeH3 = true
}
}
}
if len(u.BootstrapIPs) > 0 {
switch u.Proto {
case "udp", "tcp", "dot", "doq":
_, port := parseHostPort(u.RawURL, u.Proto)
u.dialAddrs = make([]string, len(u.BootstrapIPs))
for i, ip := range u.BootstrapIPs {
u.dialAddrs[i] = net.JoinHostPort(ip, port)
}
}
} else if resolveHost != "" {
var ips []string
if len(hintedIPs) > 0 {
ips = hintedIPs
} else if len(bootstrapNodes) > 0 {
ips = bootstrapResolve(resolveHost, bootstrapNodes)
}
if len(ips) > 0 {
u.BootstrapIPs = ips
if len(hintedIPs) == 0 {
if logSystem {
log.Printf("[INIT] Bootstrap: %s -> %v", resolveHost, ips)
}
}
if u.dialHost != "" {
_, port := parseHostPort(u.RawURL, u.Proto)
u.dialAddrs = make([]string, len(ips))
for i, ip := range ips {
u.dialAddrs[i] = net.JoinHostPort(ip, port)
}
}
} else {
if u.dialHost != "" && strings.Contains(u.dialHost, "{client-name}") {
_, port := parseHostPort(u.RawURL, u.Proto)
u.dialAddrs = []string{net.JoinHostPort(resolveHost, port)}
}
if logSystem {
log.Printf("[WARN] Bootstrap: could not resolve %s — OS resolver will be used at dial time", resolveHost)
}
}
}
if needsDDR && (u.Proto == "dot" || u.Proto == "doh" || u.Proto == "doh3" || u.Proto == "doq") {
if resolveHost != "" && (len(u.dialAddrs) > 0 || len(u.BootstrapIPs) > 0) {
echBytes, h3Supported := fetchDDRParams(u, resolveHost, u.echDomain)
if len(echBytes) > 0 && len(u.ECHConfigList) == 0 && useECH != "disable" {
u.ECHConfigList = echBytes
if logDDR {
log.Printf("[DDR] Upstream %s: Successfully secured ECH config (%d bytes)", resolveHost, len(echBytes))
}
}
if h3Supported {
nativeH3 = true
}
}
}
// Proactive DoH3 Upgrade based on SVCB ALPN discovery
if u.Proto == "doh" && cfg.Server.UpgradeDoH3 && nativeH3 && !u.h3Upgraded.Load() {
u.h3Upgraded.Store(true)
if logTLS {
log.Printf("[TLS] [UPGRADE] Upstream %s: Proactively upgraded DoH to DoH3 (QUIC) based on SVCB/HTTPS ALPN discovery natively.", resolveHost)
}
}
// STRICT Mode Check
if useECH == "strict" && len(u.ECHConfigList) == 0 && (u.Proto == "dot" || u.Proto == "doh" || u.Proto == "doh3" || u.Proto == "doq") {
return nil, fmt.Errorf("upstream %s: strict ECH enforced but no ECH config could be discovered", u.RawURL)
}
// Inject the finalized ECH settings into the active Transport payloads
if len(u.ECHConfigList) > 0 {
if logTLS {
log.Printf("[TLS] [ECH] Upstream %s: TLS Transport strictly enforcing Encrypted Client Hello (Requires TLS 1.3+)", resolveHost)
}
if useECH == "try" {
if u.baseTLSConf != nil {
u.baseTLSConfNoECH = u.baseTLSConf.Clone()
}
if u.h2Client != nil {
tr := u.h2Client.Transport.(*http.Transport).Clone()
u.h2ClientNoECH = &http.Client{Transport: tr} // Respect context deadlines securely
}
if u.h3Client != nil {
origTr := u.h3Client.Transport.(*http3.Transport)
qCfg := *origTr.QUICConfig
u.h3ClientNoECH = &http.Client{
Transport: &http3.Transport{
TLSClientConfig: origTr.TLSClientConfig.Clone(),
DisableCompression: origTr.DisableCompression,
QUICConfig: &qCfg,
Dial: origTr.Dial,
},
}
}
}
if u.baseTLSConf != nil {
u.baseTLSConf.EncryptedClientHelloConfigList = u.ECHConfigList
u.baseTLSConf.MinVersion = tls.VersionTLS13
}
if u.h2Client != nil {
if tr, ok := u.h2Client.Transport.(*http.Transport); ok {
tr.TLSClientConfig.EncryptedClientHelloConfigList = u.ECHConfigList
tr.TLSClientConfig.MinVersion = tls.VersionTLS13
}
}
if u.h3Client != nil {
if tr, ok := u.h3Client.Transport.(*http3.Transport); ok {
tr.TLSClientConfig.EncryptedClientHelloConfigList = u.ECHConfigList
tr.TLSClientConfig.MinVersion = tls.VersionTLS13
}
}
}
return u, nil
}