-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.go
201 lines (144 loc) · 3.78 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
package main
import (
"errors"
"fmt"
"net"
"strings"
"sync"
)
var outLock sync.Mutex
func resolveEntry(ch chan string) {
var response string
var err error
for {
entry := <-ch
if entry == "" {
// if nothing was sent through channel, loop again
continue
}
domain, entryType := parseLine(entry)
switch entryType {
case "A", "AAAA":
response, err = resolveA(domain)
case "PTR":
response, err = resolvePTR(domain)
case "MX":
response, err = resolveMX(domain)
case "SRV":
response, err = resolveSRV(domain)
default:
response = ""
err = errors.New("Lookup for " + entryType + " not supported")
}
printResult(domain, entryType, response, err)
waitResolvers.Done()
}
}
func parseLine(entry string) (string, string) {
var domain string
entryFields := strings.Split(entry, "\t")
if entryFields[0][len(entryFields[0])-1:] == "." {
domain = entryFields[0][:len(entryFields[0])-1]
} else {
domain = entryFields[0]
}
return domain, entryFields[1]
}
func resolveA(domain string) (string, error) {
ips, err := net.LookupIP(domain)
if err != nil {
return "", err
}
//TODO build string using buffers
var header string
response := ""
for _, ip := range ips {
if ip.To4() == nil {
header = "IPv6:"
} else {
header = "IPv4:"
}
response += "[" + header + ip.String() + "]"
}
return response, nil
}
func resolveMX(domain string) (string, error) {
mxs, err := net.LookupMX(domain)
if err != nil {
return "", err
}
//TODO build string using buffers
response := ""
for _, mx := range mxs {
response += fmt.Sprintf("[host:%s,pref:%d]", mx.Host, mx.Pref)
}
return response, nil
}
func resolvePTR(domain string) (string, error) {
//reverse ip octects and remove '.in-addr.arpa'
ptrChunks := strings.Split(domain, ".")
//allocate space
bufIP := make([]byte, len(ptrChunks[0])+len(ptrChunks[1])+len(ptrChunks[2])+len(ptrChunks[3])+3)
pointer := 0
pointer = copy(bufIP, ptrChunks[3])
pointer += copy(bufIP[pointer:], ".")
pointer += copy(bufIP[pointer:], ptrChunks[2])
pointer += copy(bufIP[pointer:], ".")
pointer += copy(bufIP[pointer:], ptrChunks[1])
pointer += copy(bufIP[pointer:], ".")
copy(bufIP[pointer:], ptrChunks[0])
domain = string(bufIP)
names, err := net.LookupAddr(domain)
if err != nil {
return "", err
}
//TODO build string using buffers
response := ""
for _, name := range names {
response += "[name:" + name + "]"
}
return response, nil
}
func resolveSRV(domain string) (string, error) {
var serviceIndex int
var protocolIndex int
// extract service and protocol
serviceIndex = strings.Index(domain, ".")
if serviceIndex != -1 {
protocolIndex = strings.Index(domain[serviceIndex+1:], ".") + serviceIndex
}
if serviceIndex == -1 || protocolIndex < serviceIndex {
return "", errors.New("SRV query '" + domain + "' should be formated as 'service.protocol.domain'")
}
service := domain[:serviceIndex]
protocol := domain[serviceIndex+1 : protocolIndex+1]
// remove underscore
if service[0:1] == "_" {
service = service[1:]
}
if protocol[0:1] == "_" {
protocol = protocol[1:]
}
cname, srvs, err := net.LookupSRV(service, protocol, domain[protocolIndex+2:])
if err != nil {
return "", err
}
//TODO build string using buffers
response := "cname:" + cname
for _, srv := range srvs {
response += fmt.Sprintf("[target:%s,port:%d,priority:%d,weight:%d]", srv.Target, srv.Port, srv.Priority, srv.Weight)
}
return response, err
}
func printResult(query string, entryType string, response string, err error) {
outLock.Lock()
defer outLock.Unlock()
fmt.Println("----------------------------------")
fmt.Println("Query: " + query)
fmt.Println("Type: " + entryType)
if err != nil {
fmt.Println("Error: " + err.Error())
} else {
fmt.Println("Response: " + response)
}
}