summaryrefslogtreecommitdiff
path: root/plugin/maxmind
diff options
context:
space:
mode:
authorcopilot-swe-agent[bot] <[email protected]>2026-03-09 06:35:47 +0000
committercopilot-swe-agent[bot] <[email protected]>2026-03-09 06:35:47 +0000
commit2600825c50d7e7f2b4427674f1aefca270bcca27 (patch)
tree18f216c6af28fd3975a0665d75ccf7b5c0078cb6 /plugin/maxmind
parent5a14eb90f575b983aa407341af91aa7654e65f12 (diff)
Refactor: all plugin subdirectories use option patterncopilot/modify-plugin-files-subdirectories
Apply the same functional options pattern from plugin/singbox to: - plugin/mihomo (mrs_in.go, mrs_out.go) - plugin/plaintext (text_in.go, common_in.go, common_out.go, text_out.go, clash_in.go, clash_out.go, json_in.go, surge_in.go, surge_out.go) - plugin/maxmind (all input/output files) - plugin/v2ray (dat_in.go, dat_out.go) - plugin/special (cutter.go, lookup.go, private.go, stdin.go, stdout.go) - lookup.go and merge.go updated to use new constructors Co-authored-by: Loyalsoldier <[email protected]>
Diffstat (limited to 'plugin/maxmind')
-rw-r--r--plugin/maxmind/common_in.go25
-rw-r--r--plugin/maxmind/common_out.go44
-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.go92
-rw-r--r--plugin/maxmind/maxmind_country_csv_in.go128
-rw-r--r--plugin/maxmind/maxmind_country_mmdb_in.go57
-rw-r--r--plugin/maxmind/maxmind_country_mmdb_out.go95
10 files changed, 312 insertions, 145 deletions
diff --git a/plugin/maxmind/common_in.go b/plugin/maxmind/common_in.go
index 3d97388c..fd34a735 100644
--- a/plugin/maxmind/common_in.go
+++ b/plugin/maxmind/common_in.go
@@ -3,7 +3,6 @@ package maxmind
import (
"encoding/json"
"path/filepath"
- "strings"
"github.com/Loyalsoldier/geoip/lib"
)
@@ -14,7 +13,7 @@ var (
defaultIPInfoCountryMMDBFile = filepath.Join("./", "ipinfo", "country.mmdb")
)
-func newGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+func NewGeoLite2CountryMMDBInFromBytes(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
var tmp struct {
URI string `json:"uri"`
Want []string `json:"wantedList"`
@@ -40,20 +39,10 @@ func newGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, dat
}
}
- // 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 &GeoLite2CountryMMDBIn{
- Type: iType,
- Action: action,
- Description: iDesc,
- URI: tmp.URI,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
+ return NewGeoLite2CountryMMDBIn(
+ iType, iDesc, action,
+ WithURI(tmp.URI),
+ WithInputWantedList(tmp.Want),
+ WithInputOnlyIPType(tmp.OnlyIPType),
+ ), nil
}
diff --git a/plugin/maxmind/common_out.go b/plugin/maxmind/common_out.go
index 21063faa..4ad8b909 100644
--- a/plugin/maxmind/common_out.go
+++ b/plugin/maxmind/common_out.go
@@ -100,7 +100,7 @@ func (d dbipCountry) HasData() bool {
return d != zeroDBIPCountry
}
-func newGeoLite2CountryMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
+func NewGeoLite2CountryMMDBOutFromBytes(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
var tmp struct {
OutputName string `json:"outputName"`
OutputDir string `json:"outputDir"`
@@ -118,39 +118,19 @@ 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,
+ WithOutputName(tmp.OutputName),
+ WithOutputDir(tmp.OutputDir, iType),
+ WithOutputWantedList(tmp.Want),
+ WithOutputOverwriteList(tmp.Overwrite),
+ WithOutputExcludedList(tmp.Exclude),
+ WithOutputOnlyIPType(tmp.OnlyIPType),
+ WithSourceMMDBURI(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..43a4ff11 100644
--- a/plugin/maxmind/maxmind_asn_csv_in.go
+++ b/plugin/maxmind/maxmind_asn_csv_in.go
@@ -24,14 +24,64 @@ 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) {
+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,
+ }
+
+ for _, opt := range opts {
+ if opt != nil {
+ opt(g)
+ }
+ }
+
+ return g
+}
+
+func WithASNIPv4File(file string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoLite2ASNCSVIn).IPv4File = strings.TrimSpace(file)
+ }
+}
+
+func WithASNIPv6File(file string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoLite2ASNCSVIn).IPv6File = strings.TrimSpace(file)
+ }
+}
+
+func WithASNWantedList(wantList map[string][]string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoLite2ASNCSVIn).Want = wantList
+ }
+}
+
+func WithASNOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoLite2ASNCSVIn).OnlyIPType = onlyIPType
+ }
+}
+
+func NewGeoLite2ASNCSVInFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) {
var tmp struct {
IPv4File string `json:"ipv4"`
IPv6File string `json:"ipv6"`
@@ -85,40 +135,28 @@ func newGeoLite2ASNCSVIn(action lib.Action, data json.RawMessage) (lib.InputConv
wantList[asn] = []string{"AS" + asn}
}
- return &GeoLite2ASNCSVIn{
- Type: TypeGeoLite2ASNCSVIn,
- Action: action,
- Description: DescGeoLite2ASNCSVIn,
- IPv4File: tmp.IPv4File,
- IPv6File: tmp.IPv6File,
- Want: wantList,
- OnlyIPType: tmp.OnlyIPType,
- }, nil
-}
-
-type GeoLite2ASNCSVIn struct {
- Type string
- Action lib.Action
- Description string
- IPv4File string
- IPv6File string
- Want map[string][]string
- OnlyIPType lib.IPType
+ return NewGeoLite2ASNCSVIn(
+ action,
+ WithASNIPv4File(tmp.IPv4File),
+ WithASNIPv6File(tmp.IPv6File),
+ WithASNWantedList(wantList),
+ WithASNOnlyIPType(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 +195,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..6b5ba25f 100644
--- a/plugin/maxmind/maxmind_country_csv_in.go
+++ b/plugin/maxmind/maxmind_country_csv_in.go
@@ -25,14 +25,83 @@ 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 WithCountryCodeFile(file string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ file = strings.TrimSpace(file)
+ if file == "" {
+ file = defaultGeoLite2CountryCodeFile
+ }
+
+ g.(*geoLite2CountryCSVIn).CountryCodeFile = file
+ }
+}
+
+func WithCountryIPv4File(file string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoLite2CountryCSVIn).IPv4File = strings.TrimSpace(file)
+ }
+}
+
+func WithCountryIPv6File(file string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoLite2CountryCSVIn).IPv6File = strings.TrimSpace(file)
+ }
+}
+
+func WithCountryWantedList(lists []string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ wantList := make(map[string]bool)
+ for _, want := range lists {
+ if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
+ wantList[want] = true
+ }
+ }
+
+ g.(*geoLite2CountryCSVIn).Want = wantList
+ }
+}
+
+func WithCountryOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoLite2CountryCSVIn).OnlyIPType = onlyIPType
+ }
+}
+
+func NewGeoLite2CountryCSVInFromBytes(action lib.Action, data []byte) (lib.InputConverter, error) {
var tmp struct {
CountryCodeFile string `json:"country"`
IPv4File string `json:"ipv4"`
@@ -47,10 +116,6 @@ func newGeoLite2CountryCSVIn(action lib.Action, data json.RawMessage) (lib.Input
}
}
- if tmp.CountryCodeFile == "" {
- 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 == "" {
@@ -58,50 +123,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,
+ WithCountryCodeFile(tmp.CountryCodeFile),
+ WithCountryIPv4File(tmp.IPv4File),
+ WithCountryIPv6File(tmp.IPv6File),
+ WithCountryWantedList(tmp.Want),
+ WithCountryOnlyIPType(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 +189,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 +236,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..c3eed5fc 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,60 @@ type GeoLite2CountryMMDBIn struct {
OnlyIPType lib.IPType
}
-func (g *GeoLite2CountryMMDBIn) GetType() string {
+func NewGeoLite2CountryMMDBIn(iType string, iDesc string, action lib.Action, opts ...lib.InputOption) lib.InputConverter {
+ g := &geoLite2CountryMMDBIn{
+ Type: iType,
+ Action: action,
+ Description: iDesc,
+ }
+
+ for _, opt := range opts {
+ if opt != nil {
+ opt(g)
+ }
+ }
+
+ return g
+}
+
+func WithURI(uri string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoLite2CountryMMDBIn).URI = strings.TrimSpace(uri)
+ }
+}
+
+func WithInputWantedList(lists []string) lib.InputOption {
+ return func(g lib.InputConverter) {
+ wantList := make(map[string]bool)
+ for _, want := range lists {
+ if want = strings.ToUpper(strings.TrimSpace(want)); want != "" {
+ wantList[want] = true
+ }
+ }
+
+ g.(*geoLite2CountryMMDBIn).Want = wantList
+ }
+}
+
+func WithInputOnlyIPType(onlyIPType lib.IPType) lib.InputOption {
+ return func(g lib.InputConverter) {
+ g.(*geoLite2CountryMMDBIn).OnlyIPType = onlyIPType
+ }
+}
+
+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 +130,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..e3613932 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,94 @@ type GeoLite2CountryMMDBOut struct {
SourceMMDBURI string
}
-func (g *GeoLite2CountryMMDBOut) GetType() string {
+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 WithOutputName(name string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ name = strings.TrimSpace(name)
+ if name == "" {
+ name = defaultGeoLite2CountryMMDBOutputName
+ }
+
+ g.(*geoLite2CountryMMDBOut).OutputName = name
+ }
+}
+
+func WithOutputDir(dir string, iType string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ dir = strings.TrimSpace(dir)
+ if dir == "" {
+ switch iType {
+ case TypeGeoLite2CountryMMDBOut:
+ dir = defaultMaxmindOutputDir
+ case TypeDBIPCountryMMDBOut:
+ dir = defaultDBIPOutputDir
+ case TypeIPInfoCountryMMDBOut:
+ dir = defaultIPInfoOutputDir
+ }
+ }
+
+ g.(*geoLite2CountryMMDBOut).OutputDir = dir
+ }
+}
+
+func WithOutputWantedList(lists []string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ g.(*geoLite2CountryMMDBOut).Want = lists
+ }
+}
+
+func WithOutputOverwriteList(lists []string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ g.(*geoLite2CountryMMDBOut).Overwrite = lists
+ }
+}
+
+func WithOutputExcludedList(lists []string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ g.(*geoLite2CountryMMDBOut).Exclude = lists
+ }
+}
+
+func WithOutputOnlyIPType(onlyIPType lib.IPType) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ g.(*geoLite2CountryMMDBOut).OnlyIPType = onlyIPType
+ }
+}
+
+func WithSourceMMDBURI(uri string) lib.OutputOption {
+ return func(g lib.OutputConverter) {
+ g.(*geoLite2CountryMMDBOut).SourceMMDBURI = uri
+ }
+}
+
+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 +194,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 +250,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 +446,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
}