-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
89 lines (76 loc) · 2.26 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
/* The net module is required to connect to the WHOIS server.
The dns module is required to resolve the whois server original name
*/
var net = require('net'),
dns = require('dns');
exports.query = function(domain, options, callback) {
/* Verify that the domain is supplied
Currently no validation is performed
*/
if(!domain) {
throw new Error("Domain name is mandatory eg: google.com");
}
// Query TLD.whois-servers.net
var server = domain.substring(domain.lastIndexOf( '.' ) + 1) +'.whois-servers.net';
/* By default user standard port 43
*/
var port = 43;
/* When we query the WHOIS it searches in all of its records
so by default we query
For more information see Verign's documentation here: http://bit.ly/IUvugY
*/
var command = 'domain ' + domain + '\r\n';
/* We check if the user sent custom options
*/
if (arguments.length == 2) {
if (Object.prototype.toString.call(arguments[1]) == "[object Function]") {
callback = options;
}
} else {
/* If the user specifies the WHOIS server
we don't send the "domain" command.
*/
server = options.server;
port = options.port;
command = domain + '\r\n';
}
/* Usually all WHOIS servers resolve to a CNAME
e.g. com.whois-servers.net resolves to whois.verisign-grs.com
e.g. uk.whois-servers.net resolves to whois.nic.uk
*/
dns.resolveCname(server, function(err, addresses) {
var host = '';
/* Sometimes WHOIS servers are not using
any aliases so in case we get any errors we use the original address
*/
if(!err) {
host = addresses[0];
} else {
host = server;
}
/* Socket data might come in chunks,
so we need to put it together
*/
var whoisdata = '';
var socket = net.createConnection(port, host, function() {
/* Once connection is established we send our command
*/
socket.write(command,'ascii');
});
/* Encoding is set to ASCII so we can
understand the receive data
*/
socket.setEncoding('ascii');
/* Wait for data to arrive
*/
socket.on('data', function(data) {
whoisdata = whoisdata + data;
}).on('close', function(had_error) {
if(had_error) {
callback('WHOIS server is not responding, domain is not registered or this TLD has no whois server.');
} else {
callback(whoisdata);
}
});
});
}