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
|
package server
import (
"context"
"log/slog"
"net/http"
"time"
"codeberg.org/miekg/dns"
"linum/internal/resolver"
"linum/internal/blocklist"
"linum/internal/cache"
)
type Server struct {
logger *slog.Logger
resolver *resolver.Resolver
cache *cache.Cache
blocklist *blocklist.Blocklist
udp *dns.Server
tcp *dns.Server
doh *http.Server
baseCtx context.Context
cancel context.CancelFunc
}
func New(udpAddr, tcpAddr, dohAddr string, logger *slog.Logger,
r *resolver.Resolver, c *cache.Cache, b *blocklist.Blocklist) (*Server, error) {
baseCtx, cancel := context.WithCancel(context.Background())
s := &Server{logger: logger, resolver: r, cache: c, blocklist: b,
baseCtx: baseCtx, cancel: cancel}
mux := dns.NewServeMux()
mux.HandleFunc(".", s.handleQuery)
if udpAddr != "" {
s.udp = &dns.Server{
Addr: udpAddr,
Net: "udp",
Handler: mux,
UDPSize: 4096,
ReadTimeout: 5 * time.Second,
ReusePort: true,
}
}
if tcpAddr != "" {
s.tcp = &dns.Server{
Addr: tcpAddr,
Net: "tcp",
Handler: mux,
ReadTimeout: 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.Shutdown(shutdownCtx)
}
if s.tcp != nil {
s.tcp.Shutdown(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(context.Background())
}
if s.tcp != nil {
s.tcp.Shutdown(context.Background())
}
if s.doh != nil {
s.doh.Close()
}
return nil
}
|