From 56ee112d1bea1d5f2bdb883bf52bf5918a440fef Mon Sep 17 00:00:00 2001 From: afeiszli Date: Fri, 12 Nov 2021 13:46:55 -0500 Subject: [PATCH] sync logic working --- netclient/functions/checkin.go | 2 +- netclient/wireguard/common.go | 17 +---------------- netclient/wireguard/unix.go | 14 +++++++++----- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/netclient/functions/checkin.go b/netclient/functions/checkin.go index bcc720ac..3bd11859 100644 --- a/netclient/functions/checkin.go +++ b/netclient/functions/checkin.go @@ -234,7 +234,7 @@ func Pull(network string, manual bool) (*models.Node, error) { } } else { if err = wireguard.SetWGConfig(network, true); err != nil { - if errors.Is(err, os.ErrNotExist) { + if errors.Is(err, os.ErrNotExist) && !ncutils.IsFreeBSD() { return Pull(network, true) } else { return nil, err diff --git a/netclient/wireguard/common.go b/netclient/wireguard/common.go index bffe9bb3..168330cc 100644 --- a/netclient/wireguard/common.go +++ b/netclient/wireguard/common.go @@ -195,7 +195,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig } } if syncconf { - err = wgclient.ConfigureDevice(deviceiface, conf) + err = SyncWGQuickConf(ifacename, confPath) } else { d, _ := wgclient.Device(deviceiface) for d != nil && d.Name == deviceiface { @@ -335,18 +335,3 @@ func ApplyConf(confPath string) error { } return err } - -// ApplyConf - applys a conf on disk to WireGuard interface -func SyncConf(confPath string) error { - os := runtime.GOOS - var err error - switch os { - /* - case "windows": - _ = SyncWindowsConf(confPath) - */ - default: - err = SyncWGQuickConf(confPath) - } - return err -} diff --git a/netclient/wireguard/unix.go b/netclient/wireguard/unix.go index 82de0ac0..8b557d0a 100644 --- a/netclient/wireguard/unix.go +++ b/netclient/wireguard/unix.go @@ -2,7 +2,9 @@ package wireguard import ( "io/ioutil" + "log" "os" + "regexp" "github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/netclient/config" @@ -56,19 +58,22 @@ func ApplyWGQuickConf(confPath string) error { return err } -// ApplyWGQuickConf - applies wg-quick commands if os supports -func SyncWGQuickConf(confPath string) error { +// SyncWGQuickConf - formats config file and runs sync command +func SyncWGQuickConf(iface string, confPath string) error { var tmpConf = confPath + ".sync.tmp" - conf, err := ncutils.RunCmd("wg-quick strip "+confPath, false) + confRaw, err := ncutils.RunCmd("wg-quick strip "+confPath, false) if err != nil { return err } + regex := regexp.MustCompile(".*Warning.*\n") + conf := regex.ReplaceAllString(confRaw, "") err = ioutil.WriteFile(tmpConf, []byte(conf), 0644) if err != nil { return err } - _, err = ncutils.RunCmd("wg sync "+confPath, false) + _, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true) if err != nil { + log.Println(err.Error()) ncutils.Log("error syncing conf, resetting") err = ApplyWGQuickConf(confPath) } @@ -76,7 +81,6 @@ func SyncWGQuickConf(confPath string) error { if errN != nil { ncutils.Log(errN.Error()) } - return err }