Created
May 10, 2017 16:59
-
-
Save r0yfire/0283d348e021fc7a4c7b88c1260ed224 to your computer and use it in GitHub Desktop.
Bulk IP Whois parser for Nodejs
This file contains hidden or 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
| 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