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
1515var (
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
2433type 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+
34103func 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 }
0 commit comments