-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhois.js
47 lines (47 loc) · 1.57 KB
/
whois.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
const net = require('net')
function query(text, options) {
return new Promise((resolve, reject) => {
let client = new net.Socket();
client.connect({
host: 'whois.dn42',
port: 43
});
client.on('connect', function () {
client.write(text + '\r\n');
})
let ans = ''
client.on('data', function(data) {
ans += data.toString()
})
client.on('close', function() {
if(options && options.raw) {
resolve(ans);
} else {
let result = {};
let currentKey = text;
for(let i of ans.split('\n')) {
if(i.startsWith('%')) {
if(i.startsWith('% Information related to')) {
currentKey = i.substring(26, i.length - 2)
result[currentKey] = {}
}
} else if(i.includes(':')) {
let a = i.split(':', 2)
result[currentKey][a[0]] = result[currentKey][a[0]] || [];
result[currentKey][a[0]].push(a[1].trim())
}
}
if(options && options.last) {
resolve(result[currentKey])
} else {
resolve(result)
}
}
})
})
}
module.exports = {
query: query,
queryRaw: (text) => query(text, {raw: true}),
queryLast: (text) => query(text, {last: true})
}