-
Notifications
You must be signed in to change notification settings - Fork 2
/
ddig-core.js
300 lines (260 loc) · 12.7 KB
/
ddig-core.js
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Exports a public function, resolve(), which accepts multiple domains and multiple resolvers.
// The other local functions handle the nested iteration to resolve each domain against each resolver.
const debug = require('debug')('ddig');
debug('Entry: [%s]', __filename);
// Import IP validation library
const isIp = require('is-ip');
// Import DNS library
const dns = require('native-dns-multisocket');
// Platform agnostic new line character
const EOL = require('os').EOL;
const path = require('path');
var configFilePath = path.resolve(__dirname);
module.exports = {
resolve(domain, resolver, options, callback) {
const startTime = Date.now();
debug('resolve() called for domain [%s] via resolver [%s (%s)] with options: %O', domain, resolver.nameServer, resolver.provider ,options);
// Initialise lookup result object
var lookupResult = {
'domain': domain,
'ipAddress': null,
'recursion': null,
'answer': null,
'nameServer': resolver.nameServer,
'provider': resolver.provider,
'msg': '',
'success': false,
'duration': 0,
'error': '',
'recordType': null,
'ttl': 0
};
// Validate the resolver IP address is valid
if (isIp(resolver.nameServer) === false) {
debug('resolve() skipping the resolver [%s] because it is not a valid IP address', resolver.nameServer);
// Populate lookup results object
lookupResult.success=false;
lookupResult.duration = Math.ceil((Date.now() - startTime));
lookupResult.msg = 'Invalid resolver IP address';
} else {
// Create DNS Question object
var question = dns.Question({
name: domain,
type: options.question.type,
});
// Create DNS Request object to "ask" the Question
var req = dns.Request({
question: question,
server: {
address: resolver.nameServer,
port: options.request.port,
type: options.request.type
},
timeout: options.request.timeout,
cache: options.request.cache,
try_edns: options.request.try_edns
});
debug('resolve() is issuing the dns.request: %O', req);
// Issue DNS request. The response is handled by event handlers below
req.send();
// Handle a DNS timeout event
req.on('timeout', function () {
debug('The %sms timeout elapsed before %s [%s] responded', options.request.timeout, resolver.nameServer, resolver.provider);
// Populate lookup result object
lookupResult.msg = 'Timeout';
lookupResult.success = false;
lookupResult.duration = Math.ceil((Date.now() - startTime));
// Return the result
callback(lookupResult);
});
// Handle DNS message event; i.e process the `answer` response
req.on('message', function (err, answer) {
if (err) {
debug('Error received: %O', err);
lookupResult.msg = 'An error occurred';
lookupResult.error = JSON.stringify(err);
lookupResult.success = false;
lookupResult.duration = Math.ceil((Date.now() - startTime));
// Return the result
callback(lookupResult);
} else{
// Check that the answer is a populated array
if (Array.isArray(answer.answer) && answer.answer.length) {
debug('The resolver [%s] provided the answer: %O', resolver.nameServer, answer);
// Populate lookup result object
lookupResult.answer = JSON.stringify(answer.answer);
lookupResult.ipAddress = module.exports.parseAnswer(answer.answer, {getIpAddress: true});
lookupResult.recursion = module.exports.parseAnswer(answer.answer, {getRecursion: true});
lookupResult.recordType = module.exports.parseAnswer(answer.answer, {getRecordType: true});
lookupResult.ttl = module.exports.parseAnswer(answer.answer, {getTTL: true});
lookupResult.msg = 'Success';
lookupResult.success = true;
let endTime = Date.now();
let duration = Math.ceil((endTime - startTime));
lookupResult.duration = duration;
} else{
debug('The resolver [%s] provided an empty answer: %O', resolver.nameServer, answer);
lookupResult.msg = 'Non-Existent Domain';
lookupResult.error = 'NXDomain';
lookupResult.success = false;
lookupResult.duration = Math.ceil((Date.now() - startTime));
}
// Return the result
callback(lookupResult);
}
});
}
},
parseAnswer(answer, options) {
debug('parseAnswer() called with ---> options: %O ---> answer: %O', options, answer);
// Validate the answer object has something to parse
//if (answer === []) {
if (Array.isArray(answer) && answer.length === 0) {
debug('"answer" is an empty array. Nothing to parse; returning "no_address"');
// No IP addresses, `answer` is an empty array
return('no_address');
}
try {
var response = '';
// Extract IP Address
if ((options===null) || (options.getIpAddress)) {
// Just get the IP address; i.e. the A record at the end.
for (let i = 0; i < answer.length; i++) {
if (Object.prototype.hasOwnProperty.call(answer[i], 'address')) {
response = answer[i].address;
}
}
} else if (options.getRecursion) {
// Get the whole nested recursion
for (let i = 0; i < answer.length; i++){
// Check if the answer element has a "data" property (which a CNAME record will have)
if(Object.prototype.hasOwnProperty.call(answer[i], 'data')){
response = response.concat(answer[i].name, ' --> ', answer[i].data, EOL);
// Check if the answer element has an "address" property (which an A record will have)
} else if (Object.prototype.hasOwnProperty.call(answer[i], 'address')) {
response = response.concat(answer[i].name, ' --> ', answer[i].address, EOL);
} else {
debug('Warning: There is an unhandled element [%s] in answer array: %O', i, answer[i]);
}
}
} else if (options.getRecordType) {
// Get the Resource Record type (CNAME, A, AAA, etc)
var rrtype = module.exports.resourceRecordType(answer[0].type);
// Add record type to the response object
response = rrtype;
debug('Resource Record Type: %s', rrtype);
} else if (options.getTTL) {
// Get the record's time-to-live value
response = answer[0].ttl;
debug('TTL: %s', answer[0].ttl);
}
debug('parseAnswer() returning: %s', response);
return(response);
} catch (error) {
debug('parseAnswer() caught an exception: %O', error);
return('error parsing answer');
}
},
isAddressUnique(ipAddress) {
debug('isAddressUnique(): Checking if %s has been seen before', ipAddress);
try {
// Loop through previously seen addresses
for (let i = 0; i < module.exports.isAddressUnique.addresses.length; i++) {
debug('Checking against: %s', module.exports.isAddressUnique.addresses[i]);
if (ipAddress === module.exports.isAddressUnique.addresses[i]) {
debug('A match has been found. Returning "False" as %s is not unique', ipAddress);
// IP Address found on the list, so it's not unique; return false
return(false);
}
}
debug('%s was not found on the list, so adding it...', ipAddress);
// We've gone through the whole list without finding the IP address, so add it to the list
module.exports.isAddressUnique.addresses.push(ipAddress);
// Return true as the address is unique
debug('The IP Address %s is unique', ipAddress);
return(true);
} catch (error) {
debug('isAddressUnique() caught an exception: %O', error);
return(false);
}
},
formatBytes(bytes, decimals = 2) {
if (bytes === 0) {
return '0 Bytes';
}
try {
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
} catch (error) {
debug('formatBytes() caught an exception: %O', error);
return(bytes + ' Bytes');
}
},
secondsToHms(seconds) {
try {
if (seconds) {
seconds = Number(seconds);
var h = Math.floor(seconds / 3600);
var m = Math.floor(seconds % 3600 / 60);
var s = Math.floor(seconds % 3600 % 60);
return ('0' + h).slice(-2) + ' hours, ' + ('0' + m).slice(-2) + ' minutes, ' + ('0' + s).slice(-2) + ' seconds';
} else {
return('<invalid>');
}
} catch (error) {
debug('secondsToHms() caught an exception: %O', error);
// an unexpected error occurred; return the original value
return(seconds + ' seconds');
}
},
getColourLevelDesc(level) {
const colourLevel = ['Colours Disabled', '16 Colours (Basic)', '256 Colours', '16 Million Colours (True Colour)'];
try {
if ((level > 3 || level < 0) || (typeof level === 'undefined')) {
// The level passed isn't in our range so detect it
const chalk = require('chalk');
level = chalk.supportsColor.level;
if (typeof level === 'undefined') {
level = 0;
}
}
return (colourLevel[level]);
} catch (error) {
debug('getColourLevelDesc() caught an exception: %O', error);
return('Unknown');
}
},
resourceRecordType(value) { // Takes the integer value returned in a DNS "answer" and returns the corresponding record name
const DNSResourceRecordsDatabase = configFilePath +'/' + 'DNSResourceRecords.json';
try {
debug('resourceRecordType() called with value: %s', value);
// Read in Resource Record database
const fs = require('fs');
debug('Reading in DNS Resource Records data from [%s]', DNSResourceRecordsDatabase);
let rawData = fs.readFileSync(DNSResourceRecordsDatabase);
let DNSRecords = JSON.parse(rawData);
// Default the return value to 'Unknown'
var returnValue = 'Unknown';
for (let i = 0; i < DNSRecords.RecordTypes.length; i++) {
//debug('Evaluating resource record database value [%s] against [%s]', DNSRecords.RecordTypes[i].value, value);
if (DNSRecords.RecordTypes[i].value === value) {
debug('Returning: %s', DNSRecords.RecordTypes[i].type);
// Set new return value of the record type found
returnValue = DNSRecords.RecordTypes[i].type;
// Stop processing the rest of the loop by skipping the array index forward to the end
i = DNSRecords.RecordTypes.length;
}
}
// Return the Record Type
return (returnValue);
} catch (error) {
debug('An error occurred in "ResourceRecordType(): %O"', error);
return ('----');
}
}
};
// Initialise address list array; stored as a property of the function object so it's values persist between function calls
module.exports.isAddressUnique.addresses = new Array();