-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
386125d
commit 4d9b88d
Showing
1 changed file
with
20 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |