From 4b504679d9edb73bb83f4cb4ac3bbb47e1c99a80 Mon Sep 17 00:00:00 2001 From: Aaron Bishop Date: Thu, 3 Apr 2025 23:10:58 +0100 Subject: [PATCH] add support for asn network range - Updates the ASN type to support the network range field. - Refactors the ASN method to utilise the LookupNetwork method of the mmdbReader in order to capture the network address and subnet. See Issue: #128 --- reader.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/reader.go b/reader.go index 7e9e0a6..99bdd9c 100644 --- a/reader.go +++ b/reader.go @@ -183,6 +183,7 @@ type AnonymousIP struct { // The ASN struct corresponds to the data in the GeoLite2 ASN database. type ASN struct { + Network string AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"` AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"` } @@ -366,12 +367,14 @@ func (r *Reader) AnonymousIP(ipAddress net.IP) (*AnonymousIP, error) { // ASN takes an IP address as a net.IP struct and returns a ASN struct and/or // an error. func (r *Reader) ASN(ipAddress net.IP) (*ASN, error) { - if isASN&r.databaseType == 0 { - return nil, InvalidMethodError{"ASN", r.Metadata().DatabaseType} + var asn ASN + network, _, err := r.mmdbReader.LookupNetwork(ipAddress, &asn) + if err != nil { + return nil, err } - var val ASN - err := r.mmdbReader.Lookup(ipAddress, &val) - return &val, err + mask, _ := network.Mask.Size() + asn.Network = fmt.Sprintf("%s/%d", network.IP.String(), mask) + return &asn, nil } // ConnectionType takes an IP address as a net.IP struct and returns a