Skip to content

feat: implement IPtoASN -> mmdb (GeoLite2-ASN / GeoLite2-Country) pipeline - #1

Merged
zxdev merged 4 commits into
netstar-labs:mainfrom
derzsidaniel-netstar:feature/mmdb-generation
Jul 22, 2026
Merged

feat: implement IPtoASN -> mmdb (GeoLite2-ASN / GeoLite2-Country) pipeline #1
zxdev merged 4 commits into
netstar-labs:mainfrom
derzsidaniel-netstar:feature/mmdb-generation

Conversation

@derzsidaniel-netstar

@derzsidaniel-netstar derzsidaniel-netstar commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

This pull request introduces a new tool, mmdb-write, for compiling CIDR specs into MaxMind DB files, and extends the iptoasn tool to support both ASN and country output formats, similar to the mm-dbip tool. It also adds a build/install script for automated MMDB generation (both GeoLite2-ASN and GeoLite2-Country format) and updates documentation for all command-line tools.

The new mmdb-write tool expects, in standard input (or an input file using the -i flag) CIDR-format input data (<cidr> <ASN> or <cidr> <country>), and writes a MaxMind-compatible database from the given IP ranges. In effect, mmdb-write can be used in Unix-style pipelines to convert any CIDR data to standardized MMDB binary format.

For country data, we need to enrich the input CIDR dataset with a countries nomenclature. This nomenclature is stored in cmd/mmdb-write/data/countries.json and is generated by the mmdb-build-countries utility. Originally, I used the geonames.org dataset, which is released under a CC attribution-required license. The original commit still contains this implementation: it requires attribution; the latest commit replaces this with a pure-Wikidata source. Two Wikidata SPARQL queries are used to query continents and countries, translated into the same languages MaxMind's native DB uses.

The resulting databases are fully compatible with any library / program using GeoLite2-ASN or GeoLite2-Country databases. The format itself is not bound by licensing terms - the reader libraries are available under ISC (no warranty given, but no attribution equired) - the country data is available under CC0 (no attribution required), if generated with IPtoASN data, the ASN IP range mappings are public domain - the resulting database (IPtoASN -> MMDB) can be used commercially, without attribution required.

The Go library remains standard library only. Only the mmdb-write cmd requires third-party MMDB libraries - this utility has a different go.mod. The main go.mod remains empty.

For example, if I want to compile the latest IPtoASN ASN data to a new iptoasn-asn.mmdb file:

go run ./cmd/iptoasn -output asn | go run ./cmd/mmdb-write -db-type GeoLite2-ASN -o iptoasn-asn.mmdb

To compile the latest IPtoASN country data to a new iptoasn-country.mmdb file (uses country field of ASN):

go run ./cmd/iptoasn -output country | go run ./cmd/mmdb-write -db-type GeoLite2-Country -o iptoasn-country.mmdb

Similarly, to compile DB-IP to dbip-country.mmdb:

go run ./cmd/mm-dbip -db country | go run ./cmd/mmdb-write -db-type GeoLite2-Country -o dbip-country.mmdb

Feel free to inspect the MMDB response to compare to a MaxMind DB:

mmdbinspect -db GeoLite2-Country.mmdb 5.15.2.14

derzsidaniel-netstar and others added 4 commits July 21, 2026 14:20
Address review nits on the IPtoASN -> mmdb pipeline:

- iptoasn: gofmt the package doc comment (was not gofmt-clean).
- mmdb-build-countries: pickContinent is now deterministic (sorted) and
  transcontinental countries are pinned via continentOverride, so
  regenerating countries.json is reproducible instead of depending on map
  iteration order. Drop a duplicate, unreachable err check in main().
- mmdb-write: extract versionString() so it is testable; document that
  registered_country mirrors country and that country pseudo-codes
  (EU/AP/ZZ/Unknown/empty) are skipped.
- tests: replace the tautological TestVersionFlag with TestVersionString;
  add TestCountryPseudoCodesSkipped and TestCountryAmericanSamoa (the
  "AS" ISO code vs "AS" ASN-prefix collision), and TestParseSpecCountryFallback
  in the core parser covering the same collision.

No change to the shipped countries.json; the override values match it.
@zxdev
zxdev merged commit db2e684 into netstar-labs:main Jul 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants