Skip to content

add support for asn network range #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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
Expand Down