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
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021
Vault (Desktop) Version 0.72 ReleasedOctober 6, 2021
EBT Compass is Now Available for Android DevicesJune 2, 2021