forked from lsongdev/node-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (74 loc) · 1.79 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
const {
TCPServer,
UDPServer,
createTCPServer,
createUDPServer,
} = 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,
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);
}
/**
* query
* @param {*} questions
*/
query(name, type, cls) {
const { port, nameServers } = this;
const { Client: createResolver } = DNS;
return Promise.race(nameServers.map(address => {
const resolve = createResolver({ dns: address, port });
return resolve(name, type, cls);
}));
}
/**
* resolve
* @param {*} domain
* @param {*} type
* @param {*} cls
*/
resolve(domain, type = 'ANY', cls = DNS.Packet.CLASS.IN) {
return this.query(domain, DNS.Packet.TYPE[type], cls);
}
resolveA(domain) {
return this.resolve(domain, 'A');
}
resolveAAAA(domain) {
return this.resolve(domain, 'AAAA');
}
resolveMX(domain) {
return this.resolve(domain, 'MX');
}
resolveCNAME(domain) {
return this.resolve(domain, 'CNAME');
}
}
DNS.TCPServer = TCPServer;
DNS.UDPServer = UDPServer;
DNS.createServer = createUDPServer;
DNS.createUDPServer = createUDPServer;
DNS.createTCPServer = createTCPServer;
DNS.DoT = require('./client/tcp');
DNS.DoH = require('./client/doh');
DNS.Client = require('./client/udp');
DNS.Google = require('./client/google');
DNS.Packet = require('./packet');
module.exports = DNS;