diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index daacd88..373da79 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -11,7 +11,7 @@ jobs: build: strategy: matrix: - go-version: [1.21.x, 1.22.x] + go-version: [1.23.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} name: "Build ${{ matrix.go-version }} test on ${{ matrix.platform }}" diff --git a/CHANGELOG.md b/CHANGELOG.md index ac2ddf4..874279f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## 2.0.0 + +* Upgrade to `github.com/oschwald/maxminddb-golang/v2`. This is a breaking + API change, but should not affect the use of the program. + ## 0.2.0 (2024-01-10) * Don't escape `&`, `<`, and `>` in JSON output diff --git a/README.md b/README.md index 0f8ca49..26f8b7f 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ _We aim always to support the current and penultimate major releases of the Go c The easiest way is via `go install`: ```bash -$ go install github.com/maxmind/mmdbinspect/cmd/mmdbinspect@latest +$ go install github.com/maxmind/mmdbinspect/v2/cmd/mmdbinspect@latest ``` This installs `mmdbinspect` to `$GOPATH/bin/mmdbinspect`. @@ -473,6 +473,6 @@ Please report bugs by filing an issue with our GitHub issue tracker at [https:// ## Copyright and License -This software is Copyright (c) 2019 - 2024 by MaxMind, Inc. +This software is Copyright (c) 2019 - 2025 by MaxMind, Inc. This is free software, licensed under the [Apache License, Version 2.0](LICENSE-APACHE) or the [MIT License](LICENSE-MIT), at your option. diff --git a/cmd/mmdbinspect/main.go b/cmd/mmdbinspect/main.go index 1619e14..319b2b2 100644 --- a/cmd/mmdbinspect/main.go +++ b/cmd/mmdbinspect/main.go @@ -9,7 +9,7 @@ import ( "os" "strings" - "github.com/maxmind/mmdbinspect/pkg/mmdbinspect" + "github.com/maxmind/mmdbinspect/v2/pkg/mmdbinspect" ) type arrayFlags []string diff --git a/go.mod b/go.mod index 4a63675..918424d 100644 --- a/go.mod +++ b/go.mod @@ -1,17 +1,15 @@ -module github.com/maxmind/mmdbinspect +module github.com/maxmind/mmdbinspect/v2 -go 1.21 - -toolchain go1.22.3 +go 1.23 require ( - github.com/oschwald/maxminddb-golang v1.13.1 + github.com/oschwald/maxminddb-golang/v2 v2.0.0-beta.2 github.com/stretchr/testify v1.10.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.21.0 // indirect + golang.org/x/sys v0.29.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 796c043..23003e0 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,13 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/oschwald/maxminddb-golang v1.13.1 h1:G3wwjdN9JmIK2o/ermkHM+98oX5fS+k5MbwsmL4MRQE= -github.com/oschwald/maxminddb-golang v1.13.1/go.mod h1:K4pgV9N/GcK694KSTmVSDTODk4IsCNThNdTmnaBZ/F8= +github.com/oschwald/maxminddb-golang/v2 v2.0.0-beta.2 h1:jG+FaCBv3h6GD5F+oenTfe3+0NmX8sCKjni5k3A5Dek= +github.com/oschwald/maxminddb-golang/v2 v2.0.0-beta.2/go.mod h1:rHaQJ5SjfCdL4sqCKa3FhklRcaXga2/qyvmQuA+ZJ6M= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/pkg/mmdbinspect/mmdbinspect.go b/pkg/mmdbinspect/mmdbinspect.go index 0884b36..a191332 100644 --- a/pkg/mmdbinspect/mmdbinspect.go +++ b/pkg/mmdbinspect/mmdbinspect.go @@ -7,16 +7,16 @@ import ( "errors" "fmt" "io/fs" - "net" + "net/netip" "os" "strings" - "github.com/oschwald/maxminddb-golang" + "github.com/oschwald/maxminddb-golang/v2" ) // RecordForNetwork holds a network and the corresponding record. type RecordForNetwork struct { - Network string + Network netip.Prefix Record any } @@ -46,9 +46,9 @@ func OpenDB(path string) (*maxminddb.Reader, error) { return db, nil } -// RecordsForNetwork fetches mmdb records inside a given network. If an -// address is provided without a netmask a /32 will be inferred for v4 -// addresses and a /128 will be inferred for v6 addresses. +// RecordsForNetwork fetches mmdb records inside a given network. If an IP +// address is provided without a prefix length, it will be treated as a +// network containing a single address (i.e., /32 for IPv4 and /128 for IPv6). func RecordsForNetwork(reader maxminddb.Reader, includeAliasedNetworks bool, maybeNetwork string) (any, error) { lookupNetwork := maybeNetwork @@ -60,33 +60,27 @@ func RecordsForNetwork(reader maxminddb.Reader, includeAliasedNetworks bool, may } } - //nolint:forbidigo // preexisting - _, network, err := net.ParseCIDR(lookupNetwork) + network, err := netip.ParsePrefix(lookupNetwork) if err != nil { return nil, fmt.Errorf("%v is not a valid IP address", maybeNetwork) } - var n *maxminddb.Networks + var opts []maxminddb.NetworksOption if includeAliasedNetworks { - n = reader.NetworksWithin(network) - } else { - n = reader.NetworksWithin(network, maxminddb.SkipAliasedNetworks) + opts = append(opts, maxminddb.IncludeAliasedNetworks) } var found []any - for n.Next() { + for res := range reader.NetworksWithin(network, opts...) { var record any - address, err := n.Network(&record) + + err := res.Decode(&record) if err != nil { return nil, fmt.Errorf("could not get next network: %w", err) } - found = append(found, RecordForNetwork{address.String(), record}) - } - - if n.Err() != nil { - return nil, fmt.Errorf("traversing networks: %w", n.Err()) + found = append(found, RecordForNetwork{res.Prefix(), record}) } return found, nil diff --git a/pkg/mmdbinspect/mmdbinspect_test.go b/pkg/mmdbinspect/mmdbinspect_test.go index ef43284..f5a1ee6 100644 --- a/pkg/mmdbinspect/mmdbinspect_test.go +++ b/pkg/mmdbinspect/mmdbinspect_test.go @@ -3,7 +3,7 @@ package mmdbinspect import ( "testing" - "github.com/oschwald/maxminddb-golang" + "github.com/oschwald/maxminddb-golang/v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" )