dsnet/down.go
Andre Kelpe e782db30e9 Implements PostUp and PostDown commands using /bin/sh
This introduces PostUp and PostDown in dsnet. PostUp and PostDown allow
the user to run arbitrary commands after the device is up or down. These
are typically used to change the firewall rules via iptables. A working
example would be

...
    "PostUp" : "iptables -A FORWARD -i dsnet -j ACCEPT; iptables -A FORWARD -o dsnet -j ACCEPT; iptables -t nat -A POSTROUTING -o ens2 -j MASQUERADE ",
    "PostDown" : "iptables -D FORWARD -i dsnet -j ACCEPT; iptables -D FORWARD -o dsnet -j ACCEPT; iptables -t nat -D POSTROUTING -o ens2 -j MASQUERADE ",
...

All commands are executed by `/bin/sh` and no filtering or sandboxing is
applied. Users of this should know what they are doing.

Fixes https://github.com/naggie/dsnet/issues/16
2020-11-19 23:21:11 +01:00

30 lines
517 B
Go

package dsnet
import (
"github.com/vishvananda/netlink"
)
func Down() {
conf := MustLoadDsnetConfig()
DelLink(conf)
RunPostDown(conf)
}
func RunPostDown(conf *DsnetConfig) {
ShellOut(conf.PostDown, "PostDown")
}
func DelLink(conf *DsnetConfig) {
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = conf.InterfaceName
link := &netlink.GenericLink{
LinkAttrs: linkAttrs,
}
err := netlink.LinkDel(link)
if err != nil {
ExitFail("Could not delete interface '%s' (%v)", conf.InterfaceName, err)
}
}