summaryrefslogtreecommitdiff
path: root/plugin/maxmind/common_out.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_out.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_out.go')
-rw-r--r--plugin/maxmind/common_out.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/plugin/maxmind/common_out.go b/plugin/maxmind/common_out.go
new file mode 100644
index 00000000..9874f58a
--- /dev/null
+++ b/plugin/maxmind/common_out.go
@@ -0,0 +1,57 @@
+package maxmind
+
+import (
+ "encoding/json"
+ "path/filepath"
+
+ "github.com/Loyalsoldier/geoip/lib"
+)
+
+var (
+ defaultOutputName = "Country.mmdb"
+ defaultMaxmindOutputDir = filepath.Join("./", "output", "maxmind")
+ defaultDBIPOutputDir = filepath.Join("./", "output", "db-ip")
+)
+
+func newMMDBOut(iType string, iDesc string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) {
+ var tmp struct {
+ OutputName string `json:"outputName"`
+ OutputDir string `json:"outputDir"`
+ Want []string `json:"wantedList"`
+ Overwrite []string `json:"overwriteList"`
+ Exclude []string `json:"excludedList"`
+ OnlyIPType lib.IPType `json:"onlyIPType"`
+ }
+
+ if len(data) > 0 {
+ if err := json.Unmarshal(data, &tmp); err != nil {
+ return nil, err
+ }
+ }
+
+ if tmp.OutputName == "" {
+ tmp.OutputName = defaultOutputName
+ }
+
+ if tmp.OutputDir == "" {
+ switch iType {
+ case TypeMaxmindMMDBOut:
+ tmp.OutputDir = defaultMaxmindOutputDir
+
+ case TypeDBIPCountryMMDBOut:
+ tmp.OutputDir = defaultDBIPOutputDir
+ }
+ }
+
+ return &MMDBOut{
+ Type: iType,
+ Action: action,
+ Description: iDesc,
+ OutputName: tmp.OutputName,
+ OutputDir: tmp.OutputDir,
+ Want: tmp.Want,
+ Overwrite: tmp.Overwrite,
+ Exclude: tmp.Exclude,
+ OnlyIPType: tmp.OnlyIPType,
+ }, nil
+}