diff --git a/cmd/delete.go b/cmd/delete.go index b86f0d0..ed7db0c 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -41,7 +41,7 @@ var deleteCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { // Delete rule number - if cmd.Flags().Lookup("by-id").Changed { + if cmd.Flags().Lookup("id").Changed { err := ipt.DeleteForwardById(ruleId) if err != nil { fmt.Println(err) @@ -49,7 +49,7 @@ var deleteCmd = &cobra.Command{ } // Loop over file content and delete rule one-by-one. - if cmd.Flags().Lookup("by-file").Changed { + if cmd.Flags().Lookup("file").Changed { file, _ := cmd.Flags().GetString("by-file") rulesFile, err := rules.NewRuleSetFromFile(file) if err != nil { diff --git a/pkg/iptables/forward.go b/pkg/iptables/forward.go index 5d53803..e136e68 100644 --- a/pkg/iptables/forward.go +++ b/pkg/iptables/forward.go @@ -135,7 +135,6 @@ func ListForward(outputFormat string) (map[int]string, error) { // 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 } @@ -150,21 +149,10 @@ func DeleteForwardById(ruleId int) error { return fmt.Errorf("failed: %v", err) } - // retrieve rule using Id number - // (sudo iptables -t nat -L PREROUTING -n --line-numbers) - rule, err := ipt.ListById(fwdTable, fwdChain, ruleId) - if err != nil { - return fmt.Errorf("unable to retrieve rule with ID: %d", ruleId) - } - - // cleaning rule (removing "-A PREROUTING", "-c 0 0", ...) - ruleSplit := strings.Split(rule, " ") - ruleSplit = append(ruleSplit[2:10], ruleSplit[13:]...) - // delete rule - err = ipt.Delete(fwdTable, fwdChain, ruleSplit...) + err = ipt.Delete(fwdTable, fwdChain, strconv.Itoa(ruleId)) if err != nil { - return fmt.Errorf("failed deleting rule #%d\n err: %v", ruleId, err) + return fmt.Errorf("failed deleting rule n. %d\nerr: %v", ruleId, err) } return nil }