-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
134 lines (122 loc) · 2.8 KB
/
api.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
// package dnscache provides an example async and multithreaded map based dnscache
package dnscache
// import
import (
"errors"
"net"
"strings"
)
//
// SIMPLE STDLIB "NET" PKG API COMPATIBLE INPLACE
//
// LookupIP ...
func LookupIP(host string) ([]net.IP, error) {
if ip, ok := getIP(host); ok {
return ip, nil
}
ip, err := net.LookupIP(host)
if err != nil {
return ip, err
}
dnsIPChan <- dnsIP{host, ip}
return ip, nil
}
// LookupHost ...
func LookupHost(host string) ([]string, error) {
if addrs, ok := getHost(host); ok {
return addrs.([]string), nil
}
addrs, err := net.LookupHost(host)
if err != nil {
return []string{}, err
}
dnsHostChan <- dnsHost{host, addrs}
return addrs, nil
}
// LookupCNAME ...
func LookupCNAME(host string) (string, error) {
if addrs, ok := getHost(host + _cname); ok {
return addrs.(string), nil
}
addrs, err := net.LookupCNAME(host)
if err != nil {
return _empty, err
}
dnsHostChan <- dnsHost{host + _cname, addrs}
return addrs, nil
}
// LookupMX ...
func LookupMX(host string) ([]*net.MX, error) {
if addrs, ok := getHost(host + _mx); ok {
return addrs.([]*net.MX), nil
}
addrs, err := net.LookupMX(host)
if err != nil {
return nil, err
}
dnsHostChan <- dnsHost{host + _mx, addrs}
return addrs, nil
}
// LookupNS ...
func LookupNS(host string) ([]*net.NS, error) {
if addrs, ok := getHost(host + _ns); ok {
return addrs.([]*net.NS), nil
}
addrs, err := net.LookupNS(host)
if err != nil {
return nil, err
}
dnsHostChan <- dnsHost{host + _ns, addrs}
return addrs, nil
}
// LookupTXT ...
func LookupTXT(host string) ([]string, error) {
if addrs, ok := getHost(host + _txt); ok {
return addrs.([]string), nil
}
addrs, err := net.LookupTXT(host)
if err != nil {
return nil, err
}
dnsHostChan <- dnsHost{host + _txt, addrs}
return addrs, nil
}
// LookupSRV ...
func LookupSRV(service, proto, name string) (string, []*net.SRV, error) {
if addrs, ok := getHost(service + proto + name + _srv); ok {
e := addrs.([]*net.SRV)
s := strings.Split(e[0].Target, _sep)
if len(s) == 2 {
e[0].Target = s[0]
return s[1], e, nil
}
return _empty, nil, errors.New("[dnscache] [internal error] [unable to decode srv record]")
}
cname, addrs, err := net.LookupSRV(service, proto, name)
if err != nil {
return _empty, nil, err
}
s := addrs
s[0].Target = s[0].Target + _sep + cname
dnsHostChan <- dnsHost{service + proto + name + _srv, s}
return cname, addrs, nil
}
// LookupAddr ...
func LookupAddr(addr string) ([]string, error) {
if hosts, ok := getHost(addr + _ptr); ok {
return hosts.([]string), nil
}
hosts, err := net.LookupAddr(addr)
if err != nil {
return []string{}, err
}
dnsHostChan <- dnsHost{addr + _ptr, hosts}
return hosts, nil
}
//
// ADDITIONAL NEW INTERFACES
//
// CleanCache ...
func CleanCache() {
cleanCacheAll()
}