From 7ec8c4be2a16b88a7b4f326d5e5559d13af58196 Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Fri, 9 Sep 2022 14:15:10 -0400 Subject: [PATCH 1/2] use RunCmd for postup/postdown --- logic/networks.go | 3 +-- logic/wireguard.go | 3 +-- netclient/ncutils/netclientutils.go | 4 ++++ netclient/wireguard/common.go | 29 +++++++++++++++++++---------- netclient/wireguard/mac.go | 6 ++---- netclient/wireguard/noquick.go | 9 +++------ 6 files changed, 30 insertions(+), 24 deletions(-) diff --git a/logic/networks.go b/logic/networks.go index 3802b9a3..18cd0fd1 100644 --- a/logic/networks.go +++ b/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 diff --git a/logic/wireguard.go b/logic/wireguard.go index 5078b7be..e85aee61 100644 --- a/logic/wireguard.go +++ b/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) } } } diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index 78ff8d49..fb64cb0f 100644 --- a/netclient/ncutils/netclientutils.go +++ b/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 { diff --git a/netclient/wireguard/common.go b/netclient/wireguard/common.go index 2bd9b39a..4175c790 100644 --- a/netclient/wireguard/common.go +++ b/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 { diff --git a/netclient/wireguard/mac.go b/netclient/wireguard/mac.go index 11b20409..4c9f08ba 100644 --- a/netclient/wireguard/mac.go +++ b/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 } diff --git a/netclient/wireguard/noquick.go b/netclient/wireguard/noquick.go index 9ffb0661..bc439196 100644 --- a/netclient/wireguard/noquick.go +++ b/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()) From 0ba3f6feaf5d934ecaea180f764a792eda5fcab5 Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Mon, 12 Sep 2022 06:42:01 -0400 Subject: [PATCH 2/2] address code review comment --- netclient/functions/daemon.go | 4 ++-- netclient/ncutils/netclientutils.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/netclient/functions/daemon.go b/netclient/functions/daemon.go index c00a91ba..d29c4fdf 100644 --- a/netclient/functions/daemon.go +++ b/netclient/functions/daemon.go @@ -87,8 +87,6 @@ func Daemon() error { func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc { ctx, cancel := context.WithCancel(context.Background()) - wg.Add(1) - go Checkin(ctx, wg) serverSet := make(map[string]bool) networks, _ := ncutils.GetSystemNetworks() for _, network := range networks { @@ -116,6 +114,8 @@ func startGoRoutines(wg *sync.WaitGroup) context.CancelFunc { go messageQueue(ctx, wg, &cfg) } } + wg.Add(1) + go Checkin(ctx, wg) return cancel } diff --git a/netclient/ncutils/netclientutils.go b/netclient/ncutils/netclientutils.go index fb64cb0f..6b1192e7 100644 --- a/netclient/ncutils/netclientutils.go +++ b/netclient/ncutils/netclientutils.go @@ -438,7 +438,7 @@ func RunCmds(commands []string, printerr bool) error { var err error for _, command := range commands { //prevent panic - if command == " " { + if len(strings.Trim(command, " ")) == 0 { continue } args := strings.Fields(command)