Skip to content

Commit 2f93db8

Browse files
Added support for district, ASN and AS
1 parent c237fab commit 2f93db8

File tree

5 files changed

+127
-59
lines changed

5 files changed

+127
-59
lines changed

LICENSE

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) 2022 ip2location
3+
Copyright (c) 2023 ip2location
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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# IP2Location Lua Package
22

3-
This Lua package 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 package 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.
3+
This Lua package 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 package 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.
44

55
This package can be used in many types of projects such as:
66

@@ -71,14 +71,17 @@ Below are the methods supported in this package.
7171
|get_usagetype|Returns the usage type.|
7272
|get_addresstype|Returns the address type.|
7373
|get_category|Returns the IAB category.|
74+
|get_district|Returns the district name.|
75+
|get_asn|Returns the autonomous system number.|
76+
|get_as|Returns the autonomous system.|
7477
|close|Closes BIN file and resets metadata.|
7578

7679
## Usage
7780

7881
```lua
7982
ip2location = require('ip2location')
8083

81-
local ip2loc = ip2location:new('IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY.BIN')
84+
local ip2loc = ip2location:new('IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN.BIN')
8285

8386
local result = ip2loc:get_all('8.8.8.8')
8487

@@ -104,6 +107,9 @@ print("elevation: " .. result.elevation)
104107
print("usagetype: " .. result.usagetype)
105108
print("addresstype: " .. result.addresstype)
106109
print("category: " .. result.category)
110+
print("district: " .. result.district)
111+
print("asn: " .. result.asn)
112+
print("as: " .. result.as)
107113

108114
ip2loc:close()
109115

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package = "ip2location"
2-
version = "8.6.1-1"
2+
version = "8.7.0-1"
33
source = {
44
url = "git://github.com/ip2location/ip2location-lua.git"
55
}
@@ -8,12 +8,14 @@ description = {
88
detailed = [[
99
This Lua package provides a fast lookup of country, region, city, latitude, longitude, ZIP code,
1010
time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name,
11-
mcc, mnc, mobile brand, elevation, usage type, address type and IAB category from IP address by using
12-
IP2Location database. This package uses a file based database available at IP2Location.com. This database
13-
simply contains IP blocks as keys, and other information such as country, region, city, latitude, longitude,
14-
ZIP code, time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name,
15-
mcc, mnc, mobile brand, elevation, usage type, address type and IAB category as values. It supports both
16-
IP address in IPv4 and IPv6.
11+
mcc, mnc, mobile brand, elevation, usage type, address type, IAB category, district,
12+
autonomous system number (ASN) and autonomous system (AS) from IP address by using IP2Location database.
13+
This package uses a file based database available at IP2Location.com. This database simply contains IP
14+
blocks as keys, and other information such as country, region, city, latitude, longitude, ZIP code,
15+
time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name,
16+
mcc, mnc, mobile brand, elevation, usage type, address type, IAB category, district,
17+
autonomous system number (ASN) and autonomous system (AS) as values. It supports both IP address
18+
in IPv4 and IPv6.
1719
]],
1820
homepage = "https://www.ip2location.com/development-libraries/ip2location/lua",
1921
license = "MIT",

ip2location.lua

Lines changed: 105 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ ip2location = {
4141
usagetype_position_offset = 0,
4242
addresstype_position_offset = 0,
4343
category_position_offset = 0,
44+
district_position_offset = 0,
45+
asn_position_offset = 0,
46+
as_position_offset = 0,
4447
country_enabled = false,
4548
region_enabled = false,
4649
city_enabled = false,
@@ -61,7 +64,10 @@ ip2location = {
6164
elevation_enabled = false,
6265
usagetype_enabled = false,
6366
addresstype_enabled = false,
64-
category_enabled = false
67+
category_enabled = false,
68+
district_enabled = false,
69+
asn_enabled = false,
70+
as_enabled = false
6571
}
6672
ip2location.__index = ip2location
6773

@@ -87,7 +93,10 @@ ip2locationrecord = {
8793
elevation = 0,
8894
usagetype = '',
8995
addresstype = '',
90-
category = ''
96+
category = '',
97+
district = '',
98+
asn = '',
99+
as = ''
91100
}
92101
ip2locationrecord.__index = ip2locationrecord
93102

@@ -100,56 +109,62 @@ local to_6to4 = bn("42550872755692912415807417417958686719")
100109
local from_teredo = bn("42540488161975842760550356425300246528")
101110
local to_teredo = bn("42540488241204005274814694018844196863")
102111

103-
local country_position = {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}
104-
local region_position = {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}
105-
local city_position = {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}
106-
local isp_position = {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}
107-
local latitude_position = {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}
108-
local longitude_position = {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}
109-
local domain_position = {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}
110-
local zipcode_position = {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}
111-
local timezone_position = {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}
112-
local netspeed_position = {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}
113-
local iddcode_position = {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}
114-
local areacode_position = {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}
115-
local weatherstationcode_position = {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}
116-
local weatherstationname_position = {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}
117-
local mcc_position = {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}
118-
local mnc_position = {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}
119-
local mobilebrand_position = {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}
120-
local elevation_position = {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}
121-
local usagetype_position = {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}
122-
local addresstype_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, 21}
123-
local 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, 22}
124-
125-
local api_version = "8.6.1"
112+
local country_position = {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}
113+
local region_position = {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}
114+
local city_position = {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}
115+
local isp_position = {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}
116+
local latitude_position = {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}
117+
local longitude_position = {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}
118+
local domain_position = {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}
119+
local zipcode_position = {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}
120+
local timezone_position = {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}
121+
local netspeed_position = {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}
122+
local iddcode_position = {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}
123+
local areacode_position = {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}
124+
local weatherstationcode_position = {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}
125+
local weatherstationname_position = {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}
126+
local mcc_position = {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}
127+
local mnc_position = {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}
128+
local mobilebrand_position = {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}
129+
local elevation_position = {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}
130+
local usagetype_position = {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}
131+
local addresstype_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, 21, 21}
132+
local 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, 22, 22}
133+
local 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, 23}
134+
local 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, 24}
135+
local 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, 25}
136+
137+
local api_version = "8.7.0"
126138

127139
local modes = {
128-
countryshort = 0x000001,
129-
countrylong = 0x000002,
130-
region = 0x000004,
131-
city = 0x000008,
132-
isp = 0x000010,
133-
latitude = 0x000020,
134-
longitude = 0x000040,
135-
domain = 0x000080,
136-
zipcode = 0x000100,
137-
timezone = 0x000200,
138-
netspeed = 0x000400,
139-
iddcode = 0x000800,
140-
areacode = 0x001000,
141-
weatherstationcode = 0x002000,
142-
weatherstationname = 0x004000,
143-
mcc = 0x008000,
144-
mnc = 0x010000,
145-
mobilebrand = 0x020000,
146-
elevation = 0x040000,
147-
usagetype = 0x080000,
148-
addresstype = 0x100000,
149-
category = 0x200000
140+
countryshort = 0x0000001,
141+
countrylong = 0x0000002,
142+
region = 0x0000004,
143+
city = 0x0000008,
144+
isp = 0x0000010,
145+
latitude = 0x0000020,
146+
longitude = 0x0000040,
147+
domain = 0x0000080,
148+
zipcode = 0x0000100,
149+
timezone = 0x0000200,
150+
netspeed = 0x0000400,
151+
iddcode = 0x0000800,
152+
areacode = 0x0001000,
153+
weatherstationcode = 0x0002000,
154+
weatherstationname = 0x0004000,
155+
mcc = 0x0008000,
156+
mnc = 0x0010000,
157+
mobilebrand = 0x0020000,
158+
elevation = 0x0040000,
159+
usagetype = 0x0080000,
160+
addresstype = 0x0100000,
161+
category = 0x0200000,
162+
district = 0x0400000,
163+
asn = 0x0800000,
164+
as = 0x1000000
150165
}
151166

152-
modes.all = modes.countryshort | modes.countrylong | modes.region | modes.city | modes.isp | modes.latitude | modes.longitude | modes.domain | modes.zipcode | modes.timezone | modes.netspeed | modes.iddcode | modes.areacode | modes.weatherstationcode | modes.weatherstationname | modes.mcc | modes.mnc | modes.mobilebrand | modes.elevation | modes.usagetype | modes.addresstype | modes.category
167+
modes.all = modes.countryshort | modes.countrylong | modes.region | modes.city | modes.isp | modes.latitude | modes.longitude | modes.domain | modes.zipcode | modes.timezone | modes.netspeed | modes.iddcode | modes.areacode | modes.weatherstationcode | modes.weatherstationname | modes.mcc | modes.mnc | modes.mobilebrand | modes.elevation | modes.usagetype | modes.addresstype | modes.category | modes.district | modes.asn | modes.as
153168

154169
local invalid_address = "Invalid IP address."
155170
local missing_file = "Invalid database file."
@@ -409,6 +424,18 @@ function ip2location:new(dbpath)
409424
x.category_position_offset = (category_position[dbt] - 2) * 4
410425
x.category_enabled = true
411426
end
427+
if district_position[dbt] ~= 0 then
428+
x.district_position_offset = (district_position[dbt] - 2) * 4
429+
x.district_enabled = true
430+
end
431+
if asn_position[dbt] ~= 0 then
432+
x.asn_position_offset = (asn_position[dbt] - 2) * 4
433+
x.asn_enabled = true
434+
end
435+
if as_position[dbt] ~= 0 then
436+
x.as_position_offset = (as_position[dbt] - 2) * 4
437+
x.as_enabled = true
438+
end
412439

413440
x.metaok = true
414441
-- printme(x)
@@ -564,6 +591,9 @@ function ip2locationrecord:loadmessage(mesg)
564591
x.usagetype = mesg
565592
x.addresstype = mesg
566593
x.category = mesg
594+
x.district = mesg
595+
x.asn = mesg
596+
x.as = mesg
567597
return x
568598
end
569599

@@ -740,10 +770,22 @@ function ip2location:query(ipaddress, mode)
740770
if (mode&modes.addresstype ~= 0) and (self.addresstype_enabled == true) then
741771
result.addresstype = readstr(readuint32row(self.addresstype_position_offset, row):asnumber(), self.f)
742772
end
773+
743774
if (mode&modes.category ~= 0) and (self.category_enabled == true) then
744775
result.category = readstr(readuint32row(self.category_position_offset, row):asnumber(), self.f)
745776
end
746777

778+
if (mode&modes.district ~= 0) and (self.district_enabled == true) then
779+
result.district = readstr(readuint32row(self.district_position_offset, row):asnumber(), self.f)
780+
end
781+
782+
if (mode&modes.asn ~= 0) and (self.asn_enabled == true) then
783+
result.asn = readstr(readuint32row(self.asn_position_offset, row):asnumber(), self.f)
784+
end
785+
786+
if (mode&modes.as ~= 0) and (self.as_enabled == true) then
787+
result.as = readstr(readuint32row(self.as_position_offset, row):asnumber(), self.f)
788+
end
747789
-- printme(result)
748790

749791
-- Lua style where you must have "return" as the last statement in a block
@@ -878,4 +920,19 @@ function ip2location:get_category(ipaddress)
878920
return self:query(ipaddress, modes.category)
879921
end
880922

923+
-- get district
924+
function ip2location:get_district(ipaddress)
925+
return self:query(ipaddress, modes.district)
926+
end
927+
928+
-- get autonomous system number (ASN)
929+
function ip2location:get_asn(ipaddress)
930+
return self:query(ipaddress, modes.asn)
931+
end
932+
933+
-- get autonomous system (AS)
934+
function ip2location:get_as(ipaddress)
935+
return self:query(ipaddress, modes.as)
936+
end
937+
881938
return ip2location

test.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ip2location = require('ip2location')
22

3-
local ip2loc = ip2location:new('IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY.BIN')
3+
local ip2loc = ip2location:new('IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-ADDRESSTYPE-CATEGORY-DISTRICT-ASN.BIN')
44

55
local result = ip2loc:get_all('8.8.8.8')
66

@@ -26,5 +26,8 @@ print("elevation: " .. result.elevation)
2626
print("usagetype: " .. result.usagetype)
2727
print("addresstype: " .. result.addresstype)
2828
print("category: " .. result.category)
29+
print("district: " .. result.district)
30+
print("asn: " .. result.asn)
31+
print("as: " .. result.as)
2932

3033
ip2loc:close()

0 commit comments

Comments
 (0)