db was overwriting on overlapping CIDRs fffaaaaaaaaaaaacccccccckkkkkk my liffffffeeeeeeee

This commit is contained in:
2026-08-16 03:58:01 -04:00
parent 6c427baab9
commit 374761f950
2 changed files with 24 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ import (
"os"
"github.com/maxmind/mmdbwriter"
"github.com/maxmind/mmdbwriter/inserter"
"github.com/maxmind/mmdbwriter/mmdbtype"
)
@@ -27,6 +28,9 @@ func run() error {
// "1.1.1.1": {
// "is_proxy": true,
// "score": 42
// },
// "1.0.0.0/8": {
// "is_hosting": true
// }
// }
var input map[string]any
@@ -52,9 +56,9 @@ func run() error {
// Valid values are 24, 28, and 32.
RecordSize: 28,
IncludeReservedNetworks: true,
DisableIPv4Aliasing: true,
})
if err != nil {
@@ -62,6 +66,20 @@ func run() error {
}
// Insert every IP/CIDR.
//
// Use TopLevelMergeWith instead of the normal Insert method.
//
// This is important when ranges overlap. For example:
//
// 1.2.0.0/16 -> {"is_proxy": true}
// 1.2.3.0/24 -> {"is_hosting": true}
//
// An address inside 1.2.3.0/24 will retain both fields:
//
// {
// "is_proxy": true,
// "is_hosting": true
// }
for address, rawRecord := range input {
network, err := parseNetwork(address)
if err != nil {
@@ -73,7 +91,10 @@ func run() error {
return fmt.Errorf("invalid record for %q: %w", address, err)
}
if err := tree.Insert(network, record); err != nil {
if err := tree.InsertFunc(
network,
inserter.TopLevelMergeWith(record),
); err != nil {
return fmt.Errorf("inserting %q: %w", address, err)
}
}