Skip to content

Instantly share code, notes, and snippets.

@theTd
Last active May 9, 2019 02:49
Show Gist options
  • Save theTd/d6869d4daec3004cd563db53712eff79 to your computer and use it in GitHub Desktop.
Save theTd/d6869d4daec3004cd563db53712eff79 to your computer and use it in GitHub Desktop.
// https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest
const http = require('http');
const https = require('https');
const url = require('url')
const fs = require('fs');
const readline = require('readline');
function parse(input) {
const writeStream = fs.createWriteStream("output");
const rl = readline.createInterface({
input: input
});
let counter = 0;
rl.on('error', error => {
console.log("error: " + error);
});
rl.on('line', line => {
if (line.startsWith('#')) return;
let columns = line.split('|');
if (columns.length != 7) {
console.error("unexpected line: " + line);
} else {
if (columns[2] != 'ipv4' || columns[1] != 'CN') {
return;
}
if (columns[6] != 'allocated' && columns[6] != 'assigned') {
console.warn("unexpected line: " + line);
return;
}
let ip = columns[3];
let mask = columns[4];
let cidrmask = 32;
while ((mask & 1) == 0) {
mask >>= 1;
cidrmask--;
if (mask === 0) {
console.warn("unexpected mask: " + columns[4]);
}
}
writeStream.write(ip + "/" + cidrmask + "\n");
counter++;
}
});
rl.on('close', () => {
console.log("totally " + counter + " line(s)");
writeStream.close();
});
}
function fromHttp(_url) {
let parsed;
try {
parsed = url.parse(_url);
} catch (err) {
console.error("invalid url: " + err);
return;
}
if (parsed.protocol == 'https:') {
https.get(parsed.href, res => parse(res));
} else if (parsed.protocol == 'http:') {
http.get(parsed.href, res => parse(res));
} else {
console.error("unsupported protocol: " + parsed.protocol);
}
}
function fromFile(filename) {
parse(fs.createReadStream(filename));
}
// fromFile('delegated-apnic-latest.txt');
fromHttp('https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment