diff options
Diffstat (limited to 'plugin/maxmind/maxmind_country_mmdb_out.go')
| -rw-r--r-- | plugin/maxmind/maxmind_country_mmdb_out.go | 95 |
1 files changed, 85 insertions, 10 deletions
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 } |
