Act

juansgaitan/dns-extractor

  • Builds
  • latest 0.0.23 / 2017-11-10
  • Created 2017-11-10
  • Last modified 2017-11-14
  • grade 6

Description

Gets the IP addresses & *Reverse DNS from an input url. *Reverse DNS is IP address to domain name mapping - the opposite of forward (normal) DNS which maps domain names to IP addresses.


API

To run the act, send a HTTP POST request to:

https://api.apify.com/v2/acts/juansgaitan~dns-extractor/runs?token=<YOUR_API_TOKEN>

The POST payload will be passed as input for the act. For more information, read the docs.


Example input

Content type: application/json; charset=utf-8

{ 
  "url": "apify.com/" 
}

Source code

Based on the apify/actor-node-puppeteer Docker image (see docs).

const Apify = require('apify');
const { URL } = require('url');
const { promisify } = require('util');
const { lookup, resolveAny, reverse } = require('dns');

const { log, dir } = console;
const [dnsLookup, dnsResolve, dnsReverse] = [
  lookup,
  resolveAny,
  reverse,
].map(promisify);

Apify.main(async () => {
  const input = await Apify.getValue('INPUT');
  log(input);

  const { url } = input;
  if (!url) throw Error('No input URL');
  log('Input URL:', url);

  const protocol = 'http://';
  const parsedUrl = `${/http/.test(url) ? '' : protocol}${url}`;

  const { host, origin } = new URL(parsedUrl);
  log('URL host:', host);
  log('URL origin:', origin);

  const output = { origin, host };

  let shouldTryReverse = true;
  try {
    Object.assign(output, {
      hostIp: await dnsLookup(host),
      hostResolve: await dnsResolve(host),
    });
  } catch (error) {
    log(error.message);
    Object.assign(output, { error: error.message });
    shouldTryReverse = false;
  }

  if (shouldTryReverse) {
    try {
      Object.assign(output, {
        hostReverse: await dnsReverse(output.hostIp.address),
      });
    } catch (error) {
      log('Error DNS Reverse', error.message);
    }
  }

  log('Output result:');
  dir(output);
  await Apify.setValue('OUTPUT', output);
});