summaryrefslogtreecommitdiff
path: root/plugin/maxmind
diff options
context:
space:
mode:
authorLoyalsoldier <[email protected]>2025-02-01 15:13:39 +0800
committerLoyalsoldier <[email protected]>2025-02-01 15:27:23 +0800
commitfdab3dd2dea595f4aecd48bfd725b4bacf315b62 (patch)
tree8d2d772a9415b74a7caa68ce5bc6a08f34fdf6b7 /plugin/maxmind
parentd9ccff71c634dae0ed85608d37b2d0c2d732fa1a (diff)
Diffstat (limited to 'plugin/maxmind')
-rw-r--r--plugin/maxmind/common_in.go14
-rw-r--r--plugin/maxmind/common_out.go29
-rw-r--r--plugin/maxmind/dbip_country_mmdb_in.go4
-rw-r--r--plugin/maxmind/dbip_country_mmdb_out.go4
-rw-r--r--plugin/maxmind/ipinfo_country_mmdb_in.go4
-rw-r--r--plugin/maxmind/ipinfo_country_mmdb_out.go4
-rw-r--r--plugin/maxmind/maxmind_asn_csv_in.go40
-rw-r--r--plugin/maxmind/maxmind_country_csv_in.go46
-rw-r--r--plugin/maxmind/maxmind_country_mmdb_in.go50
-rw-r--r--plugin/maxmind/maxmind_country_mmdb_out.go80
10 files changed, 138 insertions, 137 deletions
diff --git a/plugin/maxmind/common_in.go b/plugin/maxmind/common_in.go
index 0bf61e51..3d97388c 100644
--- a/plugin/maxmind/common_in.go
+++ b/plugin/maxmind/common_in.go
@@ -9,12 +9,12 @@ import (
)
var (
- defaultGeoLite2MMDBFile = filepath.Join("./", "geolite2", "GeoLite2-Country.mmdb")
- defaultDBIPCountryMMDBFile = filepath.Join("./", "db-ip", "dbip-country-lite.mmdb")
- defaultIPInfoCountryMMDBFile = filepath.Join("./", "ipinfo", "country.mmdb")
+ defaultGeoLite2CountryMMDBFile = filepath.Join("./", "geolite2", "GeoLite2-Country.mmdb")
+ defaultDBIPCountryMMDBFile = filepath.Join("./", "db-ip", "dbip-country-lite.mmdb")
+ defaultIPInfoCountryMMDBFile = filepath.Join("./", "ipinfo", "country.mmdb")
)
-func newMMDBIn(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+func newGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
var tmp struct {
URI string `json:"uri"`
Want []string `json:"wantedList"`
@@ -29,8 +29,8 @@ func newMMDBIn(iType string, iDesc string, action lib.Action, data json.RawMessa
if tmp.URI == "" {
switch iType {
- case TypeMaxmindMMDBIn:
- tmp.URI = defaultGeoLite2MMDBFile
+ case TypeGeoLite2CountryMMDBIn:
+ tmp.URI = defaultGeoLite2CountryMMDBFile
case TypeDBIPCountryMMDBIn:
tmp.URI = defaultDBIPCountryMMDBFile
@@ -48,7 +48,7 @@ func newMMDBIn(iType string, iDesc string, action lib.Action, data json.RawMessa
}
}
- return &MMDBIn{
+ return &GeoLite2CountryMMDBIn{
Type: iType,
Action: action,
Description: iDesc,
diff --git a/plugin/maxmind/common_out.go b/plugin/maxmind/common_out.go
index ad5edce6..bfd41aaa 100644
--- a/plugin/maxmind/common_out.go
+++ b/plugin/maxmind/common_out.go
@@ -13,13 +13,14 @@ import (
)
var (
- defaultOutputName = "Country.mmdb"
+ defaultGeoLite2CountryMMDBOutputName = "Country.mmdb"
+
defaultMaxmindOutputDir = filepath.Join("./", "output", "maxmind")
defaultDBIPOutputDir = filepath.Join("./", "output", "db-ip")
defaultIPInfoOutputDir = filepath.Join("./", "output", "ipinfo")
)
-func newMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
+func newGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
var tmp struct {
OutputName string `json:"outputName"`
OutputDir string `json:"outputDir"`
@@ -38,12 +39,12 @@ func newMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMess
}
if tmp.OutputName == "" {
- tmp.OutputName = defaultOutputName
+ tmp.OutputName = defaultGeoLite2CountryMMDBOutputName
}
if tmp.OutputDir == "" {
switch iType {
- case TypeMaxmindMMDBOut:
+ case TypeGeoLite2CountryMMDBOut:
tmp.OutputDir = defaultMaxmindOutputDir
case TypeDBIPCountryMMDBOut:
@@ -54,7 +55,7 @@ func newMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMess
}
}
- return &MMDBOut{
+ return &GeoLite2CountryMMDBOut{
Type: iType,
Action: action,
Description: iDesc,
@@ -69,18 +70,18 @@ func newMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMess
}, nil
}
-func (m *MMDBOut) GetExtraInfo() (map[string]interface{}, error) {
- if strings.TrimSpace(m.SourceMMDBURI) == "" {
+func (g *GeoLite2CountryMMDBOut) GetExtraInfo() (map[string]any, error) {
+ if strings.TrimSpace(g.SourceMMDBURI) == "" {
return nil, nil
}
var content []byte
var err error
switch {
- case strings.HasPrefix(strings.ToLower(m.SourceMMDBURI), "http://"), strings.HasPrefix(strings.ToLower(m.SourceMMDBURI), "https://"):
- content, err = lib.GetRemoteURLContent(m.SourceMMDBURI)
+ case strings.HasPrefix(strings.ToLower(g.SourceMMDBURI), "http://"), strings.HasPrefix(strings.ToLower(g.SourceMMDBURI), "https://"):
+ content, err = lib.GetRemoteURLContent(g.SourceMMDBURI)
default:
- content, err = os.ReadFile(m.SourceMMDBURI)
+ content, err = os.ReadFile(g.SourceMMDBURI)
}
if err != nil {
return nil, err
@@ -92,11 +93,11 @@ func (m *MMDBOut) GetExtraInfo() (map[string]interface{}, error) {
}
defer db.Close()
- infoList := make(map[string]interface{})
+ infoList := make(map[string]any)
networks := db.Networks(maxminddb.SkipAliasedNetworks)
for networks.Next() {
- switch m.Type {
- case TypeMaxmindMMDBOut, TypeDBIPCountryMMDBOut:
+ switch g.Type {
+ case TypeGeoLite2CountryMMDBOut, TypeDBIPCountryMMDBOut:
var record geoip2.Country
_, err := networks.Network(&record)
if err != nil {
@@ -170,7 +171,7 @@ func (m *MMDBOut) GetExtraInfo() (map[string]interface{}, error) {
}
if len(infoList) == 0 {
- return nil, fmt.Errorf("❌ [type %s | action %s] no extra info found in the source MMDB file: %s", m.Type, m.Action, m.SourceMMDBURI)
+ return nil, fmt.Errorf("❌ [type %s | action %s] no extra info found in the source MMDB file: %s", g.Type, g.Action, g.SourceMMDBURI)
}
return infoList, nil
diff --git a/plugin/maxmind/dbip_country_mmdb_in.go b/plugin/maxmind/dbip_country_mmdb_in.go
index b23e753c..304f29e5 100644
--- a/plugin/maxmind/dbip_country_mmdb_in.go
+++ b/plugin/maxmind/dbip_country_mmdb_in.go
@@ -18,9 +18,9 @@ const (
func init() {
lib.RegisterInputConfigCreator(TypeDBIPCountryMMDBIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newMMDBIn(TypeDBIPCountryMMDBIn, DescDBIPCountryMMDBIn, action, data)
+ return newGeoLite2CountryMMDBIn(TypeDBIPCountryMMDBIn, DescDBIPCountryMMDBIn, action, data)
})
- lib.RegisterInputConverter(TypeDBIPCountryMMDBIn, &MMDBIn{
+ lib.RegisterInputConverter(TypeDBIPCountryMMDBIn, &GeoLite2CountryMMDBIn{
Description: DescDBIPCountryMMDBIn,
})
}
diff --git a/plugin/maxmind/dbip_country_mmdb_out.go b/plugin/maxmind/dbip_country_mmdb_out.go
index e60a3adf..77857d95 100644
--- a/plugin/maxmind/dbip_country_mmdb_out.go
+++ b/plugin/maxmind/dbip_country_mmdb_out.go
@@ -18,9 +18,9 @@ const (
func init() {
lib.RegisterOutputConfigCreator(TypeDBIPCountryMMDBOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
- return newMMDBOut(TypeDBIPCountryMMDBOut, DescDBIPCountryMMDBOut, action, data)
+ return newGeoLite2CountryMMDBOut(TypeDBIPCountryMMDBOut, DescDBIPCountryMMDBOut, action, data)
})
- lib.RegisterOutputConverter(TypeDBIPCountryMMDBOut, &MMDBOut{
+ lib.RegisterOutputConverter(TypeDBIPCountryMMDBOut, &GeoLite2CountryMMDBOut{
Description: DescDBIPCountryMMDBOut,
})
}
diff --git a/plugin/maxmind/ipinfo_country_mmdb_in.go b/plugin/maxmind/ipinfo_country_mmdb_in.go
index 4e7b1a5c..8551e453 100644
--- a/plugin/maxmind/ipinfo_country_mmdb_in.go
+++ b/plugin/maxmind/ipinfo_country_mmdb_in.go
@@ -18,9 +18,9 @@ const (
func init() {
lib.RegisterInputConfigCreator(TypeIPInfoCountryMMDBIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newMMDBIn(TypeIPInfoCountryMMDBIn, DescIPInfoCountryMMDBIn, action, data)
+ return newGeoLite2CountryMMDBIn(TypeIPInfoCountryMMDBIn, DescIPInfoCountryMMDBIn, action, data)
})
- lib.RegisterInputConverter(TypeIPInfoCountryMMDBIn, &MMDBIn{
+ lib.RegisterInputConverter(TypeIPInfoCountryMMDBIn, &GeoLite2CountryMMDBIn{
Description: DescIPInfoCountryMMDBIn,
})
}
diff --git a/plugin/maxmind/ipinfo_country_mmdb_out.go b/plugin/maxmind/ipinfo_country_mmdb_out.go
index ac9e5a34..bed18af7 100644
--- a/plugin/maxmind/ipinfo_country_mmdb_out.go
+++ b/plugin/maxmind/ipinfo_country_mmdb_out.go
@@ -18,9 +18,9 @@ const (
func init() {
lib.RegisterOutputConfigCreator(TypeIPInfoCountryMMDBOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
- return newMMDBOut(TypeIPInfoCountryMMDBOut, DescIPInfoCountryMMDBOut, action, data)
+ return newGeoLite2CountryMMDBOut(TypeIPInfoCountryMMDBOut, DescIPInfoCountryMMDBOut, action, data)
})
- lib.RegisterOutputConverter(TypeIPInfoCountryMMDBOut, &MMDBOut{
+ lib.RegisterOutputConverter(TypeIPInfoCountryMMDBOut, &GeoLite2CountryMMDBOut{
Description: DescIPInfoCountryMMDBOut,
})
}
diff --git a/plugin/maxmind/maxmind_asn_csv_in.go b/plugin/maxmind/maxmind_asn_csv_in.go
index 8c8c3fe2..5b42be59 100644
--- a/plugin/maxmind/maxmind_asn_csv_in.go
+++ b/plugin/maxmind/maxmind_asn_csv_in.go
@@ -13,25 +13,25 @@ import (
)
const (
- TypeASNCSV = "maxmindGeoLite2ASNCSV"
- DescASNCSV = "Convert MaxMind GeoLite2 ASN CSV data to other formats"
+ TypeGeoLite2ASNCSVIn = "maxmindGeoLite2ASNCSV"
+ DescGeoLite2ASNCSVIn = "Convert MaxMind GeoLite2 ASN CSV data to other formats"
)
var (
- defaultASNIPv4File = filepath.Join("./", "geolite2", "GeoLite2-ASN-Blocks-IPv4.csv")
- defaultASNIPv6File = filepath.Join("./", "geolite2", "GeoLite2-ASN-Blocks-IPv6.csv")
+ defaultGeoLite2ASNCSVIPv4File = filepath.Join("./", "geolite2", "GeoLite2-ASN-Blocks-IPv4.csv")
+ defaultGeoLite2ASNCSVIPv6File = filepath.Join("./", "geolite2", "GeoLite2-ASN-Blocks-IPv6.csv")
)
func init() {
- lib.RegisterInputConfigCreator(TypeASNCSV, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newGeoLite2ASNCSV(action, data)
+ lib.RegisterInputConfigCreator(TypeGeoLite2ASNCSVIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+ return newGeoLite2ASNCSVIn(action, data)
})
- lib.RegisterInputConverter(TypeASNCSV, &GeoLite2ASNCSV{
- Description: DescASNCSV,
+ lib.RegisterInputConverter(TypeGeoLite2ASNCSVIn, &GeoLite2ASNCSVIn{
+ Description: DescGeoLite2ASNCSVIn,
})
}
-func newGeoLite2ASNCSV(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+func newGeoLite2ASNCSVIn(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
var tmp struct {
IPv4File string `json:"ipv4"`
IPv6File string `json:"ipv6"`
@@ -48,8 +48,8 @@ func newGeoLite2ASNCSV(action lib.Action, data json.RawMessage) (lib.InputConver
// When both of IP files are not specified,
// it means user wants to use the default ones
if tmp.IPv4File == "" && tmp.IPv6File == "" {
- tmp.IPv4File = defaultASNIPv4File
- tmp.IPv6File = defaultASNIPv6File
+ tmp.IPv4File = defaultGeoLite2ASNCSVIPv4File
+ tmp.IPv6File = defaultGeoLite2ASNCSVIPv6File
}
// Filter want list
@@ -85,10 +85,10 @@ func newGeoLite2ASNCSV(action lib.Action, data json.RawMessage) (lib.InputConver
wantList[asn] = []string{"AS" + asn}
}
- return &GeoLite2ASNCSV{
- Type: TypeASNCSV,
+ return &GeoLite2ASNCSVIn{
+ Type: TypeGeoLite2ASNCSVIn,
Action: action,
- Description: DescASNCSV,
+ Description: DescGeoLite2ASNCSVIn,
IPv4File: tmp.IPv4File,
IPv6File: tmp.IPv6File,
Want: wantList,
@@ -96,7 +96,7 @@ func newGeoLite2ASNCSV(action lib.Action, data json.RawMessage) (lib.InputConver
}, nil
}
-type GeoLite2ASNCSV struct {
+type GeoLite2ASNCSVIn struct {
Type string
Action lib.Action
Description string
@@ -106,19 +106,19 @@ type GeoLite2ASNCSV struct {
OnlyIPType lib.IPType
}
-func (g *GeoLite2ASNCSV) GetType() string {
+func (g *GeoLite2ASNCSVIn) GetType() string {
return g.Type
}
-func (g *GeoLite2ASNCSV) GetAction() lib.Action {
+func (g *GeoLite2ASNCSVIn) GetAction() lib.Action {
return g.Action
}
-func (g *GeoLite2ASNCSV) GetDescription() string {
+func (g *GeoLite2ASNCSVIn) GetDescription() string {
return g.Description
}
-func (g *GeoLite2ASNCSV) Input(container lib.Container) (lib.Container, error) {
+func (g *GeoLite2ASNCSVIn) Input(container lib.Container) (lib.Container, error) {
entries := make(map[string]*lib.Entry)
if g.IPv4File != "" {
@@ -163,7 +163,7 @@ func (g *GeoLite2ASNCSV) Input(container lib.Container) (lib.Container, error) {
return container, nil
}
-func (g *GeoLite2ASNCSV) process(file string, entries map[string]*lib.Entry) error {
+func (g *GeoLite2ASNCSVIn) process(file string, entries map[string]*lib.Entry) error {
if entries == nil {
entries = make(map[string]*lib.Entry)
}
diff --git a/plugin/maxmind/maxmind_country_csv_in.go b/plugin/maxmind/maxmind_country_csv_in.go
index 366220ab..da70c281 100644
--- a/plugin/maxmind/maxmind_country_csv_in.go
+++ b/plugin/maxmind/maxmind_country_csv_in.go
@@ -13,26 +13,26 @@ import (
)
const (
- TypeCountryCSV = "maxmindGeoLite2CountryCSV"
- DescCountryCSV = "Convert MaxMind GeoLite2 country CSV data to other formats"
+ TypeGeoLite2CountryCSVIn = "maxmindGeoLite2CountryCSV"
+ DescGeoLite2CountryCSVIn = "Convert MaxMind GeoLite2 country CSV data to other formats"
)
var (
- defaultCCFile = filepath.Join("./", "geolite2", "GeoLite2-Country-Locations-en.csv")
- defaultCountryIPv4File = filepath.Join("./", "geolite2", "GeoLite2-Country-Blocks-IPv4.csv")
- defaultCountryIPv6File = filepath.Join("./", "geolite2", "GeoLite2-Country-Blocks-IPv6.csv")
+ defaultGeoLite2CountryCodeFile = filepath.Join("./", "geolite2", "GeoLite2-Country-Locations-en.csv")
+ defaultGeoLite2CountryIPv4File = filepath.Join("./", "geolite2", "GeoLite2-Country-Blocks-IPv4.csv")
+ defaultGeoLite2CountryIPv6File = filepath.Join("./", "geolite2", "GeoLite2-Country-Blocks-IPv6.csv")
)
func init() {
- lib.RegisterInputConfigCreator(TypeCountryCSV, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newGeoLite2CountryCSV(action, data)
+ lib.RegisterInputConfigCreator(TypeGeoLite2CountryCSVIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+ return newGeoLite2CountryCSVIn(action, data)
})
- lib.RegisterInputConverter(TypeCountryCSV, &GeoLite2CountryCSV{
- Description: DescCountryCSV,
+ lib.RegisterInputConverter(TypeGeoLite2CountryCSVIn, &GeoLite2CountryCSVIn{
+ Description: DescGeoLite2CountryCSVIn,
})
}
-func newGeoLite2CountryCSV(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+func newGeoLite2CountryCSVIn(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
var tmp struct {
CountryCodeFile string `json:"country"`
IPv4File string `json:"ipv4"`
@@ -48,14 +48,14 @@ func newGeoLite2CountryCSV(action lib.Action, data json.RawMessage) (lib.InputCo
}
if tmp.CountryCodeFile == "" {
- tmp.CountryCodeFile = defaultCCFile
+ tmp.CountryCodeFile = defaultGeoLite2CountryCodeFile
}
// When both of IP files are not specified,
// it means user wants to use the default ones
if tmp.IPv4File == "" && tmp.IPv6File == "" {
- tmp.IPv4File = defaultCountryIPv4File
- tmp.IPv6File = defaultCountryIPv6File
+ tmp.IPv4File = defaultGeoLite2CountryIPv4File
+ tmp.IPv6File = defaultGeoLite2CountryIPv6File
}
// Filter want list
@@ -66,10 +66,10 @@ func newGeoLite2CountryCSV(action lib.Action, data json.RawMessage) (lib.InputCo
}
}
- return &GeoLite2CountryCSV{
- Type: TypeCountryCSV,
+ return &GeoLite2CountryCSVIn{
+ Type: TypeGeoLite2CountryCSVIn,
Action: action,
- Description: DescCountryCSV,
+ Description: DescGeoLite2CountryCSVIn,
CountryCodeFile: tmp.CountryCodeFile,
IPv4File: tmp.IPv4File,
IPv6File: tmp.IPv6File,
@@ -78,7 +78,7 @@ func newGeoLite2CountryCSV(action lib.Action, data json.RawMessage) (lib.InputCo
}, nil
}
-type GeoLite2CountryCSV struct {
+type GeoLite2CountryCSVIn struct {
Type string
Action lib.Action
Description string
@@ -89,19 +89,19 @@ type GeoLite2CountryCSV struct {
OnlyIPType lib.IPType
}
-func (g *GeoLite2CountryCSV) GetType() string {
+func (g *GeoLite2CountryCSVIn) GetType() string {
return g.Type
}
-func (g *GeoLite2CountryCSV) GetAction() lib.Action {
+func (g *GeoLite2CountryCSVIn) GetAction() lib.Action {
return g.Action
}
-func (g *GeoLite2CountryCSV) GetDescription() string {
+func (g *GeoLite2CountryCSVIn) GetDescription() string {
return g.Description
}
-func (g *GeoLite2CountryCSV) Input(container lib.Container) (lib.Container, error) {
+func (g *GeoLite2CountryCSVIn) Input(container lib.Container) (lib.Container, error) {
ccMap, err := g.getCountryCode()
if err != nil {
return nil, err
@@ -151,7 +151,7 @@ func (g *GeoLite2CountryCSV) Input(container lib.Container) (lib.Container, erro
return container, nil
}
-func (g *GeoLite2CountryCSV) getCountryCode() (map[string]string, error) {
+func (g *GeoLite2CountryCSVIn) getCountryCode() (map[string]string, error) {
var f io.ReadCloser
var err error
switch {
@@ -198,7 +198,7 @@ func (g *GeoLite2CountryCSV) getCountryCode() (map[string]string, error) {
return ccMap, nil
}
-func (g *GeoLite2CountryCSV) process(file string, ccMap map[string]string, entries map[string]*lib.Entry) error {
+func (g *GeoLite2CountryCSVIn) process(file string, ccMap map[string]string, entries map[string]*lib.Entry) error {
if len(ccMap) == 0 {
return fmt.Errorf("❌ [type %s | action %s] invalid country code data", g.Type, g.Action)
}
diff --git a/plugin/maxmind/maxmind_country_mmdb_in.go b/plugin/maxmind/maxmind_country_mmdb_in.go
index d69acbdc..7578ba23 100644
--- a/plugin/maxmind/maxmind_country_mmdb_in.go
+++ b/plugin/maxmind/maxmind_country_mmdb_in.go
@@ -13,20 +13,20 @@ import (
)
const (
- TypeMaxmindMMDBIn = "maxmindMMDB"
- DescMaxmindMMDBIn = "Convert MaxMind mmdb database to other formats"
+ TypeGeoLite2CountryMMDBIn = "maxmindMMDB"
+ DescGeoLite2CountryMMDBIn = "Convert MaxMind mmdb database to other formats"
)
func init() {
- lib.RegisterInputConfigCreator(TypeMaxmindMMDBIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newMMDBIn(TypeMaxmindMMDBIn, DescMaxmindMMDBIn, action, data)
+ lib.RegisterInputConfigCreator(TypeGeoLite2CountryMMDBIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+ return newGeoLite2CountryMMDBIn(TypeGeoLite2CountryMMDBIn, DescGeoLite2CountryMMDBIn, action, data)
})
- lib.RegisterInputConverter(TypeMaxmindMMDBIn, &MMDBIn{
- Description: DescMaxmindMMDBIn,
+ lib.RegisterInputConverter(TypeGeoLite2CountryMMDBIn, &GeoLite2CountryMMDBIn{
+ Description: DescGeoLite2CountryMMDBIn,
})
}
-type MMDBIn struct {
+type GeoLite2CountryMMDBIn struct {
Type string
Action lib.Action
Description string
@@ -35,43 +35,43 @@ type MMDBIn struct {
OnlyIPType lib.IPType
}
-func (m *MMDBIn) GetType() string {
- return m.Type
+func (g *GeoLite2CountryMMDBIn) GetType() string {
+ return g.Type
}
-func (m *MMDBIn) GetAction() lib.Action {
- return m.Action
+func (g *GeoLite2CountryMMDBIn) GetAction() lib.Action {
+ return g.Action
}
-func (m *MMDBIn) GetDescription() string {
- return m.Description
+func (g *GeoLite2CountryMMDBIn) GetDescription() string {
+ return g.Description
}
-func (m *MMDBIn) Input(container lib.Container) (lib.Container, error) {
+func (g *GeoLite2CountryMMDBIn) Input(container lib.Container) (lib.Container, error) {
var content []byte
var err error
switch {
- case strings.HasPrefix(strings.ToLower(m.URI), "http://"), strings.HasPrefix(strings.ToLower(m.URI), "https://"):
- content, err = lib.GetRemoteURLContent(m.URI)
+ case strings.HasPrefix(strings.ToLower(g.URI), "http://"), strings.HasPrefix(strings.ToLower(g.URI), "https://"):
+ content, err = lib.GetRemoteURLContent(g.URI)
default:
- content, err = os.ReadFile(m.URI)
+ content, err = os.ReadFile(g.URI)
}
if err != nil {
return nil, err
}
entries := make(map[string]*lib.Entry, 300)
- err = m.generateEntries(content, entries)
+ err = g.generateEntries(content, entries)
if err != nil {
return nil, err
}
if len(entries) == 0 {
- return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", m.Type, m.Action)
+ return nil, fmt.Errorf("❌ [type %s | action %s] no entry is generated", g.Type, g.Action)
}
var ignoreIPType lib.IgnoreIPOption
- switch m.OnlyIPType {
+ switch g.OnlyIPType {
case lib.IPv4:
ignoreIPType = lib.IgnoreIPv6
case lib.IPv6:
@@ -79,7 +79,7 @@ func (m *MMDBIn) Input(container lib.Container) (lib.Container, error) {
}
for _, entry := range entries {
- switch m.Action {
+ switch g.Action {
case lib.ActionAdd:
if err := container.Add(entry, ignoreIPType); err != nil {
return nil, err
@@ -96,7 +96,7 @@ func (m *MMDBIn) Input(container lib.Container) (lib.Container, error) {
return container, nil
}
-func (m *MMDBIn) generateEntries(content []byte, entries map[string]*lib.Entry) error {
+func (g *GeoLite2CountryMMDBIn) generateEntries(content []byte, entries map[string]*lib.Entry) error {
db, err := maxminddb.FromBytes(content)
if err != nil {
return err
@@ -109,8 +109,8 @@ func (m *MMDBIn) generateEntries(content []byte, entries map[string]*lib.Entry)
var subnet *net.IPNet
var err error
- switch m.Type {
- case TypeMaxmindMMDBIn, TypeDBIPCountryMMDBIn:
+ switch g.Type {
+ case TypeGeoLite2CountryMMDBIn, TypeDBIPCountryMMDBIn:
var record geoip2.Country
subnet, err = networks.Network(&record)
if err != nil {
@@ -144,7 +144,7 @@ func (m *MMDBIn) generateEntries(content []byte, entries map[string]*lib.Entry)
continue
}
- if len(m.Want) > 0 && !m.Want[name] {
+ if len(g.Want) > 0 && !g.Want[name] {
continue
}
diff --git a/plugin/maxmind/maxmind_country_mmdb_out.go b/plugin/maxmind/maxmind_country_mmdb_out.go
index fbcd9b3f..cca77632 100644
--- a/plugin/maxmind/maxmind_country_mmdb_out.go
+++ b/plugin/maxmind/maxmind_country_mmdb_out.go
@@ -16,20 +16,20 @@ import (
)
const (
- TypeMaxmindMMDBOut = "maxmindMMDB"
- DescMaxmindMMDBOut = "Convert data to MaxMind mmdb database format"
+ TypeGeoLite2CountryMMDBOut = "maxmindMMDB"
+ DescGeoLite2CountryMMDBOut = "Convert data to MaxMind mmdb database format"
)
func init() {
- lib.RegisterOutputConfigCreator(TypeMaxmindMMDBOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
- return newMMDBOut(TypeMaxmindMMDBOut, DescMaxmindMMDBOut, action, data)
+ lib.RegisterOutputConfigCreator(TypeGeoLite2CountryMMDBOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
+ return newGeoLite2CountryMMDBOut(TypeGeoLite2CountryMMDBOut, DescGeoLite2CountryMMDBOut, action, data)
})
- lib.RegisterOutputConverter(TypeMaxmindMMDBOut, &MMDBOut{
- Description: DescMaxmindMMDBOut,
+ lib.RegisterOutputConverter(TypeGeoLite2CountryMMDBOut, &GeoLite2CountryMMDBOut{
+ Description: DescGeoLite2CountryMMDBOut,
})
}
-type MMDBOut struct {
+type GeoLite2CountryMMDBOut struct {
Type string
Action lib.Action
Description string
@@ -43,25 +43,25 @@ type MMDBOut struct {
SourceMMDBURI string
}
-func (m *MMDBOut) GetType() string {
- return m.Type
+func (g *GeoLite2CountryMMDBOut) GetType() string {
+ return g.Type
}
-func (m *MMDBOut) GetAction() lib.Action {
- return m.Action
+func (g *GeoLite2CountryMMDBOut) GetAction() lib.Action {
+ return g.Action
}
-func (m *MMDBOut) GetDescription() string {
- return m.Description
+func (g *GeoLite2CountryMMDBOut) GetDescription() string {
+ return g.Description
}
-func (m *MMDBOut) Output(container lib.Container) error {
+func (g *GeoLite2CountryMMDBOut) Output(container lib.Container) error {
dbName := ""
dbDesc := ""
recordSize := 28
- switch m.Type {
- case TypeMaxmindMMDBOut:
+ switch g.Type {
+ case TypeGeoLite2CountryMMDBOut:
dbName = "GeoLite2-Country"
dbDesc = "Customized GeoLite2 Country database"
@@ -88,20 +88,20 @@ func (m *MMDBOut) Output(container lib.Container) error {
}
// Get extra info
- extraInfo, err := m.GetExtraInfo()
+ extraInfo, err := g.GetExtraInfo()
if err != nil {
return err
}
updated := false
- for _, name := range m.filterAndSortList(container) {
+ for _, name := range g.filterAndSortList(container) {
entry, found := container.GetEntry(name)
if !found {
log.Printf("❌ entry %s not found\n", name)
continue
}
- if err := m.marshalData(writer, entry, extraInfo); err != nil {
+ if err := g.marshalData(writer, entry, extraInfo); err != nil {
return err
}
@@ -109,13 +109,13 @@ func (m *MMDBOut) Output(container lib.Container) error {
}
if updated {
- return m.writeFile(m.OutputName, writer)
+ return g.writeFile(g.OutputName, writer)
}
return nil
}
-func (m *MMDBOut) filterAndSortList(container lib.Container) []string {
+func (g *GeoLite2CountryMMDBOut) filterAndSortList(container lib.Container) []string {
/*
Note: The IPs and/or CIDRs of the latter list will overwrite those of the former one
when duplicated data found due to MaxMind mmdb file format constraint.
@@ -127,14 +127,14 @@ func (m *MMDBOut) filterAndSortList(container lib.Container) []string {
*/
excludeMap := make(map[string]bool)
- for _, exclude := range m.Exclude {
+ for _, exclude := range g.Exclude {
if exclude = strings.ToUpper(strings.TrimSpace(exclude)); exclude != "" {
excludeMap[exclude] = true
}
}
- wantList := make([]string, 0, len(m.Want))
- for _, want := range m.Want {
+ wantList := make([]string, 0, len(g.Want))
+ for _, want := range g.Want {
if want = strings.ToUpper(strings.TrimSpace(want)); want != "" && !excludeMap[want] {
wantList = append(wantList, want)
}
@@ -144,9 +144,9 @@ func (m *MMDBOut) filterAndSortList(container lib.Container) []string {
return wantList
}
- overwriteList := make([]string, 0, len(m.Overwrite))
+ overwriteList := make([]string, 0, len(g.Overwrite))
overwriteMap := make(map[string]bool)
- for _, overwrite := range m.Overwrite {
+ for _, overwrite := range g.Overwrite {
if overwrite = strings.ToUpper(strings.TrimSpace(overwrite)); overwrite != "" && !excludeMap[overwrite] {
overwriteList = append(overwriteList, overwrite)
overwriteMap[overwrite] = true
@@ -171,10 +171,10 @@ func (m *MMDBOut) filterAndSortList(container lib.Container) []string {
return list
}
-func (m *MMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry, extraInfo map[string]interface{}) error {
+func (g *GeoLite2CountryMMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry, extraInfo map[string]any) error {
var entryCidr []string
var err error
- switch m.OnlyIPType {
+ switch g.OnlyIPType {
case lib.IPv4:
entryCidr, err = entry.MarshalText(lib.IgnoreIPv6)
case lib.IPv6:
@@ -187,10 +187,10 @@ func (m *MMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry, extraIn
}
var record mmdbtype.DataType
- switch strings.TrimSpace(m.SourceMMDBURI) {
+ switch strings.TrimSpace(g.SourceMMDBURI) {
case "": // No need to get extra info
- switch m.Type {
- case TypeMaxmindMMDBOut, TypeDBIPCountryMMDBOut:
+ switch g.Type {
+ case TypeGeoLite2CountryMMDBOut, TypeDBIPCountryMMDBOut:
record = mmdbtype.Map{
"country": mmdbtype.Map{
"iso_code": mmdbtype.String(entry.GetName()),
@@ -207,11 +207,11 @@ func (m *MMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry, extraIn
}
default: // Get extra info
- switch m.Type {
- case TypeMaxmindMMDBOut:
+ switch g.Type {
+ case TypeGeoLite2CountryMMDBOut:
info, found := extraInfo[entry.GetName()].(geoip2.Country)
if !found {
- log.Printf("⚠️ [type %s | action %s] not found extra info for list %s\n", m.Type, m.Action, entry.GetName())
+ log.Printf("⚠️ [type %s | action %s] not found extra info for list %s\n", g.Type, g.Action, entry.GetName())
record = mmdbtype.Map{
"country": mmdbtype.Map{
@@ -273,7 +273,7 @@ func (m *MMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry, extraIn
case TypeDBIPCountryMMDBOut:
info, found := extraInfo[entry.GetName()].(geoip2.Country)
if !found {
- log.Printf("⚠️ [type %s | action %s] not found extra info for list %s\n", m.Type, m.Action, entry.GetName())
+ log.Printf("⚠️ [type %s | action %s] not found extra info for list %s\n", g.Type, g.Action, entry.GetName())
record = mmdbtype.Map{
"country": mmdbtype.Map{
@@ -347,7 +347,7 @@ func (m *MMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry, extraIn
})
if !found {
- log.Printf("⚠️ [type %s | action %s] not found extra info for list %s\n", m.Type, m.Action, entry.GetName())
+ log.Printf("⚠️ [type %s | action %s] not found extra info for list %s\n", g.Type, g.Action, entry.GetName())
record = mmdbtype.Map{
"country": mmdbtype.String(entry.GetName()),
@@ -379,12 +379,12 @@ func (m *MMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry, extraIn
return nil
}
-func (m *MMDBOut) writeFile(filename string, writer *mmdbwriter.Tree) error {
- if err := os.MkdirAll(m.OutputDir, 0755); err != nil {
+func (g *GeoLite2CountryMMDBOut) writeFile(filename string, writer *mmdbwriter.Tree) error {
+ if err := os.MkdirAll(g.OutputDir, 0755); err != nil {
return err
}
- f, err := os.OpenFile(filepath.Join(m.OutputDir, filename), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
+ f, err := os.OpenFile(filepath.Join(g.OutputDir, filename), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
return err
}
@@ -394,7 +394,7 @@ func (m *MMDBOut) writeFile(filename string, writer *mmdbwriter.Tree) error {
return err
}
- log.Printf("✅ [%s] %s --> %s", m.Type, filename, m.OutputDir)
+ log.Printf("✅ [%s] %s --> %s", g.Type, filename, g.OutputDir)
return nil
}