-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (57 loc) · 1.71 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
var dns = require('native-dns');
function srv(hostname) {
this.hostname = hostname
this._server = '8.8.8.8'
this.port = 53
this.protocol = 'udp'
this._timeout = 1000
this._on = {}
this._type = 'A'
}
srv.prototype.at = function(server) {
this._server = server;
return this;
};
srv.prototype.type = function(type) {
this._type = type;
return this;
};
srv.prototype.timeout = function(timeout) {
this._timeout = timeout;
return this;
};
srv.prototype.on = function(event, fn) {
this._on[event] != undefined ? this._on[event].push(fn) : this._on[event] = [fn];
return this;
}
srv.prototype.query = function(callback) {
var question = dns.Question({
name : this.hostname,
type : this._type
})
var start = new Date().getTime();
var req = dns.Request({
question: question,
server: { address: this._server, port: this.port, type: this.protocol },
timeout: this._timeout,
});
req.on('timeout', function () {
if (this._on['timeout']) { this._on['timeout'].forEach(function(fn) { fn() }) }
}.bind(this));
req.on('message', function (err, answer) {
if (err) { if (this._on['err']) { this._on['err'].forEach(function(fn) { fn(err) }) } return }
// if (this._type === 'SRV') {
// var additional = _.object(_.pluck(answer.additional, 'name'), answer.additional);
// answer.answer.forEach(function (a) { a.address = additional[a.target].address });
// }
if (typeof callback === 'function') callback(answer)
}.bind(this));
req.on('end', function () {
var delta = (new Date().getTime()) - start;
if (this._on['end']) { this._on['end'].forEach(function(fn) {fn(delta.toString())}) }
}.bind(this));
req.send();
}
module.exports = function(hostname) {
return new srv(hostname)
};