-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (43 loc) · 1.28 KB
/
index.js
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
const dns= require("dns") //npm install dns
function getHostnamebyIp(ip){
const MAX_TRIES=10
const hostnameDispacher = {
result:null,
tries:0,
execute: function () {
const self = this;
return new Promise(function (resolve, reject) {
dns.reverse(ip,(err,hostnames)=>{
if((empty(hostnames)||hostnames==undefined || err) && self.tries<MAX_TRIES){
self.tries++;
self.execute().then(function () {
resolve(true);
});
}
else if(self.tries==MAX_TRIES){
self.result= ({message:"Max tries reached to get hostname (10)", hostname: null})
resolve(true)
}
else{
self.result= ({hostname: hostnames[0]})
resolve(true)
}
})
});
}
}
return new Promise((resolve,reject)=>{
hostnameDispacher.execute().then(res=>{
resolve(hostnameDispacher.result)
})
})
}
function empty(data){
if(typeof(data) == 'number' || typeof(data) == 'boolean') return false;
if(typeof(data) == 'undefined' || data === null) return true;
if(typeof(data.length) != 'undefined') return data.length == 0;
}
//Test (Random IP address I found)
getHostnameByIp("137.152.208.121").then(res=>{
console.log(res)
});