Repository navigation
Expand file tree
/
Copy pathserver_init.go
More file actions
511 lines (443 loc) · 14.6 KB
/
Copy pathserver_init.go
File metadata and controls
511 lines (443 loc) · 14.6 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
/*
File: server_init.go
Version: 1.22.0 (Split)
Last Updated: 17-Oct-2026 19:54 CEST
Description:
Listener initialization and OS binding orchestration.
Extracted from server.go to cleanly separate the setup of network listeners
(UDP, TCP, TLS, DoH, DoQ) from the runtime protocol payload handlers.
Changes:
1.22.0 - [SECURITY/FIX] Bound graceful shutdown hooks (`shutdownCh`) to TCP, DoT,
DoH (HTTP/2), and DoH3 (HTTP/3) listeners so active sockets close cleanly
upon `SIGTERM`/`SIGINT`.
- [SECURITY/FIX] Moved `activeDoQConns` outside the `superviseListener` closure
so a DoQ listener restart does not reset the connection counter while
existing connections are still draining.
- [SECURITY/FIX] Cloned `tlsConf` for `http3.Server` and explicitly set
`NextProtos = []string{"h3"}` to avoid mutating the shared `tls.Config`
across concurrent listeners.
1.21.0 - [SECURITY/FIX] Injected `http.LocalAddrContextKey` via `ConnContext` on
`http3.Server` (DoH3). Unlike standard `net/http.Server`, `quic-go/http3`
does not populate `http.LocalAddrContextKey` by default, which caused
`r.Context().Value(http.LocalAddrContextKey)` in `handleDoH` to return nil
and broke `port:` routing and DDR local-IP fallback on DoH3 requests.
1.20.0 - [BUG/FIX] Correctly populated the required third return parameter natively within
the DoQ/HTTP3 QUIC Connection execution loop (`return conn, nil, nil`).
Solves severe compilation errors when utilizing updated versions of `quic-go`.
1.19.0 - [SECURITY/FIX] Retuned QUIC stream ceilings and wired the global DoQ
stream budget (see server.go 1.37.0 for the handler-side bounds).
- [SECURITY/FIX] Removed log.Fatalf from every listener SERVE loop.
1.18.0 - [FEAT] Registered `/.well-known/origin-svcb` L7 discovery endpoint natively
for DoH and DoH3 listeners to comply with `draft-ietf-tls-wkech-11`.
Seamlessly generates compliant JSON payloads mapping active ECH configurations organically.
1.17.0 - [FEAT] Registered `/.well-known/ech` L7 discovery endpoint natively
for DoH and DoH3 encrypted listeners. Allows clients to bypass DNS
tampering and fetch the router's `ECHConfigList` directly over HTTPS.
1.16.0 - [SECURITY/FIX] Resolved a severe concurrency discrepancy dynamically.
Shifted the atomic `activeDoQConns` counter inherently inside the
Listener iteration loop natively.
1.15.0 - [BUG/FIX] Resolved a compilation error natively within the DoQ listener loop.
1.14.0 - [SECURITY/FIX] Addressed a severe Resource Exhaustion (DoS) vulnerability
within the DoQ (DNS-over-QUIC) listener loop natively.
*/
package main
import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/netip"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/miekg/dns"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"golang.org/x/net/netutil"
)
var (
shutdownCh = make(chan struct{})
shutdownOnce sync.Once
)
// Shutdown cleanly signals all active listeners and pools to terminate.
func Shutdown() {
shutdownOnce.Do(func() {
close(shutdownCh)
})
}
// ---------------------------------------------------------------------------
// Listener supervision
// ---------------------------------------------------------------------------
// isShuttingDown reports whether Shutdown() has been called.
//
// Non-blocking, so it is safe to poll from a serve loop without perturbing it.
func isShuttingDown() bool {
select {
case <-shutdownCh:
return true
default:
return false
}
}
// listenerBackoff returns the delay before restart attempt n (1-based).
func listenerBackoff(n int) time.Duration {
if n < 1 {
n = 1
}
shift := n - 1
if shift > 5 {
shift = 5 // 2^5 = 32s, clamped to 30s below
}
d := time.Duration(1<<uint(shift)) * time.Second
if d > 30*time.Second {
d = 30 * time.Second
}
return d
}
// superviseListener owns the full lifecycle of one protocol listener on one
// address: it runs serve, and if serve returns unexpectedly it rebinds and
// retries with backoff instead of killing the process.
func superviseListener(proto, addr string, serve func(attempt int) error) {
const maxRestarts = 10
for attempt := 0; ; attempt++ {
err := serve(attempt)
// Orderly teardown — Shutdown() closed the socket underneath us.
if isShuttingDown() {
if logSystem {
log.Printf("[LISTEN] %s listener on %s stopped (shutdown).", proto, addr)
}
return
}
// A clean, expected close that did not originate from Shutdown(). Nothing
// to recover from and nothing to complain about.
if err == nil || errors.Is(err, http.ErrServerClosed) || errors.Is(err, net.ErrClosed) {
if logSystem {
log.Printf("[LISTEN] %s listener on %s closed.", proto, addr)
}
return
}
if attempt >= maxRestarts {
log.Printf("[LISTEN] CRITICAL: %s listener on %s failed %d consecutive times and has been retired: %v. "+
"Remaining transports continue serving. Restart sdproxy once the underlying fault is resolved.",
proto, addr, attempt+1, err)
return
}
delay := listenerBackoff(attempt + 1)
log.Printf("[LISTEN] WARNING: %s listener on %s terminated: %v. Restarting in %s (attempt %d/%d).",
proto, addr, err, delay, attempt+1, maxRestarts)
select {
case <-time.After(delay):
case <-shutdownCh:
return
}
}
}
// filterListeners returns a subset of addresses that match the global support_ip_version.
func filterListeners(addrs []string) []string {
if ipVersionSupport == "both" {
return addrs
}
var out []string
for _, a := range addrs {
host, _, err := net.SplitHostPort(a)
if err != nil {
host = a
}
if addr, err := netip.ParseAddr(host); err == nil {
if ipVersionSupport == "ipv4" && !addr.Is4() {
if logSystem {
log.Printf("[LISTEN] Skipping %s: support_ip_version is 'ipv4' only", a)
}
continue
}
if ipVersionSupport == "ipv6" && !addr.Is6() {
if logSystem {
log.Printf("[LISTEN] Skipping %s: support_ip_version is 'ipv6' only", a)
}
continue
}
}
out = append(out, a)
}
return out
}
// StartServers spins up all configured DNS protocol listeners independently.
func StartServers(tlsConf *tls.Config) {
maxTCP := cfg.Server.MaxTCPConnections
if maxTCP <= 0 {
maxTCP = 250 // Conservative default for tiny routers
}
initDoQStreamBudget(maxTCP)
tcpReadTimeout := 5 * time.Second
var tcpWriteTimeout time.Duration
if cfg.Server.UpstreamTimeoutMs > 0 {
tcpWriteTimeout = time.Duration(cfg.Server.UpstreamTimeoutMs)*time.Millisecond + (2 * time.Second)
} else {
tcpWriteTimeout = 30 * time.Second // Robust default for unbounded upstream dials
}
tcpIdleTimeout := 15 * time.Second
// 1. Classic UDP (Dispatches to Linux SO_REUSEPORT or fallback workers)
startUDPServers(filterListeners(cfg.Server.ListenUDP), cfg.Server.UDPWorkers)
// 2. DNS over TCP
for _, addr := range filterListeners(cfg.Server.ListenTCP) {
addr := addr
go superviseListener("TCP", addr, func(attempt int) error {
l, err := net.Listen("tcp", addr)
if err != nil {
if attempt == 0 {
log.Fatalf("[FATAL] TCP listen failed on %s: %v", addr, err)
}
return fmt.Errorf("rebind: %w", err)
}
l = netutil.LimitListener(l, maxTCP)
server := &dns.Server{
Listener: l,
Handler: dns.HandlerFunc(handleTCP),
ReadTimeout: tcpReadTimeout,
WriteTimeout: tcpWriteTimeout,
}
if logSystem {
log.Printf("[LISTEN] TCP on %s (Max Conns: %d)", addr, maxTCP)
}
served := make(chan struct{})
defer close(served)
go func() {
select {
case <-shutdownCh:
_ = server.Shutdown()
case <-served:
}
}()
return server.ActivateAndServe()
})
}
// 3. DNS over TLS (DoT)
for _, addr := range filterListeners(cfg.Server.ListenDoT) {
addr := addr
go superviseListener("DoT", addr, func(attempt int) error {
l, err := net.Listen("tcp", addr)
if err != nil {
if attempt == 0 {
log.Fatalf("[FATAL] DoT listen failed on %s: %v", addr, err)
}
return fmt.Errorf("rebind: %w", err)
}
l = netutil.LimitListener(l, maxTCP)
dotTLS := tlsConf.Clone()
dotTLS.NextProtos = []string{"dot"}
tlsL := tls.NewListener(l, dotTLS)
server := &dns.Server{
Listener: tlsL,
Handler: dns.HandlerFunc(handleDoT),
ReadTimeout: tcpReadTimeout,
WriteTimeout: tcpWriteTimeout,
}
if logSystem {
log.Printf("[LISTEN] DoT on %s (Max Conns: %d)", addr, maxTCP)
}
served := make(chan struct{})
defer close(served)
go func() {
select {
case <-shutdownCh:
_ = server.Shutdown()
case <-served:
}
}()
return server.ActivateAndServe()
})
}
// 4. DNS over HTTPS (DoH/HTTP1.1 + HTTP/2) + DNS over HTTP/3 (DoH3/QUIC)
for _, addr := range filterListeners(cfg.Server.ListenDoH) {
addr := addr
mux := http.NewServeMux()
altSvc := buildAltSvc(addr)
mux.HandleFunc("/dns-query", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Alt-Svc", altSvc)
handleDoH(w, r)
})
mux.HandleFunc("/.well-known/origin-svcb", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Alt-Svc", altSvc)
if len(ddrECHConfig) > 0 {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "public, max-age=86400")
b64Ech := base64.StdEncoding.EncodeToString(ddrECHConfig)
jsonPayload := fmt.Sprintf(`{"endpoints":[{"ech":"%s"}]}`, b64Ech)
w.Write([]byte(jsonPayload))
} else {
http.Error(w, "Not Found", http.StatusNotFound)
}
})
mux.HandleFunc("/.well-known/ech", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Alt-Svc", altSvc)
if len(ddrECHConfig) > 0 {
w.Header().Set("Content-Type", "application/echconfig-list")
w.Header().Set("Cache-Control", "public, max-age=86400")
w.Write(ddrECHConfig)
} else {
http.Error(w, "Not Found", http.StatusNotFound)
}
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Alt-Svc", altSvc)
accept := r.Header.Get("Accept")
contentType := r.Header.Get("Content-Type")
if strings.Contains(accept, "application/dns-message") ||
strings.Contains(contentType, "application/dns-message") ||
r.URL.Query().Get("dns") != "" {
handleDoH(w, r)
return
}
if WebUIMux != nil {
WebUIMux.ServeHTTP(w, r)
} else {
http.Error(w, "Not Found", http.StatusNotFound)
}
})
dohTLS := tlsConf.Clone()
dohTLS.NextProtos = []string{"h2", "http/1.1"}
go superviseListener("DoH", addr, func(attempt int) error {
l, err := net.Listen("tcp", addr)
if err != nil {
if attempt == 0 {
log.Fatalf("[FATAL] DoH listen failed on %s: %v", addr, err)
}
return fmt.Errorf("rebind: %w", err)
}
l = netutil.LimitListener(l, maxTCP)
tlsL := tls.NewListener(l, dohTLS)
h2Server := &http.Server{
Handler: mux,
ReadTimeout: tcpReadTimeout,
ReadHeaderTimeout: 3 * time.Second,
WriteTimeout: tcpWriteTimeout,
IdleTimeout: tcpIdleTimeout,
MaxHeaderBytes: 8192,
ErrorLog: log.New(io.Discard, "", 0),
}
if logSystem {
log.Printf("[LISTEN] DoH (HTTP/1.1 + HTTP/2) on %s (Max Conns: %d)", addr, maxTCP)
if len(ddrECHConfig) > 0 {
log.Printf("[LISTEN] Registered /.well-known/origin-svcb & /.well-known/ech L7 discovery endpoints natively on %s", addr)
}
}
served := make(chan struct{})
defer close(served)
go func() {
select {
case <-shutdownCh:
_ = h2Server.Close()
case <-served:
}
}()
return h2Server.Serve(tlsL)
})
doh3TLS := tlsConf.Clone()
doh3TLS.NextProtos = []string{"h3"}
go superviseListener("DoH3", addr, func(attempt int) error {
h3Server := &http3.Server{
Addr: addr,
Handler: mux,
TLSConfig: doh3TLS,
// [SECURITY/FIX 1.21.0] Populate http.LocalAddrContextKey on HTTP/3 requests
// so handleDoH can extract the bound local IP and port identically to HTTP/1.1 & HTTP/2.
ConnContext: func(ctx context.Context, c *quic.Conn) context.Context {
return context.WithValue(ctx, http.LocalAddrContextKey, c.LocalAddr())
},
QUICConfig: &quic.Config{
Allow0RTT: true,
MaxIdleTimeout: tcpIdleTimeout,
KeepAlivePeriod: 15 * time.Second,
MaxIncomingStreams: 256,
},
}
if logSystem {
log.Printf("[LISTEN] DoH3 (QUIC) on %s", addr)
}
served := make(chan struct{})
defer close(served)
go func() {
select {
case <-shutdownCh:
_ = h3Server.Close()
case <-served:
}
}()
return h3Server.ListenAndServe()
})
}
// 5. DNS over QUIC (DoQ) — RFC 9250
for _, addr := range filterListeners(cfg.Server.ListenDoQ) {
addr := addr
var activeDoQConns atomic.Int32
go superviseListener("DoQ", addr, func(attempt int) error {
doqTLS := tlsConf.Clone()
doqTLS.NextProtos = []string{"doq"}
listener, err := quic.ListenAddr(addr, doqTLS, &quic.Config{
Allow0RTT: true,
MaxIdleTimeout: tcpIdleTimeout,
KeepAlivePeriod: 15 * time.Second,
MaxIncomingStreams: maxDoQStreamsPerConn,
})
if err != nil {
if attempt == 0 {
log.Fatalf("[FATAL] DoQ failed on %s: %v", addr, err)
}
return fmt.Errorf("rebind: %w", err)
}
if logSystem {
log.Printf("[LISTEN] DoQ on %s (Max Conns: %d, Streams: %d/conn, %d global)",
addr, maxTCP, maxDoQStreamsPerConn, cap(doqStreamSem))
}
served := make(chan struct{})
defer close(served)
go func() {
select {
case <-shutdownCh:
listener.Close()
case <-served:
}
}()
defer listener.Close()
consecutiveAcceptErrs := 0
const maxConsecutiveAcceptErrs = 20
for {
conn, err := listener.Accept(context.Background())
if err != nil {
if isShuttingDown() {
return nil
}
if logSystem {
log.Printf("[DOQ] Accept error on %s: %v", addr, err)
}
consecutiveAcceptErrs++
if consecutiveAcceptErrs >= maxConsecutiveAcceptErrs {
return fmt.Errorf("accept failed %d consecutive times: %w", consecutiveAcceptErrs, err)
}
time.Sleep(250 * time.Millisecond)
continue
}
consecutiveAcceptErrs = 0
if activeDoQConns.Add(1) > int32(maxTCP) {
activeDoQConns.Add(-1)
conn.CloseWithError(0x0, "server busy")
if logSystem {
log.Printf("[SECURITY] DoQ connection rejected on %s: Max Connections (%d) reached", addr, maxTCP)
}
continue
}
currentConn := conn
go func() {
defer activeDoQConns.Add(-1)
handleDoQConnection(currentConn)
}()
}
})
}
}