Skip to content

Commit 5855534

Browse files
committed
Added support for district, ASN and AS
1 parent 7d986cd commit 5855534

File tree

5 files changed

+129
-16
lines changed

5 files changed

+129
-16
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# IP2Location Node.js Module
55

6-
This Node.js module provides a fast lookup of country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, usage type, address type and IAB category from IP address by using IP2Location database. This module uses a file based database available at IP2Location.com. This database simply contains IP blocks as keys, and other information such as country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, usage type, address type and IAB category as values. It supports both IP address in IPv4 and IPv6.
6+
This Node.js module provides a fast lookup of country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, usage type, address type, IAB category, district, autonomous system number (ASN) and autonomous system (AS) from IP address by using IP2Location database. This module uses a file based database available at IP2Location.com. This database simply contains IP blocks as keys, and other information such as country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name, mcc, mnc, mobile brand, elevation, usage type, address type, IAB category, district, autonomous system number (ASN) and autonomous system (AS) as values. It supports both IP address in IPv4 and IPv6.
77

88
This module can be used in many types of projects such as:
99

@@ -81,6 +81,9 @@ Below are the methods supported in this module.
8181
|getUsageType|Returns the usage type.|
8282
|getAddressType|Returns the address type.|
8383
|getCategory|Returns the IAB category.|
84+
|getDistrict|Returns the district name.|
85+
|getASN|Returns the autonomous system number (ASN).|
86+
|getAS|Returns the autonomous system (AS).|
8487
|close|Closes BIN file and resets metadata.|
8588

8689

@@ -92,7 +95,7 @@ const {IP2Location} = require("ip2location-nodejs");
9295

9396
let ip2location = new IP2Location();
9497

95-
ip2location.open("./DB25.BIN");
98+
ip2location.open("./DB26.BIN");
9699

97100
testip = ['8.8.8.8', '2404:6800:4001:c01::67'];
98101

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ip2location-nodejs",
3-
"version": "9.4.3",
3+
"version": "9.5.0",
44
"description": "IP2Location geolocation component",
55
"keywords": [
66
"ip2location",

src/ip2location.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ export class IP2Location {
154154
usageType: string;
155155
addressType: string;
156156
category: string;
157+
district: string;
158+
asn: string;
159+
as: string;
157160
};
158161
/**
159162
* Returns the API version.
@@ -327,6 +330,27 @@ export class IP2Location {
327330
* @returns The IAB category.
328331
*/
329332
getCategory(myIP: string): string;
333+
/**
334+
* Returns the district name.
335+
*
336+
* @param myIP The IP address to query.
337+
* @returns The district name.
338+
*/
339+
getDistrict(myIP: string): string;
340+
/**
341+
* Returns the autonomous system number (ASN).
342+
*
343+
* @param myIP The IP address to query.
344+
* @returns The ASN.
345+
*/
346+
getASN(myIP: string): string;
347+
/**
348+
* Returns the autonomous system (AS).
349+
*
350+
* @param myIP The IP address to query.
351+
* @returns The AS.
352+
*/
353+
getAS(myIP: string): string;
330354
/**
331355
* Returns all fields.
332356
*
@@ -358,6 +382,9 @@ export class IP2Location {
358382
usageType: string;
359383
addressType: string;
360384
category: string;
385+
district: string;
386+
asn: string;
387+
as: string;
361388
};
362389
#private;
363390
}

src/ip2location.js

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,103 @@ var https = require("https");
44
const csv = require("csv-parser");
55

66
// For BIN queries
7-
const VERSION = "9.4.3";
7+
const VERSION = "9.5.0";
88
const MAX_INDEX = 65536;
99
const COUNTRY_POSITION = [
1010
0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
11+
2,
1112
];
1213
const REGION_POSITION = [
1314
0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
15+
3,
1416
];
1517
const CITY_POSITION = [
1618
0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
19+
4,
1720
];
1821
const ISP_POSITION = [
1922
0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 9, 7, 9, 9,
23+
9,
2024
];
2125
const LATITUDE_POSITION = [
2226
0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
27+
5,
2328
];
2429
const LONGITUDE_POSITION = [
2530
0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
31+
6,
2632
];
2733
const DOMAIN_POSITION = [
2834
0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8,
29-
10, 10,
35+
10, 10, 10,
3036
];
3137
const ZIP_CODE_POSITION = [
3238
0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, 7, 0, 7, 7,
39+
7,
3340
];
3441
const TIME_ZONE_POSITION = [
3542
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8, 0, 8, 8, 8, 0, 8, 8,
43+
8,
3644
];
3745
const NET_SPEED_POSITION = [
3846
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11, 8, 11, 0, 11, 0, 11, 0,
39-
11, 11,
47+
11, 11, 11,
4048
];
4149
const IDD_CODE_POSITION = [
4250
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0,
43-
12, 12,
51+
12, 12, 12,
4452
];
4553
const AREA_CODE_POSITION = [
4654
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 13, 0, 13, 0, 13, 10, 13, 0,
47-
13, 13,
55+
13, 13, 13,
4856
];
4957
const WEATHER_STATION_CODE_POSITION = [
5058
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 14, 0, 14, 0, 14,
51-
14,
59+
14, 14,
5260
];
5361
const WEATHER_STATION_NAME_POSITION = [
5462
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0,
55-
15, 15,
63+
15, 15, 15,
5664
];
5765
const MCC_POSITION = [
5866
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 16, 0, 16, 9, 16,
59-
16,
67+
16, 16,
6068
];
6169
const MNC_POSITION = [
6270
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 17, 0, 17, 10,
63-
17, 17,
71+
17, 17, 17,
6472
];
6573
const MOBILE_BRAND_POSITION = [
6674
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 18, 0, 18, 11,
67-
18, 18,
75+
18, 18, 18,
6876
];
6977
const ELEVATION_POSITION = [
7078
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19,
71-
19,
79+
19, 19,
7280
];
7381
const USAGE_TYPE_POSITION = [
7482
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20,
75-
20,
83+
20, 20,
7684
];
7785
const ADDRESS_TYPE_POSITION = [
7886
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21,
87+
21,
7988
];
8089
const CATEGORY_POSITION = [
8190
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22,
91+
22,
92+
];
93+
const DISTRICT_POSITION = [
94+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95+
23,
96+
];
97+
const ASN_POSITION = [
98+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99+
24,
100+
];
101+
const AS_POSITION = [
102+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103+
25,
82104
];
83105
const MAX_IPV4_RANGE = BigInt(4294967295);
84106
const MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
@@ -111,6 +133,9 @@ const MODES = {
111133
USAGE_TYPE: 20,
112134
ADDRESS_TYPE: 21,
113135
CATEGORY: 22,
136+
DISTRICT: 23,
137+
ASN: 24,
138+
AS: 25,
114139
ALL: 100,
115140
};
116141
const MSG_NOT_SUPPORTED =
@@ -171,6 +196,9 @@ class IP2Location {
171196
#usageTypePositionOffset = 0;
172197
#addressTypePositionOffset = 0;
173198
#categoryPositionOffset = 0;
199+
#districtPositionOffset = 0;
200+
#asnPositionOffset = 0;
201+
#asPositionOffset = 0;
174202

175203
#countryEnabled = 0;
176204
#regionEnabled = 0;
@@ -193,6 +221,9 @@ class IP2Location {
193221
#usageTypeEnabled = 0;
194222
#addressTypeEnabled = 0;
195223
#categoryEnabled = 0;
224+
#districtEnabled = 0;
225+
#asnEnabled = 0;
226+
#asEnabled = 0;
196227

197228
#myDB = {
198229
dbType: 0,
@@ -434,6 +465,12 @@ class IP2Location {
434465
: 0;
435466
this.#categoryPositionOffset =
436467
CATEGORY_POSITION[dbt] != 0 ? (CATEGORY_POSITION[dbt] - 2) << 2 : 0;
468+
this.#districtPositionOffset =
469+
DISTRICT_POSITION[dbt] != 0 ? (DISTRICT_POSITION[dbt] - 2) << 2 : 0;
470+
this.#asnPositionOffset =
471+
ASN_POSITION[dbt] != 0 ? (ASN_POSITION[dbt] - 2) << 2 : 0;
472+
this.#asPositionOffset =
473+
AS_POSITION[dbt] != 0 ? (AS_POSITION[dbt] - 2) << 2 : 0;
437474

438475
this.#countryEnabled = COUNTRY_POSITION[dbt] != 0 ? 1 : 0;
439476
this.#regionEnabled = REGION_POSITION[dbt] != 0 ? 1 : 0;
@@ -458,6 +495,9 @@ class IP2Location {
458495
this.#usageTypeEnabled = USAGE_TYPE_POSITION[dbt] != 0 ? 1 : 0;
459496
this.#addressTypeEnabled = ADDRESS_TYPE_POSITION[dbt] != 0 ? 1 : 0;
460497
this.#categoryEnabled = CATEGORY_POSITION[dbt] != 0 ? 1 : 0;
498+
this.#districtEnabled = DISTRICT_POSITION[dbt] != 0 ? 1 : 0;
499+
this.#asnEnabled = ASN_POSITION[dbt] != 0 ? 1 : 0;
500+
this.#asEnabled = AS_POSITION[dbt] != 0 ? 1 : 0;
461501

462502
if (this.#myDB.indexed == 1) {
463503
len = MAX_INDEX;
@@ -784,6 +824,25 @@ class IP2Location {
784824
);
785825
}
786826
}
827+
if (this.#districtEnabled) {
828+
if (mode == MODES.ALL || mode == MODES.DISTRICT) {
829+
data.district = this.readStr(
830+
this.read32Row(this.#districtPositionOffset, row)
831+
);
832+
}
833+
}
834+
if (this.#asnEnabled) {
835+
if (mode == MODES.ALL || mode == MODES.ASN) {
836+
data.asn = this.readStr(
837+
this.read32Row(this.#asnPositionOffset, row)
838+
);
839+
}
840+
}
841+
if (this.#asEnabled) {
842+
if (mode == MODES.ALL || mode == MODES.AS) {
843+
data.as = this.readStr(this.read32Row(this.#asPositionOffset, row));
844+
}
845+
}
787846
return;
788847
} else {
789848
if (ipFrom > ipNumber) {
@@ -823,6 +882,9 @@ class IP2Location {
823882
usageType: "?",
824883
addressType: "?",
825884
category: "?",
885+
district: "?",
886+
asn: "?",
887+
as: "?",
826888
};
827889

828890
if (REGEX_IPV4_1_MATCH.test(myIP)) {
@@ -1009,6 +1071,24 @@ class IP2Location {
10091071
return data.category;
10101072
}
10111073

1074+
// Return a string for the district name
1075+
getDistrict(myIP) {
1076+
let data = this.geoQuery(myIP, MODES.DISTRICT);
1077+
return data.district;
1078+
}
1079+
1080+
// Return a string for the autonomous system number (ASN)
1081+
getASN(myIP) {
1082+
let data = this.geoQuery(myIP, MODES.ASN);
1083+
return data.asn;
1084+
}
1085+
1086+
// Return a string for the autonomous system (AS)
1087+
getAS(myIP) {
1088+
let data = this.geoQuery(myIP, MODES.AS);
1089+
return data.as;
1090+
}
1091+
10121092
// Return all results
10131093
getAll(myIP) {
10141094
let data = this.geoQuery(myIP, MODES.ALL);

src/test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const {IP2Location, IP2LocationWebService, IPTools, Country, Region} = require("
22

33
let ip2location = new IP2Location();
44

5-
ip2location.open("./DB25.BIN");
5+
ip2location.open("./DB26.BIN");
66

77
testip = ['2001:0:4136:e378:8000:63bf:f7f7:f7f7', '2002:0803:2200::0803:2200'];
88

@@ -34,6 +34,9 @@ for (var x = 0; x < testip.length; x++) {
3434
console.log("usageType: " + ip2location.getUsageType(testip[x]));
3535
console.log("addressType: " + ip2location.getAddressType(testip[x]));
3636
console.log("category: " + ip2location.getCategory(testip[x]));
37+
console.log("district: " + ip2location.getDistrict(testip[x]));
38+
console.log("asn: " + ip2location.getASN(testip[x]));
39+
console.log("as: " + ip2location.getAS(testip[x]));
3740
console.log("==================================================================");
3841
}
3942

0 commit comments

Comments
 (0)