-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathresolver.go
237 lines (208 loc) · 5.81 KB
/
resolver.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package dnsutils
import (
"errors"
"net/http"
"strings"
"time"
"github.com/bepass-org/dnsutils/internal/dialer"
"github.com/bepass-org/dnsutils/internal/resolvers"
"github.com/bepass-org/dnsutils/internal/statute"
"github.com/miekg/dns"
)
// Resolver handles DNS lookups and caching
type Resolver struct {
options statute.ResolverOptions
resolver statute.IResolver
cache statute.DefaultCache
logger statute.Logger
hosts statute.Hosts
}
// NewResolver creates a new Resolver with default options
// Options can be provided to customize the resolver
func NewResolver(options ...Option) *Resolver {
// Create resolver with default options
p := &Resolver{
options: statute.ResolverOptions{
UseIPv4: false,
UseIPv6: false,
SearchList: nil,
Ndots: 1,
Prefer: "",
Timeout: 1 * time.Minute,
InsecureSkipVerify: true,
TLSHostname: "",
Logger: statute.DefaultLogger{},
Dialer: dialer.NewAppDialer(1 * time.Minute),
TLSDialer: dialer.NewAppTLSDialer(1 * time.Minute),
RawDialerFunc: statute.DefaultDialerFunc,
TLSDialerFunc: statute.DefaultTLSDialerFunc,
HttpClient: statute.DefaultHTTPClient(nil, nil),
},
cache: statute.DefaultCache{},
logger: statute.DefaultLogger{},
hosts: statute.Hosts{},
}
for _, option := range options {
option(p)
}
return p
}
type Option func(*Resolver)
func WithUseIPv4(useIPv4 bool) Option {
return func(r *Resolver) {
r.options.UseIPv4 = useIPv4
}
}
func WithUseIPv6(useIPv6 bool) Option {
return func(r *Resolver) {
r.options.UseIPv6 = useIPv6
}
}
func WithSearchList(searchList []string) Option {
return func(r *Resolver) {
r.options.SearchList = searchList
}
}
func WithNdots(ndots int) Option {
return func(r *Resolver) {
r.options.Ndots = ndots
}
}
func WithPrefer(prefer string) Option {
return func(r *Resolver) {
r.options.Prefer = prefer
}
}
func WithTLSHostname(tlsHostname string) Option {
return func(r *Resolver) {
r.options.TLSHostname = tlsHostname
}
}
func WithDialer(d dialer.TDialerFunc) Option {
return func(r *Resolver) {
r.options.RawDialerFunc = d
r.options.HttpClient = statute.DefaultHTTPClient(r.options.RawDialerFunc, r.options.TLSDialerFunc)
dialer.RawDialFunc = d
r.options.Dialer = dialer.NewAppDialer(r.options.Timeout)
}
}
func WithTLSDialer(t dialer.TDialerFunc) Option {
return func(r *Resolver) {
r.options.TLSDialerFunc = t
r.options.HttpClient = statute.DefaultHTTPClient(r.options.RawDialerFunc, r.options.TLSDialerFunc)
dialer.TLSDialFunc = t
r.options.TLSDialer = dialer.NewAppTLSDialer(r.options.Timeout)
}
}
func WithHttpClient(client *http.Client) Option {
return func(r *Resolver) {
r.options.HttpClient = client
}
}
func WithLogger(logger statute.Logger) Option {
return func(r *Resolver) {
r.options.Logger = logger
}
}
func WithTimeout(timeout time.Duration) Option {
return func(r *Resolver) {
r.options.Timeout = timeout
}
}
func WithInsecureSkipVerify(insecureSkipVerify bool) Option {
return func(r *Resolver) {
r.options.InsecureSkipVerify = insecureSkipVerify
}
}
func WithHost(domain string, ips []string) Option {
return func(r *Resolver) {
r.hosts[domain] = ips
}
}
func (r *Resolver) SetDNSServer(address string) error {
nsSrvType := statute.GetDNSType(address)
var err error
switch nsSrvType {
case "udp":
r.logger.Debug("initiating UDP resolver")
r.resolver, err = resolvers.NewClassicResolver(address,
resolvers.ClassicResolverOpts{
UseTCP: false,
UseTLS: false,
}, r.options)
case "tcp":
r.logger.Debug("initiating TCP resolver")
r.resolver, err = resolvers.NewClassicResolver(address,
resolvers.ClassicResolverOpts{
UseTCP: true,
UseTLS: false,
}, r.options)
case "dot":
r.logger.Debug("initiating DOT resolver")
r.resolver, err = resolvers.NewClassicResolver(address,
resolvers.ClassicResolverOpts{
UseTCP: true,
UseTLS: true,
}, r.options)
case "doh":
r.logger.Debug("initiating DOH resolver")
r.resolver, err = resolvers.NewDOHResolver(address, r.options)
case "crypt":
r.logger.Debug("initiating DNSCrypt resolver")
r.resolver, err = resolvers.NewDNSCryptResolver(address,
resolvers.DNSCryptResolverOpts{
UseTCP: true,
}, r.options)
default:
r.logger.Debug("initiating system resolver")
r.resolver, err = resolvers.NewSystemResolver(r.options)
if nsSrvType == "unknown" {
r.logger.Error("unknown dns server type! using default system resolver as fallback")
}
}
return err
}
// LookupIP resolves the FQDN to an IP address using the specified resolution mechanism.
func (r *Resolver) LookupIP(fqdn string) ([]string, error) {
// CheckHosts checks if a given domain exists in the local resolver's hosts file
// and returns the corresponding IP address if found, or an empty string if not.
if ips, ok := r.hosts[fqdn]; ok {
return ips, nil
}
// Ensure fqdn ends with a period
if !strings.HasSuffix(fqdn, ".") {
fqdn += "."
}
// Check the cache for fqdn
if cachedValue, _ := r.cache.Get(fqdn); cachedValue != nil {
r.logger.Debug("using cached value for %s", fqdn)
return cachedValue.([]string), nil
}
question := dns.Question{
Name: fqdn,
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
}
response, err := r.resolver.Lookup(question)
if err != nil {
return nil, err
}
if len(response.Answers) == 0 {
return nil, errors.New("no answers found")
}
r.logger.Debug("resolved %s to %s", fqdn, response.Answers[0].Address)
if response.Answers[0].Type == "CNAME" {
ip, err := r.LookupIP(response.Answers[0].Address)
if err != nil {
return nil, err
}
r.cache.Set(fqdn, ip)
return ip, nil
}
var ips []string
for _, answer := range response.Answers {
ips = append(ips, answer.Address)
}
r.cache.Set(fqdn, ips)
return ips, nil
}