Skip to content

Commit

Permalink
Added support for 6to4 and Teredo.
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Jul 9, 2019
1 parent 8ef9f87 commit 39370fe
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.BIN
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ip2location-nodejs",
"version": "8.1.3",
"version": "8.2.0",
"description": "IP2Location geolocation component",
"keywords": [
"ip2location",
Expand All @@ -20,7 +20,7 @@
"main": "src/ip2location.js",
"license": "MIT",
"dependencies": {
"big-integer": "^1.6.27"
"big-integer": "^1.6.44"
},
"repository": {
"type": "git",
Expand Down
50 changes: 44 additions & 6 deletions src/ip2location.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var bigInt = require("big-integer");

var fd;

var version = "8.1.3";
var version = "8.2.0";
var binfile = "";
var IPv4ColumnSize = 0;
var IPv6ColumnSize = 0;
Expand Down Expand Up @@ -76,6 +76,14 @@ var mobilebrand_enabled = 0;
var elevation_enabled = 0;
var usagetype_enabled = 0;

var MAX_IPV4_RANGE = bigInt(4294967295);
var MAX_IPV6_RANGE = bigInt("340282366920938463463374607431768211455");
var FROM_6TO4 = bigInt("42545680458834377588178886921629466624");
var TO_6TO4 = bigInt("42550872755692912415807417417958686719");
var FROM_TEREDO = bigInt("42540488161975842760550356425300246528");
var TO_TEREDO = bigInt("42540488241204005274814694018844196863");
var LAST_32BITS = bigInt("4294967295");

var mydb = {
"_DBType": 0,
"_DBColumn": 0,
Expand All @@ -95,7 +103,7 @@ var mydb = {

// Read binary data
function readbin(readbytes, pos, readtype, isbigint) {
var buff = new Buffer(readbytes);
var buff = new Buffer.alloc(readbytes);
totalread = fs.readSync(fd, buff, 0, readbytes, pos);

if (totalread == readbytes) {
Expand Down Expand Up @@ -311,7 +319,10 @@ function IP2Location_query(myIP, iptype, data) {
low = 0;
mid = 0;
high = 0;
var MAX_IP_RANGE = bigInt();

if (iptype == 4) { // IPv4
MAX_IP_RANGE = MAX_IPV4_RANGE;
high = mydb._DBCount;
_BaseAddr = mydb._BaseAddr;
_ColumnSize = IPv4ColumnSize;
Expand All @@ -324,22 +335,49 @@ function IP2Location_query(myIP, iptype, data) {
}
}
else if (iptype == 6) { // IPv6
MAX_IP_RANGE = MAX_IPV6_RANGE;
high = mydb._DBCountIPv6;
_BaseAddr = mydb._BaseAddrIPv6;
_ColumnSize = IPv6ColumnSize;
ipnum = ip2no(myIP);

if (mydb._IndexedIPv6 == 1) {
indexaddr = ipnum.shiftRight(112).toJSNumber();
low = IndexArrayIPv6[indexaddr][0];
high = IndexArrayIPv6[indexaddr][1];
if ((ipnum.geq(FROM_6TO4) && ipnum.leq(TO_6TO4)) || (ipnum.geq(FROM_TEREDO) && ipnum.leq(TO_TEREDO))) {
iptype = 4;
MAX_IP_RANGE = MAX_IPV4_RANGE;
high = mydb._DBCount;
_BaseAddr = mydb._BaseAddr;
_ColumnSize = IPv4ColumnSize;

if (ipnum.geq(FROM_6TO4) && ipnum.leq(TO_6TO4)) {
ipnum = ipnum.shiftRight(80).and(LAST_32BITS).toJSNumber();
}
else {
ipnum = ipnum.not().and(LAST_32BITS).toJSNumber();
}
if (mydb._Indexed == 1) {
indexaddr = ipnum >>> 16;
low = IndexArrayIPv4[indexaddr][0];
high = IndexArrayIPv4[indexaddr][1];
}
}
else {
if (mydb._IndexedIPv6 == 1) {
indexaddr = ipnum.shiftRight(112).toJSNumber();
low = IndexArrayIPv6[indexaddr][0];
high = IndexArrayIPv6[indexaddr][1];
}
}
}

MSG_NOT_SUPPORTED = "This method is not applicable for current IP2Location binary data file. Please upgrade your subscription package to install new data file.";

data.ip = myIP;
ipnum = bigInt(ipnum);

if (ipnum.geq(MAX_IP_RANGE)) {
ipnum = MAX_IP_RANGE.minus(1);
}

data.ip_no = ipnum.toString();

while (low <= high) {
Expand Down
3 changes: 2 additions & 1 deletion src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ var ip2loc = require("ip2location-nodejs");
// ip2loc.IP2Location_init("/root/testnodejs/db24.BIN");
ip2loc.IP2Location_init("./DB24.BIN");

testip = ['8.8.8.8', '2404:6800:4001:c01::67', '2001:0200:0102:0000:0000:0000:0000:0000', '2001:0200:0135:0000:0000:0000:0000:0000', '2001:0200:017A:0000:0000:0000:0000:0000', '2404:6800:4001:c01::93', '::FFFF:8.8.8.8', '0000:0000:0000:0000:0000:FFFF:8.8.8.8', '::8.8.8.8.8'];
// testip = ['8.8.8.8', '2404:6800:4001:c01::67', '2001:0200:0102:0000:0000:0000:0000:0000', '2001:0200:0135:0000:0000:0000:0000:0000', '2001:0200:017A:0000:0000:0000:0000:0000', '2404:6800:4001:c01::93', '::FFFF:8.8.8.8', '0000:0000:0000:0000:0000:FFFF:8.8.8.8', '::8.8.8.8.8'];
// testip = ['8.8.8.8', '2404:6800:4001:c01::93'];
testip = ['2001:0:4136:e378:8000:63bf:f7f7:f7f7', '2002:0803:2200::0803:2200'];

for (var x = 0; x < testip.length; x++) {
result = ip2loc.IP2Location_get_all(testip[x]);
Expand Down

0 comments on commit 39370fe

Please sign in to comment.