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
|
package server
import (
"context"
"log/slog"
"net/http"
"time"
"github.com/miekg/dns"
)
type Server struct {
logger *slog.Logger
udp *dns.Server
tcp *dns.Server
doh *http.Server
}
func New(udpAddr, tcpAddr, dohAddr string, logger *slog.Logger) (*Server, error) {
mux := dns.NewServeMux()
mux.HandleFunc(".", handleQuery)
s := &Server{logger: logger}
if udpAddr != "" {
s.udp = &dns.Server{
Addr: udpAddr,
Net: "udp",
Handler: mux,
UDPSize: 4096,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
ReusePort: true,
}
}
if tcpAddr != "" {
s.tcp = &dns.Server{
Addr: tcpAddr,
Net: "tcp",
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
}
if dohAddr != "" {
dohMux := http.NewServeMux()
dohMux.HandleFunc("/dns-query", s.dohHandler)
s.doh = &http.Server{
Addr: dohAddr,
Handler: dohMux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
}
return s, nil
}
func (s *Server) Run(ctx context.Context) error {
errCh := make(chan error, 3)
if s.udp != nil {
go func() {
s.logger.Info("udp listener active", "addr", s.udp.Addr)
errCh <- s.udp.ListenAndServe()
}()
}
if s.tcp != nil {
go func() {
s.logger.Info("tcp listener active", "addr", s.tcp.Addr)
errCh <- s.tcp.ListenAndServe()
}()
}
if s.doh != nil {
go func() {
s.logger.Info("doh listener active", "addr", s.doh.Addr)
errCh <- s.doh.ListenAndServe()
}()
}
select {
case <-ctx.Done():
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if s.udp != nil {
s.udp.ShutdownContext(shutdownCtx)
}
if s.tcp != nil {
s.tcp.ShutdownContext(shutdownCtx)
}
if s.doh != nil {
s.doh.Shutdown(shutdownCtx)
}
return ctx.Err()
case err := <-errCh:
return err
}
}
func (s *Server) Close() error {
if s.udp != nil {
s.udp.Shutdown()
}
if s.tcp != nil {
s.tcp.Shutdown()
}
if s.doh != nil {
s.doh.Close()
}
return nil
}
|