Skip to content

Commit

Permalink
Feat: support extended text format with prefix & suffix in line as ou…
Browse files Browse the repository at this point in the history
…tput

Output plaintext CIDR with prefix and suffix in one line by using `text` format as output, specified by args `addPrefixInLine` and `addSuffixInLine`
  • Loading branch information
Loyalsoldier committed Aug 5, 2024
1 parent b5b2429 commit 38d59d4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
24 changes: 24 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -325,6 +342,13 @@
"wantedList": ["cn", "private", "test"],
"onlyIPType": "ipv4"
}
},
{
"type": "surgeRuleSet",
"action": "output",
"args": {
"addSuffixInLine": ",no-resolve"
}
}
]
}
18 changes: 18 additions & 0 deletions plugin/plaintext/common_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ 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) {
var tmp struct {
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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}

Expand Down

0 comments on commit 38d59d4

Please sign in to comment.