-
Notifications
You must be signed in to change notification settings - Fork 159
/
server.go
57 lines (43 loc) · 1.06 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
package main
import (
"net"
"strconv"
"time"
"github.com/miekg/dns"
)
type Server struct {
host string
port int
rTimeout time.Duration
wTimeout time.Duration
}
func (s *Server) Addr() string {
return net.JoinHostPort(s.host, strconv.Itoa(s.port))
}
func (s *Server) Run() {
Handler := NewHandler()
tcpHandler := dns.NewServeMux()
tcpHandler.HandleFunc(".", Handler.DoTCP)
udpHandler := dns.NewServeMux()
udpHandler.HandleFunc(".", Handler.DoUDP)
tcpServer := &dns.Server{Addr: s.Addr(),
Net: "tcp",
Handler: tcpHandler,
ReadTimeout: s.rTimeout,
WriteTimeout: s.wTimeout}
udpServer := &dns.Server{Addr: s.Addr(),
Net: "udp",
Handler: udpHandler,
UDPSize: 65535,
ReadTimeout: s.rTimeout,
WriteTimeout: s.wTimeout}
go s.start(udpServer)
go s.start(tcpServer)
}
func (s *Server) start(ds *dns.Server) {
logger.Info("Start %s listener on %s", ds.Net, s.Addr())
err := ds.ListenAndServe()
if err != nil {
logger.Error("Start %s listener on %s failed:%s", ds.Net, s.Addr(), err.Error())
}
}