Skip to content

Commit 9a97c53

Browse files
committed
Added support for AS domain, AS usage type and AS CIDR
1 parent 28b8cf2 commit 9a97c53

File tree

9 files changed

+84
-16
lines changed

9 files changed

+84
-16
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) 2023 - 2024 IP2Location
3+
Copyright (c) 2023 - 2025 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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# IP2Location Lua Package
22
[![LuaRocks](https://img.shields.io/luarocks/v/ip2location/ip2location)](https://luarocks.org/modules/ip2location/ip2location)
33

4-
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.
4+
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), autonomous system (AS), AS domain, AS usage type and AS CIDR as values. It supports both IP address in IPv4 and IPv6.
55

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

@@ -17,9 +17,6 @@ The database will be updated in monthly basis for the greater accuracy. Free LIT
1717

1818
The paid databases are available at https://www.ip2location.com under Premium subscription package.
1919

20-
As an alternative, this library can also call the IP2Location Web Service. This requires an API key. If you don't have an existing API key, you can subscribe for one at the below:
21-
22-
https://www.ip2location.com/web-service/ip2location
2320

2421
## Developer Documentation
2522
To learn more about installation, usage, and code examples, please visit the developer documentation at [https://ip2location-lua.readthedocs.io/en/latest/index.html.](https://ip2location-lua.readthedocs.io/en/latest/index.html)

docs/source/code.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ Retrieve geolocation information for an IP address.
4747
| district | District or county name. |
4848
| asn | Autonomous system number (ASN). BIN databases. |
4949
| as | Autonomous system (AS) name. |
50+
| asdomain | Domain name of the AS registrant. |
51+
| asusagetype | Usage type of the AS registrant. |
52+
| ascidr | CIDR range for the whole AS. |
5053
```

docs/source/conf.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
# -- Project information
77

88
project = 'IP2Location Lua'
9-
copyright = '2023, IP2Location'
9+
copyright = '2025, IP2Location'
1010
author = 'IP2Location'
1111

12-
release = '1.0.0'
13-
version = '1.0.0'
12+
release = '8.8.0'
13+
version = '8.8.0'
1414

1515
# -- General configuration
1616

@@ -61,4 +61,4 @@
6161

6262
html_title = "IP2Location Lua"
6363

64-
# html_baseurl = "https://ip2location-d.readthedocs.io/en/latest/"
64+
# html_baseurl = "https://ip2location-lua.readthedocs.io/en/latest/"

docs/source/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# IP2Location Lua Package
22
[![LuaRocks](https://img.shields.io/luarocks/v/ip2location/ip2location)](https://luarocks.org/modules/ip2location/ip2location)
33

4-
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.
4+
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), autonomous system (AS), AS domain, AS usage type and AS CIDR as values. It supports both IP address in IPv4 and IPv6.
55

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

docs/source/quickstart.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ print("category: " .. result.category)
5555
print("district: " .. result.district)
5656
print("asn: " .. result.asn)
5757
print("as: " .. result.as)
58+
print("asdomain: " .. result.asdomain)
59+
print("asusagetype: " .. result.asusagetype)
60+
print("ascidr: " .. result.ascidr)
5861

5962
ip2loc:close()
6063
```
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package = "ip2location"
2-
version = "8.7.1-1"
2+
version = "8.8.0-1"
33
source = {
44
url = "git://github.com/ip2location/ip2location-lua.git"
55
}
@@ -14,10 +14,10 @@ description = {
1414
blocks as keys, and other information such as country, region, city, latitude, longitude, ZIP code,
1515
time zone, ISP, domain name, connection type, IDD code, area code, weather station code, station name,
1616
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.
17+
autonomous system number (ASN), autonomous system (AS), AS domain, AS usage type, AS CIDR as values.
18+
It supports both IP address in IPv4 and IPv6.
1919
]],
20-
homepage = "https://www.ip2location.com/development-libraries/ip2location/lua",
20+
homepage = "https://www.ip2location.com/documentation/ip2location-libraries/lua",
2121
license = "MIT",
2222
maintainer = "support@ip2location.com"
2323
}

ip2location.lua

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ ip2location = {
4444
district_position_offset = 0,
4545
asn_position_offset = 0,
4646
as_position_offset = 0,
47+
asdomain_position_offset = 0,
48+
asusagetype_position_offset = 0,
49+
ascidr_position_offset = 0,
4750
country_enabled = false,
4851
region_enabled = false,
4952
city_enabled = false,
@@ -68,6 +71,9 @@ ip2location = {
6871
district_enabled = false,
6972
asn_enabled = false,
7073
as_enabled = false,
74+
asdomain_enabled = false,
75+
asusagetype_enabled = false,
76+
ascidr_enabled = false,
7177
}
7278
ip2location.__index = ip2location
7379

@@ -97,6 +103,9 @@ ip2locationrecord = {
97103
district = "",
98104
asn = "",
99105
as = "",
106+
asdomain = "",
107+
asusagetype = "",
108+
ascidr = "",
100109
}
101110
ip2locationrecord.__index = ip2locationrecord
102111

@@ -135,8 +144,11 @@ local category_position = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
135144
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 }
136145
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 }
137146
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 }
147+
local asdomain_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, 26 }
148+
local asusagetype_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, 27 }
149+
local ascidr_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, 28 }
138150

139-
local api_version = "8.7.1"
151+
local api_version = "8.8.0"
140152

141153
local modes = {
142154
countryshort = 0x0000001,
@@ -164,6 +176,9 @@ local modes = {
164176
district = 0x0400000,
165177
asn = 0x0800000,
166178
as = 0x1000000,
179+
asdomain = 0x2000000,
180+
asusagetype = 0x4000000,
181+
ascidr = 0x8000000,
167182
}
168183

169184
modes.all = modes.countryshort
@@ -191,6 +206,9 @@ modes.all = modes.countryshort
191206
| modes.district
192207
| modes.asn
193208
| modes.as
209+
| modes.asdomain
210+
| modes.asusagetype
211+
| modes.ascidr
194212

195213
local invalid_address = "Invalid IP address."
196214
local missing_file = "Invalid database file."
@@ -467,6 +485,18 @@ function ip2location:new(dbpath)
467485
x.as_position_offset = (as_position[dbt] - 2) * 4
468486
x.as_enabled = true
469487
end
488+
if asdomain_position[dbt] ~= 0 then
489+
x.asdomain_position_offset = (asdomain_position[dbt] - 2) * 4
490+
x.asdomain_enabled = true
491+
end
492+
if asusagetype_position[dbt] ~= 0 then
493+
x.asusagetype_position_offset = (asusagetype_position[dbt] - 2) * 4
494+
x.asusagetype_enabled = true
495+
end
496+
if ascidr_position[dbt] ~= 0 then
497+
x.ascidr_position_offset = (ascidr_position[dbt] - 2) * 4
498+
x.ascidr_enabled = true
499+
end
470500

471501
x.metaok = true
472502
-- printme(x)
@@ -476,7 +506,9 @@ end
476506
-- close file and reset
477507
function ip2location:close()
478508
self.metaok = false
479-
io.close(self.f)
509+
if self.f ~= nil then
510+
io.close(self.f)
511+
end
480512
end
481513

482514
-- get IP type and calculate IP number; calculates index too if exists
@@ -639,6 +671,9 @@ function ip2locationrecord:loadmessage(mesg)
639671
x.district = mesg
640672
x.asn = mesg
641673
x.as = mesg
674+
x.asdomain = mesg
675+
x.asusagetype = mesg
676+
x.ascidr = mesg
642677
return x
643678
end
644679

@@ -839,6 +874,18 @@ function ip2location:query(ipaddress, mode)
839874
if (mode & modes.as ~= 0) and (self.as_enabled == true) then
840875
result.as = readstr(readuint32row(self.as_position_offset, row):asnumber(), self.f)
841876
end
877+
878+
if (mode & modes.asdomain ~= 0) and (self.asdomain_enabled == true) then
879+
result.asdomain = readstr(readuint32row(self.asdomain_position_offset, row):asnumber(), self.f)
880+
end
881+
882+
if (mode & modes.asusagetype ~= 0) and (self.asusagetype_enabled == true) then
883+
result.asusagetype = readstr(readuint32row(self.asusagetype_position_offset, row):asnumber(), self.f)
884+
end
885+
886+
if (mode & modes.ascidr ~= 0) and (self.ascidr_enabled == true) then
887+
result.ascidr = readstr(readuint32row(self.ascidr_position_offset, row):asnumber(), self.f)
888+
end
842889
-- printme(result)
843890

844891
-- Lua style where you must have "return" as the last statement in a block
@@ -988,4 +1035,19 @@ function ip2location:get_as(ipaddress)
9881035
return self:query(ipaddress, modes.as)
9891036
end
9901037

1038+
-- get AS domain
1039+
function ip2location:get_asdomain(ipaddress)
1040+
return self:query(ipaddress, modes.asdomain)
1041+
end
1042+
1043+
-- get AS usage type
1044+
function ip2location:get_asusagetype(ipaddress)
1045+
return self:query(ipaddress, modes.asusagetype)
1046+
end
1047+
1048+
-- get AS CIDR
1049+
function ip2location:get_ascidr(ipaddress)
1050+
return self:query(ipaddress, modes.ascidr)
1051+
end
1052+
9911053
return ip2location

test.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ print("category: " .. result.category)
2929
print("district: " .. result.district)
3030
print("asn: " .. result.asn)
3131
print("as: " .. result.as)
32+
print("asdomain: " .. result.asdomain)
33+
print("asusagetype: " .. result.asusagetype)
34+
print("ascidr: " .. result.ascidr)
3235

3336
ip2loc:close()

0 commit comments

Comments
 (0)