Skip to content

Commit

Permalink
feat: add lable tag to identify custom rules
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio Greggi <[email protected]>
  • Loading branch information
alegrey91 committed May 27, 2023
1 parent a787757 commit 189d066
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
19 changes: 17 additions & 2 deletions pkg/iptables/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"strings"
)

var (
label string = "fwdctl"
)

func validateIface(iface string) error {
if iface == "" {
return fmt.Errorf("inteface name is empty")
Expand Down Expand Up @@ -81,6 +85,8 @@ func CreateForward(iface string, proto string, dport int, saddr string, sport in
"--dport", strconv.Itoa(dport),
"-j", fwdTarget,
"--to-destination", saddr + ":" + strconv.Itoa(sport),
"-m", "comment",
"--comment", label,
}

_, err = ValidateForward(iface, proto, dport, saddr, sport)
Expand Down Expand Up @@ -114,7 +120,7 @@ func CreateForward(iface string, proto string, dport int, saddr string, sport in
return nil
}

func ListForward(outputFormat string) ([]string, error) {
func ListForward(outputFormat string) (map[int]string, error) {
ipt, err := getIPTablesInstance()
if err != nil {
return nil, fmt.Errorf("failed: %v", err)
Expand All @@ -126,7 +132,16 @@ func ListForward(outputFormat string) ([]string, error) {
return nil, fmt.Errorf("failed: %v", err)
}

return ruleList, nil
// check listed rules are tagged with custom tag
fwdRules := make(map[int]string)
for ruleId, rule := range ruleList {
fmt.Println(rule)
if strings.Contains(rule, label) {
fwdRules[ruleId] = rule
}
}

return fwdRules, nil
}

func DeleteForwardById(ruleId int) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/iptables/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ type Rule struct {
Dport int
Saddr string
Sport int
}
}
2 changes: 1 addition & 1 deletion pkg/printer/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewJson() *Json {
return &Json{}
}

func (j *Json) PrintResult(ruleList []string) error {
func (j *Json) PrintResult(ruleList map[int]string) error {
rules := rules.NewRuleSet()
for _, rule := range ruleList {
jsonRule, err := extractRuleInfo(rule)
Expand Down
2 changes: 1 addition & 1 deletion pkg/printer/printer_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type Printer interface {
PrintResult(ruleList []string) error
PrintResult(ruleList map[int]string) error
}

func NewPrinter(printFormat string) Printer {
Expand Down
4 changes: 2 additions & 2 deletions pkg/printer/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func NewTable() *Table {
return &Table{}
}

func (t *Table) PrintResult(ruleList []string) error {
table := tablewriter.NewWriter(os.Stdout)
func (t *Table) PrintResult(ruleList map[int]string) error {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"number", "interface", "protocol", "external port", "internal ip", "internal port"})
for ruleId, rule := range ruleList {
tabRule, err := extractRuleInfo(rule)
Expand Down
2 changes: 1 addition & 1 deletion pkg/printer/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewYaml() *Yaml {
return &Yaml{}
}

func (y *Yaml) PrintResult(ruleList []string) error {
func (y *Yaml) PrintResult(ruleList map[int]string) error {
rules := rules.NewRuleSet()
for _, rule := range ruleList {
jsonRule, err := extractRuleInfo(rule)
Expand Down

0 comments on commit 189d066

Please sign in to comment.