Browse Source

use RunCmd for postup/postdown

Matthew R. Kasun 2 years ago
parent
commit
7ec8c4be2a

+ 1 - 2
logic/networks.go

@@ -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

+ 1 - 2
logic/wireguard.go

@@ -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)
 			}
 		}
 	}

+ 4 - 0
netclient/ncutils/netclientutils.go

@@ -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 {

+ 19 - 10
netclient/wireguard/common.go

@@ -335,22 +335,31 @@ 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 != "" {
-		parts := strings.Split(node.PostUp, " ; ")
-		for i, part := range parts {
-			if i == 0 {
-				wireguard.Section(section_interface).Key("PostUp").SetValue(part)
+		if node.OS == "freebsd" {
+			parts := strings.Split(node.PostUp, " ; ")
+			for i, part := range parts {
+				if i == 0 {
+					wireguard.Section(section_interface).Key("PostUp").SetValue(part)
+				}
+				wireguard.Section(section_interface).Key("PostUp").AddShadow(part)
 			}
-			wireguard.Section(section_interface).Key("PostUp").AddShadow(part)
+		} else {
+			wireguard.Section(section_interface).Key("PostUp").SetValue((node.PostUp))
 		}
 	}
 	if node.PostDown != "" {
-		parts := strings.Split(node.PostDown, " ; ")
-		for i, part := range parts {
-			if i == 0 {
-				wireguard.Section(section_interface).Key("PostDown").SetValue(part)
+		if node.OS == "freebsd" {
+			parts := strings.Split(node.PostDown, " ; ")
+			for i, part := range parts {
+				if i == 0 {
+					wireguard.Section(section_interface).Key("PostDown").SetValue(part)
+				}
+				wireguard.Section(section_interface).Key("PostDown").AddShadow(part)
 			}
-			wireguard.Section(section_interface).Key("PostDown").AddShadow(part)
+		} else {
+			wireguard.Section(section_interface).Key("PostUp").SetValue((node.PostUp))
 		}
 	}
 	if node.MTU != 0 {

+ 2 - 4
netclient/wireguard/mac.go

@@ -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
 }

+ 3 - 6
netclient/wireguard/noquick.go

@@ -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())