|
@@ -183,6 +183,14 @@ func (ipt *IPTables) Delete(table, chain string, rulespec ...string) error {
|
|
return ipt.run(cmd...)
|
|
return ipt.run(cmd...)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (ipt *IPTables) DeleteIfExists(table, chain string, rulespec ...string) error {
|
|
|
|
+ exists, err := ipt.Exists(table, chain, rulespec...)
|
|
|
|
+ if err == nil && exists {
|
|
|
|
+ err = ipt.Delete(table, chain, rulespec...)
|
|
|
|
+ }
|
|
|
|
+ return err
|
|
|
|
+}
|
|
|
|
+
|
|
// List rules in specified table/chain
|
|
// List rules in specified table/chain
|
|
func (ipt *IPTables) List(table, chain string) ([]string, error) {
|
|
func (ipt *IPTables) List(table, chain string) ([]string, error) {
|
|
args := []string{"-t", table, "-S", chain}
|
|
args := []string{"-t", table, "-S", chain}
|
|
@@ -220,6 +228,21 @@ func (ipt *IPTables) ListChains(table string) ([]string, error) {
|
|
return chains, nil
|
|
return chains, nil
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// '-S' is fine with non existing rule index as long as the chain exists
|
|
|
|
+// therefore pass index 1 to reduce overhead for large chains
|
|
|
|
+func (ipt *IPTables) ChainExists(table, chain string) (bool, error) {
|
|
|
|
+ err := ipt.run("-t", table, "-S", chain, "1")
|
|
|
|
+ eerr, eok := err.(*Error)
|
|
|
|
+ switch {
|
|
|
|
+ case err == nil:
|
|
|
|
+ return true, nil
|
|
|
|
+ case eok && eerr.ExitStatus() == 1:
|
|
|
|
+ return false, nil
|
|
|
|
+ default:
|
|
|
|
+ return false, err
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
// Stats lists rules including the byte and packet counts
|
|
// Stats lists rules including the byte and packet counts
|
|
func (ipt *IPTables) Stats(table, chain string) ([][]string, error) {
|
|
func (ipt *IPTables) Stats(table, chain string) ([][]string, error) {
|
|
args := []string{"-t", table, "-L", chain, "-n", "-v", "-x"}
|
|
args := []string{"-t", table, "-L", chain, "-n", "-v", "-x"}
|
|
@@ -399,6 +422,18 @@ func (ipt *IPTables) DeleteChain(table, chain string) error {
|
|
return ipt.run("-t", table, "-X", chain)
|
|
return ipt.run("-t", table, "-X", chain)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (ipt *IPTables) ClearAndDeleteChain(table, chain string) error {
|
|
|
|
+ exists, err := ipt.ChainExists(table, chain)
|
|
|
|
+ if err != nil || !exists {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ err = ipt.run("-t", table, "-F", chain)
|
|
|
|
+ if err == nil {
|
|
|
|
+ err = ipt.run("-t", table, "-X", chain)
|
|
|
|
+ }
|
|
|
|
+ return err
|
|
|
|
+}
|
|
|
|
+
|
|
// ChangePolicy changes policy on chain to target
|
|
// ChangePolicy changes policy on chain to target
|
|
func (ipt *IPTables) ChangePolicy(table, chain, target string) error {
|
|
func (ipt *IPTables) ChangePolicy(table, chain, target string) error {
|
|
return ipt.run("-t", table, "-P", chain, target)
|
|
return ipt.run("-t", table, "-P", chain, target)
|