Eric Bergman-Terrell's Blog

Node.js Programming Tip: How to Implement WHOIS
August 24, 2015

The WHOIS service provides basic information about a given website's registration information, including the following:

If you read the WHOIS data for your own website, you may be surprised to see your email address and, perhaps more concerning, your physical address. You can pay your registration provider extra to suppress this data. But in my experience, that data is never validated...

This website offers a WHOIS utility. Give it a try!

Like many things in Node.js, it's pretty easy to perform a WHOIS query. The following code is all that's necessary.

The "config" module just has the following code relevant to WHOIS:

exports.whoisConfig = {
    whoisHost: "{put your WHOIS hostname here}"
};

The WHOIS data may be retrieved in multiple chunks. As each chunk is retrieved, the text string is appended with the current chunk. Finally, when the entire WHOIS data has been retrieved, the specified callback is called with all of the data.

var net = require('net');
var config = require('../libs/config');

exports.getWhois = function getWhois(domain, callback) {
    var client = new net.Socket();

    client.whoisText = "";

    var port = 43;
    var host = config.whoisConfig.whoisHost;

    client.connect(port, host, function () {
        global.logger.info('WHOIS: CONNECTED TO: ' + host + ':' + port);
        global.logger.info('WHOIS: domain: ' + domain);

        // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client
        client.write(domain + '\r\n');
    });

    // Add a 'data' event handler for the client socket
    // data is what the server sent to this socket
    client.on('data', function (data) {
        client.whoisText += data.toString();

        global.logger.info('WHOIS: DATA: ' + data);
    });

    // Add a 'close' event handler for the client socket
    client.on('close', function () {
        callback(client.whoisText);

        global.logger.info('WHOIS: Connection closed');

        client.destroy();
        global.logger.info('WHOIS: client destroyed');
    });

    client.on('end', function() {
        global.logger.info('WHOIS: end');
    });
};

Is it a hack to store the WHOIS text in the client object? Yes. But that's a very convenient place to store it. The best approach would be to create a new object for the entire WHOIS data retrieval, and store the text there.

For WHOIS source code for Ruby on Rails and .NET, check out this blog post.

WHOIS living in my grass?

WHOIS living in my grass?

Keywords: Node.js, node, JavaScript, Express, WHOIS, net.Socket, port 43, Ruby on Rails, .NET

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015