-
Notifications
You must be signed in to change notification settings - Fork 69
/
server.go
127 lines (108 loc) · 3.04 KB
/
server.go
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
package main
import (
"github.com/cottand/leng/internal/metric"
"time"
"github.com/miekg/dns"
)
// Server type
type Server struct {
host string
rTimeout time.Duration
wTimeout time.Duration
eventLoop *EventLoop
udpServer *dns.Server
tcpServer *dns.Server
httpServer *ServerHTTPS
udpHandler *dns.ServeMux
tcpHandler *dns.ServeMux
httpHandler *dns.ServeMux
}
// Run starts the server
func (s *Server) Run(
config *Config,
blockCache *MemoryBlockCache,
questionCache *MemoryQuestionCache,
) {
s.eventLoop = NewEventLoop(config, blockCache, questionCache)
tcpHandler := dns.NewServeMux()
tcpHandler.HandleFunc(".", s.eventLoop.DoTCP)
udpHandler := dns.NewServeMux()
udpHandler.HandleFunc(".", s.eventLoop.DoUDP)
httpHandler := dns.NewServeMux()
httpHandler.HandleFunc(".", s.eventLoop.DoHTTP)
s.tcpHandler = tcpHandler
s.udpHandler = udpHandler
s.httpHandler = httpHandler
s.tcpServer = &dns.Server{
Addr: s.host,
Net: "tcp",
Handler: tcpHandler,
ReadTimeout: s.rTimeout,
WriteTimeout: s.wTimeout,
}
s.udpServer = &dns.Server{
Addr: s.host,
Net: "udp",
Handler: udpHandler,
UDPSize: 65535,
ReadTimeout: s.rTimeout,
WriteTimeout: s.wTimeout,
}
if config.DnsOverHttpServer.Enabled {
var err error
timeout := time.Duration(config.DnsOverHttpServer.TimeoutMs) * time.Millisecond
ttl := time.Duration(config.TTL) * time.Second
s.httpServer, err = NewServerHTTPS(httpHandler, config.DnsOverHttpServer.Bind, timeout, ttl, config.DnsOverHttpServer.parsedTls)
if err != nil {
logger.Criticalf("failed to create http server %v", err)
}
go s.startHttp(config.DnsOverHttpServer.Bind)
}
go s.start(s.udpServer)
go s.start(s.tcpServer)
}
func (s *Server) start(ds *dns.Server) {
logger.Criticalf("start %s listener on %s\n", ds.Net, s.host)
if err := ds.ListenAndServe(); err != nil {
logger.Criticalf("start %s listener on %s failed: %s\n", ds.Net, s.host, err.Error())
}
}
func (s *Server) startHttp(addr string) {
logger.Criticalf("start http listener on %s\n", addr)
if err := s.httpServer.ListenAndServe(); err != nil {
logger.Criticalf("start http listener on %s failed or was closed: %s\n", addr, err.Error())
}
}
// Stop stops the server
func (s *Server) Stop() {
if s.eventLoop != nil {
s.eventLoop.muActive.Lock()
s.eventLoop.active = false
close(s.eventLoop.requestChannel)
s.eventLoop.muActive.Unlock()
}
if s.udpServer != nil {
err := s.udpServer.Shutdown()
if err != nil {
logger.Critical(err)
}
}
if s.tcpServer != nil {
err := s.tcpServer.Shutdown()
if err != nil {
logger.Critical(err)
}
}
if s.httpServer != nil {
err := s.httpServer.Stop()
if err != nil {
logger.Critical(err)
}
}
}
// ReloadConfig only supports reloading the customDnsRecords section of the config for now
func (s *Server) ReloadConfig(config *Config) {
newRecords := NewCustomDNSRecordsFromText(config.CustomDNSRecords)
s.eventLoop.customDns = NewCustomRecordsResolver(newRecords)
defer metric.CustomDNSConfigReload.Inc()
}