Skip to content

Commit

Permalink
Feat: support extended text format with prefix & suffix in line as input
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Loyalsoldier committed Aug 5, 2024
1 parent ee3687e commit b5b2429
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
33 changes: 33 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@
"onlyIPType": "ipv4"
}
},
{
"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",
Expand All @@ -113,6 +126,26 @@
"onlyIPType": "ipv6"
}
},
{
"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",
Expand Down
22 changes: 18 additions & 4 deletions plugin/plaintext/common_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -40,17 +43,28 @@ 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, "/*")
line = strings.TrimSpace(line)
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
}
Expand Down
6 changes: 6 additions & 0 deletions plugin/plaintext/text_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "" {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit b5b2429

Please sign in to comment.