From 58555343007eb845678123b67bfed3e1fbd66f14 Mon Sep 17 00:00:00 2001 From: IP2Location Date: Thu, 27 Apr 2023 15:26:11 +0800 Subject: [PATCH] Added support for district, ASN and AS --- README.md | 7 ++- package.json | 2 +- src/ip2location.d.ts | 27 +++++++++++ src/ip2location.js | 104 ++++++++++++++++++++++++++++++++++++++----- src/test.js | 5 ++- 5 files changed, 129 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index eeb5d9b..d063be8 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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.| @@ -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']; diff --git a/package.json b/package.json index 1b6c19f..69513df 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ip2location-nodejs", - "version": "9.4.3", + "version": "9.5.0", "description": "IP2Location geolocation component", "keywords": [ "ip2location", diff --git a/src/ip2location.d.ts b/src/ip2location.d.ts index cc9df08..bbc62a9 100644 --- a/src/ip2location.d.ts +++ b/src/ip2location.d.ts @@ -154,6 +154,9 @@ export class IP2Location { usageType: string; addressType: string; category: string; + district: string; + asn: string; + as: string; }; /** * Returns the API version. @@ -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. * @@ -358,6 +382,9 @@ export class IP2Location { usageType: string; addressType: string; category: string; + district: string; + asn: string; + as: string; }; #private; } diff --git a/src/ip2location.js b/src/ip2location.js index 04f6f5a..4661730 100644 --- a/src/ip2location.js +++ b/src/ip2location.js @@ -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"); @@ -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 = @@ -171,6 +196,9 @@ class IP2Location { #usageTypePositionOffset = 0; #addressTypePositionOffset = 0; #categoryPositionOffset = 0; + #districtPositionOffset = 0; + #asnPositionOffset = 0; + #asPositionOffset = 0; #countryEnabled = 0; #regionEnabled = 0; @@ -193,6 +221,9 @@ class IP2Location { #usageTypeEnabled = 0; #addressTypeEnabled = 0; #categoryEnabled = 0; + #districtEnabled = 0; + #asnEnabled = 0; + #asEnabled = 0; #myDB = { dbType: 0, @@ -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; @@ -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; @@ -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) { @@ -823,6 +882,9 @@ class IP2Location { usageType: "?", addressType: "?", category: "?", + district: "?", + asn: "?", + as: "?", }; if (REGEX_IPV4_1_MATCH.test(myIP)) { @@ -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); diff --git a/src/test.js b/src/test.js index 612a545..5bf4cfe 100644 --- a/src/test.js +++ b/src/test.js @@ -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']; @@ -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("=================================================================="); }