-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (202 loc) · 6.85 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// package dnslookup provides tools to obtain
// A, CNAME, MS, NS, PTR, TXT records of a given domain
package dnslookup
import (
"net"
"sync"
)
// mutex is a sync.Mutex object created to be used to prevent
// problems that may arise due to concurrency during DNS lookup.
var mutex sync.Mutex
// DnsRecord represents a DNS record for a specific domain
type DnsRecord struct {
// domain , the domain for which this DNS record is stored
domain string
// aRecords , a slice of A (IPv4) records.
aRecords []net.IP
// cnameRecords, a string of CNAME (Canonical Name) records.
cnameRecords string
// mxRecords, a slice of MX (Mail Exchange) records.
mxRecords []net.MX
// nsRecords, a slice of NS (Name Server) records.
nsRecords []net.NS
// ptrRecords, a slice of PTR (Pointer) records.
ptrRecords []string
// txtRecords, a slice of TXT (Text) records.
txtRecords []string
}
// NewDnsRecord is a constructor function simplifies the process of
// creating a new 'DnsRecord' instance by encapsulating the initialization logic
//
// domainName: a string representing of the domain name associated with
// the DNS record.
//
// NewDnsRecord returns a *DnsRecord object
func NewDnsRecord(domainName string) *DnsRecord {
return &DnsRecord{
domain: domainName,
}
}
// GetARecords method is designed to retrieve the A
// records for the given domain. It caches the results in
// the 'DnsRecord' instance to avoid unnecessary DNS lookups
// and uses a mutex to ensure safe concurrent access to the
// cached data.
//
// If the A records are already cached, it returns the cached
// records; otherwise, it performs a DNS lookup to fetch the
// records and caches them for future use.
//
// It returns a slice of that domain's IP addresses in the DnsRecords instance
func (d *DnsRecord) GetARecords() []net.IP {
if d.aRecords == nil {
aRecords, err := net.LookupIP(d.domain)
if err == nil {
mutex.Lock()
d.aRecords = append(d.aRecords, aRecords...)
mutex.Unlock()
}
return d.aRecords
}
return d.aRecords
}
// GetCnameRecords method is designed to retrieve the CNAME
// record for the given domain. It caches the result in the
// DnsRecord instance to avoid unnecesarry DNS lookups and uses
// a mutex to ensure safe concurrent access to the cached data.
//
// If the CNAME record is already cached, it returns the cached
// record; otherwise, it performs a DNS lookup to fetch the records
// and caches them for future use.
//
// It returns a string of Canonical Name Record belongs to that domain
// in the DnsRecord instance
func (d *DnsRecord) GetCnameRecords() string {
if d.cnameRecords == "" {
cnameRecords, err := net.LookupCNAME(d.domain)
if err == nil {
mutex.Lock()
d.cnameRecords = cnameRecords
}
mutex.Unlock()
return d.cnameRecords
}
return d.cnameRecords
}
// GetMxRecords method is designed to retrieve MX records for the
// given domain. It caches the result in the DnsRecords instance to
// avoid unnecessary DNS lookups and uses a mutex to ensure safe concurrent
// access to the cached data.
//
// If the MX record is already cached, it returns the cached
// record; otherwise, it performs a DNS lookup to fetch the records
// and caches them for future use.
//
// It returns a slice of mail server names belongs to that domain in the
// DnsRecord instance
func (d *DnsRecord) GetMxRecords() []net.MX {
if d.mxRecords == nil {
mxRecords, err := net.LookupMX(d.domain)
if err == nil {
mutex.Lock()
d.mxRecords = make([]net.MX, len(mxRecords))
for i, record := range mxRecords {
d.mxRecords[i] = *record
}
mutex.Unlock()
}
return d.mxRecords
}
return d.mxRecords
}
// GetNsRecords method is designed to retrieve NS records for the
// given domain. It caches the result in the DnsRecord instance to avoid
// unnecessary DNS lookups and uses a mutex to ensure safe concurrent access
// to the cached data.
//
// If NS records are already cached, it returns the cached record;
// otherwise, it performs a DNS lookup to fetch the records and caches
// them for future use
//
// It returns a slice of Name Server addresses belong to that domain
// in the DnsRecord instance
func (d *DnsRecord) GetNsRecords() []net.NS {
if d.nsRecords == nil {
nsRecords, err := net.LookupNS(d.domain)
if err == nil {
mutex.Lock()
d.nsRecords = make([]net.NS, len(nsRecords))
for i, record := range nsRecords {
d.nsRecords[i] = *record
}
mutex.Unlock()
}
}
return d.nsRecords
}
// GetPtrRecords method is designed to retrieve PTR records for the
// given domain. It caches the result in the DnsRecord instance to avoid
// unnecessary DNS lookups and uses a mutex to ensure safe concurrent access
// to the cached data.
//
// If PTR records are already cached , it returns the cached records;
// otherwise, it performs a DNS lookup to fetch the records and caches them
// for future use.
//
// It returns a slice of strings of Pointer records belong to that domain
// in the DnsRecord instance
func (d *DnsRecord) GetPtrRecords() []string {
if d.ptrRecords == nil {
if d.aRecords == nil {
d.GetARecords()
for _, v := range d.aRecords {
ptr, _ := net.LookupAddr(v.String())
d.ptrRecords = append(d.ptrRecords, ptr...)
}
}
for _, v := range d.aRecords {
ptr, _ := net.LookupAddr(v.String())
d.ptrRecords = append(d.ptrRecords, ptr...)
}
}
return d.ptrRecords
}
// GetTxtRecords method is designed to retrieve TXT records for the given
// domain. It caches the result in the DnsRecords instance to avoid unnecessary
// DNS lookups and uses a mutex to ensure safe concurrent access to the cached data
//
// If TXT records are already cached, it returns the cached records; otherwise;
// it performs a DNS lookup to fetch the records and caches them for future use
//
// It returns a slice of strings of TXT records belong to that domain
// in DnsRecord instance
func (d *DnsRecord) GetTxtRecords() []string {
if d.txtRecords == nil {
txtRecords, err := net.LookupTXT(d.domain)
if err == nil {
mutex.Lock()
d.txtRecords = txtRecords
}
mutex.Unlock()
}
return d.txtRecords
}
// GetAllRecords method retrieves and collect various DNS records
// for the given domain and returns them in a structured map. Each
// type of DNS record (A,CNAME,MX,NS,PTR,TXT) is obtained by calling
// separate methods, and the results are stored in the map under
// corresponding keys.
//
// It returns a map where the keys are strings and the values associated
// with these keys can be of any data type. This allows the function to store
// various types of DNS records in the same map.
func (d *DnsRecord) GetAllRecords() map[string]interface{} {
var records = make(map[string]interface{})
records["A records"] = d.GetARecords()
records["CNAME records"] = d.GetCnameRecords()
records["MX records"] = d.GetMxRecords()
records["NS records"] = d.GetNsRecords()
records["PTR records"] = d.GetPtrRecords()
records["TXT records"] = d.GetTxtRecords()
return records
}