Skip to content

Commit 6a09a16

Browse files
committed
Added support for AS domain, AS usage type and AS CIDR
1 parent e9dd727 commit 6a09a16

File tree

7 files changed

+82
-12
lines changed

7 files changed

+82
-12
lines changed

LICENSE.TXT

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.com
3+
Copyright (c) 2023 - 2025 IP2Location.com
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# IP2Location Go Package
55

6-
This Go 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.
6+
This Go 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.
77

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

@@ -19,8 +19,6 @@ The database will be updated in monthly basis for the greater accuracy. Free LIT
1919

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

22-
As an alternative, this package 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:
23-
https://www.ip2location.com/web-service/ip2location
2422

2523
Developer Documentation
2624
=====================

docs/source/code.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ Retrieve geolocation information for an IP address.
4444
| District | District or county name. |
4545
| Asn | Autonomous system number (ASN). BIN databases. |
4646
| As | Autonomous system (AS) name. |
47+
| Asdomain | Domain name of the AS registrant. |
48+
| Asusagetype | Usage type of the AS registrant. |
49+
| Ascidr | CIDR range for the whole AS. |
4750
```
4851

4952
## IPTools Class

docs/source/conf.py

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

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

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

1515
# -- General configuration
1616

docs/source/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# IP2Location Go Package
55

6-
This Go 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.
6+
This Go 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.
77

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

docs/source/quickstart.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func main() {
7878
fmt.Printf("district: %s\n", results.District)
7979
fmt.Printf("asn: %s\n", results.Asn)
8080
fmt.Printf("as: %s\n", results.As)
81+
fmt.Printf("asdomain: %s\n", results.Asdomain)
82+
fmt.Printf("asusagetype: %s\n", results.Asusagetype)
83+
fmt.Printf("ascidr: %s\n", results.Ascidr)
8184
fmt.Printf("api version: %s\n", ip2location.Api_version())
8285

8386
db.Close()

ip2location.go

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This ip2location package provides a fast lookup of country, region, city, latitude, longitude, ZIP code, time zone,
22
// ISP, domain name, connection type, IDD code, area code, weather station code, station name, MCC, MNC,
3-
// mobile brand, elevation, usage type, address type, IAB category, district, autonomous system number (ASN) and
4-
// autonomous system (AS) from IP address by using IP2Location database.
3+
// mobile brand, elevation, usage type, address type, IAB category, district, autonomous system number (ASN),
4+
// autonomous system (AS), AS domain, AS usage type and AS CIDR from IP address by using IP2Location database.
55
package ip2location
66

77
import (
@@ -73,6 +73,9 @@ type IP2Locationrecord struct {
7373
District string
7474
Asn string
7575
As string
76+
Asdomain string
77+
Asusagetype string
78+
Ascidr string
7679
}
7780

7881
type DB struct {
@@ -103,6 +106,9 @@ type DB struct {
103106
district_position_offset uint32
104107
asn_position_offset uint32
105108
as_position_offset uint32
109+
asdomain_position_offset uint32
110+
asusagetype_position_offset uint32
111+
ascidr_position_offset uint32
106112

107113
country_enabled bool
108114
region_enabled bool
@@ -128,6 +134,9 @@ type DB struct {
128134
district_enabled bool
129135
asn_enabled bool
130136
as_enabled bool
137+
asdomain_enabled bool
138+
asusagetype_enabled bool
139+
ascidr_enabled bool
131140

132141
metaok bool
133142
}
@@ -158,8 +167,11 @@ var category_position = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
158167
var district_position = [27]uint8{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}
159168
var asn_position = [27]uint8{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}
160169
var as_position = [27]uint8{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}
170+
var asdomain_position = [27]uint8{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, 26}
171+
var asusagetype_position = [27]uint8{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, 27}
172+
var ascidr_position = [27]uint8{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, 28}
161173

162-
const api_version string = "9.7.1"
174+
const api_version string = "9.8.0"
163175

164176
var max_ipv4_range = uint128.From64(4294967295)
165177
var max_ipv6_range = uint128.From64(0)
@@ -196,8 +208,11 @@ const category uint32 = 0x0200000
196208
const district uint32 = 0x0400000
197209
const asn uint32 = 0x0800000
198210
const as uint32 = 0x1000000
211+
const asdomain uint32 = 0x2000000
212+
const asusagetype uint32 = 0x4000000
213+
const ascidr uint32 = 0x8000000
199214

200-
const all uint32 = countryshort | countrylong | region | city | isp | latitude | longitude | domain | zipcode | timezone | netspeed | iddcode | areacode | weatherstationcode | weatherstationname | mcc | mnc | mobilebrand | elevation | usagetype | addresstype | category | district | asn | as
215+
const all uint32 = countryshort | countrylong | region | city | isp | latitude | longitude | domain | zipcode | timezone | netspeed | iddcode | areacode | weatherstationcode | weatherstationname | mcc | mnc | mobilebrand | elevation | usagetype | addresstype | category | district | asn | as | asdomain | asusagetype | ascidr
201216

202217
const invalid_address string = "Invalid IP address."
203218
const missing_file string = "Invalid database file."
@@ -550,6 +565,18 @@ func OpenDBWithReader(reader DBReader) (*DB, error) {
550565
db.as_position_offset = uint32(as_position[dbt]-2) << 2
551566
db.as_enabled = true
552567
}
568+
if asdomain_position[dbt] != 0 {
569+
db.asdomain_position_offset = uint32(asdomain_position[dbt]-2) << 2
570+
db.asdomain_enabled = true
571+
}
572+
if asusagetype_position[dbt] != 0 {
573+
db.asusagetype_position_offset = uint32(asusagetype_position[dbt]-2) << 2
574+
db.asusagetype_enabled = true
575+
}
576+
if ascidr_position[dbt] != 0 {
577+
db.ascidr_position_offset = uint32(ascidr_position[dbt]-2) << 2
578+
db.ascidr_enabled = true
579+
}
553580

554581
db.metaok = true
555582

@@ -617,6 +644,9 @@ func loadmessage(mesg string) IP2Locationrecord {
617644
x.District = mesg
618645
x.Asn = mesg
619646
x.As = mesg
647+
x.Asdomain = mesg
648+
x.Asusagetype = mesg
649+
x.Ascidr = mesg
620650

621651
return x
622652
}
@@ -912,6 +942,21 @@ func (d *DB) Get_as(ipaddress string) (IP2Locationrecord, error) {
912942
return d.query(ipaddress, as)
913943
}
914944

945+
// Get_as will return the AS domain based on the queried IP address.
946+
func (d *DB) Get_asdomain(ipaddress string) (IP2Locationrecord, error) {
947+
return d.query(ipaddress, asdomain)
948+
}
949+
950+
// Get_as will return the AS usage type based on the queried IP address.
951+
func (d *DB) Get_asusagetype(ipaddress string) (IP2Locationrecord, error) {
952+
return d.query(ipaddress, asusagetype)
953+
}
954+
955+
// Get_as will return the AS CIDR based on the queried IP address.
956+
func (d *DB) Get_ascidr(ipaddress string) (IP2Locationrecord, error) {
957+
return d.query(ipaddress, ascidr)
958+
}
959+
915960
// main query
916961
func (d *DB) query(ipaddress string, mode uint32) (IP2Locationrecord, error) {
917962
x := loadmessage(not_supported) // default message
@@ -1153,6 +1198,24 @@ func (d *DB) query(ipaddress string, mode uint32) (IP2Locationrecord, error) {
11531198
}
11541199
}
11551200

1201+
if mode&asdomain != 0 && d.asdomain_enabled {
1202+
if x.Asdomain, err = d.readstr(d.readuint32_row(row, d.asdomain_position_offset)); err != nil {
1203+
return x, err
1204+
}
1205+
}
1206+
1207+
if mode&asusagetype != 0 && d.asusagetype_enabled {
1208+
if x.Asusagetype, err = d.readstr(d.readuint32_row(row, d.asusagetype_position_offset)); err != nil {
1209+
return x, err
1210+
}
1211+
}
1212+
1213+
if mode&ascidr != 0 && d.ascidr_enabled {
1214+
if x.Ascidr, err = d.readstr(d.readuint32_row(row, d.ascidr_position_offset)); err != nil {
1215+
return x, err
1216+
}
1217+
}
1218+
11561219
return x, nil
11571220
} else {
11581221
if ipno.Cmp(ipfrom) < 0 {
@@ -1196,4 +1259,7 @@ func Printrecord(x IP2Locationrecord) {
11961259
fmt.Printf("district: %s\n", x.District)
11971260
fmt.Printf("asn: %s\n", x.Asn)
11981261
fmt.Printf("as: %s\n", x.As)
1262+
fmt.Printf("asdomain: %s\n", x.Asdomain)
1263+
fmt.Printf("asusagetype: %s\n", x.Asusagetype)
1264+
fmt.Printf("ascidr: %s\n", x.Ascidr)
11991265
}

0 commit comments

Comments
 (0)