-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
70 lines (60 loc) · 1.8 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
const { TCPClient, UDPClient, DOHClient, TLSClient } = require('./client');
const { TCPServer, UDPServer, DOHServer, TLSServer, ServerUtilities } = require('./server');
const { EventEmitter } = require('events');
const dnsPacket = require('dns-packet');
class DDNS extends EventEmitter {
constructor(options) {
super();
Object.assign(this, {
client: UDPClient,
port: 53,
protocol: 4,
timeout: 1,
nameServers: [
'8.8.8.8',
'1.1.1.1'
],
rootServers: [
'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm'
].map(x => `${x}.root-servers.net`)
}, options);
};
#query = function(name, type, cls) {
const { port, nameServers, client, protocol, timeout } = this;
return Promise.race(nameServers.map(address => {
return new Promise((resolve,reject) => {
client.Query(address,[{type: type, name: name, class:cls}],dnsPacket.RECURSION_DESIRED,port,protocol,timeout).then((result)=>{
resolve(result);
}).catch((error)=>{
reject("error: "+error);
})
});
}));
};
Resolve = function(name, type = 'ANY', cls = 'IN') {
return this.#query(name, type, cls);
};
ResolveA = function(name) {
return this.#query(name, 'A', 'IN');
};
ResolveAAAA = function(name) {
return this.#query(name, 'AAAA', 'IN');
};
ResolveMX = function(name) {
return this.#query(name, 'MX', 'IN');
};
ResolveCNAME = function(name) {
return this.#query(name, 'CNAME', 'IN');
};
}
DDNS.TCPClient = TCPClient;
DDNS.UDPClient = UDPClient;
DDNS.DOHClient = DOHClient;
DDNS.TLSClient = TLSClient;
DDNS.UDPServer = UDPServer;
DDNS.TCPServer = TCPServer;
DDNS.DOHServer = DOHServer;
DDNS.TLSServer = TLSServer;
DDNS.ServerUtilities = ServerUtilities;
module.exports = DDNS;