From 7a8eea2aef9fb90071044c5ce108f499bfd74065 Mon Sep 17 00:00:00 2001 From: water Date: Tue, 11 Sep 2018 10:52:46 +0800 Subject: [PATCH 1/3] Add flush table method --- iptables/iptables.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/iptables/iptables.go b/iptables/iptables.go index 8db2597..b7c4e7d 100644 --- a/iptables/iptables.go +++ b/iptables/iptables.go @@ -324,6 +324,12 @@ func (ipt *IPTables) ClearChain(table, chain string) error { } } +// flush table + +func (ipt* IPTables) FlushTable(table string) error{ + return ipt.run("-t", table, "--flush") +} + // RenameChain renames the old chain to the new one. func (ipt *IPTables) RenameChain(table, oldChain, newChain string) error { return ipt.run("-t", table, "-E", oldChain, newChain) From 3fa05b2e9c9c6116b16bdfd3553e23d50e1578de Mon Sep 17 00:00:00 2001 From: water Date: Tue, 11 Sep 2018 11:13:23 +0800 Subject: [PATCH 2/3] add flush chain --- iptables/iptables.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/iptables/iptables.go b/iptables/iptables.go index b7c4e7d..612cd99 100644 --- a/iptables/iptables.go +++ b/iptables/iptables.go @@ -324,12 +324,16 @@ func (ipt *IPTables) ClearChain(table, chain string) error { } } -// flush table - +// Flush table func (ipt* IPTables) FlushTable(table string) error{ return ipt.run("-t", table, "--flush") } +// Flush chain +func (ipt* IPTables) FlushChain(table string, chain string) error{ + return ipt.run("-t", table, "--flush", chain) +} + // RenameChain renames the old chain to the new one. func (ipt *IPTables) RenameChain(table, oldChain, newChain string) error { return ipt.run("-t", table, "-E", oldChain, newChain) From 5ade8e0b812f675802921f6665cf305a10a04690 Mon Sep 17 00:00:00 2001 From: water Date: Wed, 19 Sep 2018 10:59:50 +0800 Subject: [PATCH 3/3] gofmt and add test case for FlushChain method --- iptables/iptables.go | 9 ++------- iptables/iptables_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/iptables/iptables.go b/iptables/iptables.go index 612cd99..a3a0992 100644 --- a/iptables/iptables.go +++ b/iptables/iptables.go @@ -324,13 +324,8 @@ func (ipt *IPTables) ClearChain(table, chain string) error { } } -// Flush table -func (ipt* IPTables) FlushTable(table string) error{ - return ipt.run("-t", table, "--flush") -} - -// Flush chain -func (ipt* IPTables) FlushChain(table string, chain string) error{ +// Flush table chain +func (ipt *IPTables) FlushChain(table string, chain string) error { return ipt.run("-t", table, "--flush", chain) } diff --git a/iptables/iptables_test.go b/iptables/iptables_test.go index dcd996c..0a207be 100644 --- a/iptables/iptables_test.go +++ b/iptables/iptables_test.go @@ -152,6 +152,18 @@ func runChainTests(t *testing.T, ipt *IPTables) { t.Fatal("DeleteChain of non-empty chain returned IsNotExist") } + // lets re-put a simple rule in again + err = ipt.Append("filter", chain, "-s", "0/0", "-j", "ACCEPT") + if err != nil { + t.Fatalf("Append failed: %v", err) + } + + // lets flush the chain + err = ipt.FlushChain("filter", chain) + if err != nil { + t.Fatalf("Flush table chain failed: %v", err) + } + err = ipt.ClearChain("filter", chain) if err != nil { t.Fatalf("ClearChain (of non-empty) failed: %v", err)