summaryrefslogtreecommitdiff
path: root/plugin/maxmind
diff options
context:
space:
mode:
authorcopilot-swe-agent[bot] <[email protected]>2026-04-28 18:24:00 +0000
committerGitHub <[email protected]>2026-04-28 18:24:00 +0000
commit8834c0be63ee88e0ad23fe621d5f5fe344b32089 (patch)
tree49029d2fd927e8832d05420d1bf1cf850aa22325 /plugin/maxmind
parent4f125e579472e5ed87fd052ef68ab80f5fe679b0 (diff)
Refactor all plugins to use functional options patterncopilot/refactor-plugins-functional-options
Agent-Logs-Url: https://github.com/Loyalsoldier/geoip/sessions/e2b66c9a-3d01-490c-9b31-32109cfe4feb Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/maxmind')
-rw-r--r--plugin/maxmind/common_in.go90
-rw-r--r--plugin/maxmind/common_out.go121
-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.go153
-rw-r--r--plugin/maxmind/maxmind_country_csv_in.go119
-rw-r--r--plugin/maxmind/maxmind_country_mmdb_in.go16
-rw-r--r--plugin/maxmind/maxmind_country_mmdb_out.go20
10 files changed, 352 insertions, 183 deletions
diff --git a/plugin/maxmind/common_in.go b/plugin/maxmind/common_in.go
index 3d97388c..a937ef98 100644
--- a/plugin/maxmind/common_in.go
+++ b/plugin/maxmind/common_in.go
@@ -14,46 +14,78 @@ var (
defaultIPInfoCountryMMDBFile = filepath.Join("./", "ipinfo", "country.mmdb")
)
-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"`
- OnlyIPType lib.IPType `json:"onlyIPType"`
+func NewGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, opts ...lib.InputOption) lib.InputConverter {
+ g := &geoLite2CountryMMDBIn{
+ Type: iType,
+ Action: action,
+ Description: iDesc,
}
- if len(data) > 0 {
- if err := json.Unmarshal(data, &tmp); err != nil {
- return nil, err
+ for _, opt := range opts {
+ if opt != nil {
+ opt(g)
}
}
- if tmp.URI == "" {
- switch iType {
- case TypeGeoLite2CountryMMDBIn:
- tmp.URI = defaultGeoLite2CountryMMDBFile
+ return g
+}
+
+func WithGeoLite2CountryMMDBInURI(iType, uri string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ uri = strings.TrimSpace(uri)
+ if uri == "" {
+ switch iType {
+ case TypeGeoLite2CountryMMDBIn:
+ uri = defaultGeoLite2CountryMMDBFile
+ case TypeDBIPCountryMMDBIn:
+ uri = defaultDBIPCountryMMDBFile
+ case TypeIPInfoCountryMMDBIn:
+ uri = defaultIPInfoCountryMMDBFile
+ }
+ }
- case TypeDBIPCountryMMDBIn:
- tmp.URI = defaultDBIPCountryMMDBFile
+ s.(*geoLite2CountryMMDBIn).URI = uri
+ }
+}
- case TypeIPInfoCountryMMDBIn:
- tmp.URI = defaultIPInfoCountryMMDBFile
+func WithGeoLite2CountryMMDBInWantedList(lists []string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ wantList := make(map[string]bool)
+ for _, want := range lists {
+ if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
+ wantList[want] = true
+ }
}
+
+ s.(*geoLite2CountryMMDBIn).Want = wantList
+ }
+}
+
+func WithGeoLite2CountryMMDBInOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*geoLite2CountryMMDBIn).OnlyIPType = onlyIPType
+ }
+}
+
+func NewGeoLite2CountryMMDBInFromBytes(iType string, iDesc string, action lib.Action, data []byte) (lib.InputConverter, error) {
+ var tmp struct {
+ URI string `json:"uri"`
+ Want []string `json:"wantedList"`
+ OnlyIPType lib.IPType `json:"onlyIPType"`
}
- // Filter want list
- wantList := make(map[string]bool)
- for _, want := range tmp.Want {
- if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
- wantList[want] = true
+ if len(data) > 0 {
+ if err := json.Unmarshal(data, &tmp); err != nil {
+ return nil, err
}
}
- return &GeoLite2CountryMMDBIn{
- Type: iType,
- Action: action,
- Description: iDesc,
- URI: tmp.URI,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
+ return NewGeoLite2CountryMMDBIn(
+ iType,
+ iDesc,
+ action,
+ WithGeoLite2CountryMMDBInURI(iType, tmp.URI),
+ WithGeoLite2CountryMMDBInWantedList(tmp.Want),
+ WithGeoLite2CountryMMDBInOnlyIPType(tmp.OnlyIPType),
+ ), nil
}
diff --git a/plugin/maxmind/common_out.go b/plugin/maxmind/common_out.go
index 21063faa..9b809f74 100644
--- a/plugin/maxmind/common_out.go
+++ b/plugin/maxmind/common_out.go
@@ -100,7 +100,82 @@ func (d dbipCountry) HasData() bool {
return d != zeroDBIPCountry
}
-func newGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
+func NewGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, opts ...lib.OutputOption) lib.OutputConverter {
+ g := &geoLite2CountryMMDBOut{
+ Type: iType,
+ Action: action,
+ Description: iDesc,
+ }
+
+ for _, opt := range opts {
+ if opt != nil {
+ opt(g)
+ }
+ }
+
+ return g
+}
+
+func WithGeoLite2CountryMMDBOutOutputName(name string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ name = strings.TrimSpace(name)
+ if name == "" {
+ name = defaultGeoLite2CountryMMDBOutputName
+ }
+
+ s.(*geoLite2CountryMMDBOut).OutputName = name
+ }
+}
+
+func WithGeoLite2CountryMMDBOutOutputDir(iType, dir string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ dir = strings.TrimSpace(dir)
+ if dir == "" {
+ switch iType {
+ case TypeGeoLite2CountryMMDBOut:
+ dir = defaultMaxmindOutputDir
+ case TypeDBIPCountryMMDBOut:
+ dir = defaultDBIPOutputDir
+ case TypeIPInfoCountryMMDBOut:
+ dir = defaultIPInfoOutputDir
+ }
+ }
+
+ s.(*geoLite2CountryMMDBOut).OutputDir = dir
+ }
+}
+
+func WithGeoLite2CountryMMDBOutWantedList(lists []string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*geoLite2CountryMMDBOut).Want = lists
+ }
+}
+
+func WithGeoLite2CountryMMDBOutOverwriteList(lists []string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*geoLite2CountryMMDBOut).Overwrite = lists
+ }
+}
+
+func WithGeoLite2CountryMMDBOutExcludedList(lists []string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*geoLite2CountryMMDBOut).Exclude = lists
+ }
+}
+
+func WithGeoLite2CountryMMDBOutOnlyIPType(onlyIPType lib.IPType) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*geoLite2CountryMMDBOut).OnlyIPType = onlyIPType
+ }
+}
+
+func WithGeoLite2CountryMMDBOutSourceMMDBURI(uri string) lib.OutputOption {
+ return func(s lib.OutputConverter) {
+ s.(*geoLite2CountryMMDBOut).SourceMMDBURI = uri
+ }
+}
+
+func NewGeoLite2CountryMMDBOutFromBytes(iType string, iDesc string, action lib.Action, data []byte) (lib.OutputConverter, error) {
var tmp struct {
OutputName string `json:"outputName"`
OutputDir string `json:"outputDir"`
@@ -118,39 +193,21 @@ func newGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, da
}
}
- if tmp.OutputName == "" {
- tmp.OutputName = defaultGeoLite2CountryMMDBOutputName
- }
-
- if tmp.OutputDir == "" {
- switch iType {
- case TypeGeoLite2CountryMMDBOut:
- tmp.OutputDir = defaultMaxmindOutputDir
-
- case TypeDBIPCountryMMDBOut:
- tmp.OutputDir = defaultDBIPOutputDir
-
- case TypeIPInfoCountryMMDBOut:
- tmp.OutputDir = defaultIPInfoOutputDir
- }
- }
-
- return &GeoLite2CountryMMDBOut{
- Type: iType,
- Action: action,
- Description: iDesc,
- OutputName: tmp.OutputName,
- OutputDir: tmp.OutputDir,
- Want: tmp.Want,
- Overwrite: tmp.Overwrite,
- Exclude: tmp.Exclude,
- OnlyIPType: tmp.OnlyIPType,
-
- SourceMMDBURI: tmp.SourceMMDBURI,
- }, nil
+ return NewGeoLite2CountryMMDBOut(
+ iType,
+ iDesc,
+ action,
+ WithGeoLite2CountryMMDBOutOutputName(tmp.OutputName),
+ WithGeoLite2CountryMMDBOutOutputDir(iType, tmp.OutputDir),
+ WithGeoLite2CountryMMDBOutWantedList(tmp.Want),
+ WithGeoLite2CountryMMDBOutOverwriteList(tmp.Overwrite),
+ WithGeoLite2CountryMMDBOutExcludedList(tmp.Exclude),
+ WithGeoLite2CountryMMDBOutOnlyIPType(tmp.OnlyIPType),
+ WithGeoLite2CountryMMDBOutSourceMMDBURI(tmp.SourceMMDBURI),
+ ), nil
}
-func (g *GeoLite2CountryMMDBOut) GetExtraInfo() (map[string]any, error) {
+func (g *geoLite2CountryMMDBOut) GetExtraInfo() (map[string]any, error) {
if strings.TrimSpace(g.SourceMMDBURI) == "" {
return nil, nil
}
diff --git a/plugin/maxmind/dbip_country_mmdb_in.go b/plugin/maxmind/dbip_country_mmdb_in.go
index 304f29e5..713b9626 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 newGeoLite2CountryMMDBIn(TypeDBIPCountryMMDBIn, DescDBIPCountryMMDBIn, action, data)
+ return NewGeoLite2CountryMMDBInFromBytes(TypeDBIPCountryMMDBIn, DescDBIPCountryMMDBIn, action, data)
})
- lib.RegisterInputConverter(TypeDBIPCountryMMDBIn, &GeoLite2CountryMMDBIn{
+ 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 77857d95..73d8c5a8 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 newGeoLite2CountryMMDBOut(TypeDBIPCountryMMDBOut, DescDBIPCountryMMDBOut, action, data)
+ return NewGeoLite2CountryMMDBOutFromBytes(TypeDBIPCountryMMDBOut, DescDBIPCountryMMDBOut, action, data)
})
- lib.RegisterOutputConverter(TypeDBIPCountryMMDBOut, &GeoLite2CountryMMDBOut{
+ 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 8551e453..c27bafa3 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 newGeoLite2CountryMMDBIn(TypeIPInfoCountryMMDBIn, DescIPInfoCountryMMDBIn, action, data)
+ return NewGeoLite2CountryMMDBInFromBytes(TypeIPInfoCountryMMDBIn, DescIPInfoCountryMMDBIn, action, data)
})
- lib.RegisterInputConverter(TypeIPInfoCountryMMDBIn, &GeoLite2CountryMMDBIn{
+ 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 bed18af7..e10b4ca6 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 newGeoLite2CountryMMDBOut(TypeIPInfoCountryMMDBOut, DescIPInfoCountryMMDBOut, action, data)
+ return NewGeoLite2CountryMMDBOutFromBytes(TypeIPInfoCountryMMDBOut, DescIPInfoCountryMMDBOut, action, data)
})
- lib.RegisterOutputConverter(TypeIPInfoCountryMMDBOut, &GeoLite2CountryMMDBOut{
+ 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 d924e3f9..8444277a 100644
--- a/plugin/maxmind/maxmind_asn_csv_in.go
+++ b/plugin/maxmind/maxmind_asn_csv_in.go
@@ -24,101 +24,138 @@ var (
func init() {
lib.RegisterInputConfigCreator(TypeGeoLite2ASNCSVIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newGeoLite2ASNCSVIn(action, data)
+ return NewGeoLite2ASNCSVInFromBytes(action, data)
})
- lib.RegisterInputConverter(TypeGeoLite2ASNCSVIn, &GeoLite2ASNCSVIn{
+ lib.RegisterInputConverter(TypeGeoLite2ASNCSVIn, &geoLite2ASNCSVIn{
Description: DescGeoLite2ASNCSVIn,
})
}
-func newGeoLite2ASNCSVIn(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- var tmp struct {
- IPv4File string `json:"ipv4"`
- IPv6File string `json:"ipv6"`
- Want lib.WantedListExtended `json:"wantedList"`
- OnlyIPType lib.IPType `json:"onlyIPType"`
+type geoLite2ASNCSVIn struct {
+ Type string
+ Action lib.Action
+ Description string
+ IPv4File string
+ IPv6File string
+ Want map[string][]string
+ OnlyIPType lib.IPType
+}
+
+func NewGeoLite2ASNCSVIn(action lib.Action, opts ...lib.InputOption) lib.InputConverter {
+ g := &geoLite2ASNCSVIn{
+ Type: TypeGeoLite2ASNCSVIn,
+ Action: action,
+ Description: DescGeoLite2ASNCSVIn,
}
- if len(data) > 0 {
- if err := json.Unmarshal(data, &tmp); err != nil {
- return nil, err
+ for _, opt := range opts {
+ if opt != nil {
+ opt(g)
}
}
- // When both of IP files are not specified,
- // it means user wants to use the default ones
- if tmp.IPv4File == "" && tmp.IPv6File == "" {
- tmp.IPv4File = defaultGeoLite2ASNCSVIPv4File
- tmp.IPv6File = defaultGeoLite2ASNCSVIPv6File
+ return g
+}
+
+func WithGeoLite2ASNCSVInIPv4File(file string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*geoLite2ASNCSVIn).IPv4File = file
}
+}
+
+func WithGeoLite2ASNCSVInIPv6File(file string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*geoLite2ASNCSVIn).IPv6File = file
+ }
+}
+
+func WithGeoLite2ASNCSVInWantedList(want lib.WantedListExtended) lib.InputOption {
+ return func(s lib.InputConverter) {
+ wantList := make(map[string][]string)
- // Filter want list
- wantList := make(map[string][]string) // map[asn][]listname or map[asn][]asn
+ for list, asnList := range want.TypeMap {
+ list = strings.ToUpper(strings.TrimSpace(list))
+ if list == "" {
+ continue
+ }
+
+ for _, asn := range asnList {
+ asn = strings.TrimPrefix(strings.ToLower(strings.TrimSpace(asn)), "as")
+ if asn == "" {
+ continue
+ }
- for list, asnList := range tmp.Want.TypeMap {
- list = strings.ToUpper(strings.TrimSpace(list))
- if list == "" {
- continue
+ if listArr, found := wantList[asn]; found {
+ listArr = append(listArr, list)
+ wantList[asn] = listArr
+ } else {
+ wantList[asn] = []string{list}
+ }
+ }
}
- for _, asn := range asnList {
+ for _, asn := range want.TypeSlice {
asn = strings.TrimPrefix(strings.ToLower(strings.TrimSpace(asn)), "as")
if asn == "" {
continue
}
- if listArr, found := wantList[asn]; found {
- listArr = append(listArr, list)
- wantList[asn] = listArr
- } else {
- wantList[asn] = []string{list}
- }
+ wantList[asn] = []string{"AS" + asn}
}
+
+ s.(*geoLite2ASNCSVIn).Want = wantList
}
+}
- for _, asn := range tmp.Want.TypeSlice {
- asn = strings.TrimPrefix(strings.ToLower(strings.TrimSpace(asn)), "as")
- if asn == "" {
- continue
- }
+func WithGeoLite2ASNCSVInOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*geoLite2ASNCSVIn).OnlyIPType = onlyIPType
+ }
+}
- wantList[asn] = []string{"AS" + asn}
+func NewGeoLite2ASNCSVInFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) {
+ var tmp struct {
+ IPv4File string `json:"ipv4"`
+ IPv6File string `json:"ipv6"`
+ Want lib.WantedListExtended `json:"wantedList"`
+ OnlyIPType lib.IPType `json:"onlyIPType"`
}
- return &GeoLite2ASNCSVIn{
- Type: TypeGeoLite2ASNCSVIn,
- Action: action,
- Description: DescGeoLite2ASNCSVIn,
- IPv4File: tmp.IPv4File,
- IPv6File: tmp.IPv6File,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
-}
+ if len(data) > 0 {
+ if err := json.Unmarshal(data, &tmp); err != nil {
+ return nil, err
+ }
+ }
-type GeoLite2ASNCSVIn struct {
- Type string
- Action lib.Action
- Description string
- IPv4File string
- IPv6File string
- Want map[string][]string
- OnlyIPType lib.IPType
+ // When both of IP files are not specified,
+ // it means user wants to use the default ones
+ if tmp.IPv4File == "" && tmp.IPv6File == "" {
+ tmp.IPv4File = defaultGeoLite2ASNCSVIPv4File
+ tmp.IPv6File = defaultGeoLite2ASNCSVIPv6File
+ }
+
+ return NewGeoLite2ASNCSVIn(
+ action,
+ WithGeoLite2ASNCSVInIPv4File(tmp.IPv4File),
+ WithGeoLite2ASNCSVInIPv6File(tmp.IPv6File),
+ WithGeoLite2ASNCSVInWantedList(tmp.Want),
+ WithGeoLite2ASNCSVInOnlyIPType(tmp.OnlyIPType),
+ ), nil
}
-func (g *GeoLite2ASNCSVIn) GetType() string {
+func (g *geoLite2ASNCSVIn) GetType() string {
return g.Type
}
-func (g *GeoLite2ASNCSVIn) GetAction() lib.Action {
+func (g *geoLite2ASNCSVIn) GetAction() lib.Action {
return g.Action
}
-func (g *GeoLite2ASNCSVIn) GetDescription() string {
+func (g *geoLite2ASNCSVIn) GetDescription() string {
return g.Description
}
-func (g *GeoLite2ASNCSVIn) 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 != "" {
@@ -157,7 +194,7 @@ func (g *GeoLite2ASNCSVIn) Input(container lib.Container) (lib.Container, error)
return container, nil
}
-func (g *GeoLite2ASNCSVIn) 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 93d38c37..f344fc77 100644
--- a/plugin/maxmind/maxmind_country_csv_in.go
+++ b/plugin/maxmind/maxmind_country_csv_in.go
@@ -25,14 +25,78 @@ var (
func init() {
lib.RegisterInputConfigCreator(TypeGeoLite2CountryCSVIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newGeoLite2CountryCSVIn(action, data)
+ return NewGeoLite2CountryCSVInFromBytes(action, data)
})
- lib.RegisterInputConverter(TypeGeoLite2CountryCSVIn, &GeoLite2CountryCSVIn{
+ lib.RegisterInputConverter(TypeGeoLite2CountryCSVIn, &geoLite2CountryCSVIn{
Description: DescGeoLite2CountryCSVIn,
})
}
-func newGeoLite2CountryCSVIn(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+type geoLite2CountryCSVIn struct {
+ Type string
+ Action lib.Action
+ Description string
+ CountryCodeFile string
+ IPv4File string
+ IPv6File string
+ Want map[string]bool
+ OnlyIPType lib.IPType
+}
+
+func NewGeoLite2CountryCSVIn(action lib.Action, opts ...lib.InputOption) lib.InputConverter {
+ g := &geoLite2CountryCSVIn{
+ Type: TypeGeoLite2CountryCSVIn,
+ Action: action,
+ Description: DescGeoLite2CountryCSVIn,
+ }
+
+ for _, opt := range opts {
+ if opt != nil {
+ opt(g)
+ }
+ }
+
+ return g
+}
+
+func WithGeoLite2CountryCSVInCountryCodeFile(file string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*geoLite2CountryCSVIn).CountryCodeFile = file
+ }
+}
+
+func WithGeoLite2CountryCSVInIPv4File(file string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*geoLite2CountryCSVIn).IPv4File = file
+ }
+}
+
+func WithGeoLite2CountryCSVInIPv6File(file string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*geoLite2CountryCSVIn).IPv6File = file
+ }
+}
+
+func WithGeoLite2CountryCSVInWantedList(lists []string) lib.InputOption {
+ return func(s lib.InputConverter) {
+ wantList := make(map[string]bool)
+ for _, want := range lists {
+ if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
+ wantList[want] = true
+ }
+ }
+
+ s.(*geoLite2CountryCSVIn).Want = wantList
+ }
+}
+
+func WithGeoLite2CountryCSVInOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(s lib.InputConverter) {
+ s.(*geoLite2CountryCSVIn).OnlyIPType = onlyIPType
+ }
+}
+
+func NewGeoLite2CountryCSVInFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) {
var tmp struct {
CountryCodeFile string `json:"country"`
IPv4File string `json:"ipv4"`
@@ -58,50 +122,29 @@ func newGeoLite2CountryCSVIn(action lib.Action, data json.RawMessage) (lib.Input
tmp.IPv6File = defaultGeoLite2CountryIPv6File
}
- // Filter want list
- wantList := make(map[string]bool)
- for _, want := range tmp.Want {
- if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
- wantList[want] = true
- }
- }
-
- return &GeoLite2CountryCSVIn{
- Type: TypeGeoLite2CountryCSVIn,
- Action: action,
- Description: DescGeoLite2CountryCSVIn,
- CountryCodeFile: tmp.CountryCodeFile,
- IPv4File: tmp.IPv4File,
- IPv6File: tmp.IPv6File,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
-}
-
-type GeoLite2CountryCSVIn struct {
- Type string
- Action lib.Action
- Description string
- CountryCodeFile string
- IPv4File string
- IPv6File string
- Want map[string]bool
- OnlyIPType lib.IPType
+ return NewGeoLite2CountryCSVIn(
+ action,
+ WithGeoLite2CountryCSVInCountryCodeFile(tmp.CountryCodeFile),
+ WithGeoLite2CountryCSVInIPv4File(tmp.IPv4File),
+ WithGeoLite2CountryCSVInIPv6File(tmp.IPv6File),
+ WithGeoLite2CountryCSVInWantedList(tmp.Want),
+ WithGeoLite2CountryCSVInOnlyIPType(tmp.OnlyIPType),
+ ), nil
}
-func (g *GeoLite2CountryCSVIn) GetType() string {
+func (g *geoLite2CountryCSVIn) GetType() string {
return g.Type
}
-func (g *GeoLite2CountryCSVIn) GetAction() lib.Action {
+func (g *geoLite2CountryCSVIn) GetAction() lib.Action {
return g.Action
}
-func (g *GeoLite2CountryCSVIn) GetDescription() string {
+func (g *geoLite2CountryCSVIn) GetDescription() string {
return g.Description
}
-func (g *GeoLite2CountryCSVIn) 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
@@ -145,7 +188,7 @@ func (g *GeoLite2CountryCSVIn) Input(container lib.Container) (lib.Container, er
return container, nil
}
-func (g *GeoLite2CountryCSVIn) getCountryCode() (map[string]string, error) {
+func (g *geoLite2CountryCSVIn) getCountryCode() (map[string]string, error) {
var f io.ReadCloser
var err error
switch {
@@ -192,7 +235,7 @@ func (g *GeoLite2CountryCSVIn) getCountryCode() (map[string]string, error) {
return ccMap, nil
}
-func (g *GeoLite2CountryCSVIn) 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 1f6d5386..d9015e34 100644
--- a/plugin/maxmind/maxmind_country_mmdb_in.go
+++ b/plugin/maxmind/maxmind_country_mmdb_in.go
@@ -18,14 +18,14 @@ const (
func init() {
lib.RegisterInputConfigCreator(TypeGeoLite2CountryMMDBIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
- return newGeoLite2CountryMMDBIn(TypeGeoLite2CountryMMDBIn, DescGeoLite2CountryMMDBIn, action, data)
+ return NewGeoLite2CountryMMDBInFromBytes(TypeGeoLite2CountryMMDBIn, DescGeoLite2CountryMMDBIn, action, data)
})
- lib.RegisterInputConverter(TypeGeoLite2CountryMMDBIn, &GeoLite2CountryMMDBIn{
+ lib.RegisterInputConverter(TypeGeoLite2CountryMMDBIn, &geoLite2CountryMMDBIn{
Description: DescGeoLite2CountryMMDBIn,
})
}
-type GeoLite2CountryMMDBIn struct {
+type geoLite2CountryMMDBIn struct {
Type string
Action lib.Action
Description string
@@ -34,19 +34,19 @@ type GeoLite2CountryMMDBIn struct {
OnlyIPType lib.IPType
}
-func (g *GeoLite2CountryMMDBIn) GetType() string {
+func (g *geoLite2CountryMMDBIn) GetType() string {
return g.Type
}
-func (g *GeoLite2CountryMMDBIn) GetAction() lib.Action {
+func (g *geoLite2CountryMMDBIn) GetAction() lib.Action {
return g.Action
}
-func (g *GeoLite2CountryMMDBIn) GetDescription() string {
+func (g *geoLite2CountryMMDBIn) GetDescription() string {
return g.Description
}
-func (g *GeoLite2CountryMMDBIn) Input(container lib.Container) (lib.Container, error) {
+func (g *geoLite2CountryMMDBIn) Input(container lib.Container) (lib.Container, error) {
var content []byte
var err error
switch {
@@ -89,7 +89,7 @@ func (g *GeoLite2CountryMMDBIn) Input(container lib.Container) (lib.Container, e
return container, nil
}
-func (g *GeoLite2CountryMMDBIn) generateEntries(content []byte, entries map[string]*lib.Entry) error {
+func (g *geoLite2CountryMMDBIn) generateEntries(content []byte, entries map[string]*lib.Entry) error {
db, err := maxminddb.OpenBytes(content)
if err != nil {
return err
diff --git a/plugin/maxmind/maxmind_country_mmdb_out.go b/plugin/maxmind/maxmind_country_mmdb_out.go
index fc72e83c..1bbaad6e 100644
--- a/plugin/maxmind/maxmind_country_mmdb_out.go
+++ b/plugin/maxmind/maxmind_country_mmdb_out.go
@@ -22,14 +22,14 @@ const (
func init() {
lib.RegisterOutputConfigCreator(TypeGeoLite2CountryMMDBOut, func(action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
- return newGeoLite2CountryMMDBOut(TypeGeoLite2CountryMMDBOut, DescGeoLite2CountryMMDBOut, action, data)
+ return NewGeoLite2CountryMMDBOutFromBytes(TypeGeoLite2CountryMMDBOut, DescGeoLite2CountryMMDBOut, action, data)
})
- lib.RegisterOutputConverter(TypeGeoLite2CountryMMDBOut, &GeoLite2CountryMMDBOut{
+ lib.RegisterOutputConverter(TypeGeoLite2CountryMMDBOut, &geoLite2CountryMMDBOut{
Description: DescGeoLite2CountryMMDBOut,
})
}
-type GeoLite2CountryMMDBOut struct {
+type geoLite2CountryMMDBOut struct {
Type string
Action lib.Action
Description string
@@ -43,19 +43,19 @@ type GeoLite2CountryMMDBOut struct {
SourceMMDBURI string
}
-func (g *GeoLite2CountryMMDBOut) GetType() string {
+func (g *geoLite2CountryMMDBOut) GetType() string {
return g.Type
}
-func (g *GeoLite2CountryMMDBOut) GetAction() lib.Action {
+func (g *geoLite2CountryMMDBOut) GetAction() lib.Action {
return g.Action
}
-func (g *GeoLite2CountryMMDBOut) GetDescription() string {
+func (g *geoLite2CountryMMDBOut) GetDescription() string {
return g.Description
}
-func (g *GeoLite2CountryMMDBOut) Output(container lib.Container) error {
+func (g *geoLite2CountryMMDBOut) Output(container lib.Container) error {
dbName := ""
dbDesc := ""
dbLanguages := []string{"en"}
@@ -119,7 +119,7 @@ func (g *GeoLite2CountryMMDBOut) Output(container lib.Container) error {
return nil
}
-func (g *GeoLite2CountryMMDBOut) 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.
@@ -175,7 +175,7 @@ func (g *GeoLite2CountryMMDBOut) filterAndSortList(container lib.Container) []st
return list
}
-func (g *GeoLite2CountryMMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry, extraInfo map[string]any) error {
+func (g *geoLite2CountryMMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib.Entry, extraInfo map[string]any) error {
entryCidr, err := entry.MarshalText(lib.GetIgnoreIPType(g.OnlyIPType))
if err != nil {
return err
@@ -371,7 +371,7 @@ func (g *GeoLite2CountryMMDBOut) marshalData(writer *mmdbwriter.Tree, entry *lib
return nil
}
-func (g *GeoLite2CountryMMDBOut) writeFile(filename string, writer *mmdbwriter.Tree) error {
+func (g *geoLite2CountryMMDBOut) writeFile(filename string, writer *mmdbwriter.Tree) error {
if err := os.MkdirAll(g.OutputDir, 0755); err != nil {
return err
}