Browse Source

sync logic working

afeiszli 3 years ago
parent
commit
56ee112d1b
3 changed files with 11 additions and 22 deletions
  1. 1 1
      netclient/functions/checkin.go
  2. 1 16
      netclient/wireguard/common.go
  3. 9 5
      netclient/wireguard/unix.go

+ 1 - 1
netclient/functions/checkin.go

@@ -234,7 +234,7 @@ func Pull(network string, manual bool) (*models.Node, error) {
 		}
 		}
 	} else {
 	} else {
 		if err = wireguard.SetWGConfig(network, true); err != nil {
 		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)
 				return Pull(network, true)
 			} else {
 			} else {
 				return nil, err
 				return nil, err

+ 1 - 16
netclient/wireguard/common.go

@@ -195,7 +195,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 			}
 			}
 		}
 		}
 		if syncconf {
 		if syncconf {
-			err = wgclient.ConfigureDevice(deviceiface, conf)
+			err = SyncWGQuickConf(ifacename, confPath)
 		} else {
 		} else {
 			d, _ := wgclient.Device(deviceiface)
 			d, _ := wgclient.Device(deviceiface)
 			for d != nil && d.Name == deviceiface {
 			for d != nil && d.Name == deviceiface {
@@ -335,18 +335,3 @@ func ApplyConf(confPath string) error {
 	}
 	}
 	return err
 	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
-}

+ 9 - 5
netclient/wireguard/unix.go

@@ -2,7 +2,9 @@ package wireguard
 
 
 import (
 import (
 	"io/ioutil"
 	"io/ioutil"
+	"log"
 	"os"
 	"os"
+	"regexp"
 
 
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/netclient/config"
 	"github.com/gravitl/netmaker/netclient/config"
@@ -56,19 +58,22 @@ func ApplyWGQuickConf(confPath string) error {
 	return err
 	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"
 	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 {
 	if err != nil {
 		return err
 		return err
 	}
 	}
+	regex := regexp.MustCompile(".*Warning.*\n")
+	conf := regex.ReplaceAllString(confRaw, "")
 	err = ioutil.WriteFile(tmpConf, []byte(conf), 0644)
 	err = ioutil.WriteFile(tmpConf, []byte(conf), 0644)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	_, err = ncutils.RunCmd("wg sync "+confPath, false)
+	_, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true)
 	if err != nil {
 	if err != nil {
+		log.Println(err.Error())
 		ncutils.Log("error syncing conf, resetting")
 		ncutils.Log("error syncing conf, resetting")
 		err = ApplyWGQuickConf(confPath)
 		err = ApplyWGQuickConf(confPath)
 	}
 	}
@@ -76,7 +81,6 @@ func SyncWGQuickConf(confPath string) error {
 	if errN != nil {
 	if errN != nil {
 		ncutils.Log(errN.Error())
 		ncutils.Log(errN.Error())
 	}
 	}
-
 	return err
 	return err
 }
 }