-
Notifications
You must be signed in to change notification settings - Fork 43
/
native_resolver.go
74 lines (65 loc) · 1.76 KB
/
native_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
package netx
import (
"context"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"net"
)
/*
Resolver is a DNS resolver.
It is used to resolve a domain name to an IP address.
*/
var (
defaultResolver = &net.Resolver{}
pureGoResolver = &net.Resolver{PreferGo: true}
)
func _execDefault(host string, config *ReliableDNSConfig) []string {
defer func() {
if err := recover(); err != nil {
log.Debugf("net.Resolver panic: %v", err)
utils.PrintCurrentGoroutineRuntimeStack()
}
}()
ctx, cancel := context.WithTimeout(config.GetBaseContext(), config.Timeout)
defer cancel()
result, err := defaultResolver.LookupHost(ctx, host)
if err != nil {
log.Debugf("default dns resolver lookup failed: %v", err)
}
if len(result) > 0 {
return result
}
return nil
}
func _execPureGoDefault(host string, config *ReliableDNSConfig) []string {
defer func() {
if err := recover(); err != nil {
log.Debugf("net.Resolver panic: %v", err)
utils.PrintCurrentGoroutineRuntimeStack()
}
}()
ctx, cancel := context.WithTimeout(config.GetBaseContext(), config.Timeout)
defer cancel()
result, err := pureGoResolver.LookupHost(ctx, host)
if err != nil {
log.Debugf("default dns resolver lookup failed: %v", err)
}
if len(result) > 0 {
return result
}
return nil
}
// nativeLookupHost looks up the given host using the local resolver.
// It returns a slice of that host's addresses.
func nativeLookupHost(host string, config *ReliableDNSConfig) {
result := _execDefault(host, config)
if len(result) > 0 {
for _, ip := range result {
config.call("", host, ip, "system-default", "system-default")
}
return
}
for _, ip := range _execPureGoDefault(host, config) {
config.call("", host, ip, "system-prefergo", "system-prefergo")
}
}