Skip to content

Commit

Permalink
Refactored + Get domain ip
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardonunesdev committed Sep 13, 2017
1 parent 386125d commit 4d9b88d
Showing 1 changed file with 20 additions and 62 deletions.
82 changes: 20 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,30 @@
'use strict';

let dns = require('dns');
let ping = require('ping');
let Promise = require('bluebird');
let request = require('request');
let fs = require('fs');
const Promise = require('bluebird');
const ping = require('ping');
const dns = require('dns');

let uniq = (arr) => { return Array.from(new Set(arr)); };
let ouputData = (data) => { console.log(data.domain+"\t"+data.ip+"\t"+data.ping+"\t"+data.status); };
let debug = (msg) => { console.log(msg); };

let domains = uniq(fs.readFileSync('domains.txt').toString().split("\n"));

let pingDomain = (data) => {
const getDomainIp = (domain) => {
return new Promise((resolve, reject) => {
if (data.tries >= 5) {
ouputData(data);
return resolve();
}

data.tries++;

dns.lookup(data.domain, { family: 4 }, (error1, ip, family) => {
if (error1) {
data.ip = 'failed';
return resolve(pingDomain(data));
dns.lookup(domain, { family: 4 }, (error, ip, family) => {
if (error) {
return resolve({ success: false, error: error.message });
}

data.ip = ip;

ping.sys.probe(data.ip, (isAlive) => {

data.ping = (isAlive ? 'yes' : 'no');

// TODO: Beautify duplicate code

request('http://'+data.domain, (error2, response, body) => {
if (error2) {
request('https://'+data.domain, (error3, response, body) => {
if (error3) {
data.status = 'failed';
ouputData(data);
return resolve();
}

data.status = response.statusCode;
ouputData(data);
return resolve();
});
} else {
data.status = response.statusCode;
ouputData(data);
return resolve();
}
});
});
return resolve({ success: true, ip: ip });
});
});
};

debug('Pinging '+domains.length+' domain(s)'+"\n");

Promise.map(domains, (domain) => {
if (domain && (typeof domain === 'string') && (domain.length > 0)) {
let data = { domain: domain, ip: '', ping: '', status: '', tries: 0 };
return pingDomain(data);
}
})
.then(() => {
debug("\n"+'Done');
const domainPing = (domain) => {
return new Promise((resolve, reject) => {
getDomainIp(domain)
.then((res) => {
return resolve(res);
})
.catch((error) => {
return reject({ success: false, error: error.message });
});
});
};

module.exports = domainPing;

0 comments on commit 4d9b88d

Please sign in to comment.