From 38d59d447932375cc11f91b09ffff71498eaf32e Mon Sep 17 00:00:00 2001 From: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Date: Tue, 6 Aug 2024 05:15:45 +0800 Subject: [PATCH] Feat: support extended text format with prefix & suffix in line as output Output plaintext CIDR with prefix and suffix in one line by using `text` format as output, specified by args `addPrefixInLine` and `addSuffixInLine` --- config-example.json | 24 ++++++++++++++++++++++++ plugin/plaintext/common_out.go | 18 ++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/config-example.json b/config-example.json index d4e02631d76..eede923d917 100644 --- a/config-example.json +++ b/config-example.json @@ -276,6 +276,23 @@ "onlyIPType": "ipv6" } }, + { + "type": "text", + "action": "output", + "args": { + "outputDir": "./publish", + "onlyIPType": "ipv6", + "addPrefixInLine": "iptables -A INPUT -d ", + "addSuffixInLine": " -j DROP" + } + }, + { + "type": "text", + "action": "output", + "args": { + "addPrefixInLine": "deny from " + } + }, { "type": "stdout", "action": "output" @@ -325,6 +342,13 @@ "wantedList": ["cn", "private", "test"], "onlyIPType": "ipv4" } + }, + { + "type": "surgeRuleSet", + "action": "output", + "args": { + "addSuffixInLine": ",no-resolve" + } } ] } diff --git a/plugin/plaintext/common_out.go b/plugin/plaintext/common_out.go index ff4ab63bb19..cb38345d871 100644 --- a/plugin/plaintext/common_out.go +++ b/plugin/plaintext/common_out.go @@ -25,6 +25,9 @@ type textOut struct { OutputDir string Want []string OnlyIPType lib.IPType + + AddPrefixInLine string + AddSuffixInLine string } func newTextOut(iType string, action lib.Action, data json.RawMessage) (lib.OutputConverter, error) { @@ -32,6 +35,9 @@ func newTextOut(iType string, action lib.Action, data json.RawMessage) (lib.Outp OutputDir string `json:"outputDir"` Want []string `json:"wantedList"` OnlyIPType lib.IPType `json:"onlyIPType"` + + AddPrefixInLine string `json:"addPrefixInLine"` + AddSuffixInLine string `json:"addSuffixInLine"` } if len(data) > 0 { @@ -60,6 +66,9 @@ func newTextOut(iType string, action lib.Action, data json.RawMessage) (lib.Outp OutputDir: tmp.OutputDir, Want: tmp.Want, OnlyIPType: tmp.OnlyIPType, + + AddPrefixInLine: tmp.AddPrefixInLine, + AddSuffixInLine: tmp.AddSuffixInLine, }, nil } @@ -101,7 +110,13 @@ func (t *textOut) marshalBytes(entry *lib.Entry) ([]byte, error) { func (t *textOut) marshalBytesForTextOut(buf *bytes.Buffer, entryCidr []string) error { for _, cidr := range entryCidr { + if t.AddPrefixInLine != "" { + buf.WriteString(t.AddPrefixInLine) + } buf.WriteString(cidr) + if t.AddSuffixInLine != "" { + buf.WriteString(t.AddSuffixInLine) + } buf.WriteString("\n") } return nil @@ -149,6 +164,9 @@ func (t *textOut) marshalBytesForSurgeRuleSetOut(buf *bytes.Buffer, entryCidr [] buf.WriteString("IP-CIDR6,") } buf.WriteString(cidr) + if t.AddSuffixInLine != "" { + buf.WriteString(t.AddSuffixInLine) + } buf.WriteString("\n") }