Created
December 4, 2016 10:47
-
-
Save wen-long/4e3c58d7f5cf30ef491f5d39ad469ce9 to your computer and use it in GitHub Desktop.
结合公司用的 pac 使用,获取 apnic 的 geoip 的中国段
This file contains 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
var request = require('sync-request'); | |
function convertAddress(ipchars) { | |
var bytes = ipchars.split('.'); | |
var result = (((bytes[0] & 0xff) << 24) | | |
((bytes[1] & 0xff) << 16) | | |
((bytes[2] & 0xff) << 8) | | |
(bytes[3] & 0xff)) >>> 0; | |
return result; | |
} | |
function convertNetwork(shift) { | |
var result = (0xffffffff << (32-shift)) >>> 0; | |
return result; | |
} | |
var res = request('GET', 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'); | |
var s = res.getBody().toString() | |
var vec = s.split("\n") | |
var regex = new RegExp("apnic\\|[A-Z]{2}\\|ipv4.*"); | |
//console.log(regex) | |
console.log("var lists_my = [") | |
for (index in vec) | |
{ | |
if (vec[index].match(regex)) { | |
var vec2 = vec[index].split("|") | |
if (vec2[1] == "CN") { | |
var ip_s = vec2[3] | |
var net_s = vec2[4] | |
var ip = convertAddress(ip_s) | |
var tmp = 32 - Math.log2(net_s); | |
var net = convertNetwork(tmp) | |
console.log("[" + ip + ", " + net + "],") | |
} | |
} | |
} | |
console.log("]") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment