Skip to content

Commit

Permalink
Ping ip address + Validate result
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardonunesdev committed Sep 13, 2017
1 parent d5c1551 commit 1586afe
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,51 @@ const getDomainIp = (domain) => {
return new Promise((resolve, reject) => {
dns.lookup(domain, { family: 4 }, (error, ip, family) => {
if (error) {
return resolve({ success: false, error: error.message });
return resolve({ success: false, error: error });
}
return resolve({ success: true, ip: ip });
return resolve({ success: true, ip: ip, family: family });
});
});
};

const pingIp = (ip) => {
return new Promise((resolve, reject) => {
ping.sys.probe(ip, (alive) => {
return resolve({ success: true, alive: alive });
});
});
};

const domainPing = (domain) => {
return new Promise((resolve, reject) => {
let data = {
success: null
};

getDomainIp(domain)
.then((res) => {
return resolve(res);
if (res.success) {
data.ip = res;
return pingIp(res.ip);
} else {
data.success = false;
data.error = res.error;
return reject(data);
}
})
.then((res) => {
if (res.success) {
data.ping = res;
data.success = true;
return resolve(data);
} else {
data.success = false;
data.error = res.error;
return reject(data);
}
})
.catch((error) => {
return reject({ success: false, error: error.message });
return reject({ success: false, error: error });
});
});
};
Expand Down

0 comments on commit 1586afe

Please sign in to comment.