-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
174 lines (161 loc) · 3.29 KB
/
utils.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
package main
import (
"strings"
"github.com/miekg/dns"
)
func inarray(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
func searchipfromextra(name string, exrr []dns.RR) []string {
name = strings.ToLower(name)
var res []string
for _, v := range exrr {
if strings.ToLower(v.Header().Name) == name {
if v.Header().Rrtype == dns.TypeA {
res = append(res, v.(*dns.A).A.String())
} else if v.Header().Rrtype == dns.TypeAAAA {
res = append(res, v.(*dns.AAAA).AAAA.String())
}
}
}
return res
}
func dupmerge(src []string, dst []string) []string {
for _, d := range dst {
dup := false
for _, s := range src {
if s == d {
dup = true
break
}
}
if !dup {
src = append(src, d)
}
}
return src
}
func searchnsfromns(name string, nss []dns.RR) []string {
name = strings.ToLower(name)
var res []string
for _, v := range nss {
if v.Header().Rrtype == dns.TypeNS {
res = append(res, strings.ToLower(v.(*dns.NS).Ns))
} else if v.Header().Rrtype == dns.TypeSOA {
soaname := v.(*dns.SOA).Hdr.Name
soaname = strings.ToLower(soaname)
if soaname != name {
//getnsip(soaname, 0)
//analyze(soaname)
soarecord := mp[soaname]
if soarecord != nil {
res = dupmerge(res, soarecord.ns)
}
// if sv, ok := mp[name]; ok {
// sv.soa = soaname
// } else {
// mp[name] = &dnsrecord{
// name: name,
// ns: []string{},
// soa: soaname,
// ip: []string{},
// }
// }
}
res = dupmerge(res, []string{strings.ToLower(v.(*dns.SOA).Ns)})
}
}
return res
}
func searchaaaaafromansver(ans []dns.RR) []string {
var res []string
for _, v := range ans {
if v.Header().Rrtype == dns.TypeA {
res = append(res, v.(*dns.A).A.String())
} else if v.Header().Rrtype == dns.TypeAAAA {
res = append(res, v.(*dns.AAAA).AAAA.String())
}
}
return res
}
func appendnstodnsrecord(name string, nsrecord string) {
name = strings.ToLower(name)
nsrecord = strings.ToLower(nsrecord)
record := mp[name]
if record == nil {
mp[name] = &dnsrecord{
name: name,
ns: []string{nsrecord},
ip: []string{},
}
} else {
for _, ns := range record.ns {
if ns == nsrecord {
return
}
}
record.ns = append(record.ns, nsrecord)
}
}
func mergeaaaaatodnsrecord(name string, aaaaa []string) {
name = strings.ToLower(name)
record := mp[name]
if record == nil {
mp[name] = &dnsrecord{
name: name,
ip: aaaaa,
ns: []string{},
}
} else {
for _, aaa := range aaaaa {
exist := false
for _, ip := range record.ip {
if ip == aaa {
exist = true
}
}
if !exist {
record.ip = append(record.ip, aaa)
}
}
}
}
func mergenstodnsrecord(name string, aaaaa []string) {
name = strings.ToLower(name)
for i, v := range aaaaa {
aaaaa[i] = strings.ToLower(v)
}
record := mp[name]
if record == nil {
mp[name] = &dnsrecord{
name: name,
ns: aaaaa,
ip: []string{},
}
} else {
for _, aaa := range aaaaa {
exist := false
for _, ns := range record.ns {
if ns == aaa {
exist = true
}
}
if !exist {
record.ns = append(record.ns, aaa)
}
}
}
}
func getuplevelname(name string) string {
idx := strings.Index(name, ".")
ret := name[idx+1:]
if ret == "" {
ret = "."
}
return ret
}