MaxMind’s GeoLite2 databases map IP addresses to geographic information (country, city) and to the network’s autonomous system (ASN). Rye can open these .mmdb files and look up an IP’s country or ASN directly.
This package needs a build with -tags "b_contrib,b_maxmind". You also need the database files, which are free but require a MaxMind account:
GeoLite2-Country.mmdb — for country lookupsGeoLite2-ASN.mmdb — for ASN lookupsOpen a country database and pass an IP to Lookup-country. The result is a dict with the IP, the ISO country code, and the English country name.
; Open the country database
db: Open maxmind://GeoLite2-Country.mmdb
; Look up a country for an IP
loc: db .Lookup-country "8.8.8.8"
; { ip: "8.8.8.8" country_code: "US" country_name: "United States" }
print loc -> "country_code"
; "US"
db .Close
An ASN database tells you which autonomous system (usually an ISP or company) an IP belongs to.
; Open the ASN database
db: Open maxmind://GeoLite2-ASN.mmdb
; Look up the autonomous system for an IP
asn: db .Lookup-asn "8.8.8.8"
; { ip: "8.8.8.8" asn: 15169 organization: "Google LLC" }
print asn -> "organization"
; "Google LLC"
db .Close
Each handle is tied to the kind of database it was opened from, so use a country database with Lookup-country and an ASN database with Lookup-asn. Invalid IPs and lookups with no result return an error, which you can handle with fix or either.