Skip to content

Commit

Permalink
fix: update delete command to iptables-nft command
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 189d066 commit f510350
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
6 changes: 3 additions & 3 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -41,15 +41,15 @@ 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)
}
}

// 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 {
Expand Down
16 changes: 2 additions & 14 deletions pkg/iptables/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit f510350

Please sign in to comment.