Browse Source

mac client semi-functional

afeiszli 3 years ago
parent
commit
44084b7df9

+ 3 - 0
netclient/functions/common.go

@@ -6,6 +6,7 @@ import (
 	"log"
 	"log"
 	"net"
 	"net"
 	"os"
 	"os"
+	"strings"
 
 
 	nodepb "github.com/gravitl/netmaker/grpc"
 	nodepb "github.com/gravitl/netmaker/grpc"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/models"
@@ -251,6 +252,8 @@ func WipeLocal(network string) error {
 	if ifacename != "" {
 	if ifacename != "" {
 		if err = wireguard.RemoveConf(ifacename, true); err == nil {
 		if err = wireguard.RemoveConf(ifacename, true); err == nil {
 			ncutils.PrintLog("removed WireGuard interface: "+ifacename, 1)
 			ncutils.PrintLog("removed WireGuard interface: "+ifacename, 1)
+		} else if strings.Contains(err.Error(), "does not exist") {
+			err = nil
 		}
 		}
 	}
 	}
 
 

BIN
netclient/netclient-darwin


BIN
netclient/netclient-darwin-testing


BIN
netclient/netclient-darwin.zip


+ 5 - 4
netclient/wireguard/common.go

@@ -96,6 +96,11 @@ func SetPeers(iface string, keepalive int32, peers []wgtypes.PeerConfig) error {
 			}
 			}
 		}
 		}
 	}
 	}
+	if ncutils.IsMac() {
+		log.Println("DELETE ME: setting mac peers")
+		err = SetMacPeerRoutes(iface)
+		return err
+	}
 
 
 	return nil
 	return nil
 }
 }
@@ -159,18 +164,15 @@ 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
 	var deviceiface string
 	var deviceiface string
 	if ncutils.IsMac() {
 	if ncutils.IsMac() {
-		log.Println("DELETE ME: check for local iface")
 		deviceiface, err = local.GetMacIface(node.Address)
 		deviceiface, err = local.GetMacIface(node.Address)
 		if err != nil || deviceiface == "" {
 		if err != nil || deviceiface == "" {
 			deviceiface = ifacename
 			deviceiface = ifacename
 		}
 		}
 	}
 	}
 	if syncconf {
 	if syncconf {
-		log.Println("DELETE ME: syncconf")
 		err = SyncWGQuickConf(ifacename, confPath)
 		err = SyncWGQuickConf(ifacename, confPath)
 	} else {
 	} else {
 		if !ncutils.IsMac() {
 		if !ncutils.IsMac() {
-			log.Println("DELETE ME: get device")
 			d, _ := wgclient.Device(deviceiface)
 			d, _ := wgclient.Device(deviceiface)
 			for d != nil && d.Name == deviceiface {
 			for d != nil && d.Name == deviceiface {
 				RemoveConf(ifacename, false) // remove interface first
 				RemoveConf(ifacename, false) // remove interface first
@@ -179,7 +181,6 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig
 			}
 			}
 		}
 		}
 		if !ncutils.IsWindows() {
 		if !ncutils.IsWindows() {
-			log.Println("DELETE ME: apply conf")
 			err = ApplyConf(*node, ifacename, confPath)
 			err = ApplyConf(*node, ifacename, confPath)
 			if err != nil {
 			if err != nil {
 				ncutils.PrintLog("failed to create wireguard interface", 1)
 				ncutils.PrintLog("failed to create wireguard interface", 1)

+ 69 - 12
netclient/wireguard/mac.go

@@ -1,6 +1,7 @@
 package wireguard
 package wireguard
 
 
 import (
 import (
+	"bufio"
 	"errors"
 	"errors"
 	"os"
 	"os"
 	"strconv"
 	"strconv"
@@ -14,7 +15,12 @@ import (
 func AddInterface(iface string) (string, error) {
 func AddInterface(iface string) (string, error) {
 	ncutils.RunCmd("mkdir -p /var/run/wireguard/", true)
 	ncutils.RunCmd("mkdir -p /var/run/wireguard/", true)
 	ncutils.RunCmd("wireguard-go utun", true)
 	ncutils.RunCmd("wireguard-go utun", true)
-	return ncutils.GetNewIface("/var/run/wireguard/")
+	realIface, err := ncutils.GetNewIface("/var/run/wireguard/")
+	if iface != "" && err == nil {
+		ifacePath := "/var/run/wireguard/" + iface + ".name"
+		err = os.WriteFile(ifacePath, []byte(realIface), 0644)
+	}
+	return realIface, err
 }
 }
 
 
 func GetRealIface(iface string) (string, error) {
 func GetRealIface(iface string) (string, error) {
@@ -55,14 +61,18 @@ func DeleteRoutes(iface string) error {
 	return nil
 	return nil
 }
 }
 
 
-func DeleteInterface(iface string) error {
+func DeleteInterface(iface string, realIface string) error {
 	var err error
 	var err error
+	var out string
 	if iface != "" {
 	if iface != "" {
-		ncutils.RunCmd("rm -f /var/run/wireguard/"+iface+".sock", true)
+		os.Remove("/var/run/wireguard/" + realIface + ".sock")
+		os.Remove("/var/run/wireguard/" + iface + ".name")
 	}
 	}
-	_, err = ncutils.RunCmd("ifconfig "+iface+" down", false)
+	out, err = ncutils.RunCmd("ifconfig "+realIface+" down", false)
 	if strings.Contains(err.Error(), "does not exist") {
 	if strings.Contains(err.Error(), "does not exist") {
 		err = nil
 		err = nil
+	} else if err != nil && out != "" {
+		err = errors.New(out)
 	}
 	}
 	return err
 	return err
 }
 }
@@ -103,7 +113,7 @@ func AddRoute(addr string, iface string) error {
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	if out == "" {
+	if !(strings.Contains(out, iface)) {
 		_, err = ncutils.RunCmd("route -q -n add -"+inetx+" "+addr+" -interface "+iface, true)
 		_, err = ncutils.RunCmd("route -q -n add -"+inetx+" "+addr+" -interface "+iface, true)
 	}
 	}
 	return err
 	return err
@@ -129,15 +139,17 @@ func GetConfig(path string) string {
 func WgQuickUpMac(node models.Node, iface string, confPath string) error {
 func WgQuickUpMac(node models.Node, iface string, confPath string) error {
 	var err error
 	var err error
 	var realIface string
 	var realIface string
-	DeleteInterface(iface)
-	DeleteRoutes(iface)
-
+	realIface, err = GetRealIface(iface)
+	if realIface != "" && err == nil {
+		DeleteInterface(iface, realIface)
+		DeleteRoutes(realIface)
+	}
 	realIface, err = AddInterface(iface)
 	realIface, err = AddInterface(iface)
 	if err != nil {
 	if err != nil {
 		ncutils.PrintLog("error creating wg interface", 1)
 		ncutils.PrintLog("error creating wg interface", 1)
 		return err
 		return err
 	}
 	}
-	time.Sleep(1)
+	time.Sleep(time.Second / 2)
 	err = SetConfig(realIface, confPath)
 	err = SetConfig(realIface, confPath)
 	if err != nil {
 	if err != nil {
 		ncutils.PrintLog("error setting config for "+realIface, 1)
 		ncutils.PrintLog("error setting config for "+realIface, 1)
@@ -146,6 +158,10 @@ func WgQuickUpMac(node models.Node, iface string, confPath string) error {
 	var ips []string
 	var ips []string
 	ips = append(node.AllowedIPs, node.Address)
 	ips = append(node.AllowedIPs, node.Address)
 	ips = append(ips, node.Address6)
 	ips = append(ips, node.Address6)
+	peerIPs := getPeerIPs(realIface)
+	if len(peerIPs) > 0 {
+		ips = append(ips, peerIPs...)
+	}
 	for _, i := range ips {
 	for _, i := range ips {
 		if i != "" {
 		if i != "" {
 			err = AddAddress(realIface, i)
 			err = AddAddress(realIface, i)
@@ -172,7 +188,7 @@ func WgQuickUpMac(node models.Node, iface string, confPath string) error {
 	}
 	}
 	//next, wg-quick runs set_endpoint_direct_route
 	//next, wg-quick runs set_endpoint_direct_route
 	//next, wg-quick runs monitor_daemon
 	//next, wg-quick runs monitor_daemon
-	time.Sleep(1)
+	time.Sleep(time.Second / 2)
 	if node.PostUp != "" {
 	if node.PostUp != "" {
 		runcmds := strings.Split(node.PostUp, "; ")
 		runcmds := strings.Split(node.PostUp, "; ")
 		ncutils.RunCmds(runcmds, true)
 		ncutils.RunCmds(runcmds, true)
@@ -180,11 +196,52 @@ func WgQuickUpMac(node models.Node, iface string, confPath string) error {
 	return err
 	return err
 }
 }
 
 
+func SetMacPeerRoutes(iface string) error {
+	var err error
+	realIface := iface
+	/*
+		realIface, err := GetRealIface(iface)
+		if err != nil || realIface == "" {
+			return err
+		}
+	*/
+	peerIPs := getPeerIPs(realIface)
+	if len(peerIPs) == 0 {
+		return err
+	}
+	for _, i := range peerIPs {
+		if i != "" {
+			err = AddRoute(i, realIface)
+			if err != nil {
+				ncutils.PrintLog("error adding route to "+realIface+" for "+i, 1)
+				return err
+			}
+		}
+	}
+	return err
+}
+
+func getPeerIPs(realIface string) []string {
+	allowedIps := []string{}
+	out, err := ncutils.RunCmd("wg show "+realIface+" allowed-ips", false)
+	if err != nil {
+		return allowedIps
+	}
+	scanner := bufio.NewScanner(strings.NewReader(out))
+	for scanner.Scan() {
+		fields := strings.Fields(scanner.Text())
+		if len(fields) > 1 {
+			allowedIps = append(allowedIps, fields[1:]...)
+		}
+	}
+	return allowedIps
+}
+
 func WgQuickDownShortMac(iface string) error {
 func WgQuickDownShortMac(iface string) error {
 	var err error
 	var err error
 	realIface, err := GetRealIface(iface)
 	realIface, err := GetRealIface(iface)
 	if realIface != "" {
 	if realIface != "" {
-		err = DeleteInterface(iface)
+		err = DeleteInterface(iface, realIface)
 	}
 	}
 	return err
 	return err
 }
 }
@@ -193,7 +250,7 @@ func WgQuickDownMac(node models.Node, iface string) error {
 	var err error
 	var err error
 	realIface, err := GetRealIface(iface)
 	realIface, err := GetRealIface(iface)
 	if realIface != "" {
 	if realIface != "" {
-		err = DeleteInterface(iface)
+		err = DeleteInterface(iface, realIface)
 	} else if err != nil {
 	} else if err != nil {
 		return err
 		return err
 	}
 	}

+ 1 - 1
netclient/wireguard/unix.go

@@ -61,7 +61,7 @@ func ApplyWGQuickConf(confPath string) error {
 // ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS
 // ApplyMacOSConf - applies system commands similar to wg-quick using golang for MacOS
 func ApplyMacOSConf(node models.Node, ifacename string, confPath string) error {
 func ApplyMacOSConf(node models.Node, ifacename string, confPath string) error {
 	var err error
 	var err error
-	err = WgQuickDownMac(node, ifacename)
+	_ = WgQuickDownMac(node, ifacename)
 	err = WgQuickUpMac(node, ifacename, confPath)
 	err = WgQuickUpMac(node, ifacename, confPath)
 	return err
 	return err
 }
 }