summaryrefslogtreecommitdiff
path: root/plugin/maxmind/common_in.go
diff options
context:
space:
mode:
authorLoyalsoldier <[email protected]>2024-11-03 09:14:50 +0800
committerLoyalsoldier <[email protected]>2024-11-03 09:37:26 +0800
commitb7daf132abc33011bedcbf55b0a47fec7187c641 (patch)
tree5232779535ac8f118cb44910ee4419164b992ed3 /plugin/maxmind/common_in.go
parent2e70d7f5e301c5cda8bdb5658a027e037f27f445 (diff)
Feat: support DB-IP country mmdb format as input & output
Download DB-IP free country lite mmdb file here: https://db-ip.com/db/download/ip-to-country-lite
Diffstat (limited to 'plugin/maxmind/common_in.go')
-rw-r--r--plugin/maxmind/common_in.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/plugin/maxmind/common_in.go b/plugin/maxmind/common_in.go
new file mode 100644
index 00000000..e2a95d70
--- /dev/null
+++ b/plugin/maxmind/common_in.go
@@ -0,0 +1,55 @@
+package maxmind
+
+import (
+ "encoding/json"
+ "path/filepath"
+ "strings"
+
+ "github.com/Loyalsoldier/geoip/lib"
+)
+
+var (
+ defaultGeoLite2MMDBFile = filepath.Join("./", "geolite2", "GeoLite2-Country.mmdb")
+ defaultDBIPCountryMMDBFile = filepath.Join("./", "db-ip", "dbip-country-lite.mmdb")
+)
+
+func newMMDBIn(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"`
+ }
+
+ if len(data) > 0 {
+ if err := json.Unmarshal(data, &tmp); err != nil {
+ return nil, err
+ }
+ }
+
+ if tmp.URI == "" {
+ switch iType {
+ case TypeMaxmindMMDBIn:
+ tmp.URI = defaultGeoLite2MMDBFile
+
+ case TypeDBIPCountryMMDBIn:
+ tmp.URI = defaultDBIPCountryMMDBFile
+ }
+ }
+
+ // 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 &MMDBIn{
+ Type: iType,
+ Action: action,
+ Description: iDesc,
+ URI: tmp.URI,
+ Want: wantList,
+ OnlyIPType: tmp.OnlyIPType,
+ }, nil
+}