-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakedns.go
107 lines (87 loc) · 2.23 KB
/
fakedns.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
package fakedns
import (
"crypto/tls"
"log"
"net"
"regexp"
"github.com/hupe1980/golog"
"github.com/miekg/dns"
)
type Options struct {
FallbackDNSResolver string
TTL uint32
IPsV4 []string
IPsV6 []string
Rebind *Rebind
Text []string
MX string
Logger golog.Logger
}
type FakeDNS struct {
options *Options
server *dns.Server
logger golog.Logger
}
func New(domain *regexp.Regexp, options *Options) *FakeDNS {
if options.FallbackDNSResolver != "" {
if _, _, err := net.SplitHostPort(options.FallbackDNSResolver); err != nil {
options.FallbackDNSResolver = net.JoinHostPort(options.FallbackDNSResolver, "53")
}
}
if options.Logger == nil {
options.Logger = golog.NewGoLogger(golog.INFO, log.Default())
}
server := &dns.Server{
Handler: &handler{
rebind: options.Rebind,
ttl: options.TTL,
fallbackDNS: options.FallbackDNSResolver,
re: domain,
ipV4Pool: NewRoundRobin(options.IPsV4...),
ipV6Pool: NewRoundRobin(options.IPsV6...),
text: options.Text,
mx: options.MX,
logger: options.Logger,
},
}
fakeDNS := &FakeDNS{
options: options,
server: server,
logger: options.Logger,
}
return fakeDNS
}
func (t *FakeDNS) ListenAndServe(addr, network string) error {
if addr != "" {
if _, _, err := net.SplitHostPort(addr); err != nil {
addr = net.JoinHostPort(addr, "53")
}
}
t.server.Addr = addr
t.server.Net = network
t.logger.Printf(golog.INFO, "[*] Starting server on %s", addr)
return t.server.ListenAndServe()
}
func (t *FakeDNS) ListenAndServeTLS(addr, certFile, keyFile string) error {
if addr != "" {
if _, _, err := net.SplitHostPort(addr); err != nil {
addr = net.JoinHostPort(addr, "53")
}
}
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return err
}
config := &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
}
t.server.TLSConfig = config
t.server.Addr = addr
t.server.Net = "tcp-tls"
t.logger.Printf(golog.INFO, "Starting server on %s", addr)
return t.server.ListenAndServe()
}
func (t *FakeDNS) Shutdown() error {
return t.server.Shutdown()
}