Skip to content

Commit ac53325

Browse files
committed
Feat: update MMDB database releated dependencies
1 parent 77e5f4a commit ac53325

File tree

5 files changed

+206
-110
lines changed

5 files changed

+206
-110
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ go 1.24.0
55
require (
66
github.com/klauspost/compress v1.18.3
77
github.com/maxmind/mmdbwriter v1.2.0
8-
github.com/oschwald/geoip2-golang v1.13.0
9-
github.com/oschwald/maxminddb-golang v1.13.1
8+
github.com/oschwald/geoip2-golang/v2 v2.1.0
9+
github.com/oschwald/maxminddb-golang/v2 v2.1.1
1010
github.com/sagernet/sing-box v1.12.17
1111
github.com/spf13/cobra v1.10.2
1212
github.com/tailscale/hujson v0.0.0-20250605163823-992244df8c5a
@@ -19,7 +19,6 @@ require (
1919
require (
2020
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2121
github.com/miekg/dns v1.1.67 // indirect
22-
github.com/oschwald/maxminddb-golang/v2 v2.1.1 // indirect
2322
github.com/sagernet/sing v0.7.14 // indirect
2423
github.com/spf13/pflag v1.0.9 // indirect
2524
github.com/tidwall/match v1.1.1 // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ github.com/maxmind/mmdbwriter v1.2.0 h1:hyvDopImmgvle3aR8AaddxXnT0iQH2KWJX3vNfkw
1111
github.com/maxmind/mmdbwriter v1.2.0/go.mod h1:EQmKHhk2y9DRVvyNxwCLKC5FrkXZLx4snc5OlLY5XLE=
1212
github.com/miekg/dns v1.1.67 h1:kg0EHj0G4bfT5/oOys6HhZw4vmMlnoZ+gDu8tJ/AlI0=
1313
github.com/miekg/dns v1.1.67/go.mod h1:fujopn7TB3Pu3JM69XaawiU0wqjpL9/8xGop5UrTPps=
14-
github.com/oschwald/geoip2-golang v1.13.0 h1:Q44/Ldc703pasJeP5V9+aFSZFmBN7DKHbNsSFzQATJI=
15-
github.com/oschwald/geoip2-golang v1.13.0/go.mod h1:P9zG+54KPEFOliZ29i7SeYZ/GM6tfEL+rgSn03hYuUo=
16-
github.com/oschwald/maxminddb-golang v1.13.1 h1:G3wwjdN9JmIK2o/ermkHM+98oX5fS+k5MbwsmL4MRQE=
17-
github.com/oschwald/maxminddb-golang v1.13.1/go.mod h1:K4pgV9N/GcK694KSTmVSDTODk4IsCNThNdTmnaBZ/F8=
14+
github.com/oschwald/geoip2-golang/v2 v2.1.0 h1:DjnLhNJu9WHwTrmoiQFvgmyJoczhdnm7LB23UBI2Amo=
15+
github.com/oschwald/geoip2-golang/v2 v2.1.0/go.mod h1:qdVmcPgrTJ4q2eP9tHq/yldMTdp2VMr33uVdFbHBiBc=
1816
github.com/oschwald/maxminddb-golang/v2 v2.1.1 h1:lA8FH0oOrM4u7mLvowq8IT6a3Q/qEnqRzLQn9eH5ojc=
1917
github.com/oschwald/maxminddb-golang/v2 v2.1.1/go.mod h1:PLdx6PR+siSIoXqqy7C7r3SB3KZnhxWr1Dp6g0Hacl8=
2018
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=

plugin/maxmind/common_out.go

Lines changed: 127 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"strings"
99

1010
"github.com/Loyalsoldier/geoip/lib"
11-
"github.com/oschwald/geoip2-golang"
12-
"github.com/oschwald/maxminddb-golang"
11+
"github.com/oschwald/geoip2-golang/v2"
12+
"github.com/oschwald/maxminddb-golang/v2"
1313
)
1414

1515
var (
@@ -20,6 +20,15 @@ var (
2020
defaultIPInfoOutputDir = filepath.Join("./", "output", "ipinfo")
2121
)
2222

23+
// Reference: https://github.com/oschwald/geoip2-golang/blob/HEAD/models.go
24+
var (
25+
zeroDBIPLanguageNames dbipLanguageNames
26+
zeroDBIPContinent dbipContinent
27+
zeroDBIPCountryRecord dbipCountryRecord
28+
zeroDBIPRepresentedCountry dbipRepresentedCountry
29+
zeroDBIPCountry dbipCountry
30+
)
31+
2332
// Reference: https://ipinfo.io/lite
2433
type ipInfoLite struct {
2534
ASN string `maxminddb:"asn"`
@@ -31,6 +40,66 @@ type ipInfoLite struct {
3140
CountryCode string `maxminddb:"country_code"`
3241
}
3342

43+
// Reference: https://github.com/oschwald/geoip2-golang/blob/HEAD/models.go
44+
type dbipLanguageNames struct {
45+
geoip2.Names
46+
47+
// Persian localized name
48+
Persian string `json:"fa,omitzero" maxminddb:"fa"`
49+
// Korean localized name
50+
Korean string `json:"ko,omitzero" maxminddb:"ko"`
51+
}
52+
53+
func (d dbipLanguageNames) HasData() bool {
54+
return d != zeroDBIPLanguageNames
55+
}
56+
57+
// Reference: https://github.com/oschwald/geoip2-golang/blob/HEAD/models.go
58+
type dbipContinent struct {
59+
geoip2.Continent
60+
61+
Names dbipLanguageNames `json:"names,omitzero" maxminddb:"names"`
62+
}
63+
64+
func (d dbipContinent) HasData() bool {
65+
return d != zeroDBIPContinent
66+
}
67+
68+
// Reference: https://github.com/oschwald/geoip2-golang/blob/HEAD/models.go
69+
type dbipCountryRecord struct {
70+
geoip2.CountryRecord
71+
72+
Names dbipLanguageNames `json:"names,omitzero" maxminddb:"names"`
73+
}
74+
75+
func (d dbipCountryRecord) HasData() bool {
76+
return d != zeroDBIPCountryRecord
77+
}
78+
79+
// Reference: https://github.com/oschwald/geoip2-golang/blob/HEAD/models.go
80+
type dbipRepresentedCountry struct {
81+
geoip2.RepresentedCountry
82+
83+
Names dbipLanguageNames `json:"names,omitzero" maxminddb:"names"`
84+
}
85+
86+
func (d dbipRepresentedCountry) HasData() bool {
87+
return d != zeroDBIPRepresentedCountry
88+
}
89+
90+
// Reference: https://github.com/oschwald/geoip2-golang/blob/HEAD/models.go
91+
type dbipCountry struct {
92+
Traits geoip2.CountryTraits `json:"traits,omitzero" maxminddb:"traits"`
93+
Continent dbipContinent `json:"continent,omitzero" maxminddb:"continent"`
94+
RepresentedCountry dbipRepresentedCountry `json:"represented_country,omitzero" maxminddb:"represented_country"`
95+
Country dbipCountryRecord `json:"country,omitzero" maxminddb:"country"`
96+
RegisteredCountry dbipCountryRecord `json:"registered_country,omitzero" maxminddb:"registered_country"`
97+
}
98+
99+
func (d dbipCountry) HasData() bool {
100+
return d != zeroDBIPCountry
101+
}
102+
34103
func newGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
35104
var tmp struct {
36105
OutputName string `json:"outputName"`
@@ -98,65 +167,102 @@ func (g *GeoLite2CountryMMDBOut) GetExtraInfo() (map[string]any, error) {
98167
return nil, err
99168
}
100169

101-
db, err := maxminddb.FromBytes(content)
170+
db, err := maxminddb.OpenBytes(content)
102171
if err != nil {
103172
return nil, err
104173
}
105174
defer db.Close()
106175

107176
infoList := make(map[string]any)
108-
networks := db.Networks(maxminddb.SkipAliasedNetworks)
109-
for networks.Next() {
177+
for network := range db.Networks() {
110178
switch g.Type {
111-
case TypeGeoLite2CountryMMDBOut, TypeDBIPCountryMMDBOut:
179+
case TypeGeoLite2CountryMMDBOut:
112180
var record geoip2.Country
113-
_, err := networks.Network(&record)
181+
err := network.Decode(&record)
114182
if err != nil {
115183
return nil, err
116184
}
117185

118186
switch {
119-
case strings.TrimSpace(record.Country.IsoCode) != "":
120-
countryCode := strings.ToUpper(strings.TrimSpace(record.Country.IsoCode))
187+
case strings.TrimSpace(record.Country.ISOCode) != "":
188+
countryCode := strings.ToUpper(strings.TrimSpace(record.Country.ISOCode))
121189
if _, found := infoList[countryCode]; !found {
122190
infoList[countryCode] = geoip2.Country{
123191
Continent: record.Continent,
124192
Country: record.Country,
125193
}
126194
}
127195

128-
case strings.TrimSpace(record.RegisteredCountry.IsoCode) != "":
129-
countryCode := strings.ToUpper(strings.TrimSpace(record.RegisteredCountry.IsoCode))
196+
case strings.TrimSpace(record.RegisteredCountry.ISOCode) != "":
197+
countryCode := strings.ToUpper(strings.TrimSpace(record.RegisteredCountry.ISOCode))
130198
if _, found := infoList[countryCode]; !found {
131199
infoList[countryCode] = geoip2.Country{
132200
Continent: record.Continent,
133201
Country: record.RegisteredCountry,
134202
}
135203
}
136204

137-
case strings.TrimSpace(record.RepresentedCountry.IsoCode) != "":
138-
countryCode := strings.ToUpper(strings.TrimSpace(record.RepresentedCountry.IsoCode))
205+
case strings.TrimSpace(record.RepresentedCountry.ISOCode) != "":
206+
countryCode := strings.ToUpper(strings.TrimSpace(record.RepresentedCountry.ISOCode))
139207
if _, found := infoList[countryCode]; !found {
140208
infoList[countryCode] = geoip2.Country{
141209
Continent: record.Continent,
142-
Country: struct {
143-
Names map[string]string `maxminddb:"names"`
144-
IsoCode string `maxminddb:"iso_code"`
145-
GeoNameID uint `maxminddb:"geoname_id"`
146-
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
147-
}{
210+
Country: geoip2.CountryRecord{
148211
Names: record.RepresentedCountry.Names,
149-
IsoCode: record.RepresentedCountry.IsoCode,
212+
ISOCode: record.RepresentedCountry.ISOCode,
150213
GeoNameID: record.RepresentedCountry.GeoNameID,
151214
IsInEuropeanUnion: record.RepresentedCountry.IsInEuropeanUnion,
152215
},
153216
}
154217
}
155218
}
156219

220+
case TypeDBIPCountryMMDBOut:
221+
var record dbipCountry
222+
err := network.Decode(&record)
223+
if err != nil {
224+
return nil, err
225+
}
226+
227+
switch {
228+
case strings.TrimSpace(record.Country.ISOCode) != "":
229+
countryCode := strings.ToUpper(strings.TrimSpace(record.Country.ISOCode))
230+
if _, found := infoList[countryCode]; !found {
231+
infoList[countryCode] = dbipCountry{
232+
Continent: record.Continent,
233+
Country: record.Country,
234+
}
235+
}
236+
237+
case strings.TrimSpace(record.RegisteredCountry.ISOCode) != "":
238+
countryCode := strings.ToUpper(strings.TrimSpace(record.RegisteredCountry.ISOCode))
239+
if _, found := infoList[countryCode]; !found {
240+
infoList[countryCode] = dbipCountry{
241+
Continent: record.Continent,
242+
Country: record.RegisteredCountry,
243+
}
244+
}
245+
246+
case strings.TrimSpace(record.RepresentedCountry.ISOCode) != "":
247+
countryCode := strings.ToUpper(strings.TrimSpace(record.RepresentedCountry.ISOCode))
248+
if _, found := infoList[countryCode]; !found {
249+
infoList[countryCode] = dbipCountry{
250+
Continent: record.Continent,
251+
Country: dbipCountryRecord{
252+
CountryRecord: geoip2.CountryRecord{
253+
ISOCode: record.RepresentedCountry.ISOCode,
254+
GeoNameID: record.RepresentedCountry.GeoNameID,
255+
IsInEuropeanUnion: record.RepresentedCountry.IsInEuropeanUnion,
256+
},
257+
Names: record.RepresentedCountry.Names,
258+
},
259+
}
260+
}
261+
}
262+
157263
case TypeIPInfoCountryMMDBOut:
158264
var record ipInfoLite
159-
_, err := networks.Network(&record)
265+
err := network.Decode(&record)
160266
if err != nil {
161267
return nil, err
162268
}
@@ -174,10 +280,6 @@ func (g *GeoLite2CountryMMDBOut) GetExtraInfo() (map[string]any, error) {
174280

175281
}
176282

177-
if networks.Err() != nil {
178-
return nil, networks.Err()
179-
}
180-
181283
if len(infoList) == 0 {
182284
return nil, fmt.Errorf("❌ [type %s | action %s] no extra info found in the source MMDB file: %s", g.Type, g.Action, g.SourceMMDBURI)
183285
}

plugin/maxmind/maxmind_country_mmdb_in.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package maxmind
33
import (
44
"encoding/json"
55
"fmt"
6-
"net"
76
"os"
87
"strings"
98

109
"github.com/Loyalsoldier/geoip/lib"
11-
"github.com/oschwald/geoip2-golang"
12-
"github.com/oschwald/maxminddb-golang"
10+
"github.com/oschwald/geoip2-golang/v2"
11+
"github.com/oschwald/maxminddb-golang/v2"
1312
)
1413

1514
const (
@@ -97,38 +96,36 @@ func (g *GeoLite2CountryMMDBIn) Input(container lib.Container) (lib.Container, e
9796
}
9897

9998
func (g *GeoLite2CountryMMDBIn) generateEntries(content []byte, entries map[string]*lib.Entry) error {
100-
db, err := maxminddb.FromBytes(content)
99+
db, err := maxminddb.OpenBytes(content)
101100
if err != nil {
102101
return err
103102
}
104103
defer db.Close()
105104

106-
networks := db.Networks(maxminddb.SkipAliasedNetworks)
107-
for networks.Next() {
105+
for network := range db.Networks() {
108106
var name string
109-
var subnet *net.IPNet
110107
var err error
111108

112109
switch g.Type {
113110
case TypeGeoLite2CountryMMDBIn, TypeDBIPCountryMMDBIn:
114111
var record geoip2.Country
115-
subnet, err = networks.Network(&record)
112+
err = network.Decode(&record)
116113
if err != nil {
117114
return err
118115
}
119116

120117
switch {
121-
case strings.TrimSpace(record.Country.IsoCode) != "":
122-
name = strings.ToUpper(strings.TrimSpace(record.Country.IsoCode))
123-
case strings.TrimSpace(record.RegisteredCountry.IsoCode) != "":
124-
name = strings.ToUpper(strings.TrimSpace(record.RegisteredCountry.IsoCode))
125-
case strings.TrimSpace(record.RepresentedCountry.IsoCode) != "":
126-
name = strings.ToUpper(strings.TrimSpace(record.RepresentedCountry.IsoCode))
118+
case strings.TrimSpace(record.Country.ISOCode) != "":
119+
name = strings.ToUpper(strings.TrimSpace(record.Country.ISOCode))
120+
case strings.TrimSpace(record.RegisteredCountry.ISOCode) != "":
121+
name = strings.ToUpper(strings.TrimSpace(record.RegisteredCountry.ISOCode))
122+
case strings.TrimSpace(record.RepresentedCountry.ISOCode) != "":
123+
name = strings.ToUpper(strings.TrimSpace(record.RepresentedCountry.ISOCode))
127124
}
128125

129126
case TypeIPInfoCountryMMDBIn:
130127
var record ipInfoLite
131-
subnet, err = networks.Network(&record)
128+
err = network.Decode(&record)
132129
if err != nil {
133130
return err
134131
}
@@ -138,7 +135,7 @@ func (g *GeoLite2CountryMMDBIn) generateEntries(content []byte, entries map[stri
138135
return lib.ErrNotSupportedFormat
139136
}
140137

141-
if name == "" || subnet == nil {
138+
if name == "" || !network.Found() {
142139
continue
143140
}
144141

@@ -151,16 +148,12 @@ func (g *GeoLite2CountryMMDBIn) generateEntries(content []byte, entries map[stri
151148
entry = lib.NewEntry(name)
152149
}
153150

154-
if err := entry.AddPrefix(subnet); err != nil {
151+
if err := entry.AddPrefix(network.Prefix()); err != nil {
155152
return err
156153
}
157154

158155
entries[name] = entry
159156
}
160157

161-
if networks.Err() != nil {
162-
return networks.Err()
163-
}
164-
165158
return nil
166159
}

0 commit comments

Comments
 (0)