summaryrefslogtreecommitdiff
path: root/plugin/plaintext
diff options
context:
space:
mode:
authorLoyalsoldier <[email protected]>2024-08-10 08:48:56 +0800
committerLoyalsoldier <[email protected]>2024-08-10 09:03:41 +0800
commit55d12b35087ba35e9bd6411342702da22542ec82 (patch)
tree110abef0299984c812388f49c1b89235b7153f2d /plugin/plaintext
parent4f49a2a7e90a722531ef8eb9ecfee418747fab40 (diff)
Feat: support JSON data as input format
For the arg `jsonPath` in config, see syntax explanation here: https://github.com/tidwall/gjson/blob/master/SYNTAX.md
Diffstat (limited to 'plugin/plaintext')
-rw-r--r--plugin/plaintext/common_in.go31
-rw-r--r--plugin/plaintext/json_in.go22
-rw-r--r--plugin/plaintext/text_in.go6
3 files changed, 59 insertions, 0 deletions
diff --git a/plugin/plaintext/common_in.go b/plugin/plaintext/common_in.go
index cbc64c00..4c8d48c6 100644
--- a/plugin/plaintext/common_in.go
+++ b/plugin/plaintext/common_in.go
@@ -2,10 +2,12 @@ package plaintext
import (
"bufio"
+ "fmt"
"io"
"strings"
"github.com/Loyalsoldier/geoip/lib"
+ "github.com/tidwall/gjson"
"gopkg.in/yaml.v2"
)
@@ -18,6 +20,7 @@ type textIn struct {
InputDir string
OnlyIPType lib.IPType
+ JSONPath []string
RemovePrefixesInLine []string
RemoveSuffixesInLine []string
}
@@ -27,6 +30,8 @@ func (t *textIn) scanFile(reader io.Reader, entry *lib.Entry) error {
switch t.Type {
case typeTextIn:
err = t.scanFileForTextIn(reader, entry)
+ case typeJSONIn:
+ err = t.scanFileForJSONIn(reader, entry)
case typeClashRuleSetClassicalIn:
err = t.scanFileForClashClassicalRuleSetIn(reader, entry)
case typeClashRuleSetIPCIDRIn:
@@ -185,3 +190,29 @@ func (t *textIn) scanFileForSurgeRuleSetIn(reader io.Reader, entry *lib.Entry) e
return nil
}
+
+func (t *textIn) scanFileForJSONIn(reader io.Reader, entry *lib.Entry) error {
+ data, err := io.ReadAll(reader)
+ if err != nil {
+ return err
+ }
+
+ if !gjson.ValidBytes(data) {
+ return fmt.Errorf("invalid JSON data")
+ }
+
+ // JSON Path syntax:
+ // https://github.com/tidwall/gjson/blob/master/SYNTAX.md
+ for _, path := range t.JSONPath {
+ path = strings.TrimSpace(path)
+
+ result := gjson.GetBytes(data, path)
+ for _, cidr := range result.Array() {
+ if err := entry.AddPrefix(cidr.String()); err != nil {
+ return err
+ }
+ }
+ }
+
+ return nil
+}
diff --git a/plugin/plaintext/json_in.go b/plugin/plaintext/json_in.go
new file mode 100644
index 00000000..784eba09
--- /dev/null
+++ b/plugin/plaintext/json_in.go
@@ -0,0 +1,22 @@
+package plaintext
+
+import (
+ "encoding/json"
+
+ "github.com/Loyalsoldier/geoip/lib"
+)
+
+const (
+ typeJSONIn = "json"
+ descJSONIn = "Convert JSON data to other formats"
+)
+
+func init() {
+ lib.RegisterInputConfigCreator(typeJSONIn, func(action lib.Action, data json.RawMessage) (lib.InputConverter, error) {
+ return newTextIn(typeJSONIn, action, data)
+ })
+
+ lib.RegisterInputConverter(typeJSONIn, &textIn{
+ Description: descJSONIn,
+ })
+}
diff --git a/plugin/plaintext/text_in.go b/plugin/plaintext/text_in.go
index d9aea269..4f2c2c71 100644
--- a/plugin/plaintext/text_in.go
+++ b/plugin/plaintext/text_in.go
@@ -33,6 +33,7 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input
InputDir string `json:"inputDir"`
OnlyIPType lib.IPType `json:"onlyIPType"`
+ JSONPath []string `json:"jsonPath"`
RemovePrefixesInLine []string `json:"removePrefixesInLine"`
RemoveSuffixesInLine []string `json:"removeSuffixesInLine"`
}
@@ -55,6 +56,10 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input
return nil, fmt.Errorf("type %s | action %s name & uri must be specified together", typeTextIn, action)
}
+ if iType == typeJSONIn && len(tmp.JSONPath) == 0 {
+ return nil, fmt.Errorf("type %s | action %s missing jsonPath", typeJSONIn, action)
+ }
+
return &textIn{
Type: iType,
Action: action,
@@ -64,6 +69,7 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input
InputDir: tmp.InputDir,
OnlyIPType: tmp.OnlyIPType,
+ JSONPath: tmp.JSONPath,
RemovePrefixesInLine: tmp.RemovePrefixesInLine,
RemoveSuffixesInLine: tmp.RemoveSuffixesInLine,
}, nil