-
Notifications
You must be signed in to change notification settings - Fork 0
/
mxlookup.go
189 lines (170 loc) · 5.79 KB
/
mxlookup.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
/*
* ZDNS Copyright 2016 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package mxlookup
import (
"flag"
"strings"
"sync"
"github.com/miekg/dns"
"github.com/Rich5/zdns"
"github.com/Rich5/zdns/cachehash"
"github.com/Rich5/zdns/modules/miekg"
)
// result to be returned by scan of host
type CachedAddresses struct {
IPv4Addresses []string
IPv6Addresses []string
}
type MXRecord struct {
Name string `json:"name" groups:"short,normal,long,trace"`
Type string `json:"type" groups:"short,normal,long,trace"`
Class string `json:"class" groups:"short,normal,long,trace"`
Preference uint16 `json:"preference" groups:"short,normal,long,trace"`
IPv4Addresses []string `json:"ipv4_addresses,omitempty" groups:"short,normal,long,trace"`
IPv6Addresses []string `json:"ipv6_addresses,omitempty" groups:"short,normal,long,trace"`
TTL uint32 `json:"ttl" groups:"ttl,normal,long,trace"`
}
type Result struct {
Servers []MXRecord `json:"exchanges" groups:"short,normal,long,trace"`
}
// Per Connection Lookup ======================================================
//
type Lookup struct {
Factory *RoutineLookupFactory
miekg.Lookup
}
func dotName(name string) string {
return strings.Join([]string{name, "."}, "")
}
func (s *Lookup) LookupIPs(name string) (CachedAddresses, []interface{}) {
s.Factory.Factory.CHmu.Lock()
// XXX this should be changed to a miekglookup
res, found := s.Factory.Factory.CacheHash.Get(name)
s.Factory.Factory.CHmu.Unlock()
if found {
return res.(CachedAddresses), make([]interface{}, 0)
}
var retv CachedAddresses
trace := make([]interface{}, 0)
// ipv4
if s.Factory.Factory.IPv4Lookup || !s.Factory.Factory.IPv6Lookup {
res, secondTrace, status, _ := s.DoTypedMiekgLookup(name, dns.TypeA)
trace = append(trace, secondTrace...)
if status == zdns.STATUS_NOERROR {
cast, _ := res.(miekg.Result)
for _, innerRes := range cast.Answers {
castInnerRes, ok := innerRes.(miekg.Answer)
if !ok {
continue
}
retv.IPv4Addresses = append(retv.IPv4Addresses, castInnerRes.Answer)
}
}
}
// ipv6
if s.Factory.Factory.IPv6Lookup {
res, secondTrace, status, _ := s.DoTypedMiekgLookup(name, dns.TypeAAAA)
trace = append(trace, secondTrace...)
if status == zdns.STATUS_NOERROR {
cast, _ := res.(miekg.Result)
for _, innerRes := range cast.Answers {
castInnerRes, ok := innerRes.(miekg.Answer)
if !ok {
continue
}
retv.IPv6Addresses = append(retv.IPv6Addresses, castInnerRes.Answer)
}
}
}
s.Factory.Factory.CHmu.Lock()
s.Factory.Factory.CacheHash.Add(name, retv)
s.Factory.Factory.CHmu.Unlock()
return retv, trace
}
func (s *Lookup) DoLookup(name string) (interface{}, []interface{}, zdns.Status, error) {
retv := Result{Servers: []MXRecord{}}
res, trace, status, err := s.DoTypedMiekgLookup(name, dns.TypeMX)
if status != zdns.STATUS_NOERROR {
return retv, trace, status, err
}
r, ok := res.(miekg.Result)
if !ok {
panic("could not cast correctly")
}
for _, ans := range r.Answers {
if mxAns, ok := ans.(miekg.MXAnswer); ok {
name = strings.TrimSuffix(mxAns.Answer.Answer, ".")
rec := MXRecord{TTL: mxAns.Ttl, Type: mxAns.Type, Class: mxAns.Class, Name: name, Preference: mxAns.Preference}
ips, secondTrace := s.LookupIPs(name)
rec.IPv4Addresses = ips.IPv4Addresses
rec.IPv6Addresses = ips.IPv6Addresses
retv.Servers = append(retv.Servers, rec)
trace = append(trace, secondTrace...)
}
}
return retv, trace, zdns.STATUS_NOERROR, nil
}
// Per GoRoutine Factory ======================================================
//
type RoutineLookupFactory struct {
miekg.RoutineLookupFactory
Factory *GlobalLookupFactory
}
func (s *RoutineLookupFactory) MakeLookup() (zdns.Lookup, error) {
a := Lookup{Factory: s}
nameServer := s.Factory.RandomNameServer()
a.Initialize(nameServer, dns.TypeMX, dns.ClassINET, &s.RoutineLookupFactory)
return &a, nil
}
// Global Factory =============================================================
//
type GlobalLookupFactory struct {
miekg.GlobalLookupFactory
IPv4Lookup bool
IPv6Lookup bool
MXCacheSize int
CacheHash *cachehash.CacheHash
CHmu sync.Mutex
}
func (s *GlobalLookupFactory) AddFlags(f *flag.FlagSet) {
f.BoolVar(&s.IPv4Lookup, "ipv4-lookup", false, "perform A lookups for each MX server")
f.BoolVar(&s.IPv6Lookup, "ipv6-lookup", false, "perform AAAA record lookups for each MX server")
f.IntVar(&s.MXCacheSize, "mx-cache-size", 1000, "number of records to store in MX -> A/AAAA cache")
}
func (s *GlobalLookupFactory) Initialize(c *zdns.GlobalConf) error {
s.GlobalLookupFactory.Initialize(c)
s.GlobalConf = c
s.CacheHash = new(cachehash.CacheHash)
s.CacheHash.Init(s.MXCacheSize)
return nil
}
// Command-line Help Documentation. This is the descriptive text what is
// returned when you run zdns module --help
func (s *GlobalLookupFactory) Help() string {
return ""
}
func (s *GlobalLookupFactory) MakeRoutineFactory(threadID int) (zdns.RoutineLookupFactory, error) {
r := new(RoutineLookupFactory)
r.Initialize(s.GlobalConf)
r.RoutineLookupFactory.Factory = &s.GlobalLookupFactory
r.Factory = s
r.ThreadID = threadID
return r, nil
}
// Global Registration ========================================================
//
func init() {
s := new(GlobalLookupFactory)
zdns.RegisterLookup("MXLOOKUP", s)
}