diff --git a/iptables-helper.service b/iptables-helper.service new file mode 100644 index 0000000..db5cf55 --- /dev/null +++ b/iptables-helper.service @@ -0,0 +1,12 @@ +[Unit] +Description=Iptables Helper +After=network.target + +[Service] +Environment="HOME=/root" +ExecStartPre=iptables-restore /etc/iptables.rule +ExecStart=/opt/iptables-helper/iptables-helper +Restart=on-failure + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/pkg/utils/iptables/handler.go b/pkg/utils/iptables/handler.go index 89c5c96..9939c14 100644 --- a/pkg/utils/iptables/handler.go +++ b/pkg/utils/iptables/handler.go @@ -62,6 +62,9 @@ func AddRule(rule Rule) error { cmd := command.Commander{} _, err = cmd.ExecuteWithResult("sudo iptables " + strings.Join(args, " ")) + if err == nil { + SaveRule() + } return err } @@ -70,5 +73,14 @@ func DelRuleByCmd(cmd string) error { cmd = strings.Split(cmd, "&")[0] commander := command.Commander{} _, err := commander.ExecuteWithResult("sudo iptables -D " + cmd) + if err == nil { + SaveRule() + } return err } + +func SaveRule() { + cmd := "sh -c \"iptables-save | tee /etc/iptables.rule\"" + commander := command.Commander{} + commander.Execute("sudo " + cmd) +}