Browse Source

adding small fixes and token convert script

afeiszli 3 years ago
parent
commit
93188fb32a

+ 7 - 9
netclient/daemon/macos.go

@@ -4,7 +4,6 @@ import (
 	"fmt"
 	"log"
 	"os"
-	"path/filepath"
 
 	"github.com/gravitl/netmaker/netclient/ncutils"
 )
@@ -13,13 +12,12 @@ const MAC_SERVICE_NAME = "com.gravitl.netclient"
 
 func SetupMacDaemon(interval string) error {
 
-	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
-	if err != nil {
-		return err
-	}
-	binarypath := dir + "/netclient"
-
 	if !ncutils.FileExists("/etc/netclient/netclient") {
+		binarypath, err := os.Executable()
+		if err != nil {
+			return err
+		}
+		ncutils.PrintLog("installing binary from "+binarypath, 0)
 		err = ncutils.Copy(binarypath, "/etc/netclient/netclient")
 		if err != nil {
 			log.Println(err)
@@ -27,8 +25,8 @@ func SetupMacDaemon(interval string) error {
 		}
 	}
 
-	_, errN := os.Stat("~/Library/LaunchAgents")
-	if os.IsNotExist(errN) {
+	_, err := os.Stat("~/Library/LaunchAgents")
+	if os.IsNotExist(err) {
 		os.Mkdir("~/Library/LaunchAgents", 0755)
 	}
 	err = CreateMacService(MAC_SERVICE_NAME, interval)

+ 8 - 4
netclient/ncutils/netclientutils.go

@@ -5,7 +5,6 @@ import (
 	"errors"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"log"
 	"math/rand"
 	"net"
@@ -319,8 +318,9 @@ func GetNetclientPathSpecific() string {
 	}
 }
 
+// GetNewIface - Gets the name of the real interface created on Mac
 func GetNewIface(dir string) (string, error) {
-	files, _ := ioutil.ReadDir(dir)
+	files, _ := os.ReadDir(dir)
 	var newestFile string
 	var newestTime int64 = 0
 	var err error
@@ -342,13 +342,13 @@ func GetNewIface(dir string) (string, error) {
 	return resultArr[0], err
 }
 
+// GetFileAsString - returns the string contents of a given file
 func GetFileAsString(path string) (string, error) {
 	content, err := os.ReadFile(path)
 	if err != nil {
 		return "", err
 	}
-	text := string(content)
-	return text, err
+	return string(content), err
 }
 
 // GetNetclientPathSpecific - gets specific netclient config path
@@ -467,6 +467,7 @@ func stringAfter(original string, substring string) string {
 	return original[adjustedPosition:]
 }
 
+//ShortenString - Brings string down to specified length. Stops names from being too long
 func ShortenString(input string, length int) string {
 	output := input
 	if len(input) > length {
@@ -475,6 +476,7 @@ func ShortenString(input string, length int) string {
 	return output
 }
 
+//DNSFormatString - Formats a string with correct usage for DNS
 func DNSFormatString(input string) string {
 	reg, err := regexp.Compile("[^a-zA-Z0-9-]+")
 	if err != nil {
@@ -484,6 +486,7 @@ func DNSFormatString(input string) string {
 	return reg.ReplaceAllString(input, "")
 }
 
+//GetHostname - Gets hostname of machine
 func GetHostname() string {
 	hostname, err := os.Hostname()
 	if err != nil {
@@ -495,6 +498,7 @@ func GetHostname() string {
 	return hostname
 }
 
+//CheckUID - Checks to make sure user has root privileges
 func CheckUID() {
 	// start our application
 	out, err := RunCmd("id -u", true)

BIN
netclient/netclient-darwin


+ 0 - 1
netclient/wireguard/common.go

@@ -97,7 +97,6 @@ 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
 	}

+ 2 - 12
netclient/wireguard/mac.go

@@ -26,7 +26,6 @@ func WgQuickDownMac(node models.Node, iface string) error {
 
 // RemoveConfMac - bring down mac interface and remove routes
 func RemoveConfMac(iface string) error {
-	var err error
 	realIface, err := getRealIface(iface)
 	if realIface != "" {
 		err = deleteInterface(iface, realIface)
@@ -54,9 +53,7 @@ func WgQuickUpMac(node models.Node, iface string, confPath string) error {
 		ncutils.PrintLog("error setting config for "+realIface, 1)
 		return err
 	}
-	var ips []string
-	ips = append(node.AllowedIPs, node.Address)
-	ips = append(ips, node.Address6)
+	var ips = append(node.AllowedIPs, node.Address, node.Address6)
 	peerIPs := getPeerIPs(realIface)
 	if len(peerIPs) > 0 {
 		ips = append(ips, peerIPs...)
@@ -230,15 +227,8 @@ func getConfig(path string) string {
 }
 
 // SetMacPeerRoutes - sets routes for interface from the peer list for all AllowedIps
-func SetMacPeerRoutes(iface string) error {
+func SetMacPeerRoutes(realIface 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

+ 14 - 0
scripts/token-convert.sh

@@ -0,0 +1,14 @@
+#!/bin/bash
+
+set -e
+
+token=$1
+
+token_json=$(echo $token | base64 -d)
+
+api_addr=$(echo $token_json | jq -r '.apiconn')
+grpc_addr=$(echo $token_json | jq -r '.grpcconn')
+network=$(echo $token_json | jq -r '.network')
+key=$(echo $token_json | jq -r '.key')
+
+echo ./netclient join -k $key -n $network --apiserver $api_addr --grpcserver $grpc_addr