-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathevil-dns.js
130 lines (107 loc) · 2.34 KB
/
evil-dns.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
var dns = require('dns'),
net = require('net'),
dnsLookup = dns.lookup,
domains = [];
/**
* Override core DNS lookup function
**/
dns.lookup = function(domain, options, callback) {
var i;
if (arguments.length === 2) {
callback = options;
options = {};
}
var 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++) {
var 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);
};
/**
* Add a domain to the override list
*
* @param domain String or RegExp matching domain
* @param ip String IPv4 or IPv6
**/
function add(domain, ip) {
var 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) {
var 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) {
var 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
};