Skip to content

Commit

Permalink
Fixed IPv6 not supported error for Teredo/6to4 when using IPv4 BIN
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Nov 29, 2024
1 parent d2eb83f commit e16a47b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 52 deletions.
74 changes: 37 additions & 37 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
{
"name": "ip2proxy-nodejs",
"version": "4.3.1",
"description": "IP2Proxy proxy detection component",
"keywords": [
"vpn-detection",
"proxy-detection",
"anonymous-proxy",
"ip2proxy",
"proxy-database",
"vpn",
"tor",
"proxy"
],
"homepage": "https://www.ip2location.com/development-libraries/ip2proxy/nodejs",
"author": {
"name": "IP2Location.com",
"email": "[email protected]",
"url": "https://www.ip2location.com/"
},
"files": [
"src/ip2proxy.js",
"src/test.js",
"src/ip2proxy.d.ts",
"./README.md"
],
"main": "src/ip2proxy.js",
"types": "src/ip2proxy.d.ts",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/ip2location/ip2proxy-nodejs.git"
},
"devDependencies": {
"prettier": "2.4.0"
}
}
{
"name": "ip2proxy-nodejs",
"version": "4.3.2",
"description": "IP2Proxy proxy detection component",
"keywords": [
"vpn-detection",
"proxy-detection",
"anonymous-proxy",
"ip2proxy",
"proxy-database",
"vpn",
"tor",
"proxy"
],
"homepage": "https://www.ip2location.com/development-libraries/ip2proxy/nodejs",
"author": {
"name": "IP2Location.com",
"email": "[email protected]",
"url": "https://www.ip2location.com/"
},
"files": [
"src/ip2proxy.js",
"src/test.js",
"src/ip2proxy.d.ts",
"./README.md"
],
"main": "src/ip2proxy.js",
"types": "src/ip2proxy.d.ts",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/ip2location/ip2proxy-nodejs.git"
},
"devDependencies": {
"prettier": "2.4.0"
}
}
67 changes: 52 additions & 15 deletions src/ip2proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fsp = fs.promises;
const https = require("https");

// For BIN queries
const VERSION = "4.3.1";
const VERSION = "4.3.2";
const MAX_INDEX = 65536;
const COUNTRY_POSITION = [0, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3];
const REGION_POSITION = [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4];
Expand All @@ -25,6 +25,8 @@ const TO_6TO4 = BigInt("42550872755692912415807417417958686719");
const FROM_TEREDO = BigInt("42540488161975842760550356425300246528");
const TO_TEREDO = BigInt("42540488241204005274814694018844196863");
const LAST_32_BITS = BigInt("4294967295");
const FROM_IPV4_MAPPED_IPV6 = BigInt("281470681743360");
const TO_IPV4_MAPPED_IPV6 = BigInt("281474976710655");
const MODES = {
COUNTRY_SHORT: 1,
COUNTRY_LONG: 2,
Expand Down Expand Up @@ -127,6 +129,7 @@ class IP2Proxy {
fileSize: 0,
};
#fd;
#fh;

constructor() {}

Expand Down Expand Up @@ -396,8 +399,8 @@ class IP2Proxy {

try {
if (this.#binFile && this.#binFile != "") {
let fh = await fsp.open(this.#binFile, "r");
this.#fd = fh.fd;
this.#fh = await fsp.open(this.#binFile, "r");
this.#fd = this.#fh.fd;

let len = 64; // 64-byte header
let row = await this.readRowAsync(len, 1);
Expand Down Expand Up @@ -549,7 +552,7 @@ class IP2Proxy {
}
}

// Reset everything (do not use in async case due to race conditions)
// Reset everything
close() {
try {
this.#myDB.baseAddress = 0;
Expand All @@ -568,7 +571,33 @@ class IP2Proxy {
this.#myDB.productCode = 0;
this.#myDB.productType = 0;
this.#myDB.fileSize = 0;
fs.closeSync(this.#fd);
fs.closeSync(this.#fd); // normal file handle
return 0;
} catch (err) {
return -1;
}
}

// Reset everything
async closeAsync() {
try {
this.#myDB.baseAddress = 0;
this.#myDB.dbCount = 0;
this.#myDB.dbColumn = 0;
this.#myDB.dbType = 0;
this.#myDB.dbDay = 0;
this.#myDB.dbMonth = 0;
this.#myDB.dbYear = 0;
this.#myDB.baseAddressIPV6 = 0;
this.#myDB.dbCountIPV6 = 0;
this.#myDB.indexed = 0;
this.#myDB.indexedIPV6 = 0;
this.#myDB.indexBaseAddress = 0;
this.#myDB.indexBaseAddressIPV6 = 0;
this.#myDB.productCode = 0;
this.#myDB.productType = 0;
this.#myDB.fileSize = 0;
await this.#fh.close(); // FileHandler object opened by fs.Promises
return 0;
} catch (err) {
return -1;
Expand Down Expand Up @@ -615,7 +644,8 @@ class IP2Proxy {

if (
(ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4) ||
(ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO)
(ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO) ||
(ipNumber >= FROM_IPV4_MAPPED_IPV6 && ipNumber <= TO_IPV4_MAPPED_IPV6)
) {
ipType = 4;
MAX_IP_RANGE = MAX_IPV4_RANGE;
Expand All @@ -625,15 +655,21 @@ class IP2Proxy {

if (ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4) {
ipNumber = Number((ipNumber >> BigInt(80)) & LAST_32_BITS);
} else {
} else if (ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO) {
ipNumber = Number(~ipNumber & LAST_32_BITS);
} else {
ipNumber = Number(ipNumber - FROM_IPV4_MAPPED_IPV6);
}
if (this.#myDB.indexed == 1) {
indexAddress = ipNumber >>> 16;
low = this.#indexArrayIPV4[indexAddress][0];
high = this.#indexArrayIPV4[indexAddress][1];
}
} else {
if (this.#myDB.dbCountIPV6 == 0) {
loadMesg(data, MSG_IPV6_UNSUPPORTED);
return;
}
firstCol = 16; // IPv6 is 16 bytes
if (this.#myDB.indexedIPV6 == 1) {
indexAddress = Number(ipNumber >> BigInt(112));
Expand Down Expand Up @@ -835,7 +871,8 @@ class IP2Proxy {

if (
(ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4) ||
(ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO)
(ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO) ||
(ipNumber >= FROM_IPV4_MAPPED_IPV6 && ipNumber <= TO_IPV4_MAPPED_IPV6)
) {
ipType = 4;
MAX_IP_RANGE = MAX_IPV4_RANGE;
Expand All @@ -845,15 +882,21 @@ class IP2Proxy {

if (ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4) {
ipNumber = Number((ipNumber >> BigInt(80)) & LAST_32_BITS);
} else {
} else if (ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO) {
ipNumber = Number(~ipNumber & LAST_32_BITS);
} else {
ipNumber = Number(ipNumber - FROM_IPV4_MAPPED_IPV6);
}
if (this.#myDB.indexed == 1) {
indexAddress = ipNumber >>> 16;
low = this.#indexArrayIPV4[indexAddress][0];
high = this.#indexArrayIPV4[indexAddress][1];
}
} else {
if (this.#myDB.dbCountIPV6 == 0) {
loadMesg(data, MSG_IPV6_UNSUPPORTED);
return;
}
firstCol = 16; // IPv6 is 16 bytes
if (this.#myDB.indexedIPV6 == 1) {
indexAddress = Number(ipNumber >> BigInt(112));
Expand Down Expand Up @@ -1059,9 +1102,6 @@ class IP2Proxy {
} else if (this.#myDB.dbType == 0) {
loadMesg(data, MSG_MISSING_FILE);
return data;
} else if (ipType == 6 && this.#myDB.dbCountIPV6 == 0) {
loadMesg(data, MSG_IPV6_UNSUPPORTED);
return data;
} else {
this.proxyQueryData(myIP, ipType, data, mode);
return data;
Expand Down Expand Up @@ -1110,9 +1150,6 @@ class IP2Proxy {
} else if (this.#myDB.dbType == 0) {
loadMesg(data, MSG_MISSING_FILE);
return data;
} else if (ipType == 6 && this.#myDB.dbCountIPV6 == 0) {
loadMesg(data, MSG_IPV6_UNSUPPORTED);
return data;
} else {
await this.proxyQueryDataAsync(myIP, ipType, data, mode);
return data;
Expand Down

0 comments on commit e16a47b

Please sign in to comment.