diff --git a/pkg/iptables/forward.go b/pkg/iptables/forward.go index 99a4044..5d53803 100644 --- a/pkg/iptables/forward.go +++ b/pkg/iptables/forward.go @@ -6,6 +6,10 @@ import ( "strings" ) +var ( + label string = "fwdctl" +) + func validateIface(iface string) error { if iface == "" { return fmt.Errorf("inteface name is empty") @@ -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) @@ -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) @@ -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 { diff --git a/pkg/iptables/rule.go b/pkg/iptables/rule.go index f0e7a79..70001c6 100644 --- a/pkg/iptables/rule.go +++ b/pkg/iptables/rule.go @@ -6,4 +6,4 @@ type Rule struct { Dport int Saddr string Sport int -} \ No newline at end of file +} diff --git a/pkg/printer/json.go b/pkg/printer/json.go index 7a72c43..4cf50ca 100644 --- a/pkg/printer/json.go +++ b/pkg/printer/json.go @@ -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) diff --git a/pkg/printer/printer_interface.go b/pkg/printer/printer_interface.go index 3692378..6e68b1c 100644 --- a/pkg/printer/printer_interface.go +++ b/pkg/printer/printer_interface.go @@ -6,7 +6,7 @@ import ( ) type Printer interface { - PrintResult(ruleList []string) error + PrintResult(ruleList map[int]string) error } func NewPrinter(printFormat string) Printer { diff --git a/pkg/printer/table.go b/pkg/printer/table.go index 38311dd..39c54ef 100644 --- a/pkg/printer/table.go +++ b/pkg/printer/table.go @@ -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) diff --git a/pkg/printer/yaml.go b/pkg/printer/yaml.go index 1845956..cc6c09c 100644 --- a/pkg/printer/yaml.go +++ b/pkg/printer/yaml.go @@ -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)