Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs');
- var ip_mapping = {
- city_ip: {},
- contry_ip: {},
- isp_name: [],
- city_name: [],
- province_name: [],
- query_ip : function(ip) {
- var result = this.query_ip_table(ip, this.contry_ip, this.contry_mapping);
- if (result == 'CN') {
- result = this.query_ip_table(ip, this.city_ip, this.city_mapping);
- if (result == undefined) {
- return 'CN';
- } else {
- var province = this.province_name[result[0]];
- var city = this.city_name[result[1]];
- var isp = this.isp_name[result[2]];
- return province + ":" + city + ":" + isp;
- }
- } else {
- return result;
- }
- },
- query_ip_table: function(ip, table) {
- var ip_long = this.ip2long(ip);
- var ip_seg1 = ip_long >> 16;
- var sub_table = table[ip_seg1];
- if (sub_table != undefined) {
- var ips = ip_long & 0xffff;
- var index = this.binary_search(sub_table, ips);
- if (index >= sub_table.length) index = sub_table.length - 1;
- var data = undefined;
- while (true) {
- data = sub_table[index];
- if (data[0] <= ips && data[1] >= ips) {
- break;
- } else {
- index = index - 1;
- }
- if (index < 0) {
- return undefined;
- }
- }
- return data[2];
- } else {
- return undefined;
- }
- },
- binary_search: function(list, item) {
- var index = list.length - 1
- , last = list.length - 1
- , first = 0;
- if (list.length == 0) return 0;
- if (item >= list[last][0]) return last + 1;
- if (item <= list[0][0]) return 0;
- while (true) {
- index = Math.floor((first + last) / 2);
- if (item == list[index][0]) {
- break;
- } else if (item > list[index][0]) {
- first = index;
- } else {
- last = index;
- }
- if ((last - first) <= 1) {
- break;
- }
- }
- while (item == list[index + 1][0]) {
- index = index + 1;
- }
- return index;
- },
- readlines: function (input, func) {
- var remaining = '';
- input.on('data', function(data) {
- remaining += data;
- var index = remaining.indexOf('\n');
- while (index > -1) {
- var line = remaining.substring(0, index);
- remaining = remaining.substring(index + 1);
- func(line);
- index = remaining.indexOf('\n');
- }
- });
- input.on('end', function() {
- if (remaining.length > 0) {
- func(remaining);
- }
- });
- },
- ip2long: function(ip) {
- var ip_seg = ip.split('.');
- var buff = new Buffer(4);
- for (var i in ip_seg) {
- buff.writeUInt8(parseInt(ip_seg[i]), i);
- }
- return buff.readUInt32BE(0);
- },
- break_ip_seg: function(ip_start, ip_end) {
- var seg1 = ip_start >> 16;
- var seg2 = ip_end >> 16;
- if (seg1 == seg2) {
- return [[ip_start, ip_end]];
- }
- var result = [];
- for (var i = seg1; i<= seg2; i++) {
- var start = i << 16;
- var end = (i << 16) + 0xffff;
- if (i == seg1) {
- start = ip_start;
- }
- if (i == seg2) {
- end = ip_end ;
- }
- result.push([start, end]);
- }
- return result;
- },
- build_mapping: function(city, contry) {
- var isp_name_mapping = {};
- var city_name_mapping = {};
- var province_name_mapping = {};
- var city_file = fs.createReadStream(city);
- this.readlines(city_file, function(line) {
- var line_values = line.trim().split(',');
- var ip_segs = ip_mapping.break_ip_seg(ip_mapping.ip2long(line_values[6]), ip_mapping.ip2long(line_values[7]));
- // id, ip_start,ip_end,province,city,isp,ip_start_str,ip_end_str
- for (var j in ip_segs) {
- var values = line_values;
- values[1] = ip_segs[j][0];
- values[2] = ip_segs[j][1];
- var ip_seg1 = parseInt(values[1]) >> 16;
- var province_id = ip_mapping.get_hash_id(province_name_mapping, ip_mapping.province_name, values[3]);
- var city_id = ip_mapping.get_hash_id(city_name_mapping, ip_mapping.city_name, values[4]);
- var isp_id = ip_mapping.get_hash_id(isp_name_mapping, ip_mapping.isp_name, values[5]);
- var offset = ip_mapping.insert_ip_seg(ip_mapping.city_ip, ip_seg1, parseInt(values[1]), parseInt(values[2]), [province_id, city_id, isp_id])
- }
- });
- var contry_file = fs.createReadStream(contry);
- this.readlines(contry_file, function(line) {
- var line_values = line.trim().split(',');
- var ip_segs = ip_mapping.break_ip_seg(ip_mapping.ip2long(line_values[4]), ip_mapping.ip2long(line_values[5]));
- // id, ip_start,ip_end,contry,ip_start_str,ip_end_str
- for (var j in ip_segs) {
- var values = line_values;
- values[1] = ip_segs[j][0];
- values[2] = ip_segs[j][1];
- var ip_seg1 = parseInt(values[1]) >> 16;
- var offset = ip_mapping.insert_ip_seg(ip_mapping.contry_ip, ip_seg1, parseInt(values[1]), parseInt(values[2]), values[3]);
- }
- });
- },
- insert_ip_seg: function(ip_list, ip_seg, ip1, ip2, data) {
- var ip1s = ip1 & 0xffff;
- var ip2s = ip2 & 0xffff;
- if (ip_list[ip_seg] == undefined) {
- ip_list[ip_seg] = [];
- ip_list[ip_seg].push([ip1s, ip2s, data]);
- } else {
- var index = ip_mapping.binary_search(ip_list[ip_seg], ip1s);
- if (index < ip_list[ip_seg].length) index++;
- while (true) {
- if (index == 0) break;
- var prev = ip_list[ip_seg][index - 1];
- if (prev[0] >= ip1s || (prev[0] == ip1s && (prev[1] - prev[0]) < (ip2s - ip1s))) {
- index = index - 1;
- } else {
- break;
- }
- }
- ip_list[ip_seg].splice(index, 0, [ip1s, ip2s, data]);
- }
- },
- get_hash_id: function(mapping_list, name_list, data) {
- var map_id = mapping_list[data];
- if (map_id == undefined) {
- name_list.push(data);
- mapping_list[data] = name_list.length - 1;
- map_id = mapping_list[data];
- }
- return map_id;
- }
- }
- module.exports = ip_mapping;
Add Comment
Please, Sign In to add comment