use RunCmd for postup/postdown

This commit is contained in:
Matthew R. Kasun
2022-09-09 14:15:10 -04:00
parent 0ade279ca7
commit 7ec8c4be2a
6 changed files with 30 additions and 24 deletions

View File

@@ -659,8 +659,7 @@ func deleteInterface(ifacename string, postdown string) error {
}
_, err = ncutils.RunCmd(ipExec+" link del "+ifacename, false)
if postdown != "" {
runcmds := strings.Split(postdown, "; ")
err = ncutils.RunCmds(runcmds, false)
_, err = ncutils.RunCmd(postdown, false)
}
}
return err

View File

@@ -191,8 +191,7 @@ func removeLocalServer(node *models.Node) error {
logger.Log(1, out)
}
if node.PostDown != "" {
runcmds := strings.Split(node.PostDown, "; ")
_ = ncutils.RunCmds(runcmds, false)
ncutils.RunCmd(node.PostDown, false)
}
}
}

View File

@@ -437,6 +437,10 @@ func Copy(src, dst string) error {
func RunCmds(commands []string, printerr bool) error {
var err error
for _, command := range commands {
//prevent panic
if command == " " {
continue
}
args := strings.Fields(command)
out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
if err != nil && printerr {

View File

@@ -335,7 +335,9 @@ func WriteWgConfig(node *models.Node, privateKey string, peers []wgtypes.PeerCon
// wireguard.Section(section_interface).Key("DNS").SetValue(cfg.Server.CoreDNSAddr)
//}
//need to split postup/postdown because ini lib adds a ` and the ` breaks freebsd
//works fine on others
if node.PostUp != "" {
if node.OS == "freebsd" {
parts := strings.Split(node.PostUp, " ; ")
for i, part := range parts {
if i == 0 {
@@ -343,8 +345,12 @@ func WriteWgConfig(node *models.Node, privateKey string, peers []wgtypes.PeerCon
}
wireguard.Section(section_interface).Key("PostUp").AddShadow(part)
}
} else {
wireguard.Section(section_interface).Key("PostUp").SetValue((node.PostUp))
}
}
if node.PostDown != "" {
if node.OS == "freebsd" {
parts := strings.Split(node.PostDown, " ; ")
for i, part := range parts {
if i == 0 {
@@ -352,6 +358,9 @@ func WriteWgConfig(node *models.Node, privateKey string, peers []wgtypes.PeerCon
}
wireguard.Section(section_interface).Key("PostDown").AddShadow(part)
}
} else {
wireguard.Section(section_interface).Key("PostUp").SetValue((node.PostUp))
}
}
if node.MTU != 0 {
wireguard.Section(section_interface).Key("MTU").SetValue(strconv.FormatInt(int64(node.MTU), 10))

View File

@@ -19,8 +19,7 @@ func WgQuickDownMac(node *models.Node, iface string) error {
return err
}
if node.PostDown != "" {
runcmds := strings.Split(node.PostDown, "; ")
ncutils.RunCmds(runcmds, true)
ncutils.RunCmd(node.PostDown, true)
}
return nil
}
@@ -85,8 +84,7 @@ func WgQuickUpMac(node *models.Node, iface string, confPath string) error {
//next, wg-quick runs monitor_daemon
time.Sleep(time.Second / 2)
if node.PostUp != "" {
runcmds := strings.Split(node.PostUp, "; ")
ncutils.RunCmds(runcmds, true)
ncutils.RunCmd(node.PostUp, true)
}
return err
}

View File

@@ -99,8 +99,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename, confPath string, isConnec
return err
}
if node.PostDown != "" {
runcmds := strings.Split(node.PostDown, "; ")
_ = ncutils.RunCmds(runcmds, false)
ncutils.RunCmd(node.PostDown, false)
}
// set MTU of node interface
if _, err := ncutils.RunCmd(ipExec+" link set mtu "+strconv.Itoa(int(node.MTU))+" up dev "+ifacename, true); err != nil {
@@ -108,8 +107,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename, confPath string, isConnec
return err
}
if node.PostUp != "" {
runcmds := strings.Split(node.PostUp, "; ")
_ = ncutils.RunCmds(runcmds, true)
ncutils.RunCmd(node.PostUp, false)
}
if node.Address6 != "" {
logger.Log(1, "adding address: ", node.Address6)
@@ -139,8 +137,7 @@ func RemoveWithoutWGQuick(ifacename string) error {
nodeconf, err := config.ReadConfig(network)
if nodeconf != nil && err == nil {
if nodeconf.Node.PostDown != "" {
runcmds := strings.Split(nodeconf.Node.PostDown, "; ")
_ = ncutils.RunCmds(runcmds, false)
ncutils.RunCmd(nodeconf.Node.PostDown, false)
}
} else if err != nil {
logger.Log(1, "error retrieving config: ", err.Error())