Guest User

Untitled

a guest
Oct 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. var fs = require('fs');
  2.  
  3. var ip_mapping = {
  4. city_ip: {},
  5. contry_ip: {},
  6. isp_name: [],
  7. city_name: [],
  8. province_name: [],
  9. query_ip : function(ip) {
  10. var result = this.query_ip_table(ip, this.contry_ip, this.contry_mapping);
  11. if (result == 'CN') {
  12. result = this.query_ip_table(ip, this.city_ip, this.city_mapping);
  13. if (result == undefined) {
  14. return 'CN';
  15. } else {
  16. var province = this.province_name[result[0]];
  17. var city = this.city_name[result[1]];
  18. var isp = this.isp_name[result[2]];
  19. return province + ":" + city + ":" + isp;
  20. }
  21. } else {
  22. return result;
  23. }
  24. },
  25. query_ip_table: function(ip, table) {
  26. var ip_long = this.ip2long(ip);
  27. var ip_seg1 = ip_long >> 16;
  28. var sub_table = table[ip_seg1];
  29. if (sub_table != undefined) {
  30. var ips = ip_long & 0xffff;
  31. var index = this.binary_search(sub_table, ips);
  32. if (index >= sub_table.length) index = sub_table.length - 1;
  33. var data = undefined;
  34. while (true) {
  35. data = sub_table[index];
  36. if (data[0] <= ips && data[1] >= ips) {
  37. break;
  38. } else {
  39. index = index - 1;
  40. }
  41. if (index < 0) {
  42. return undefined;
  43. }
  44. }
  45. return data[2];
  46. } else {
  47. return undefined;
  48. }
  49. },
  50. binary_search: function(list, item) {
  51. var index = list.length - 1
  52. , last = list.length - 1
  53. , first = 0;
  54. if (list.length == 0) return 0;
  55. if (item >= list[last][0]) return last + 1;
  56. if (item <= list[0][0]) return 0;
  57. while (true) {
  58. index = Math.floor((first + last) / 2);
  59. if (item == list[index][0]) {
  60. break;
  61. } else if (item > list[index][0]) {
  62. first = index;
  63. } else {
  64. last = index;
  65. }
  66. if ((last - first) <= 1) {
  67. break;
  68. }
  69. }
  70. while (item == list[index + 1][0]) {
  71. index = index + 1;
  72. }
  73. return index;
  74. },
  75. readlines: function (input, func) {
  76. var remaining = '';
  77. input.on('data', function(data) {
  78. remaining += data;
  79. var index = remaining.indexOf('\n');
  80. while (index > -1) {
  81. var line = remaining.substring(0, index);
  82. remaining = remaining.substring(index + 1);
  83. func(line);
  84. index = remaining.indexOf('\n');
  85. }
  86. });
  87. input.on('end', function() {
  88. if (remaining.length > 0) {
  89. func(remaining);
  90. }
  91. });
  92. },
  93. ip2long: function(ip) {
  94. var ip_seg = ip.split('.');
  95. var buff = new Buffer(4);
  96. for (var i in ip_seg) {
  97. buff.writeUInt8(parseInt(ip_seg[i]), i);
  98. }
  99. return buff.readUInt32BE(0);
  100. },
  101. break_ip_seg: function(ip_start, ip_end) {
  102. var seg1 = ip_start >> 16;
  103. var seg2 = ip_end >> 16;
  104. if (seg1 == seg2) {
  105. return [[ip_start, ip_end]];
  106. }
  107. var result = [];
  108. for (var i = seg1; i<= seg2; i++) {
  109. var start = i << 16;
  110. var end = (i << 16) + 0xffff;
  111. if (i == seg1) {
  112. start = ip_start;
  113. }
  114. if (i == seg2) {
  115. end = ip_end ;
  116. }
  117. result.push([start, end]);
  118. }
  119. return result;
  120. },
  121. build_mapping: function(city, contry) {
  122. var isp_name_mapping = {};
  123. var city_name_mapping = {};
  124. var province_name_mapping = {};
  125.  
  126. var city_file = fs.createReadStream(city);
  127. this.readlines(city_file, function(line) {
  128. var line_values = line.trim().split(',');
  129. var ip_segs = ip_mapping.break_ip_seg(ip_mapping.ip2long(line_values[6]), ip_mapping.ip2long(line_values[7]));
  130. // id, ip_start,ip_end,province,city,isp,ip_start_str,ip_end_str
  131. for (var j in ip_segs) {
  132. var values = line_values;
  133. values[1] = ip_segs[j][0];
  134. values[2] = ip_segs[j][1];
  135. var ip_seg1 = parseInt(values[1]) >> 16;
  136. var province_id = ip_mapping.get_hash_id(province_name_mapping, ip_mapping.province_name, values[3]);
  137. var city_id = ip_mapping.get_hash_id(city_name_mapping, ip_mapping.city_name, values[4]);
  138. var isp_id = ip_mapping.get_hash_id(isp_name_mapping, ip_mapping.isp_name, values[5]);
  139. 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])
  140. }
  141. });
  142.  
  143. var contry_file = fs.createReadStream(contry);
  144. this.readlines(contry_file, function(line) {
  145. var line_values = line.trim().split(',');
  146. var ip_segs = ip_mapping.break_ip_seg(ip_mapping.ip2long(line_values[4]), ip_mapping.ip2long(line_values[5]));
  147. // id, ip_start,ip_end,contry,ip_start_str,ip_end_str
  148. for (var j in ip_segs) {
  149. var values = line_values;
  150. values[1] = ip_segs[j][0];
  151. values[2] = ip_segs[j][1];
  152. var ip_seg1 = parseInt(values[1]) >> 16;
  153. var offset = ip_mapping.insert_ip_seg(ip_mapping.contry_ip, ip_seg1, parseInt(values[1]), parseInt(values[2]), values[3]);
  154. }
  155. });
  156. },
  157. insert_ip_seg: function(ip_list, ip_seg, ip1, ip2, data) {
  158. var ip1s = ip1 & 0xffff;
  159. var ip2s = ip2 & 0xffff;
  160. if (ip_list[ip_seg] == undefined) {
  161. ip_list[ip_seg] = [];
  162. ip_list[ip_seg].push([ip1s, ip2s, data]);
  163. } else {
  164. var index = ip_mapping.binary_search(ip_list[ip_seg], ip1s);
  165. if (index < ip_list[ip_seg].length) index++;
  166. while (true) {
  167. if (index == 0) break;
  168. var prev = ip_list[ip_seg][index - 1];
  169. if (prev[0] >= ip1s || (prev[0] == ip1s && (prev[1] - prev[0]) < (ip2s - ip1s))) {
  170. index = index - 1;
  171. } else {
  172. break;
  173. }
  174. }
  175. ip_list[ip_seg].splice(index, 0, [ip1s, ip2s, data]);
  176. }
  177. },
  178. get_hash_id: function(mapping_list, name_list, data) {
  179. var map_id = mapping_list[data];
  180. if (map_id == undefined) {
  181. name_list.push(data);
  182. mapping_list[data] = name_list.length - 1;
  183. map_id = mapping_list[data];
  184. }
  185. return map_id;
  186. }
  187. }
  188.  
  189.  
  190.  
  191. module.exports = ip_mapping;
Add Comment
Please, Sign In to add comment