|
1 | 1 | var path = require('path');
|
2 | 2 |
|
3 | 3 | var gindex = [],
|
4 |
| - gCities = {}, |
5 |
| - poweredUp = false; |
| 4 | + gCities = {}, |
| 5 | + poweredUp = false; |
6 | 6 |
|
7 | 7 | var GEO_FIELD_MIN = 0,
|
8 |
| - GEO_FIELD_MAX = 1, |
9 |
| - GEO_FIELD_COUNTRY = 2, |
10 |
| - GEO_FIELD_REGION = 3, |
11 |
| - GEO_DIR_DATA = path.join(__dirname, '..', 'geo'); |
| 8 | + GEO_FIELD_MAX = 1, |
| 9 | + GEO_FIELD_COUNTRY = 2, |
| 10 | + GEO_FIELD_REGION = 3, |
| 11 | + GEO_DIR_DATA = path.join(__dirname, '..', 'geo'); |
12 | 12 |
|
13 | 13 | // Load and parse geo base
|
14 | 14 | function _powerup() {
|
15 |
| - var fs = require('fs'); |
16 |
| - var cidr = fs.readFileSync(path.join(GEO_DIR_DATA, 'cidr_optim.txt')); |
17 |
| - cidr = cidr.toString().split('\n'); |
18 |
| - |
19 |
| - for (var i = cidr.length - 1; i >= 0; i--) { |
20 |
| - if (!cidr[i]) continue; |
21 |
| - var line = cidr[i].split('\t'); |
22 |
| - gindex.push([ |
23 |
| - parseInt(line[0]), // GEO_FIELD_MIN |
24 |
| - parseInt(line[1]), // GEO_FIELD_MAX |
25 |
| - line[3], // GEO_FIELD_COUNTRY |
26 |
| - (line[4] == "-") ? 0 : parseInt(line[4]) // GEO_FIELD_REGION |
27 |
| - ]); |
28 |
| - } |
29 |
| - |
30 |
| - var rawCities = fs.readFileSync(path.join(GEO_DIR_DATA, 'cities.txt'), 'utf-8'); |
31 |
| - var cities = rawCities.split('\n'); |
32 |
| - |
33 |
| - for (i = 0; i < cities.length; i++) { |
34 |
| - var city = cities[i].split('\t'); |
35 |
| - gCities[city[0]] = { |
36 |
| - city: city[1], |
37 |
| - regionName: city[2], |
38 |
| - district: city[3], |
39 |
| - lat: city[4], |
40 |
| - lon: city[5] |
41 |
| - }; |
42 |
| - } |
| 15 | + var fs = require('fs'); |
| 16 | + var cidr = fs.readFileSync(path.join(GEO_DIR_DATA, 'cidr_optim.txt')); |
| 17 | + cidr = cidr.toString().split('\n'); |
| 18 | + |
| 19 | + for (var i = cidr.length - 1; i >= 0; i--) { |
| 20 | + if (!cidr[i]) continue; |
| 21 | + var line = cidr[i].split('\t'); |
| 22 | + gindex.push([ |
| 23 | + parseInt(line[0]), // GEO_FIELD_MIN |
| 24 | + parseInt(line[1]), // GEO_FIELD_MAX |
| 25 | + line[3], // GEO_FIELD_COUNTRY |
| 26 | + (line[4] == "-") ? 0 : parseInt(line[4]) // GEO_FIELD_REGION |
| 27 | + ]); |
| 28 | + } |
| 29 | + |
| 30 | + var rawCities = fs.readFileSync(path.join(GEO_DIR_DATA, 'cities.txt'), 'utf-8'); |
| 31 | + var cities = rawCities.split('\n'); |
| 32 | + |
| 33 | + for (i = 0; i < cities.length; i++) { |
| 34 | + var city = cities[i].split('\t'); |
| 35 | + gCities[city[0]] = { |
| 36 | + city: city[1], |
| 37 | + regionName: city[2], |
| 38 | + district: city[3], |
| 39 | + lat: city[4], |
| 40 | + lon: city[5] |
| 41 | + }; |
| 42 | + } |
43 | 43 |
|
44 |
| - gindex.sort(function(a, b) { |
45 |
| - return a[GEO_FIELD_MIN] - b[GEO_FIELD_MIN]; |
46 |
| - }); |
| 44 | + gindex.sort(function(a, b) { |
| 45 | + return a[GEO_FIELD_MIN] - b[GEO_FIELD_MIN]; |
| 46 | + }); |
47 | 47 |
|
48 |
| - poweredUp = true; |
| 48 | + poweredUp = true; |
49 | 49 | }
|
50 | 50 |
|
51 | 51 | function _normalize(row) {
|
52 |
| - var cityInfo = gCities[row[GEO_FIELD_REGION]]; |
| 52 | + var cityInfo = gCities[row[GEO_FIELD_REGION]]; |
53 | 53 |
|
54 |
| - var result = { |
55 |
| - country: row[GEO_FIELD_COUNTRY], |
56 |
| - region: row[GEO_FIELD_REGION] |
57 |
| - }; |
| 54 | + var result = { |
| 55 | + country: row[GEO_FIELD_COUNTRY], |
| 56 | + region: row[GEO_FIELD_REGION] |
| 57 | + }; |
58 | 58 |
|
59 |
| - for (var key in cityInfo) { |
60 |
| - if (cityInfo.hasOwnProperty(key)) { |
61 |
| - result[key] = cityInfo[key]; |
62 |
| - } |
| 59 | + for (var key in cityInfo) { |
| 60 | + if (cityInfo.hasOwnProperty(key)) { |
| 61 | + result[key] = cityInfo[key]; |
63 | 62 | }
|
| 63 | + } |
64 | 64 |
|
65 |
| - return result; |
| 65 | + return result; |
66 | 66 | }
|
67 | 67 |
|
68 | 68 | // IP to long converter
|
69 | 69 | module.exports.ip2long = function(ip) {
|
70 |
| - ip = ip.split('.'); |
71 |
| - var c = ip.length; |
72 |
| - ip[0] = parseInt(ip[0]) || 0; |
73 |
| - ip[1] = parseInt(ip[1]) || 0; |
74 |
| - ip[2] = parseInt(ip[2]) || 0; |
75 |
| - ip[3] = parseInt(ip[3]) || 0; |
76 |
| - |
77 |
| - return ip[0] * (c === 1 || 16777216) |
78 |
| - + ip[1] * (c <= 2 || 65536) |
79 |
| - + ip[2] * (c <= 3 || 256) |
80 |
| - + ip[3] * 1; |
| 70 | + ip = ip.split('.'); |
| 71 | + var c = ip.length; |
| 72 | + ip[0] = parseInt(ip[0]) || 0; |
| 73 | + ip[1] = parseInt(ip[1]) || 0; |
| 74 | + ip[2] = parseInt(ip[2]) || 0; |
| 75 | + ip[3] = parseInt(ip[3]) || 0; |
| 76 | + |
| 77 | + return ip[0] * (c === 1 || 16777216) |
| 78 | + + ip[1] * (c <= 2 || 65536) |
| 79 | + + ip[2] * (c <= 3 || 256) |
| 80 | + + ip[3] * 1; |
81 | 81 | };
|
82 | 82 |
|
83 | 83 | // Binary search through the geo index
|
84 | 84 | module.exports.lookup = function(ip) {
|
85 | 85 |
|
86 |
| - if (!ip) return -1; |
87 |
| - if (!poweredUp) _powerup(); |
| 86 | + if (!ip) return -1; |
| 87 | + if (!poweredUp) _powerup(); |
88 | 88 |
|
89 |
| - var find = this.ip2long(ip); |
| 89 | + var find = this.ip2long(ip); |
90 | 90 |
|
91 |
| - var low = 0, high = gindex.length - 1, i; |
| 91 | + var low = 0, high = gindex.length - 1, i; |
92 | 92 |
|
93 |
| - while (low <= high) { |
94 |
| - i = Math.floor((low + high) / 2); |
95 |
| - if (gindex[i][GEO_FIELD_MIN] > find) { high = i - 1; continue; } |
96 |
| - if (gindex[i][GEO_FIELD_MIN] < find) { low = i + 1; continue; } |
97 |
| - break; |
98 |
| - } |
| 93 | + while (low <= high) { |
| 94 | + i = Math.floor((low + high) / 2); |
| 95 | + if (gindex[i][GEO_FIELD_MIN] > find) { high = i - 1; continue; } |
| 96 | + if (gindex[i][GEO_FIELD_MIN] < find) { low = i + 1; continue; } |
| 97 | + break; |
| 98 | + } |
99 | 99 |
|
100 |
| - // checking given index and two neighbouring indexes at i-1 and i+1 |
101 |
| - if (find >= gindex[i][GEO_FIELD_MIN] && find <= gindex[i][GEO_FIELD_MAX]) |
102 |
| - return _normalize(gindex[i]); |
| 100 | + // checking given index and two neighbouring indexes at i-1 and i+1 |
| 101 | + if (find >= gindex[i][GEO_FIELD_MIN] && find <= gindex[i][GEO_FIELD_MAX]) |
| 102 | + return _normalize(gindex[i]); |
103 | 103 |
|
104 |
| - if ((i !== 0) && (find >= gindex[i-1][GEO_FIELD_MIN] && find <= gindex[i-1][GEO_FIELD_MAX])) |
105 |
| - return _normalize(gindex[i-1]); |
| 104 | + if ((i !== 0) && (find >= gindex[i-1][GEO_FIELD_MIN] && find <= gindex[i-1][GEO_FIELD_MAX])) |
| 105 | + return _normalize(gindex[i-1]); |
106 | 106 |
|
107 |
| - if ((i !== gindex.length - 1) && (find >= gindex[i+1][GEO_FIELD_MIN] && find <= gindex[i+1][GEO_FIELD_MAX])) |
108 |
| - return _normalize(gindex[i+1]); |
| 107 | + if ((i !== gindex.length - 1) && (find >= gindex[i+1][GEO_FIELD_MIN] && find <= gindex[i+1][GEO_FIELD_MAX])) |
| 108 | + return _normalize(gindex[i+1]); |
109 | 109 |
|
110 |
| - return null; |
| 110 | + return null; |
111 | 111 | };
|
0 commit comments