diff options
Diffstat (limited to 'plugin/maxmind/common_in.go')
| -rw-r--r-- | plugin/maxmind/common_in.go | 55 |
1 files changed, 38 insertions, 17 deletions
diff --git a/plugin/maxmind/common_in.go b/plugin/maxmind/common_in.go index 3d97388c..1f2db85d 100644 --- a/plugin/maxmind/common_in.go +++ b/plugin/maxmind/common_in.go @@ -2,19 +2,48 @@ package maxmind import ( "encoding/json" + "log" "path/filepath" "strings" "github.com/Loyalsoldier/geoip/lib" ) +func WithMMDBInURI(uri string) lib.InputOption { + return func(c lib.InputConverter) { + uri = strings.TrimSpace(uri) + if uri == "" { + log.Fatalf("❌ [type %s | action %s] missing uri", c.GetType(), c.GetAction()) + } + c.(*GeoLite2CountryMMDBIn).URI = uri + } +} + +func WithMMDBInWantedList(lists []string) lib.InputOption { + return func(c lib.InputConverter) { + wantList := make(map[string]bool) + for _, want := range lists { + if want = strings.ToUpper(strings.TrimSpace(want)); want != "" { + wantList[want] = true + } + } + c.(*GeoLite2CountryMMDBIn).Want = wantList + } +} + +func WithMMDBInOnlyIPType(onlyIPType lib.IPType) lib.InputOption { + return func(c lib.InputConverter) { + c.(*GeoLite2CountryMMDBIn).OnlyIPType = onlyIPType + } +} + var ( defaultGeoLite2CountryMMDBFile = filepath.Join("./", "geolite2", "GeoLite2-Country.mmdb") defaultDBIPCountryMMDBFile = filepath.Join("./", "db-ip", "dbip-country-lite.mmdb") 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 []byte) (lib.InputConverter, error) { var tmp struct { URI string `json:"uri"` Want []string `json:"wantedList"` @@ -40,20 +69,12 @@ 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, + WithMMDBInURI(tmp.URI), + WithMMDBInWantedList(tmp.Want), + WithMMDBInOnlyIPType(tmp.OnlyIPType), + ), nil } |
