Skip to content

Commit

Permalink
Added support for district, ASN and AS
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Apr 27, 2023
1 parent 7d986cd commit 5855534
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 16 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# IP2Location Node.js Module

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.
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.

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

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


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

let ip2location = new IP2Location();

ip2location.open("./DB25.BIN");
ip2location.open("./DB26.BIN");

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ip2location-nodejs",
"version": "9.4.3",
"version": "9.5.0",
"description": "IP2Location geolocation component",
"keywords": [
"ip2location",
Expand Down
27 changes: 27 additions & 0 deletions src/ip2location.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ export class IP2Location {
usageType: string;
addressType: string;
category: string;
district: string;
asn: string;
as: string;
};
/**
* Returns the API version.
Expand Down Expand Up @@ -327,6 +330,27 @@ export class IP2Location {
* @returns The IAB category.
*/
getCategory(myIP: string): string;
/**
* Returns the district name.
*
* @param myIP The IP address to query.
* @returns The district name.
*/
getDistrict(myIP: string): string;
/**
* Returns the autonomous system number (ASN).
*
* @param myIP The IP address to query.
* @returns The ASN.
*/
getASN(myIP: string): string;
/**
* Returns the autonomous system (AS).
*
* @param myIP The IP address to query.
* @returns The AS.
*/
getAS(myIP: string): string;
/**
* Returns all fields.
*
Expand Down Expand Up @@ -358,6 +382,9 @@ export class IP2Location {
usageType: string;
addressType: string;
category: string;
district: string;
asn: string;
as: string;
};
#private;
}
Expand Down
104 changes: 92 additions & 12 deletions src/ip2location.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,103 @@ var https = require("https");
const csv = require("csv-parser");

// For BIN queries
const VERSION = "9.4.3";
const VERSION = "9.5.0";
const MAX_INDEX = 65536;
const COUNTRY_POSITION = [
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,
2,
];
const REGION_POSITION = [
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,
3,
];
const CITY_POSITION = [
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,
4,
];
const ISP_POSITION = [
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,
9,
];
const LATITUDE_POSITION = [
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,
5,
];
const LONGITUDE_POSITION = [
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,
6,
];
const DOMAIN_POSITION = [
0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8,
10, 10,
10, 10, 10,
];
const ZIP_CODE_POSITION = [
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,
7,
];
const TIME_ZONE_POSITION = [
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,
8,
];
const NET_SPEED_POSITION = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11, 8, 11, 0, 11, 0, 11, 0,
11, 11,
11, 11, 11,
];
const IDD_CODE_POSITION = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0,
12, 12,
12, 12, 12,
];
const AREA_CODE_POSITION = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 13, 0, 13, 0, 13, 10, 13, 0,
13, 13,
13, 13, 13,
];
const WEATHER_STATION_CODE_POSITION = [
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,
14,
14, 14,
];
const WEATHER_STATION_NAME_POSITION = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0,
15, 15,
15, 15, 15,
];
const MCC_POSITION = [
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,
16,
16, 16,
];
const MNC_POSITION = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 17, 0, 17, 10,
17, 17,
17, 17, 17,
];
const MOBILE_BRAND_POSITION = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 18, 0, 18, 11,
18, 18,
18, 18, 18,
];
const ELEVATION_POSITION = [
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,
19,
19, 19,
];
const USAGE_TYPE_POSITION = [
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,
20,
20, 20,
];
const ADDRESS_TYPE_POSITION = [
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,
21,
];
const CATEGORY_POSITION = [
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,
22,
];
const DISTRICT_POSITION = [
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,
23,
];
const ASN_POSITION = [
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,
24,
];
const AS_POSITION = [
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,
25,
];
const MAX_IPV4_RANGE = BigInt(4294967295);
const MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
Expand Down Expand Up @@ -111,6 +133,9 @@ const MODES = {
USAGE_TYPE: 20,
ADDRESS_TYPE: 21,
CATEGORY: 22,
DISTRICT: 23,
ASN: 24,
AS: 25,
ALL: 100,
};
const MSG_NOT_SUPPORTED =
Expand Down Expand Up @@ -171,6 +196,9 @@ class IP2Location {
#usageTypePositionOffset = 0;
#addressTypePositionOffset = 0;
#categoryPositionOffset = 0;
#districtPositionOffset = 0;
#asnPositionOffset = 0;
#asPositionOffset = 0;

#countryEnabled = 0;
#regionEnabled = 0;
Expand All @@ -193,6 +221,9 @@ class IP2Location {
#usageTypeEnabled = 0;
#addressTypeEnabled = 0;
#categoryEnabled = 0;
#districtEnabled = 0;
#asnEnabled = 0;
#asEnabled = 0;

#myDB = {
dbType: 0,
Expand Down Expand Up @@ -434,6 +465,12 @@ class IP2Location {
: 0;
this.#categoryPositionOffset =
CATEGORY_POSITION[dbt] != 0 ? (CATEGORY_POSITION[dbt] - 2) << 2 : 0;
this.#districtPositionOffset =
DISTRICT_POSITION[dbt] != 0 ? (DISTRICT_POSITION[dbt] - 2) << 2 : 0;
this.#asnPositionOffset =
ASN_POSITION[dbt] != 0 ? (ASN_POSITION[dbt] - 2) << 2 : 0;
this.#asPositionOffset =
AS_POSITION[dbt] != 0 ? (AS_POSITION[dbt] - 2) << 2 : 0;

this.#countryEnabled = COUNTRY_POSITION[dbt] != 0 ? 1 : 0;
this.#regionEnabled = REGION_POSITION[dbt] != 0 ? 1 : 0;
Expand All @@ -458,6 +495,9 @@ class IP2Location {
this.#usageTypeEnabled = USAGE_TYPE_POSITION[dbt] != 0 ? 1 : 0;
this.#addressTypeEnabled = ADDRESS_TYPE_POSITION[dbt] != 0 ? 1 : 0;
this.#categoryEnabled = CATEGORY_POSITION[dbt] != 0 ? 1 : 0;
this.#districtEnabled = DISTRICT_POSITION[dbt] != 0 ? 1 : 0;
this.#asnEnabled = ASN_POSITION[dbt] != 0 ? 1 : 0;
this.#asEnabled = AS_POSITION[dbt] != 0 ? 1 : 0;

if (this.#myDB.indexed == 1) {
len = MAX_INDEX;
Expand Down Expand Up @@ -784,6 +824,25 @@ class IP2Location {
);
}
}
if (this.#districtEnabled) {
if (mode == MODES.ALL || mode == MODES.DISTRICT) {
data.district = this.readStr(
this.read32Row(this.#districtPositionOffset, row)
);
}
}
if (this.#asnEnabled) {
if (mode == MODES.ALL || mode == MODES.ASN) {
data.asn = this.readStr(
this.read32Row(this.#asnPositionOffset, row)
);
}
}
if (this.#asEnabled) {
if (mode == MODES.ALL || mode == MODES.AS) {
data.as = this.readStr(this.read32Row(this.#asPositionOffset, row));
}
}
return;
} else {
if (ipFrom > ipNumber) {
Expand Down Expand Up @@ -823,6 +882,9 @@ class IP2Location {
usageType: "?",
addressType: "?",
category: "?",
district: "?",
asn: "?",
as: "?",
};

if (REGEX_IPV4_1_MATCH.test(myIP)) {
Expand Down Expand Up @@ -1009,6 +1071,24 @@ class IP2Location {
return data.category;
}

// Return a string for the district name
getDistrict(myIP) {
let data = this.geoQuery(myIP, MODES.DISTRICT);
return data.district;
}

// Return a string for the autonomous system number (ASN)
getASN(myIP) {
let data = this.geoQuery(myIP, MODES.ASN);
return data.asn;
}

// Return a string for the autonomous system (AS)
getAS(myIP) {
let data = this.geoQuery(myIP, MODES.AS);
return data.as;
}

// Return all results
getAll(myIP) {
let data = this.geoQuery(myIP, MODES.ALL);
Expand Down
5 changes: 4 additions & 1 deletion src/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {IP2Location, IP2LocationWebService, IPTools, Country, Region} = require("

let ip2location = new IP2Location();

ip2location.open("./DB25.BIN");
ip2location.open("./DB26.BIN");

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

Expand Down Expand Up @@ -34,6 +34,9 @@ for (var x = 0; x < testip.length; x++) {
console.log("usageType: " + ip2location.getUsageType(testip[x]));
console.log("addressType: " + ip2location.getAddressType(testip[x]));
console.log("category: " + ip2location.getCategory(testip[x]));
console.log("district: " + ip2location.getDistrict(testip[x]));
console.log("asn: " + ip2location.getASN(testip[x]));
console.log("as: " + ip2location.getAS(testip[x]));
console.log("==================================================================");
}

Expand Down

0 comments on commit 5855534

Please sign in to comment.