|
@@ -186,6 +186,20 @@ func (ipt *IPTables) Insert(table, chain string, pos int, rulespec ...string) er
|
|
|
return ipt.run(cmd...)
|
|
|
}
|
|
|
|
|
|
+// InsertUnique acts like Insert except that it won't insert a duplicate (no matter the position in the chain)
|
|
|
+func (ipt *IPTables) InsertUnique(table, chain string, pos int, rulespec ...string) error {
|
|
|
+ exists, err := ipt.Exists(table, chain, rulespec...)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ if !exists {
|
|
|
+ return ipt.Insert(table, chain, pos, rulespec...)
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
// Append appends rulespec to specified table/chain
|
|
|
func (ipt *IPTables) Append(table, chain string, rulespec ...string) error {
|
|
|
cmd := append([]string{"-t", table, "-A", chain}, rulespec...)
|