Browse Source

Merge pull request #1033 from gravitl/bugfix_v0.13.0_cidr_parsing

Bugfix v0.13.0 cidr parsing
Alex Feiszli 3 years ago
parent
commit
baeae557ce
3 changed files with 19 additions and 37 deletions
  1. 1 2
      netclient/functions/register.go
  2. 18 8
      netclient/wireguard/common.go
  3. 0 27
      netclient/wireguard/windows.go

+ 1 - 2
netclient/functions/register.go

@@ -5,7 +5,6 @@ import (
 	"crypto/rand"
 	"crypto/rand"
 	"encoding/json"
 	"encoding/json"
 	"errors"
 	"errors"
-	"log"
 	"net/http"
 	"net/http"
 	"os"
 	"os"
 
 
@@ -56,7 +55,7 @@ func RegisterWithServer(private *ed25519.PrivateKey, cfg *config.ClientConfig) e
 		CommonName: tls.NewCName(cfg.Node.Name),
 		CommonName: tls.NewCName(cfg.Node.Name),
 	}
 	}
 	url := "https://" + cfg.Server.API + "/api/server/register"
 	url := "https://" + cfg.Server.API + "/api/server/register"
-	log.Println("register at ", url)
+	logger.Log(1, "register at "+url)
 
 
 	token, err := Authenticate(cfg)
 	token, err := Authenticate(cfg)
 	if err != nil {
 	if err != nil {

+ 18 - 8
netclient/wireguard/common.go

@@ -147,9 +147,10 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 	// spin up userspace / windows interface + apply the conf file
 	// spin up userspace / windows interface + apply the conf file
 	confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
 	confPath := ncutils.GetNetclientPathSpecific() + ifacename + ".conf"
 	var deviceiface = ifacename
 	var deviceiface = ifacename
+	var mErr error
 	if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
 	if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
-		deviceiface, err = local.GetMacIface(node.PrimaryAddress())
-		if err != nil || deviceiface == "" {
+		deviceiface, mErr = local.GetMacIface(node.PrimaryAddress())
+		if mErr != nil || deviceiface == "" {
 			deviceiface = ifacename
 			deviceiface = ifacename
 		}
 		}
 	}
 	}
@@ -162,8 +163,8 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 	ifaceReady := strings.Contains(output, deviceiface)
 	ifaceReady := strings.Contains(output, deviceiface)
 	for !ifaceReady && !(time.Now().After(starttime.Add(time.Second << 4))) {
 	for !ifaceReady && !(time.Now().After(starttime.Add(time.Second << 4))) {
 		if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
 		if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
-			deviceiface, err = local.GetMacIface(node.PrimaryAddress())
-			if err != nil || deviceiface == "" {
+			deviceiface, mErr = local.GetMacIface(node.PrimaryAddress())
+			if mErr != nil || deviceiface == "" {
 				deviceiface = ifacename
 				deviceiface = ifacename
 			}
 			}
 		}
 		}
@@ -194,6 +195,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 		if err != nil {
 		if err != nil {
 			logger.Log(1, "error setting peers: ", err.Error())
 			logger.Log(1, "error setting peers: ", err.Error())
 		}
 		}
+
 		time.Sleep(time.Second)
 		time.Sleep(time.Second)
 	}
 	}
 
 
@@ -215,9 +217,9 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 		} else {
 		} else {
 			logger.Log(1, "could not set cidr route properly: ", cidrErr.Error())
 			logger.Log(1, "could not set cidr route properly: ", cidrErr.Error())
 		}
 		}
+
 		local.SetCurrentPeerRoutes(ifacename, node.Address6, peers)
 		local.SetCurrentPeerRoutes(ifacename, node.Address6, peers)
 	}
 	}
-
 	return err
 	return err
 }
 }
 
 
@@ -298,9 +300,17 @@ func ApplyConf(node *models.Node, ifacename string, confPath string) error {
 	var nodeCfg config.ClientConfig
 	var nodeCfg config.ClientConfig
 	nodeCfg.Network = node.Network
 	nodeCfg.Network = node.Network
 	nodeCfg.ReadConfig()
 	nodeCfg.ReadConfig()
-	ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange)
-	if err == nil {
-		local.SetCIDRRoute(node.Interface, ip.String(), cidr)
+	if nodeCfg.NetworkSettings.AddressRange != "" {
+		ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange)
+		if err == nil {
+			local.SetCIDRRoute(node.Interface, ip.String(), cidr)
+		}
+	}
+	if nodeCfg.NetworkSettings.AddressRange6 != "" {
+		ip, cidr, err := net.ParseCIDR(nodeCfg.NetworkSettings.AddressRange6)
+		if err == nil {
+			local.SetCIDRRoute(node.Interface, ip.String(), cidr)
+		}
 	}
 	}
 
 
 	return err
 	return err

+ 0 - 27
netclient/wireguard/windows.go

@@ -9,16 +9,6 @@ import (
 
 
 // ApplyWindowsConf - applies the WireGuard configuration file on Windows
 // ApplyWindowsConf - applies the WireGuard configuration file on Windows
 func ApplyWindowsConf(confPath string) error {
 func ApplyWindowsConf(confPath string) error {
-	/*
-		pathStrings := strings.Split(confPath, ncutils.GetWGPathSpecific())
-		if len(pathStrings) == 2 {
-			copyConfPath := fmt.Sprintf("%s\\%s", ncutils.WINDOWS_WG_DPAPI_PATH, pathStrings[1])
-			err := ncutils.Copy(confPath, copyConfPath)
-			if err != nil {
-				logger.Log(err.Error(), 1)
-			}
-		}
-	*/
 	var commandLine = fmt.Sprintf(`wireguard.exe /installtunnelservice "%s"`, confPath)
 	var commandLine = fmt.Sprintf(`wireguard.exe /installtunnelservice "%s"`, confPath)
 	if _, err := ncutils.RunCmdFormatted(commandLine, false); err != nil {
 	if _, err := ncutils.RunCmdFormatted(commandLine, false); err != nil {
 		return err
 		return err
@@ -31,22 +21,5 @@ func RemoveWindowsConf(ifacename string, printlog bool) error {
 	if _, err := ncutils.RunCmd("wireguard.exe /uninstalltunnelservice "+ifacename, printlog); err != nil {
 	if _, err := ncutils.RunCmd("wireguard.exe /uninstalltunnelservice "+ifacename, printlog); err != nil {
 		logger.Log(1, err.Error())
 		logger.Log(1, err.Error())
 	}
 	}
-	/*
-		dpapipath := fmt.Sprintf("%s\\%s.conf.dpapi", ncutils.WINDOWS_WG_DPAPI_PATH, ifacename)
-		confpath := fmt.Sprintf("%s\\%s.conf", ncutils.WINDOWS_WG_DPAPI_PATH, ifacename)
-		if ncutils.FileExists(confpath) {
-			err := os.Remove(confpath)
-			if err != nil {
-				logger.Log(err.Error(), 1)
-			}
-		}
-		time.Sleep(time.Second >> 2)
-		if ncutils.FileExists(dpapipath) {
-			err := os.Remove(dpapipath)
-			if err != nil {
-				logger.Log(err.Error(), 1)
-			}
-		}
-	*/
 	return nil
 	return nil
 }
 }