-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.js
70 lines (55 loc) · 2.33 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require("fs");
// Read country data
const countryData = [];
const dataIndexFromGeoID = {};
fs.readFileSync("GeoLite2-Country-Locations-en.csv").toString().split("\n").slice(1, -1).forEach(line => {
const fields = line.split(',');
const geoID = fields[0];
const code = fields[4] || fields[2];
const name = (fields[5] || fields[3]).replace(/"/g, '');
dataIndexFromGeoID[geoID] = countryData.push({code, name})-1;
});
// Read block entries and fill up buffers as we go
const u32FromIPString = ip => ip.split(".").map(b => parseInt(b)).reduce((r, b) => (r << 8) + b) >>> 0;
const blockPrefix = [];
const blockSize = [];
const blockDataIndex = [];
fs.readFileSync("GeoLite2-Country-Blocks-IPv4.csv").toString().split("\n").slice(1).forEach(line => {
const fields = line.split(/[,\/]/g);
const prefix = fields[0];
const size = fields[1];
const geoID = fields[2];
if(!(prefix && size && geoID)) return;
blockPrefix.push(u32FromIPString(prefix));
blockSize.push(32 - parseInt(size));
blockDataIndex.push(dataIndexFromGeoID[geoID]);
});
// CIDR block matching function, to be included in the build
function geolocate(ipString) {
const ip = u32FromIPString(ipString);
// Starting off with a few passes of binary search to reduce the lookup range
// Last time I checked, there were ~233k entries in the IPv4 countries table,
// so it takes 15 passes to reduce the lookup to a small (~7) number of entries
let floor = 0;
let range = blockPrefix.length-1;
for(let i = 0; i < 15; i++) { // This will likely be unrolled by V8, btw
range >>= 1;
if(ip > blockPrefix[floor+range])
floor += range;
}
// Match blocks bitwise until one matches, then return the associated country data
for(let i = floor; i < floor+range; i++)
if(!((ip ^ blockPrefix[i]) >>> blockSize[i]))
return countryData[blockDataIndex[i]];
// Nothing found
return undefined;
}
// And finally, generate the self-contained file
fs.writeFileSync("geolocate.js",`
const u32FromIPString = ${ u32FromIPString.toString() };
module.exports = ${ geolocate.toString() }
const countryData = ${ JSON.stringify(countryData) };
const blockPrefix = Uint32Array.from([${ blockPrefix.map(x => "0x"+x.toString(16)).join(",") }]);
const blockSize = Uint8Array.from(${ JSON.stringify(blockSize) });
const blockDataIndex = Uint8Array.from(${ JSON.stringify(blockDataIndex) });
`.trim());