diff options
| author | Loyalsoldier <[email protected]> | 2024-08-06 04:48:42 +0800 |
|---|---|---|
| committer | Loyalsoldier <[email protected]> | 2024-08-06 04:48:42 +0800 |
| commit | b5b24293ef86fd00d2673b1133b853a511ef9211 (patch) | |
| tree | 5c9019c9e6f840a5d251f3afd53aa44efedbf14f | |
| parent | ee3687e191acbde3aabc13cf4f5dca93a3d80af5 (diff) | |
Feat: support extended text format with prefix & suffix in line as input
IP and CIDR with prefix and suffix in one line now can be processed by using `text` format as input, specified by args `removePrefixesInLine` and `removeSuffixesInLine`.
| -rw-r--r-- | config-example.json | 33 | ||||
| -rw-r--r-- | plugin/plaintext/common_in.go | 22 | ||||
| -rw-r--r-- | plugin/plaintext/text_in.go | 6 |
3 files changed, 57 insertions, 4 deletions
diff --git a/config-example.json b/config-example.json index 253d1746..d4e02631 100644 --- a/config-example.json +++ b/config-example.json @@ -108,6 +108,19 @@ "type": "text", "action": "add", "args": { + "inputDir": "./data", + "onlyIPType": "ipv4", + "removePrefixesInLine": [ + "iptables -A INPUT -s", + "iptables -A INPUT -d" + ], + "removeSuffixesInLine": ["-j ACCEPT", "-j DROP"] + } + }, + { + "type": "text", + "action": "add", + "args": { "name": "mylist", "uri": "./an/example/dir/mycidr.txt", "onlyIPType": "ipv6" @@ -117,6 +130,26 @@ "type": "text", "action": "add", "args": { + "name": "mylist", + "uri": "./an/example/dir/mycidr.txt", + "onlyIPType": "ipv6", + "removePrefixesInLine": ["allow from", "deny from"] + } + }, + { + "type": "text", + "action": "add", + "args": { + "name": "mylist", + "uri": "./an/example/dir/mycidr.txt", + "onlyIPType": "ipv6", + "removeSuffixesInLine": [";", ","] + } + }, + { + "type": "text", + "action": "add", + "args": { "name": "cn", "uri": "https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt", "onlyIPType": "ipv4" diff --git a/plugin/plaintext/common_in.go b/plugin/plaintext/common_in.go index f5be6aa6..cbc64c00 100644 --- a/plugin/plaintext/common_in.go +++ b/plugin/plaintext/common_in.go @@ -17,6 +17,9 @@ type textIn struct { URI string InputDir string OnlyIPType lib.IPType + + RemovePrefixesInLine []string + RemoveSuffixesInLine []string } func (t *textIn) scanFile(reader io.Reader, entry *lib.Entry) error { @@ -40,10 +43,8 @@ func (t *textIn) scanFile(reader io.Reader, entry *lib.Entry) error { func (t *textIn) scanFileForTextIn(reader io.Reader, entry *lib.Entry) error { scanner := bufio.NewScanner(reader) for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - if line == "" { - continue - } + line := scanner.Text() + line, _, _ = strings.Cut(line, "#") line, _, _ = strings.Cut(line, "//") line, _, _ = strings.Cut(line, "/*") @@ -51,6 +52,19 @@ func (t *textIn) scanFileForTextIn(reader io.Reader, entry *lib.Entry) error { if line == "" { continue } + + line = strings.ToLower(line) + for _, prefix := range t.RemovePrefixesInLine { + line = strings.TrimSpace(strings.TrimPrefix(line, strings.ToLower(strings.TrimSpace(prefix)))) + } + for _, suffix := range t.RemoveSuffixesInLine { + line = strings.TrimSpace(strings.TrimSuffix(line, strings.ToLower(strings.TrimSpace(suffix)))) + } + line = strings.TrimSpace(line) + if line == "" { + continue + } + if err := entry.AddPrefix(line); err != nil { return err } diff --git a/plugin/plaintext/text_in.go b/plugin/plaintext/text_in.go index 6ebf15d9..d9aea269 100644 --- a/plugin/plaintext/text_in.go +++ b/plugin/plaintext/text_in.go @@ -32,6 +32,9 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input URI string `json:"uri"` InputDir string `json:"inputDir"` OnlyIPType lib.IPType `json:"onlyIPType"` + + RemovePrefixesInLine []string `json:"removePrefixesInLine"` + RemoveSuffixesInLine []string `json:"removeSuffixesInLine"` } if strings.TrimSpace(iType) == "" { @@ -60,6 +63,9 @@ func newTextIn(iType string, action lib.Action, data json.RawMessage) (lib.Input URI: tmp.URI, InputDir: tmp.InputDir, OnlyIPType: tmp.OnlyIPType, + + RemovePrefixesInLine: tmp.RemovePrefixesInLine, + RemoveSuffixesInLine: tmp.RemoveSuffixesInLine, }, nil } |
