forked from JamesHight/node-evil-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evil-dns-promises.js
158 lines (130 loc) · 3.01 KB
/
evil-dns-promises.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
var dns = require('dns'),
net = require('net'),
dnsLookup = dns.lookup,
dnsLookupPromises = dns.promises.lookup,
domains = [];
/**
* Override core DNS lookup function
**/
dns.lookup = function(domain, options, callback) {
let i;
if (arguments.length === 2) {
callback = options;
options = {};
}
let family = (typeof(options) === 'object') ? options.family : options;
if (family) {
family = +family;
if (family !== 4 && family !== 6) {
throw new Error('invalid argument: `family` must be 4 or 6');
}
}
for (i = 0; i < domains.length; i++) {
const entry = domains[i];
if (domain.match(entry.domain)) {
if (!family || family === entry.family) {
return callback(null, entry.ip, entry.family);
}
}
}
return dnsLookup.call(this, domain, options, callback);
};
/**
* Override core promises DNS lookup function
**/
dns.promises.lookup = function(domain, options) {
let i;
let family = (typeof(options) === 'object') ? options.family : options;
if (family) {
family = +family;
if (family !== 4 && family !== 6) {
return Promise.reject(new Error('invalid argument: `family` must be 4 or 6'));
}
}
for (i = 0; i < domains.length; i++) {
const entry = domains[i];
if (domain.match(entry.domain)) {
if (!family || family === entry.family) {
return Promise.resolve({address: entry.ip, family: entry.family});
}
}
}
return dnsLookupPromises.call(this, domain, options);
}
/**
* Add a domain to the override list
*
* @param domain String or RegExp matching domain
* @param ip String IPv4 or IPv6
**/
function add(domain, ip) {
let entry = { ip: ip };
if (net.isIPv4(entry.ip)) {
entry.family = 4;
}
else if (net.isIPv6(entry.ip)) {
entry.family = 6;
}
else {
throw new Error('Invalid ip: ' + entry.ip);
}
if (domain instanceof RegExp) {
entry.source = domain;
entry.domain = domain;
}
else {
entry.source = domain;
entry.domain = createRegex(domain);
}
domains.push(entry);
}
/**
* Remove a domain from the override list
*
* @param domain String or RegExp
* @param ip String optional, if not set all domains equivalent domain will be removed
**/
function remove(domain, ip) {
let i;
for (i = 0; i < domains.length; i++) {
if (domain instanceof RegExp) {
if (domains[i].source instanceof RegExp
&& domains[i].source.source === domain.source
&& (!ip || ip === domains[i].ip)) {
domains.splice(i, 1);
i--;
}
}
else {
if (domains[i].source === domain && (!ip || ip === domains[i].ip)) {
domains.splice(i, 1);
i--;
}
}
}
}
/**
* Remove all domains from the override list
**/
function clear() {
domains = [];
}
function createRegex(val) {
let parts = val.split('*'),
i;
for (i = 0; i < parts.length; i++) {
parts[i] = regexEscape(parts[i]);
}
val = parts.join('.*');
val = '^' + val + '$';
return new RegExp(val, 'i');
}
function regexEscape(val) {
return val.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
module.exports = {
add: add,
remove: remove,
clear: clear,
domains: domains
};