Skip to content

Commit 983bece

Browse files
committed
Added provider field and exception handling for incorrect BIN database.
1 parent 18a28c9 commit 983bece

File tree

5 files changed

+55
-47
lines changed

5 files changed

+55
-47
lines changed

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 IP2Location.com
3+
Copyright (c) 2021 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Below are the methods supported in this class.
2626
|---|---|
2727
|Open|Open the IP2Proxy BIN data for lookup.|
2828
|Close|Close and clean up the file pointer.|
29-
|getPackageVersion|Get the package version (1 to 10 for PX1 to PX10 respectively).|
29+
|getPackageVersion|Get the package version (1 to 11 for PX1 to PX11 respectively).|
3030
|getModuleVersion|Get the module version.|
3131
|getDatabaseVersion|Get the database version.|
3232
|isProxy|Check whether if an IP address was a proxy. Returned value:<ul><li>-1 : errors</li><li>0 : not a proxy</li><li>1 : a proxy</li><li>2 : a data center IP address or search engine robot</li></ul>|
@@ -43,14 +43,15 @@ Below are the methods supported in this class.
4343
|getAS|Return the autonomous system name of the proxy.|
4444
|getLastSeen|Return the number of days that the proxy was last seen.|
4545
|getThreat|Return the threat type of the proxy.|
46+
|getProvider|Return the provider of the proxy.|
4647

4748
## Usage
4849

4950
```javascript
5051

5152
var ip2proxy = require("ip2proxy-nodejs");
5253

53-
if (ip2proxy.Open("./IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL.BIN") == 0) {
54+
if (ip2proxy.Open("./IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGETYPE-ASN-LASTSEEN-THREAT-RESIDENTIAL-PROVIDER.BIN") == 0) {
5455
ip = '199.83.103.79';
5556

5657
console.log("GetModuleVersion: " + ip2proxy.getModuleVersion());
@@ -71,6 +72,7 @@ if (ip2proxy.Open("./IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGET
7172
console.log("AS: " + ip2proxy.getAS(ip));
7273
console.log("LastSeen: " + ip2proxy.getLastSeen(ip));
7374
console.log("Threat: " + ip2proxy.getThreat(ip));
75+
console.log("Provider: " + ip2proxy.getProvider(ip));
7476

7577
// function for all fields
7678
var all = ip2proxy.getAll(ip);
@@ -87,6 +89,7 @@ if (ip2proxy.Open("./IP2PROXY-IP-PROXYTYPE-COUNTRY-REGION-CITY-ISP-DOMAIN-USAGET
8789
console.log("AS: " + all.AS);
8890
console.log("LastSeen: " + all.Last_Seen);
8991
console.log("Threat: " + all.Threat);
92+
console.log("Provider: " + all.Provider);
9093
}
9194
else {
9295
console.log("Error reading BIN file.");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ip2proxy-nodejs",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "IP2Proxy proxy detection component",
55
"keywords": [
66
"vpn-detection",

src/ip2proxy.js

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var bigInt = require("big-integer");
44

55
var fd;
66

7-
var version = "3.0.0";
7+
var version = "3.1.0";
88
var binfile = "";
99
var IPv4ColumnSize = 0;
1010
var IPv6ColumnSize = 0;
@@ -16,17 +16,18 @@ var maxindex = 65536;
1616
var IndexArrayIPv4 = Array(maxindex);
1717
var IndexArrayIPv6 = Array(maxindex);
1818

19-
var country_pos = [0, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3];
20-
var region_pos = [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4];
21-
var city_pos = [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5];
22-
var isp_pos = [0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6];
23-
var proxytype_pos = [0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2];
24-
var domain_pos = [0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7];
25-
var usagetype_pos = [0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8];
26-
var asn_pos = [0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9];
27-
var as_pos = [0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10];
28-
var lastseen_pos = [0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11];
29-
var threat_pos = [0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12];
19+
var country_pos = [0, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3];
20+
var region_pos = [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4];
21+
var city_pos = [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5];
22+
var isp_pos = [0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6];
23+
var proxytype_pos = [0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2];
24+
var domain_pos = [0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7];
25+
var usagetype_pos = [0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8];
26+
var asn_pos = [0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 9];
27+
var as_pos = [0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10];
28+
var lastseen_pos = [0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11];
29+
var threat_pos = [0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12];
30+
var provider_pos = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13];
3031

3132
var country_pos_offset = 0;
3233
var region_pos_offset = 0;
@@ -39,6 +40,7 @@ var asn_pos_offset = 0;
3940
var as_pos_offset = 0;
4041
var lastseen_pos_offset = 0;
4142
var threat_pos_offset = 0;
43+
var provider_pos_offset = 0;
4244

4345
var country_enabled = 0;
4446
var region_enabled = 0;
@@ -51,6 +53,7 @@ var asn_enabled = 0;
5153
var as_enabled = 0;
5254
var lastseen_enabled = 0;
5355
var threat_enabled = 0;
56+
var provider_enabled = 0;
5457

5558
var MAX_IPV4_RANGE = bigInt(4294967295);
5659
var MAX_IPV6_RANGE = bigInt("340282366920938463463374607431768211455");
@@ -71,7 +74,10 @@ var mydb = {
7174
"_DBCountIPv6": 0,
7275
"_BaseAddrIPv6": 0,
7376
"_IndexBaseAddr": 0,
74-
"_IndexBaseAddrIPv6": 0
77+
"_IndexBaseAddrIPv6": 0,
78+
"_ProductCode": 0,
79+
"_ProductType": 0,
80+
"_FileSize": 0
7581
};
7682

7783
var modes = {
@@ -88,13 +94,15 @@ var modes = {
8894
"AS": 11,
8995
"LAST_SEEN": 12,
9096
"THREAT": 13,
97+
"PROVIDER": 14,
9198
"ALL": 100
9299
};
93100

94101
var MSG_NOT_SUPPORTED = "NOT SUPPORTED";
95102
var MSG_INVALID_IP = "INVALID IP ADDRESS";
96103
var MSG_MISSING_FILE = "MISSING FILE";
97104
var MSG_IPV6_UNSUPPORTED = "IPV6 ADDRESS MISSING IN IPV4 BIN";
105+
var MSG_INVALID_BIN = "Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file.";
98106

99107
// Read row data
100108
function readrow(readbytes, pos) {
@@ -249,26 +257,21 @@ function loadbin() {
249257
mydb._BaseAddrIPv6 = read32(18);
250258
mydb._IndexBaseAddr = read32(22);
251259
mydb._IndexBaseAddrIPv6 = read32(26);
260+
mydb._ProductCode = read8(30);
261+
// below 2 fields just read for now, not being used yet
262+
mydb._ProductType = read8(31);
263+
mydb._FileSize = read32(32);
264+
265+
// check if is correct BIN (should be 2 for IP2Proxy BIN file), also checking for zipped file (PK being the first 2 chars)
266+
if ((mydb._ProductCode != 2 && mydb._DBYear >= 21) || (mydb._DBType == 80 && mydb._DBColumn == 75)) { // only BINs from Jan 2021 onwards have this byte set
267+
throw new Error(MSG_INVALID_BIN);
268+
}
252269

253270
IPv4ColumnSize = mydb._DBColumn << 2; // 4 bytes each column
254271
IPv6ColumnSize = 16 + ((mydb._DBColumn - 1) << 2); // 4 bytes each column, except IPFrom column which is 16 bytes
255272

256273
var dbt = mydb._DBType;
257274

258-
// since both IPv4 and IPv6 use 4 bytes for the below columns, can just do it once here
259-
// country_pos_offset = (country_pos[dbt] != 0) ? (country_pos[dbt] - 1) << 2 : 0;
260-
// region_pos_offset = (region_pos[dbt] != 0) ? (region_pos[dbt] - 1) << 2 : 0;
261-
// city_pos_offset = (city_pos[dbt] != 0) ? (city_pos[dbt] - 1) << 2 : 0;
262-
// isp_pos_offset = (isp_pos[dbt] != 0) ? (isp_pos[dbt] - 1) << 2 : 0;
263-
// proxytype_pos_offset = (proxytype_pos[dbt] != 0) ? (proxytype_pos[dbt] - 1) << 2 : 0;
264-
// domain_pos_offset = (domain_pos[dbt] != 0) ? (domain_pos[dbt] - 1) << 2 : 0;
265-
// usagetype_pos_offset = (usagetype_pos[dbt] != 0) ? (usagetype_pos[dbt] - 1) << 2 : 0;
266-
// asn_pos_offset = (asn_pos[dbt] != 0) ? (asn_pos[dbt] - 1) << 2 : 0;
267-
// as_pos_offset = (as_pos[dbt] != 0) ? (as_pos[dbt] - 1) << 2 : 0;
268-
// lastseen_pos_offset = (lastseen_pos[dbt] != 0) ? (lastseen_pos[dbt] - 1) << 2 : 0;
269-
// threat_pos_offset = (threat_pos[dbt] != 0) ? (threat_pos[dbt] - 1) << 2 : 0;
270-
271-
// slightly different offset for reading by row
272275
country_pos_offset = (country_pos[dbt] != 0) ? (country_pos[dbt] - 2) << 2 : 0;
273276
region_pos_offset = (region_pos[dbt] != 0) ? (region_pos[dbt] - 2) << 2 : 0;
274277
city_pos_offset = (city_pos[dbt] != 0) ? (city_pos[dbt] - 2) << 2 : 0;
@@ -280,6 +283,7 @@ function loadbin() {
280283
as_pos_offset = (as_pos[dbt] != 0) ? (as_pos[dbt] - 2) << 2 : 0;
281284
lastseen_pos_offset = (lastseen_pos[dbt] != 0) ? (lastseen_pos[dbt] - 2) << 2 : 0;
282285
threat_pos_offset = (threat_pos[dbt] != 0) ? (threat_pos[dbt] - 2) << 2 : 0;
286+
provider_pos_offset = (provider_pos[dbt] != 0) ? (provider_pos[dbt] - 2) << 2 : 0;
283287

284288
country_enabled = (country_pos[dbt] != 0) ? 1 : 0;
285289
region_enabled = (region_pos[dbt] != 0) ? 1 : 0;
@@ -292,6 +296,7 @@ function loadbin() {
292296
as_enabled = (as_pos[dbt] != 0) ? 1 : 0;
293297
lastseen_enabled = (lastseen_pos[dbt] != 0) ? 1 : 0;
294298
threat_enabled = (threat_pos[dbt] != 0) ? 1 : 0;
299+
provider_enabled = (provider_pos[dbt] != 0) ? 1 : 0;
295300

296301
var pointer = mydb._IndexBaseAddr;
297302

@@ -350,6 +355,9 @@ exports.Close = function Close() {
350355
mydb._DBCountIPv6 = 0;
351356
mydb._IndexBaseAddr = 0;
352357
mydb._IndexBaseAddrIPv6 = 0;
358+
mydb._ProductCode = 0;
359+
mydb._ProductType = 0;
360+
mydb._FileSize = 0;
353361
return 0;
354362
}
355363
catch(err) {
@@ -366,8 +374,6 @@ function loadmesg(data, mesg) {
366374
}
367375

368376
function proxyquery_data(myIP, iptype, data, mode) {
369-
_DBType = mydb._DBType;
370-
_DBColumn = mydb._DBColumn;
371377
low = 0;
372378
mid = 0;
373379
high = 0;
@@ -442,21 +448,18 @@ function proxyquery_data(myIP, iptype, data, mode) {
442448
var firstcol = 4;
443449
if (iptype == 6) { // IPv6
444450
firstcol = 16;
445-
// rowoffset = rowoffset + 12; // coz below is assuming all columns are 4 bytes, so got 12 left to go to make 16 bytes total
446451
}
447452

448453
var row = readrow(_ColumnSize - firstcol, rowoffset + firstcol);
449454

450455
if (proxytype_enabled) {
451456
if (mode == modes.ALL || mode == modes.PROXY_TYPE || mode == modes.IS_PROXY) {
452-
// data.Proxy_Type = readstr(read32(rowoffset + proxytype_pos_offset));
453457
data.Proxy_Type = readstr(read32_row(proxytype_pos_offset, row));
454458
}
455459
}
456460

457461
if (country_enabled) {
458462
if (mode == modes.ALL || mode == modes.COUNTRY_SHORT || mode == modes.COUNTRY_LONG || mode == modes.IS_PROXY) {
459-
// countrypos = read32(rowoffset + country_pos_offset);
460463
countrypos = read32_row(country_pos_offset, row);
461464
}
462465
if (mode == modes.ALL || mode == modes.COUNTRY_SHORT || mode == modes.IS_PROXY) {
@@ -469,59 +472,55 @@ function proxyquery_data(myIP, iptype, data, mode) {
469472

470473
if (region_enabled) {
471474
if (mode == modes.ALL || mode == modes.REGION) {
472-
// data.Region = readstr(read32(rowoffset + region_pos_offset));
473475
data.Region = readstr(read32_row(region_pos_offset, row));
474476
}
475477
}
476478

477479
if (city_enabled) {
478480
if (mode == modes.ALL || mode == modes.CITY) {
479-
// data.City = readstr(read32(rowoffset + city_pos_offset));
480481
data.City = readstr(read32_row(city_pos_offset, row));
481482
}
482483
}
483484
if (isp_enabled) {
484485
if (mode == modes.ALL || mode == modes.ISP) {
485-
// data.ISP = readstr(read32(rowoffset + isp_pos_offset));
486486
data.ISP = readstr(read32_row(isp_pos_offset, row));
487487
}
488488
}
489489
if (domain_enabled) {
490490
if (mode == modes.ALL || mode == modes.DOMAIN) {
491-
// data.Domain = readstr(read32(rowoffset + domain_pos_offset));
492491
data.Domain = readstr(read32_row(domain_pos_offset, row));
493492
}
494493
}
495494
if (usagetype_enabled) {
496495
if (mode == modes.ALL || mode == modes.USAGE_TYPE) {
497-
// data.Usage_Type = readstr(read32(rowoffset + usagetype_pos_offset));
498496
data.Usage_Type = readstr(read32_row(usagetype_pos_offset, row));
499497
}
500498
}
501499
if (asn_enabled) {
502500
if (mode == modes.ALL || mode == modes.ASN) {
503-
// data.ASN = readstr(read32(rowoffset + asn_pos_offset));
504501
data.ASN = readstr(read32_row(asn_pos_offset, row));
505502
}
506503
}
507504
if (as_enabled) {
508505
if (mode == modes.ALL || mode == modes.AS) {
509-
// data.AS = readstr(read32(rowoffset + as_pos_offset));
510506
data.AS = readstr(read32_row(as_pos_offset, row));
511507
}
512508
}
513509
if (lastseen_enabled) {
514510
if (mode == modes.ALL || mode == modes.LAST_SEEN) {
515-
// data.Last_Seen = readstr(read32(rowoffset + lastseen_pos_offset));
516511
data.Last_Seen = readstr(read32_row(lastseen_pos_offset, row));
517512
}
518513
}
519514
if (threat_enabled) {
520515
if (mode == modes.ALL || mode == modes.THREAT) {
521-
// data.Threat = readstr(read32(rowoffset + threat_pos_offset));
522516
data.Threat = readstr(read32_row(threat_pos_offset, row));
523517
}
524518
}
519+
if (provider_enabled) {
520+
if (mode == modes.ALL || mode == modes.PROVIDER) {
521+
data.Provider = readstr(read32_row(provider_pos_offset, row));
522+
}
523+
}
525524

526525
if (data.Country_Short == "-" || data.Proxy_Type == "-") {
527526
data.Is_Proxy = 0;
@@ -564,7 +563,8 @@ function proxyquery(myIP, mode) {
564563
"ASN": "?",
565564
"AS": "?",
566565
"Last_Seen": "?",
567-
"Threat": "?"
566+
"Threat": "?",
567+
"Provider": "?"
568568
};
569569

570570
if (/^[:0]+:F{4}:(\d+\.){3}\d+$/i.test(myIP)) {
@@ -695,6 +695,11 @@ exports.getThreat = function getThreat(myIP) {
695695
return data.Threat;
696696
}
697697

698+
// Returns a string for the provider
699+
exports.getProvider = function getProvider(myIP) {
700+
data = proxyquery(myIP, modes.PROVIDER);
701+
return data.Provider;
702+
}
698703
// Returns all results
699704
exports.getAll = function getAll(myIP) {
700705
data = proxyquery(myIP, modes.ALL);

src/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// var ip2proxy = require("./ip2proxy.js");
22
var ip2proxy = require("ip2proxy-nodejs");
33

4-
ip2proxy.Open("PX10.BIN");
4+
ip2proxy.Open("PX11.BIN");
55

66
testip = ['8.8.8.8', '199.83.103.79'];
77

0 commit comments

Comments
 (0)