Skip to content

Instantly share code, notes, and snippets.

@r0yfire
Created May 10, 2017 16:59
Show Gist options
  • Select an option

  • Save r0yfire/0283d348e021fc7a4c7b88c1260ed224 to your computer and use it in GitHub Desktop.

Select an option

Save r0yfire/0283d348e021fc7a4c7b88c1260ed224 to your computer and use it in GitHub Desktop.
Bulk IP Whois parser for Nodejs
const LineByLineReader = require('line-by-line');
//
// Read bulk whois data from file
//
const reader = new LineByLineReader('whois_dump.txt');
let whoisdata = {
'organization': [],
'inetnum': [],
'inet6num': [],
'person': [],
'as-set': [],
'as-block': [],
'aut-num': [],
'domain': [],
};
let data = [];
reader.on('line', line => {
if (line.length < 2 && data.length > 2) {
let item = parse(data.join('\n'));
let type = Object.keys(item)[0];
if (whoisdata[type]) {
whoisdata[type].push(item)
}
data = [];
}
if (line.length > 1 && line.substring(0) !== '#') {
data.push(line);
}
});
reader.on('end', () => {
console.log(JSON.stringify(whoisdata, null, 2));
});
const parse = (data) => {
let whoisObj = {};
let str = data.toString();
let lines = str.split(/(\r?\n)/g);
let lastKey = '';
for (let i in lines) {
let line = lines[i];
if (line && line.trim() && line.indexOf('%') != 0 && line.indexOf('#') != 0) {
let dataValuePair = [lastKey, line.trim()];
if (line.includes(': ') && line.substring(0, 1) !== ' ') {
dataValuePair = line.split(":");
}
if (dataValuePair.length >= 2) {
let name = dataValuePair[0].trim()
, value = dataValuePair.slice(1).join(":").trim();
lastKey = name;
if (whoisObj[name] instanceof Array) {
whoisObj[name].push(value);
} else {
if (whoisObj[name] && whoisObj[name] != value) {
//if there is several values with same name organizing them as array
let tmp = whoisObj[name];
whoisObj[name] = [];
whoisObj[name].push(tmp);
whoisObj[name].push(value);
}
else
whoisObj[name] = value;
}
}
else if (Array.isArray(whoisObj[lastKey])) {
whoisObj[lastKey].push(line.trim())
}
}
}
return whoisObj;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment