All files / subquest index.js

92.31% Statements 60/65
76.19% Branches 16/21
88.24% Functions 15/17
93.44% Lines 57/61
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164    4x 4x 4x 4x 4x 4x     4x     50x     50x 50x               4x 3x           4x   2x 1x 1x       2x 2x 2x 2x 2x 2x       2x     2x         2x                   4x   3x     3x 2x       1x             1x 1x     1x                 4x 1x       4x   4x         5x     4x     4x 1x 1x       3x 1x 1x       2x     2x         2x     2x         1x 1x       1x     1x 50x 50x       50x 1x          
'use strict'
 
const dns = require('dns')
const	util = require('util');
const async = require('async');
const fs = require('fs');
const path = require('path')
const EOL = /\r?\n/g;
 
// Send DNS requests for current subdomain
const probeDNS = (subdomain, tld, cb) => {
 
	// Build the domain name
	const domain = `${subdomain}.${tld}`;
 
	// Run the resolve request
	dns.resolve(domain, 'A', err => {
		cb(err, domain);
	});
}
 
/**
 * Return the default DNS servers
 * @return {array} An array of DNS used by the module
 */
exports.getDefaultResolvers = function() {
	return fs.readFileSync(
		path.join(__dirname, 'resolvers.txt')
	).toString().trim().split(EOL)
}
 
// Check whether a dns server is valid.
exports.isValidDnsServer = function(dnsServer, timeout, cb) {
	// Ensure arguments are good
	if( typeof timeout === 'function' ) {
		cb = timeout
		timeout = 4000
	}
 
	// Set custom callback handler
	let called = false, timeoutPromise = null
	let dnsCallback = (err) => {
		clearTimeout(timeoutPromise);
		Iif(called) { return }
		called = true
		cb(err)
	};
 
	// Force to use this dns server
	dns.setServers([dnsServer]);
 
	// Set a custom timeout for DNS request
	timeoutPromise = setTimeout(_ => {
		dnsCallback(new Error('Request timeout exceeded!'))
	}, timeout);
 
	// Try to resolve google.com
	dns.resolve4('www.google.com', dnsCallback);
}
 
/**
 * Get the best resolver in the following order:
 * 1. User supplied.
 * 2. From our list.
 * @param  {string} server The DNS server address as string
 * @param  {function} callback The callback to run once has done
 */
exports.getResolvers = function(server, callback){
	// Results array
	let dnsServers = exports.getDefaultResolvers();
 
	// Return default dns servers
	if (typeof server === 'undefined') {
		callback(dnsServers);
	}
	
	// Return default dns servers
	else Iif (typeof server === 'function') {
		callback = server;
		callback(dnsServers);
	}
 
	// Validate custom DNS server than add to resolvers list
	else {
		exports.isValidDnsServer(server, 4000, (err) => {
			Iif (err === null && dnsServers.indexOf(server) === -1) {
				dnsServers.unshift(server)
			}
			callback(dnsServers)
		});
	}
}
 
/**
 * Get the dictionary files names
 * @return {array} Array of file names
 */
exports.getDictionaryNames = function(){
	return fs.readdirSync(path.join(__dirname, 'dictionary'));
}
 
// Send requests to a DNS resolver and find valid sub-domains
exports.getSubDomains = function(options, callback = () => {}) {
	// Default subdomain scan options
	let defaults = {
		dictionary: 'top_50'
	};
 
	// Clean undefined options
	Object.keys(options).forEach(key => options[key] === undefined && delete options[key]);
 
	// Extend default options with user defined ones
	options = Object.assign({}, defaults, options);
 
	// Exit if no host option
	if(!options.host)  {
		callback(new Error('The host property is missing.'))
		return;
	};
 
	// Optionally run a bing search
	if(options.bingSearch === true){
		var bingSearch = require('./lib/bingSearch.js');
		return bingSearch.find(options.host, callback)
	}
 
  // Get the resolvers list
	exports.getResolvers(options.dnsServer, (servers) => {
 
		// Set new servers list for the requests
		dns.setServers(servers);
 
    // Init dictionary array
    var dictionary;
 
    try {
 
      // Get dictionary lines
      dictionary = fs.readFileSync(
        path.join(__dirname, `dictionary/${options.dictionary}.txt`)
      );
 
    } catch (e) {
      callback(new Error(`The dictionary ${options.dictionary} was not found, make sure it exists in the dictionary folder.`));
      return;
    }
 
    // Get dictionary content and split lines in array rows
    dictionary = dictionary.toString().trim().split(EOL);
 
		// Probe each subdomain
		async.mapSeries(dictionary, (subdomain, cb) => {
			probeDNS(subdomain, options.host, (err, res) => {
				cb(null, err ? null : res)
			})
		}, (err, results) => {
			// Filter from any empty result
			results = results.filter(v => v)
			callback(null, results);
		})
 
	})
}