-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.js
97 lines (83 loc) · 2.2 KB
/
index.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
const {
TCPServer,
UDPServer,
DOHServer,
createTCPServer,
createUDPServer,
createDOHServer,
createServer,
} = require('./server');
const EventEmitter = require('events');
/**
* [DNS description]
* @docs https://tools.ietf.org/html/rfc1034
* @docs https://tools.ietf.org/html/rfc1035
*/
class DNS extends EventEmitter {
constructor(options) {
super();
Object.assign(this, {
port : 53,
retries : 3,
timeout : 3,
recursive : true,
resolverProtocol : 'UDP',
nameServers : [
'8.8.8.8',
'114.114.114.114',
],
rootServers: [
'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm',
].map(x => `${x}.root-servers.net`),
}, options);
}
/**
* resolve
* @param {*} domain
* @param {*} type
* @param {*} cls
*/
resolve(domain, type = 'ANY', cls = DNS.Packet.CLASS.IN, options = {}) {
const { port, nameServers, resolverProtocol = 'UDP' } = this;
const createResolver = DNS[resolverProtocol + 'Client'];
return Promise.race(nameServers.map(address => {
const resolve = createResolver({ dns: address, port });
return resolve(domain, type, cls, options);
}));
}
resolveA(domain, clientIp) {
return this.resolve(domain, 'A', undefined, clientIp);
}
resolveAAAA(domain) {
return this.resolve(domain, 'AAAA');
}
resolveMX(domain) {
return this.resolve(domain, 'MX');
}
resolveCNAME(domain) {
return this.resolve(domain, 'CNAME');
}
resolvePTR(domain) {
return this.resolve(domain, 'PTR');
}
resolveDNSKEY(domain) {
return this.resolve(domain, 'DNSKEY');
}
resolveRRSIG(domain) {
return this.resolve(domain, 'RRSIG');
}
}
DNS.TCPServer = TCPServer;
DNS.UDPServer = UDPServer;
DNS.DOHServer = DOHServer;
DNS.createUDPServer = createUDPServer;
DNS.createTCPServer = createTCPServer;
DNS.createDOHServer = createDOHServer;
DNS.createServer = createServer;
DNS.TCPClient = require('./client/tcp');
DNS.DOHClient = require('./client/doh');
DNS.UDPClient = require('./client/udp');
DNS.GoogleClient = require('./client/google');
DNS.Packet = require('./packet');
module.exports = DNS;