mirror of
				https://github.com/gravitl/netmaker.git
				synced 2025-11-01 04:32:40 +08:00 
			
		
		
		
	Merge pull request #925 from gravitl/refactor_v0.12.0_logging
replace ncutil.Log/PrintLog with logger.Log
This commit is contained in:
		| @@ -3,6 +3,7 @@ package logger | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"sort" | ||||
| 	"sync" | ||||
| 	"time" | ||||
| @@ -17,6 +18,15 @@ const TimeFormat = "2006-01-02 15:04:05" | ||||
| // == fields == | ||||
| var currentLogs = make(map[string]string) | ||||
| var mu sync.Mutex | ||||
| var program string | ||||
|  | ||||
| func init() { | ||||
| 	fullpath, err := os.Executable() | ||||
| 	if err != nil { | ||||
| 		fullpath = "" | ||||
| 	} | ||||
| 	program = filepath.Base(fullpath) | ||||
| } | ||||
|  | ||||
| // Log - handles adding logs | ||||
| func Log(verbosity int, message ...string) { | ||||
| @@ -25,13 +35,18 @@ func Log(verbosity int, message ...string) { | ||||
| 	var currentTime = time.Now() | ||||
| 	var currentMessage = MakeString(" ", message...) | ||||
| 	if int32(verbosity) <= getVerbose() && getVerbose() >= 0 { | ||||
| 		fmt.Printf("[netmaker] %s %s \n", currentTime.Format(TimeFormat), currentMessage) | ||||
| 		fmt.Printf("[%s] %s %s \n", program, currentTime.Format(TimeFormat), currentMessage) | ||||
| 	} | ||||
| 	if program == "netmaker" { | ||||
| 		currentLogs[currentMessage] = currentTime.Format("2006-01-02 15:04:05.999999999") | ||||
| 	} | ||||
| 	currentLogs[currentMessage] = currentTime.Format("2006-01-02 15:04:05.999999999") | ||||
| } | ||||
|  | ||||
| // Dump - dumps all logs into a formatted string | ||||
| func Dump() string { | ||||
| 	if program != "netmaker" { | ||||
| 		return "" | ||||
| 	} | ||||
| 	var dumpString = "" | ||||
| 	type keyVal struct { | ||||
| 		Key   string | ||||
| @@ -65,6 +80,9 @@ func Dump() string { | ||||
|  | ||||
| // DumpFile - appends log dump log file | ||||
| func DumpFile(filePath string) { | ||||
| 	if program != "netmaker" { | ||||
| 		return | ||||
| 	} | ||||
| 	f, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) | ||||
| 	if err != nil { | ||||
| 		fmt.Println(MakeString(" ", "could not open log file", filePath)) | ||||
|   | ||||
| @@ -13,7 +13,6 @@ import ( | ||||
| 	"github.com/gravitl/netmaker/logic/acls" | ||||
| 	"github.com/gravitl/netmaker/logic/acls/nodeacls" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| 	"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||||
| ) | ||||
|  | ||||
| @@ -316,17 +315,17 @@ func GetAllowedIPs(node, peer *models.Node) []net.IPNet { | ||||
| 		for _, iprange := range ranges { // go through each cidr for egress gateway | ||||
| 			_, ipnet, err := net.ParseCIDR(iprange) // confirming it's valid cidr | ||||
| 			if err != nil { | ||||
| 				ncutils.PrintLog("could not parse gateway IP range. Not adding "+iprange, 1) | ||||
| 				logger.Log(1, "could not parse gateway IP range. Not adding ", iprange) | ||||
| 				continue // if can't parse CIDR | ||||
| 			} | ||||
| 			nodeEndpointArr := strings.Split(peer.Endpoint, ":") // getting the public ip of node | ||||
| 			if ipnet.Contains(net.ParseIP(nodeEndpointArr[0])) { // ensuring egress gateway range does not contain endpoint of node | ||||
| 				ncutils.PrintLog("egress IP range of "+iprange+" overlaps with "+node.Endpoint+", omitting", 2) | ||||
| 				logger.Log(2, "egress IP range of ", iprange, " overlaps with ", node.Endpoint, ", omitting") | ||||
| 				continue // skip adding egress range if overlaps with node's ip | ||||
| 			} | ||||
| 			// TODO: Could put in a lot of great logic to avoid conflicts / bad routes | ||||
| 			if ipnet.Contains(net.ParseIP(node.LocalAddress)) { // ensuring egress gateway range does not contain public ip of node | ||||
| 				ncutils.PrintLog("egress IP range of "+iprange+" overlaps with "+node.LocalAddress+", omitting", 2) | ||||
| 				logger.Log(2, "egress IP range of ", iprange, " overlaps with ", node.LocalAddress, ", omitting") | ||||
| 				continue // skip adding egress range if overlaps with node's local ip | ||||
| 			} | ||||
| 			if err != nil { | ||||
|   | ||||
| @@ -4,6 +4,7 @@ import ( | ||||
| 	"errors" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| 	"github.com/gravitl/netmaker/netclient/daemon" | ||||
| 	"github.com/gravitl/netmaker/netclient/functions" | ||||
| @@ -51,12 +52,12 @@ func Join(cfg *config.ClientConfig, privateKey string) error { | ||||
| 	err = functions.JoinNetwork(cfg, privateKey, false) | ||||
| 	if err != nil && !cfg.DebugOn { | ||||
| 		if !strings.Contains(err.Error(), "ALREADY_INSTALLED") { | ||||
| 			ncutils.PrintLog("error installing: "+err.Error(), 1) | ||||
| 			logger.Log(1, "error installing: ", err.Error()) | ||||
| 			err = functions.LeaveNetwork(cfg.Network, true) | ||||
| 			if err != nil { | ||||
| 				err = functions.WipeLocal(cfg.Network) | ||||
| 				if err != nil { | ||||
| 					ncutils.PrintLog("error removing artifacts: "+err.Error(), 1) | ||||
| 					logger.Log(1, "error removing artifacts: ", err.Error()) | ||||
| 				} | ||||
| 			} | ||||
| 			if cfg.Daemon != "off" { | ||||
| @@ -64,25 +65,25 @@ func Join(cfg *config.ClientConfig, privateKey string) error { | ||||
| 					err = daemon.RemoveSystemDServices() | ||||
| 				} | ||||
| 				if err != nil { | ||||
| 					ncutils.PrintLog("error removing services: "+err.Error(), 1) | ||||
| 					logger.Log(1, "error removing services: ", err.Error()) | ||||
| 				} | ||||
| 				if ncutils.IsFreeBSD() { | ||||
| 					daemon.RemoveFreebsdDaemon() | ||||
| 				} | ||||
| 			} | ||||
| 		} else { | ||||
| 			ncutils.PrintLog("success", 0) | ||||
| 			logger.Log(0, "success") | ||||
| 		} | ||||
| 		if err != nil && strings.Contains(err.Error(), "ALREADY_INSTALLED") { | ||||
| 			ncutils.PrintLog(err.Error(), 0) | ||||
| 			logger.Log(0, err.Error()) | ||||
| 			err = nil | ||||
| 		} | ||||
| 		return err | ||||
| 	} | ||||
| 	ncutils.PrintLog("joined "+cfg.Network, 1) | ||||
| 	logger.Log(1, "joined ", cfg.Network) | ||||
| 	/* | ||||
| 		if ncutils.IsWindows() { | ||||
| 			ncutils.PrintLog("setting up WireGuard app", 0) | ||||
| 			logger.Log("setting up WireGuard app", 0) | ||||
| 			time.Sleep(time.Second >> 1) | ||||
| 			functions.Pull(cfg.Network, true) | ||||
| 		} | ||||
| @@ -94,14 +95,14 @@ func Join(cfg *config.ClientConfig, privateKey string) error { | ||||
| func Leave(cfg *config.ClientConfig, force bool) error { | ||||
| 	err := functions.LeaveNetwork(cfg.Network, force) | ||||
| 	if err != nil { | ||||
| 		ncutils.PrintLog("error attempting to leave network "+cfg.Network, 1) | ||||
| 		logger.Log(1, "error attempting to leave network "+cfg.Network) | ||||
| 	} else { | ||||
| 		ncutils.PrintLog("success", 0) | ||||
| 		logger.Log(0, "success") | ||||
| 	} | ||||
| 	nets, err := ncutils.GetSystemNetworks() | ||||
| 	if err == nil && len(nets) == 1 { | ||||
| 		if nets[0] == cfg.Node.CommID { | ||||
| 			ncutils.PrintLog("detected comms as remaining network, removing...", 1) | ||||
| 			logger.Log(1, "detected comms as remaining network, removing...") | ||||
| 			err = functions.LeaveNetwork(nets[0], true) | ||||
| 		} | ||||
| 	} | ||||
| @@ -112,30 +113,30 @@ func Leave(cfg *config.ClientConfig, force bool) error { | ||||
| func Pull(cfg *config.ClientConfig) error { | ||||
| 	var err error | ||||
| 	if cfg.Network == "all" { | ||||
| 		ncutils.PrintLog("No network selected. Running Pull for all networks.", 0) | ||||
| 		logger.Log(0, "No network selected. Running Pull for all networks.") | ||||
| 		networks, err := ncutils.GetSystemNetworks() | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog("Error retrieving networks. Exiting.", 1) | ||||
| 			logger.Log(1, "Error retrieving networks. Exiting.") | ||||
| 			return err | ||||
| 		} | ||||
| 		for _, network := range networks { | ||||
| 			_, err = functions.Pull(network, true) | ||||
| 			if err != nil { | ||||
| 				ncutils.PrintLog("Error pulling network config for network: "+network+"\n"+err.Error(), 1) | ||||
| 				logger.Log(1, "Error pulling network config for network: ", network, "\n", err.Error()) | ||||
| 			} else { | ||||
| 				ncutils.PrintLog("pulled network config for "+network, 1) | ||||
| 				logger.Log(1, "pulled network config for "+network) | ||||
| 			} | ||||
| 		} | ||||
| 		err = nil | ||||
| 	} else { | ||||
| 		_, err = functions.Pull(cfg.Network, true) | ||||
| 	} | ||||
| 	ncutils.PrintLog("reset network and peer configs", 1) | ||||
| 	logger.Log(1, "reset network and peer configs") | ||||
| 	if err == nil { | ||||
| 		ncutils.PrintLog("reset network and peer configs", 1) | ||||
| 		ncutils.PrintLog("success", 1) | ||||
| 		logger.Log(1, "reset network and peer configs") | ||||
| 		logger.Log(1, "success") | ||||
| 	} else { | ||||
| 		ncutils.PrintLog("error occurred pulling configs from server", 1) | ||||
| 		logger.Log(0, "error occurred pulling configs from server") | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
| @@ -148,9 +149,9 @@ func List(cfg config.ClientConfig) error { | ||||
|  | ||||
| // Uninstall - runs uninstall command from cli | ||||
| func Uninstall() error { | ||||
| 	ncutils.PrintLog("uninstalling netclient...", 0) | ||||
| 	logger.Log(0, "uninstalling netclient...") | ||||
| 	err := functions.Uninstall() | ||||
| 	ncutils.PrintLog("uninstalled netclient", 0) | ||||
| 	logger.Log(0, "uninstalled netclient") | ||||
| 	return err | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -9,6 +9,7 @@ import ( | ||||
| 	"log" | ||||
| 	"os" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| 	"github.com/urfave/cli/v2" | ||||
| @@ -87,7 +88,7 @@ func (config *ClientConfig) ReadConfig() { | ||||
| 	//f, err := os.Open(file) | ||||
| 	f, err := os.OpenFile(file, os.O_RDONLY, 0600) | ||||
| 	if err != nil { | ||||
| 		ncutils.PrintLog("trouble opening file: "+err.Error(), 1) | ||||
| 		logger.Log(1, "trouble opening file: ", err.Error()) | ||||
| 		nofile = true | ||||
| 		//fmt.Println("Could not access " + home + "/.netconfig,  proceeding...") | ||||
| 	} | ||||
| @@ -134,11 +135,11 @@ func SaveBackup(network string) error { | ||||
| 	if FileExists(configPath) { | ||||
| 		input, err := os.ReadFile(configPath) | ||||
| 		if err != nil { | ||||
| 			ncutils.Log("failed to read " + configPath + " to make a backup") | ||||
| 			logger.Log(0, "failed to read ", configPath, " to make a backup") | ||||
| 			return err | ||||
| 		} | ||||
| 		if err = os.WriteFile(backupPath, input, 0600); err != nil { | ||||
| 			ncutils.Log("failed to copy backup to " + backupPath) | ||||
| 			logger.Log(0, "failed to copy backup to ", backupPath) | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| @@ -152,15 +153,15 @@ func ReplaceWithBackup(network string) error { | ||||
| 	if FileExists(backupPath) { | ||||
| 		input, err := os.ReadFile(backupPath) | ||||
| 		if err != nil { | ||||
| 			ncutils.Log("failed to read file " + backupPath + " to backup network: " + network) | ||||
| 			logger.Log(0, "failed to read file ", backupPath, " to backup network: ", network) | ||||
| 			return err | ||||
| 		} | ||||
| 		if err = os.WriteFile(configPath, input, 0600); err != nil { | ||||
| 			ncutils.Log("failed backup " + backupPath + " to " + configPath) | ||||
| 			logger.Log(0, "failed backup ", backupPath, " to ", configPath) | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| 	ncutils.Log("used backup file for network: " + network) | ||||
| 	logger.Log(0, "used backup file for network: ", network) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -5,6 +5,7 @@ import ( | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| ) | ||||
|  | ||||
| @@ -110,10 +111,10 @@ func FreebsdDaemon(command string) { | ||||
| // CleanupFreebsd - removes config files and netclient binary | ||||
| func CleanupFreebsd() { | ||||
| 	if err := os.RemoveAll(ncutils.GetNetclientPath()); err != nil { | ||||
| 		ncutils.PrintLog("Removing netclient configs: "+err.Error(), 1) | ||||
| 		logger.Log(1, "Removing netclient configs: ", err.Error()) | ||||
| 	} | ||||
| 	if err := os.Remove(EXEC_DIR + "netclient"); err != nil { | ||||
| 		ncutils.PrintLog("Removing netclient binary: "+err.Error(), 1) | ||||
| 		logger.Log(1, "Removing netclient binary: ", err.Error()) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -122,13 +123,13 @@ func RemoveFreebsdDaemon() { | ||||
| 	if ncutils.FileExists("/etc/rc.d/netclient") { | ||||
| 		err := os.Remove("/etc/rc.d/netclient") | ||||
| 		if err != nil { | ||||
| 			ncutils.Log("Error removing /etc/rc.d/netclient. Please investigate.") | ||||
| 			logger.Log(0, "Error removing /etc/rc.d/netclient. Please investigate.") | ||||
| 		} | ||||
| 	} | ||||
| 	if ncutils.FileExists("/etc/rc.conf.d/netclient") { | ||||
| 		err := os.Remove("/etc/rc.conf.d/netclient") | ||||
| 		if err != nil { | ||||
| 			ncutils.Log("Error removing /etc/rc.conf.d/netclient. Please investigate.") | ||||
| 			logger.Log(0, "Error removing /etc/rc.conf.d/netclient. Please investigate.") | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -6,6 +6,7 @@ import ( | ||||
| 	"path/filepath" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| ) | ||||
|  | ||||
| @@ -48,7 +49,7 @@ func CleanupMac() { | ||||
| 		err = os.Remove("/Library/LaunchDaemons/" + MAC_SERVICE_NAME + ".plist") | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		ncutils.PrintLog(err.Error(), 1) | ||||
| 		logger.Log(1, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	os.RemoveAll(ncutils.GetNetclientPath()) | ||||
|   | ||||
| @@ -8,6 +8,7 @@ import ( | ||||
| 	"path/filepath" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| ) | ||||
|  | ||||
| @@ -76,7 +77,7 @@ WantedBy=multi-user.target | ||||
|  | ||||
| // RestartSystemD - restarts systemd service | ||||
| func RestartSystemD() { | ||||
| 	ncutils.PrintLog("restarting netclient.service", 1) | ||||
| 	logger.Log(1, "restarting netclient.service") | ||||
| 	time.Sleep(time.Second) | ||||
| 	_, _ = ncutils.RunCmd("systemctl restart netclient.service", true) | ||||
| } | ||||
| @@ -84,10 +85,10 @@ func RestartSystemD() { | ||||
| // CleanupLinux - cleans up neclient configs | ||||
| func CleanupLinux() { | ||||
| 	if err := os.RemoveAll(ncutils.GetNetclientPath()); err != nil { | ||||
| 		ncutils.PrintLog("Removing netclient configs: "+err.Error(), 1) | ||||
| 		logger.Log(1, "Removing netclient configs: ", err.Error()) | ||||
| 	} | ||||
| 	if err := os.Remove(EXEC_DIR + "netclient"); err != nil { | ||||
| 		ncutils.PrintLog("Removing netclient binary: "+err.Error(), 1) | ||||
| 		logger.Log(1, "Removing netclient binary: ", err.Error()) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -109,18 +110,18 @@ func RemoveSystemDServices() error { | ||||
| 		if ncutils.FileExists("/etc/systemd/system/netclient.service") { | ||||
| 			err = os.Remove("/etc/systemd/system/netclient.service") | ||||
| 			if err != nil { | ||||
| 				ncutils.Log("Error removing /etc/systemd/system/netclient.service. Please investigate.") | ||||
| 				logger.Log(0, "Error removing /etc/systemd/system/netclient.service. Please investigate.") | ||||
| 			} | ||||
| 		} | ||||
| 		if ncutils.FileExists("/etc/systemd/system/netclient.timer") { | ||||
| 			err = os.Remove("/etc/systemd/system/netclient.timer") | ||||
| 			if err != nil { | ||||
| 				ncutils.Log("Error removing /etc/systemd/system/netclient.timer. Please investigate.") | ||||
| 				logger.Log(0, "Error removing /etc/systemd/system/netclient.timer. Please investigate.") | ||||
| 			} | ||||
| 		} | ||||
| 		ncutils.RunCmd("systemctl daemon-reload", false) | ||||
| 		ncutils.RunCmd("systemctl reset-failed", false) | ||||
| 		ncutils.Log("removed systemd remnants if any existed") | ||||
| 		logger.Log(0, "removed systemd remnants if any existed") | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
| @@ -6,6 +6,7 @@ import ( | ||||
| 	"os" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| ) | ||||
|  | ||||
| @@ -19,18 +20,18 @@ func SetupWindowsDaemon() error { | ||||
| 	} | ||||
|  | ||||
| 	if !ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "winsw.exe") { | ||||
| 		ncutils.Log("performing first time daemon setup") | ||||
| 		logger.Log(0, "performing first time daemon setup") | ||||
| 		err := ncutils.GetEmbedded() | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		ncutils.Log("finished daemon setup") | ||||
| 		logger.Log(0, "finished daemon setup") | ||||
| 	} | ||||
| 	// install daemon, will not overwrite | ||||
| 	ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe install`, false) | ||||
| 	// start daemon, will not restart or start another | ||||
| 	ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`, false) | ||||
| 	ncutils.Log(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1) + `winsw.exe start`) | ||||
| 	logger.Log(0, strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe start`) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| @@ -68,7 +69,7 @@ func writeServiceConfig() error { | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| 		ncutils.Log("wrote the daemon config file to the Netclient directory") | ||||
| 		logger.Log(0, "wrote the daemon config file to the Netclient directory") | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| @@ -77,7 +78,7 @@ func writeServiceConfig() error { | ||||
|  | ||||
| // StopWindowsDaemon - stops the Windows daemon | ||||
| func StopWindowsDaemon() { | ||||
| 	ncutils.Log("stopping Windows, Netclient daemon") | ||||
| 	logger.Log(0, "stopping Windows, Netclient daemon") | ||||
| 	// stop daemon, will not overwrite | ||||
| 	ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe stop`, true) | ||||
| } | ||||
| @@ -86,25 +87,25 @@ func StopWindowsDaemon() { | ||||
| func RemoveWindowsDaemon() { | ||||
| 	// uninstall daemon, will not restart or start another | ||||
| 	ncutils.RunCmd(strings.Replace(ncutils.GetNetclientPathSpecific(), `\\`, `\`, -1)+`winsw.exe uninstall`, true) | ||||
| 	ncutils.Log("uninstalled Windows, Netclient daemon") | ||||
| 	logger.Log(0, "uninstalled Windows, Netclient daemon") | ||||
| } | ||||
|  | ||||
| // func copyWinswOver() error { | ||||
|  | ||||
| // 	input, err := ioutil.ReadFile(".\\winsw.exe") | ||||
| // 	if err != nil { | ||||
| // 		ncutils.Log("failed to find winsw.exe") | ||||
| // 		logger.Log(0, "failed to find winsw.exe") | ||||
| // 		return err | ||||
| // 	} | ||||
| // 	if err = ioutil.WriteFile(ncutils.GetNetclientPathSpecific()+"winsw.exe", input, 0644); err != nil { | ||||
| // 		ncutils.Log("failed to copy winsw.exe to " + ncutils.GetNetclientPath()) | ||||
| // 		logger.Log(0, "failed to copy winsw.exe to " + ncutils.GetNetclientPath()) | ||||
| // 		return err | ||||
| // 	} | ||||
| // 	if err = os.Remove(".\\winsw.exe"); err != nil { | ||||
| // 		ncutils.Log("failed to cleanup local winsw.exe, feel free to delete it") | ||||
| // 		logger.Log(0, "failed to cleanup local winsw.exe, feel free to delete it") | ||||
| // 		return err | ||||
| // 	} | ||||
| // 	ncutils.Log("finished copying winsw.exe") | ||||
| // 	logger.Log(0, "finished copying winsw.exe") | ||||
| // 	return nil | ||||
| // } | ||||
|  | ||||
| @@ -115,7 +116,7 @@ func RemoveWindowsDaemon() { | ||||
| // 	// Create the file | ||||
| // 	file, err := os.Create(fileName) | ||||
| // 	if err != nil { | ||||
| // 		ncutils.Log("could not create file on OS for Winsw") | ||||
| // 		logger.Log(0, "could not create file on OS for Winsw") | ||||
| // 		return err | ||||
| // 	} | ||||
| // 	defer file.Close() | ||||
| @@ -127,19 +128,19 @@ func RemoveWindowsDaemon() { | ||||
| // 		}, | ||||
| // 	} | ||||
| // 	// Put content on file | ||||
| // 	ncutils.Log("downloading service tool...") | ||||
| // 	logger.Log(0, "downloading service tool...") | ||||
| // 	resp, err := client.Get(fullURLFile) | ||||
| // 	if err != nil { | ||||
| // 		ncutils.Log("could not GET Winsw") | ||||
| // 		logger.Log(0, "could not GET Winsw") | ||||
| // 		return err | ||||
| // 	} | ||||
| // 	defer resp.Body.Close() | ||||
|  | ||||
| // 	_, err = io.Copy(file, resp.Body) | ||||
| // 	if err != nil { | ||||
| // 		ncutils.Log("could not mount winsw.exe") | ||||
| // 		logger.Log(0, "could not mount winsw.exe") | ||||
| // 		return err | ||||
| // 	} | ||||
| // 	ncutils.Log("finished downloading Winsw") | ||||
| // 	logger.Log(0, "finished downloading Winsw") | ||||
| // 	return nil | ||||
| // } | ||||
|   | ||||
| @@ -10,6 +10,7 @@ import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	nodepb "github.com/gravitl/netmaker/grpc" | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/auth" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| @@ -120,13 +121,13 @@ func GetNode(network string) models.Node { | ||||
| func Uninstall() error { | ||||
| 	networks, err := ncutils.GetSystemNetworks() | ||||
| 	if err != nil { | ||||
| 		ncutils.PrintLog("unable to retrieve networks: "+err.Error(), 1) | ||||
| 		ncutils.PrintLog("continuing uninstall without leaving networks", 1) | ||||
| 		logger.Log(1, "unable to retrieve networks: ", err.Error()) | ||||
| 		logger.Log(1, "continuing uninstall without leaving networks") | ||||
| 	} else { | ||||
| 		for _, network := range networks { | ||||
| 			err = LeaveNetwork(network, true) | ||||
| 			if err != nil { | ||||
| 				ncutils.PrintLog("Encounter issue leaving network "+network+": "+err.Error(), 1) | ||||
| 				logger.Log(1, "Encounter issue leaving network ", network, ": ", err.Error()) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| @@ -140,7 +141,7 @@ func Uninstall() error { | ||||
| 	} else if ncutils.IsFreeBSD() { | ||||
| 		daemon.CleanupFreebsd() | ||||
| 	} else if !ncutils.IsKernel() { | ||||
| 		ncutils.PrintLog("manual cleanup required", 1) | ||||
| 		logger.Log(1, "manual cleanup required") | ||||
| 	} | ||||
|  | ||||
| 	return err | ||||
| @@ -184,9 +185,9 @@ func LeaveNetwork(network string, force bool) error { | ||||
| 					grpc.Header(&header), | ||||
| 				) | ||||
| 				if err != nil { | ||||
| 					ncutils.PrintLog("encountered error deleting node: "+err.Error(), 1) | ||||
| 					logger.Log(1, "encountered error deleting node: ", err.Error()) | ||||
| 				} else { | ||||
| 					ncutils.PrintLog("removed machine from "+node.Network+" network on remote server", 1) | ||||
| 					logger.Log(1, "removed machine from ", node.Network, " network on remote server") | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| @@ -211,15 +212,15 @@ func LeaveNetwork(network string, force bool) error { | ||||
| 				local.RemoveCIDRRoute(removeIface, cfg.Node.Address, cidr) | ||||
| 			} | ||||
| 		} else { | ||||
| 			ncutils.PrintLog("could not flush peer routes when leaving network, "+cfg.Node.Network, 1) | ||||
| 			logger.Log(1, "could not flush peer routes when leaving network, ", cfg.Node.Network) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	err = WipeLocal(node.Network) | ||||
| 	if err != nil { | ||||
| 		ncutils.PrintLog("unable to wipe local config", 1) | ||||
| 		logger.Log(1, "unable to wipe local config") | ||||
| 	} else { | ||||
| 		ncutils.PrintLog("removed "+node.Network+" network locally", 1) | ||||
| 		logger.Log(1, "removed ", node.Network, " network locally") | ||||
| 	} | ||||
|  | ||||
| 	currentNets, err := ncutils.GetSystemNetworks() | ||||
| @@ -262,7 +263,7 @@ func WipeLocal(network string) error { | ||||
| 	ifacename := nodecfg.Interface | ||||
| 	if ifacename != "" { | ||||
| 		if err = wireguard.RemoveConf(ifacename, true); err == nil { | ||||
| 			ncutils.PrintLog("removed WireGuard interface: "+ifacename, 1) | ||||
| 			logger.Log(1, "removed WireGuard interface: ", ifacename) | ||||
| 		} else if strings.Contains(err.Error(), "does not exist") { | ||||
| 			err = nil | ||||
| 		} | ||||
|   | ||||
| @@ -13,6 +13,7 @@ import ( | ||||
|  | ||||
| 	mqtt "github.com/eclipse/paho.mqtt.golang" | ||||
| 	"github.com/go-ping/ping" | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/auth" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| @@ -55,7 +56,7 @@ func Daemon() error { | ||||
|  | ||||
| 	// == subscribe to all nodes on each comms network on machine == | ||||
| 	for currCommsNet := range commsNetworks { | ||||
| 		ncutils.PrintLog("started comms network daemon, "+currCommsNet, 1) | ||||
| 		logger.Log(1, "started comms network daemon, ", currCommsNet) | ||||
| 		ctx, cancel := context.WithCancel(context.Background()) | ||||
| 		networkcontext.Store(currCommsNet, cancel) | ||||
| 		go messageQueue(ctx, currCommsNet) | ||||
| @@ -75,27 +76,27 @@ func Daemon() error { | ||||
| 		} | ||||
| 	} | ||||
| 	cancel() | ||||
| 	ncutils.Log("shutting down netclient daemon") | ||||
| 	logger.Log(0, "shutting down netclient daemon") | ||||
| 	wg.Wait() | ||||
| 	ncutils.Log("shutdown complete") | ||||
| 	logger.Log(0, "shutdown complete") | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // UpdateKeys -- updates private key and returns new publickey | ||||
| func UpdateKeys(nodeCfg *config.ClientConfig, client mqtt.Client) error { | ||||
| 	ncutils.Log("received message to update wireguard keys for network " + nodeCfg.Network) | ||||
| 	logger.Log(0, "received message to update wireguard keys for network ", nodeCfg.Network) | ||||
| 	key, err := wgtypes.GeneratePrivateKey() | ||||
| 	if err != nil { | ||||
| 		ncutils.Log("error generating privatekey " + err.Error()) | ||||
| 		logger.Log(0, "error generating privatekey ", err.Error()) | ||||
| 		return err | ||||
| 	} | ||||
| 	file := ncutils.GetNetclientPathSpecific() + nodeCfg.Node.Interface + ".conf" | ||||
| 	if err := wireguard.UpdatePrivateKey(file, key.String()); err != nil { | ||||
| 		ncutils.Log("error updating wireguard key " + err.Error()) | ||||
| 		logger.Log(0, "error updating wireguard key ", err.Error()) | ||||
| 		return err | ||||
| 	} | ||||
| 	if storeErr := wireguard.StorePrivKey(key.String(), nodeCfg.Network); storeErr != nil { | ||||
| 		ncutils.Log("failed to save private key" + storeErr.Error()) | ||||
| 		logger.Log(0, "failed to save private key", storeErr.Error()) | ||||
| 		return storeErr | ||||
| 	} | ||||
|  | ||||
| @@ -129,25 +130,25 @@ func PingServer(commsCfg *config.ClientConfig) error { | ||||
| func setSubscriptions(client mqtt.Client, nodeCfg *config.ClientConfig) { | ||||
| 	if nodeCfg.DebugOn { | ||||
| 		if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil { | ||||
| 			ncutils.Log(token.Error().Error()) | ||||
| 			logger.Log(0, token.Error().Error()) | ||||
| 			return | ||||
| 		} | ||||
| 		ncutils.Log("subscribed to all topics for debugging purposes") | ||||
| 		logger.Log(0, "subscribed to all topics for debugging purposes") | ||||
| 	} | ||||
|  | ||||
| 	if token := client.Subscribe(fmt.Sprintf("update/%s/%s", nodeCfg.Node.Network, nodeCfg.Node.ID), 0, mqtt.MessageHandler(NodeUpdate)); token.Wait() && token.Error() != nil { | ||||
| 		ncutils.Log(token.Error().Error()) | ||||
| 		logger.Log(0, token.Error().Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	if nodeCfg.DebugOn { | ||||
| 		ncutils.Log(fmt.Sprintf("subscribed to node updates for node %s update/%s/%s", nodeCfg.Node.Name, nodeCfg.Node.Network, nodeCfg.Node.ID)) | ||||
| 		logger.Log(0, fmt.Sprintf("subscribed to node updates for node %s update/%s/%s", nodeCfg.Node.Name, nodeCfg.Node.Network, nodeCfg.Node.ID)) | ||||
| 	} | ||||
| 	if token := client.Subscribe(fmt.Sprintf("peers/%s/%s", nodeCfg.Node.Network, nodeCfg.Node.ID), 0, mqtt.MessageHandler(UpdatePeers)); token.Wait() && token.Error() != nil { | ||||
| 		ncutils.Log(token.Error().Error()) | ||||
| 		logger.Log(0, token.Error().Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	if nodeCfg.DebugOn { | ||||
| 		ncutils.Log(fmt.Sprintf("subscribed to peer updates for node %s peers/%s/%s", nodeCfg.Node.Name, nodeCfg.Node.Network, nodeCfg.Node.ID)) | ||||
| 		logger.Log(0, fmt.Sprintf("subscribed to peer updates for node %s peers/%s/%s", nodeCfg.Node.Name, nodeCfg.Node.Network, nodeCfg.Node.ID)) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -157,15 +158,15 @@ func unsubscribeNode(client mqtt.Client, nodeCfg *config.ClientConfig) { | ||||
| 	client.Unsubscribe(fmt.Sprintf("update/%s/%s", nodeCfg.Node.Network, nodeCfg.Node.ID)) | ||||
| 	var ok = true | ||||
| 	if token := client.Unsubscribe(fmt.Sprintf("update/%s/%s", nodeCfg.Node.Network, nodeCfg.Node.ID)); token.Wait() && token.Error() != nil { | ||||
| 		ncutils.PrintLog("unable to unsubscribe from updates for node "+nodeCfg.Node.Name+"\n"+token.Error().Error(), 1) | ||||
| 		logger.Log(1, "unable to unsubscribe from updates for node ", nodeCfg.Node.Name, "\n", token.Error().Error()) | ||||
| 		ok = false | ||||
| 	} | ||||
| 	if token := client.Unsubscribe(fmt.Sprintf("peers/%s/%s", nodeCfg.Node.Network, nodeCfg.Node.ID)); token.Wait() && token.Error() != nil { | ||||
| 		ncutils.PrintLog("unable to unsubscribe from peer updates for node "+nodeCfg.Node.Name+"\n"+token.Error().Error(), 1) | ||||
| 		logger.Log(1, "unable to unsubscribe from peer updates for node ", nodeCfg.Node.Name, "\n", token.Error().Error()) | ||||
| 		ok = false | ||||
| 	} | ||||
| 	if ok { | ||||
| 		ncutils.PrintLog("successfully unsubscribed node "+nodeCfg.Node.ID+" : "+nodeCfg.Node.Name, 0) | ||||
| 		logger.Log(1, "successfully unsubscribed node ", nodeCfg.Node.ID, " : ", nodeCfg.Node.Name) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -175,11 +176,11 @@ func messageQueue(ctx context.Context, commsNet string) { | ||||
| 	var commsCfg config.ClientConfig | ||||
| 	commsCfg.Network = commsNet | ||||
| 	commsCfg.ReadConfig() | ||||
| 	ncutils.Log("netclient daemon started for network: " + commsNet) | ||||
| 	logger.Log(0, "netclient daemon started for network: ", commsNet) | ||||
| 	client := setupMQTT(&commsCfg, false) | ||||
| 	defer client.Disconnect(250) | ||||
| 	<-ctx.Done() | ||||
| 	ncutils.Log("shutting down daemon for comms network " + commsNet) | ||||
| 	logger.Log(0, "shutting down daemon for comms network ", commsNet) | ||||
| } | ||||
|  | ||||
| // setupMQTT creates a connection to broker and return client | ||||
| @@ -199,7 +200,7 @@ func setupMQTT(commsCfg *config.ClientConfig, publish bool) mqtt.Client { | ||||
| 		if !publish { | ||||
| 			networks, err := ncutils.GetSystemNetworks() | ||||
| 			if err != nil { | ||||
| 				ncutils.Log("error retriving networks " + err.Error()) | ||||
| 				logger.Log(0, "error retriving networks ", err.Error()) | ||||
| 			} | ||||
| 			for _, network := range networks { | ||||
| 				var currNodeCfg config.ClientConfig | ||||
| @@ -212,13 +213,13 @@ func setupMQTT(commsCfg *config.ClientConfig, publish bool) mqtt.Client { | ||||
| 	opts.SetOrderMatters(true) | ||||
| 	opts.SetResumeSubs(true) | ||||
| 	opts.SetConnectionLostHandler(func(c mqtt.Client, e error) { | ||||
| 		ncutils.Log("detected broker connection lost, running pull for " + commsCfg.Node.Network) | ||||
| 		logger.Log(0, "detected broker connection lost, running pull for ", commsCfg.Node.Network) | ||||
| 		_, err := Pull(commsCfg.Node.Network, true) | ||||
| 		if err != nil { | ||||
| 			ncutils.Log("could not run pull, server unreachable: " + err.Error()) | ||||
| 			ncutils.Log("waiting to retry...") | ||||
| 			logger.Log(0, "could not run pull, server unreachable: ", err.Error()) | ||||
| 			logger.Log(0, "waiting to retry...") | ||||
| 		} | ||||
| 		ncutils.Log("connection re-established with mqtt server") | ||||
| 		logger.Log(0, "connection re-established with mqtt server") | ||||
| 	}) | ||||
|  | ||||
| 	client := mqtt.NewClient(opts) | ||||
| @@ -226,20 +227,20 @@ func setupMQTT(commsCfg *config.ClientConfig, publish bool) mqtt.Client { | ||||
| 	for { | ||||
| 		//if after 12 seconds, try a gRPC pull on the last try | ||||
| 		if time.Now().After(tperiod) { | ||||
| 			ncutils.Log("running pull for " + commsCfg.Node.Network) | ||||
| 			logger.Log(0, "running pull for ", commsCfg.Node.Network) | ||||
| 			_, err := Pull(commsCfg.Node.Network, true) | ||||
| 			if err != nil { | ||||
| 				ncutils.Log("could not run pull, exiting " + commsCfg.Node.Network + " setup: " + err.Error()) | ||||
| 				logger.Log(0, "could not run pull, exiting ", commsCfg.Node.Network, " setup: ", err.Error()) | ||||
| 				return client | ||||
| 			} | ||||
| 			time.Sleep(time.Second) | ||||
| 		} | ||||
| 		if token := client.Connect(); token.Wait() && token.Error() != nil { | ||||
| 			ncutils.Log("unable to connect to broker, retrying ...") | ||||
| 			logger.Log(0, "unable to connect to broker, retrying ...") | ||||
| 			if time.Now().After(tperiod) { | ||||
| 				ncutils.Log("could not connect to broker, exiting " + commsCfg.Node.Network + " setup: " + token.Error().Error()) | ||||
| 				logger.Log(0, "could not connect to broker, exiting ", commsCfg.Node.Network, " setup: ", token.Error().Error()) | ||||
| 				if strings.Contains(token.Error().Error(), "connectex") || strings.Contains(token.Error().Error(), "i/o timeout") { | ||||
| 					ncutils.PrintLog("connection issue detected.. pulling and restarting daemon", 0) | ||||
| 					logger.Log(0, "connection issue detected.. pulling and restarting daemon") | ||||
| 					Pull(commsCfg.Node.Network, true) | ||||
| 					daemon.Restart() | ||||
| 				} | ||||
| @@ -262,11 +263,11 @@ func publishSignal(commsCfg, nodeCfg *config.ClientConfig, signal byte) error { | ||||
| } | ||||
|  | ||||
| func initialPull(network string) { | ||||
| 	ncutils.Log("pulling latest config for " + network) | ||||
| 	logger.Log(0, "pulling latest config for ", network) | ||||
| 	var configPath = fmt.Sprintf("%snetconfig-%s", ncutils.GetNetclientPathSpecific(), network) | ||||
| 	fileInfo, err := os.Stat(configPath) | ||||
| 	if err != nil { | ||||
| 		ncutils.Log("could not stat config file: " + configPath) | ||||
| 		logger.Log(0, "could not stat config file: ", configPath) | ||||
| 		return | ||||
| 	} | ||||
| 	// speed up UDP rest | ||||
| @@ -280,8 +281,8 @@ func initialPull(network string) { | ||||
| 			if sleepTime > 3600 { | ||||
| 				sleepTime = 3600 | ||||
| 			} | ||||
| 			ncutils.Log("failed to pull for network " + network) | ||||
| 			ncutils.Log(fmt.Sprintf("waiting %d seconds to retry...", sleepTime)) | ||||
| 			logger.Log(0, "failed to pull for network ", network) | ||||
| 			logger.Log(0, fmt.Sprintf("waiting %d seconds to retry...", sleepTime)) | ||||
| 			time.Sleep(time.Second * time.Duration(sleepTime)) | ||||
| 			sleepTime = sleepTime * 2 | ||||
| 		} | ||||
|   | ||||
| @@ -10,6 +10,7 @@ import ( | ||||
| 	"runtime" | ||||
|  | ||||
| 	nodepb "github.com/gravitl/netmaker/grpc" | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/auth" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| @@ -72,7 +73,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro | ||||
| 		if err == nil { | ||||
| 			cfg.Node.LocalAddress = intIP | ||||
| 		} else { | ||||
| 			ncutils.PrintLog("error retrieving private address: "+err.Error(), 1) | ||||
| 			logger.Log(1, "error retrieving private address: ", err.Error()) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -84,7 +85,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro | ||||
| 			cfg.Node.Endpoint, err = ncutils.GetPublicIP() | ||||
| 		} | ||||
| 		if err != nil || cfg.Node.Endpoint == "" { | ||||
| 			ncutils.Log("Error setting cfg.Node.Endpoint.") | ||||
| 			logger.Log(0, "Error setting cfg.Node.Endpoint.") | ||||
| 			return err | ||||
| 		} | ||||
| 	} | ||||
| @@ -111,8 +112,8 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro | ||||
| 	//	if ncutils.IsLinux() { | ||||
| 	//		_, err := exec.LookPath("resolvectl") | ||||
| 	//		if err != nil { | ||||
| 	//			ncutils.PrintLog("resolvectl not present", 2) | ||||
| 	//			ncutils.PrintLog("unable to configure DNS automatically, disabling automated DNS management", 2) | ||||
| 	//			logger.Log("resolvectl not present", 2) | ||||
| 	//			logger.Log("unable to configure DNS automatically, disabling automated DNS management", 2) | ||||
| 	//			cfg.Node.DNSOn = "no" | ||||
| 	//		} | ||||
| 	//	} | ||||
| @@ -148,7 +149,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro | ||||
| 		Version:             ncutils.Version, | ||||
| 	} | ||||
|  | ||||
| 	ncutils.Log("joining " + cfg.Network + " at " + cfg.Server.GRPCAddress) | ||||
| 	logger.Log(0, "joining "+cfg.Network+" at "+cfg.Server.GRPCAddress) | ||||
| 	var wcclient nodepb.NodeServiceClient | ||||
|  | ||||
| 	conn, err := grpc.Dial(cfg.Server.GRPCAddress, | ||||
| @@ -178,8 +179,8 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro | ||||
| 		return err | ||||
| 	} | ||||
| 	if node.IsPending == "yes" { | ||||
| 		ncutils.Log("Node is marked as PENDING.") | ||||
| 		ncutils.Log("Awaiting approval from Admin before configuring WireGuard.") | ||||
| 		logger.Log(0, "Node is marked as PENDING.") | ||||
| 		logger.Log(0, "Awaiting approval from Admin before configuring WireGuard.") | ||||
| 		if cfg.Daemon != "off" { | ||||
| 			return daemon.InstallDaemon(cfg) | ||||
| 		} | ||||
| @@ -199,7 +200,7 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	ncutils.PrintLog("node created on remote server...updating configs", 1) | ||||
| 	logger.Log(1, "node created on remote server...updating configs") | ||||
|  | ||||
| 	// keep track of the old listenport value | ||||
| 	oldListenPort := node.ListenPort | ||||
| @@ -219,17 +220,17 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string, iscomms bool) erro | ||||
| 	} | ||||
| 	// attempt to make backup | ||||
| 	if err = config.SaveBackup(node.Network); err != nil { | ||||
| 		ncutils.Log("failed to make backup, node will not auto restore if config is corrupted") | ||||
| 		logger.Log(0, "failed to make backup, node will not auto restore if config is corrupted") | ||||
| 	} | ||||
|  | ||||
| 	ncutils.Log("retrieving peers") | ||||
| 	logger.Log(0, "retrieving peers") | ||||
| 	peers, hasGateway, gateways, err := server.GetPeers(node.MacAddress, cfg.Network, cfg.Server.GRPCAddress, node.IsDualStack == "yes", node.IsIngressGateway == "yes", node.IsServer == "yes") | ||||
| 	if err != nil && !ncutils.IsEmptyRecord(err) { | ||||
| 		ncutils.Log("failed to retrieve peers") | ||||
| 		logger.Log(0, "failed to retrieve peers") | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	ncutils.Log("starting wireguard") | ||||
| 	logger.Log(0, "starting wireguard") | ||||
| 	err = wireguard.InitWireguard(&node, privateKey, peers, hasGateway, gateways, false) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| @@ -273,8 +274,8 @@ func formatName(node models.Node) string { | ||||
| 		node.Name = ncutils.ShortenString(node.Name, models.MAX_NAME_LENGTH) | ||||
| 	} | ||||
| 	if !node.NameInNodeCharSet() || len(node.Name) > models.MAX_NAME_LENGTH { | ||||
| 		ncutils.PrintLog("could not properly format name: "+node.Name, 1) | ||||
| 		ncutils.PrintLog("setting name to blank", 1) | ||||
| 		logger.Log(1, "could not properly format name: "+node.Name) | ||||
| 		logger.Log(1, "setting name to blank") | ||||
| 		node.Name = "" | ||||
| 	} | ||||
| 	return node.Name | ||||
| @@ -290,7 +291,7 @@ func setListenPort(oldListenPort int32, cfg *config.ClientConfig) { | ||||
| 		cfg.Node.ListenPort, errN = ncutils.GetFreePort(cfg.Node.ListenPort) | ||||
| 		if errN != nil { | ||||
| 			cfg.Node.ListenPort = newListenPort | ||||
| 			ncutils.PrintLog("Error retrieving port: "+errN.Error(), 1) | ||||
| 			logger.Log(1, "Error retrieving port: ", errN.Error()) | ||||
| 		} | ||||
|  | ||||
| 		// if newListenPort has been modified to find an available port, publish to server | ||||
|   | ||||
| @@ -5,6 +5,7 @@ import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	nodepb "github.com/gravitl/netmaker/grpc" | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/auth" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| @@ -43,7 +44,7 @@ func List(network string) error { | ||||
| 	for _, network := range networks { | ||||
| 		net, err := getNetwork(network) | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog(network+": Could not retrieve network configuration.", 1) | ||||
| 			logger.Log(1, network+": Could not retrieve network configuration.") | ||||
| 			return err | ||||
| 		} | ||||
| 		nets = append(nets, net) | ||||
|   | ||||
| @@ -2,12 +2,12 @@ package functions | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"runtime" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	mqtt "github.com/eclipse/paho.mqtt.golang" | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| 	"github.com/gravitl/netmaker/netclient/local" | ||||
| @@ -21,9 +21,9 @@ import ( | ||||
|  | ||||
| // All -- mqtt message hander for all ('#') topics | ||||
| var All mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { | ||||
| 	ncutils.Log("default message handler -- received message but not handling") | ||||
| 	ncutils.Log("Topic: " + string(msg.Topic())) | ||||
| 	//ncutils.Log("Message: " + string(msg.Payload())) | ||||
| 	logger.Log(0, "default message handler -- received message but not handling") | ||||
| 	logger.Log(0, "Topic: "+string(msg.Topic())) | ||||
| 	//logger.Log(0, "Message: " + string(msg.Payload())) | ||||
| } | ||||
|  | ||||
| // NodeUpdate -- mqtt message handler for /update/<NodeID> topic | ||||
| @@ -41,11 +41,11 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) { | ||||
| 	} | ||||
| 	err := json.Unmarshal([]byte(data), &newNode) | ||||
| 	if err != nil { | ||||
| 		ncutils.Log("error unmarshalling node update data" + err.Error()) | ||||
| 		logger.Log(0, "error unmarshalling node update data"+err.Error()) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	ncutils.Log("received message to update node " + newNode.Name) | ||||
| 	logger.Log(0, "received message to update node "+newNode.Name) | ||||
| 	// see if cache hit, if so skip | ||||
| 	var currentMessage = read(newNode.Network, lastNodeUpdate) | ||||
| 	if currentMessage == string(data) { | ||||
| @@ -64,15 +64,15 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) { | ||||
| 	nodeCfg.Node = newNode | ||||
| 	switch newNode.Action { | ||||
| 	case models.NODE_DELETE: | ||||
| 		ncutils.PrintLog(fmt.Sprintf("received delete request for %s", nodeCfg.Node.Name), 0) | ||||
| 		logger.Log(0, "received delete request for %s", nodeCfg.Node.Name) | ||||
| 		unsubscribeNode(client, &nodeCfg) | ||||
| 		if err = LeaveNetwork(nodeCfg.Node.Network, true); err != nil { | ||||
| 			if !strings.Contains("rpc error", err.Error()) { | ||||
| 				ncutils.PrintLog(fmt.Sprintf("failed to leave, please check that local files for network %s were removed", nodeCfg.Node.Network), 0) | ||||
| 				logger.Log(0, "failed to leave, please check that local files for network", nodeCfg.Node.Network, "were removed") | ||||
| 				return | ||||
| 			} | ||||
| 		} | ||||
| 		ncutils.PrintLog(fmt.Sprintf("%s was removed", nodeCfg.Node.Name), 0) | ||||
| 		logger.Log(0, nodeCfg.Node.Name, " was removed") | ||||
| 		return | ||||
| 	case models.NODE_UPDATE_KEY: | ||||
| 		// == get the current key for node == | ||||
| @@ -81,7 +81,7 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) { | ||||
| 			break | ||||
| 		} | ||||
| 		if err := UpdateKeys(&nodeCfg, client); err != nil { | ||||
| 			ncutils.PrintLog("err updating wireguard keys, reusing last key\n"+err.Error(), 0) | ||||
| 			logger.Log(0, "err updating wireguard keys, reusing last key\n", err.Error()) | ||||
| 			if key, parseErr := wgtypes.ParseKey(oldPrivateKey); parseErr == nil { | ||||
| 				wireguard.StorePrivKey(key.String(), nodeCfg.Network) | ||||
| 				nodeCfg.Node.PublicKey = key.PublicKey().String() | ||||
| @@ -94,31 +94,31 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) { | ||||
| 	// Save new config | ||||
| 	nodeCfg.Node.Action = models.NODE_NOOP | ||||
| 	if err := config.Write(&nodeCfg, nodeCfg.Network); err != nil { | ||||
| 		ncutils.PrintLog("error updating node configuration: "+err.Error(), 0) | ||||
| 		logger.Log(0, "error updating node configuration: ", err.Error()) | ||||
| 	} | ||||
| 	nameserver := nodeCfg.Server.CoreDNSAddr | ||||
| 	privateKey, err := wireguard.RetrievePrivKey(newNode.Network) | ||||
| 	if err != nil { | ||||
| 		ncutils.Log("error reading PrivateKey " + err.Error()) | ||||
| 		logger.Log(0, "error reading PrivateKey "+err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	file := ncutils.GetNetclientPathSpecific() + nodeCfg.Node.Interface + ".conf" | ||||
|  | ||||
| 	if err := wireguard.UpdateWgInterface(file, privateKey, nameserver, newNode); err != nil { | ||||
| 		ncutils.Log("error updating wireguard config " + err.Error()) | ||||
| 		logger.Log(0, "error updating wireguard config "+err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	if keepaliveChange { | ||||
| 		wireguard.UpdateKeepAlive(file, newNode.PersistentKeepalive) | ||||
| 	} | ||||
| 	if ifaceDelta { // if a change caused an ifacedelta we need to notify the server to update the peers | ||||
| 		ncutils.Log("applying WG conf to " + file) | ||||
| 		logger.Log(0, "applying WG conf to "+file) | ||||
| 		if ncutils.IsWindows() { | ||||
| 			wireguard.RemoveConfGraceful(nodeCfg.Node.Interface) | ||||
| 		} | ||||
| 		err = wireguard.ApplyConf(&nodeCfg.Node, nodeCfg.Node.Interface, file) | ||||
| 		if err != nil { | ||||
| 			ncutils.Log("error restarting wg after node update " + err.Error()) | ||||
| 			logger.Log(0, "error restarting wg after node update "+err.Error()) | ||||
| 			return | ||||
| 		} | ||||
|  | ||||
| @@ -133,27 +133,27 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) { | ||||
| 		//	} | ||||
| 		doneErr := publishSignal(&commsCfg, &nodeCfg, ncutils.DONE) | ||||
| 		if doneErr != nil { | ||||
| 			ncutils.Log("could not notify server to update peers after interface change") | ||||
| 			logger.Log(0, "could not notify server to update peers after interface change") | ||||
| 		} else { | ||||
| 			ncutils.Log("signalled finished interface update to server") | ||||
| 			logger.Log(0, "signalled finished interface update to server") | ||||
| 		} | ||||
| 	} else if hubChange { | ||||
| 		doneErr := publishSignal(&commsCfg, &nodeCfg, ncutils.DONE) | ||||
| 		if doneErr != nil { | ||||
| 			ncutils.Log("could not notify server to update peers after hub change") | ||||
| 			logger.Log(0, "could not notify server to update peers after hub change") | ||||
| 		} else { | ||||
| 			ncutils.Log("signalled finished hub update to server") | ||||
| 			logger.Log(0, "signalled finished hub update to server") | ||||
| 		} | ||||
| 	} | ||||
| 	//deal with DNS | ||||
| 	if newNode.DNSOn != "yes" && shouldDNSChange && nodeCfg.Node.Interface != "" { | ||||
| 		ncutils.Log("settng DNS off") | ||||
| 		if err := removeHostDNS(nodeCfg.Node.Interface, ncutils.IsWindows()); err != nil { | ||||
| 			ncutils.Log("error removing netmaker profile from /etc/hosts " + err.Error()) | ||||
| 		logger.Log(0, "settng DNS off") | ||||
| 		if err := removeHostDNS(nodeCfg.Network, ncutils.IsWindows()); err != nil { | ||||
| 			logger.Log(0, "error removing netmaker profile from /etc/hosts "+err.Error()) | ||||
| 		} | ||||
| 		//		_, err := ncutils.RunCmd("/usr/bin/resolvectl revert "+nodeCfg.Node.Interface, true) | ||||
| 		//		if err != nil { | ||||
| 		//			ncutils.Log("error applying dns" + err.Error()) | ||||
| 		//			logger.Log(0, "error applying dns" + err.Error()) | ||||
| 		//		} | ||||
| 	} | ||||
| } | ||||
| @@ -172,7 +172,7 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) { | ||||
| 	} | ||||
| 	err := json.Unmarshal([]byte(data), &peerUpdate) | ||||
| 	if err != nil { | ||||
| 		ncutils.Log("error unmarshalling peer data") | ||||
| 		logger.Log(0, "error unmarshalling peer data") | ||||
| 		return | ||||
| 	} | ||||
| 	// see if cached hit, if so skip | ||||
| @@ -185,7 +185,7 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) { | ||||
| 	file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf" | ||||
| 	err = wireguard.UpdateWgPeers(file, peerUpdate.Peers) | ||||
| 	if err != nil { | ||||
| 		ncutils.Log("error updating wireguard peers" + err.Error()) | ||||
| 		logger.Log(0, "error updating wireguard peers"+err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	//err = wireguard.SyncWGQuickConf(cfg.Node.Interface, file) | ||||
| @@ -193,28 +193,28 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) { | ||||
| 	if ncutils.IsMac() { | ||||
| 		iface, err = local.GetMacIface(cfg.Node.Address) | ||||
| 		if err != nil { | ||||
| 			ncutils.Log("error retrieving mac iface: " + err.Error()) | ||||
| 			logger.Log(0, "error retrieving mac iface: "+err.Error()) | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
| 	err = wireguard.SetPeers(iface, &cfg.Node, peerUpdate.Peers) | ||||
| 	if err != nil { | ||||
| 		ncutils.Log("error syncing wg after peer update: " + err.Error()) | ||||
| 		logger.Log(0, "error syncing wg after peer update: "+err.Error()) | ||||
| 		return | ||||
| 	} | ||||
| 	ncutils.Log("received peer update for node " + cfg.Node.Name + " " + cfg.Node.Network) | ||||
| 	logger.Log(0, "received peer update for node "+cfg.Node.Name+" "+cfg.Node.Network) | ||||
| 	//skip dns updates if this is a peer update for comms network | ||||
| 	if cfg.Node.NetworkSettings.IsComms == "yes" { | ||||
| 		return | ||||
| 	} | ||||
| 	if cfg.Node.DNSOn == "yes" { | ||||
| 		if err := setHostDNS(peerUpdate.DNS, cfg.Node.Interface, ncutils.IsWindows()); err != nil { | ||||
| 			ncutils.Log("error updating /etc/hosts " + err.Error()) | ||||
| 		if err := setHostDNS(peerUpdate.DNS, cfg.Node.Network, ncutils.IsWindows()); err != nil { | ||||
| 			logger.Log(0, "error updating /etc/hosts "+err.Error()) | ||||
| 			return | ||||
| 		} | ||||
| 	} else { | ||||
| 		if err := removeHostDNS(cfg.Node.Interface, ncutils.IsWindows()); err != nil { | ||||
| 			ncutils.Log("error removing profile from /etc/hosts " + err.Error()) | ||||
| 		if err := removeHostDNS(cfg.Node.Network, ncutils.IsWindows()); err != nil { | ||||
| 			logger.Log(0, "error removing profile from /etc/hosts "+err.Error()) | ||||
| 			return | ||||
| 		} | ||||
| 	} | ||||
|   | ||||
| @@ -7,6 +7,7 @@ import ( | ||||
| 	"sync" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/netclient/auth" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| @@ -19,11 +20,11 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup, currentComms map[string]bo | ||||
| 	for { | ||||
| 		select { | ||||
| 		case <-ctx.Done(): | ||||
| 			ncutils.Log("checkin routine closed") | ||||
| 			logger.Log(0, "checkin routine closed") | ||||
| 			return | ||||
| 			//delay should be configuraable -> use cfg.Node.NetworkSettings.DefaultCheckInInterval ?? | ||||
| 		case <-time.After(time.Second * 60): | ||||
| 			// ncutils.Log("Checkin running") | ||||
| 			// logger.Log(0, "Checkin running") | ||||
| 			//read latest config | ||||
| 			networks, err := ncutils.GetSystemNetworks() | ||||
| 			if err != nil { | ||||
| @@ -43,41 +44,41 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup, currentComms map[string]bo | ||||
| 					if nodeCfg.Node.IsStatic != "yes" { | ||||
| 						extIP, err := ncutils.GetPublicIP() | ||||
| 						if err != nil { | ||||
| 							ncutils.PrintLog("error encountered checking public ip addresses: "+err.Error(), 1) | ||||
| 							logger.Log(1, "error encountered checking public ip addresses: ", err.Error()) | ||||
| 						} | ||||
| 						if nodeCfg.Node.Endpoint != extIP && extIP != "" { | ||||
| 							ncutils.PrintLog("endpoint has changed from "+nodeCfg.Node.Endpoint+" to "+extIP, 1) | ||||
| 							logger.Log(1, "endpoint has changed from ", nodeCfg.Node.Endpoint, " to ", extIP) | ||||
| 							nodeCfg.Node.Endpoint = extIP | ||||
| 							if err := PublishNodeUpdate(&currCommsCfg, &nodeCfg); err != nil { | ||||
| 								ncutils.Log("could not publish endpoint change") | ||||
| 								logger.Log(0, "could not publish endpoint change") | ||||
| 							} | ||||
| 						} | ||||
| 						intIP, err := getPrivateAddr() | ||||
| 						if err != nil { | ||||
| 							ncutils.PrintLog("error encountered checking private ip addresses: "+err.Error(), 1) | ||||
| 							logger.Log(1, "error encountered checking private ip addresses: ", err.Error()) | ||||
| 						} | ||||
| 						if nodeCfg.Node.LocalAddress != intIP && intIP != "" { | ||||
| 							ncutils.PrintLog("local Address has changed from "+nodeCfg.Node.LocalAddress+" to "+intIP, 1) | ||||
| 							logger.Log(1, "local Address has changed from ", nodeCfg.Node.LocalAddress, " to ", intIP) | ||||
| 							nodeCfg.Node.LocalAddress = intIP | ||||
| 							if err := PublishNodeUpdate(&currCommsCfg, &nodeCfg); err != nil { | ||||
| 								ncutils.Log("could not publish local address change") | ||||
| 								logger.Log(0, "could not publish local address change") | ||||
| 							} | ||||
| 						} | ||||
| 					} else if nodeCfg.Node.IsLocal == "yes" && nodeCfg.Node.LocalRange != "" { | ||||
| 						localIP, err := ncutils.GetLocalIP(nodeCfg.Node.LocalRange) | ||||
| 						if err != nil { | ||||
| 							ncutils.PrintLog("error encountered checking local ip addresses: "+err.Error(), 1) | ||||
| 							logger.Log(1, "error encountered checking local ip addresses: ", err.Error()) | ||||
| 						} | ||||
| 						if nodeCfg.Node.Endpoint != localIP && localIP != "" { | ||||
| 							ncutils.PrintLog("endpoint has changed from "+nodeCfg.Node.Endpoint+" to "+localIP, 1) | ||||
| 							logger.Log(1, "endpoint has changed from "+nodeCfg.Node.Endpoint+" to ", localIP) | ||||
| 							nodeCfg.Node.Endpoint = localIP | ||||
| 							if err := PublishNodeUpdate(&currCommsCfg, &nodeCfg); err != nil { | ||||
| 								ncutils.Log("could not publish localip change") | ||||
| 								logger.Log(0, "could not publish localip change") | ||||
| 							} | ||||
| 						} | ||||
| 					} | ||||
| 					if err := PingServer(&currCommsCfg); err != nil { | ||||
| 						ncutils.PrintLog("could not ping server on comms net, "+currCommsCfg.Network+"\n"+err.Error(), 0) | ||||
| 						logger.Log(0, "could not ping server on comms net, ", currCommsCfg.Network, "\n", err.Error()) | ||||
| 					} else { | ||||
| 						Hello(&currCommsCfg, &nodeCfg) | ||||
| 					} | ||||
| @@ -99,18 +100,18 @@ func PublishNodeUpdate(commsCfg, nodeCfg *config.ClientConfig) error { | ||||
| 	if err = publish(commsCfg, nodeCfg, fmt.Sprintf("update/%s", nodeCfg.Node.ID), data, 1); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	ncutils.PrintLog("sent a node update to server for node"+nodeCfg.Node.Name+", "+nodeCfg.Node.ID, 1) | ||||
| 	logger.Log(0, "sent a node update to server for node", nodeCfg.Node.Name, ", ", nodeCfg.Node.ID) | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // Hello -- ping the broker to let server know node it's alive and well | ||||
| func Hello(commsCfg, nodeCfg *config.ClientConfig) { | ||||
| 	if err := publish(commsCfg, nodeCfg, fmt.Sprintf("ping/%s", nodeCfg.Node.ID), []byte(ncutils.Version), 0); err != nil { | ||||
| 		ncutils.Log(fmt.Sprintf("error publishing ping, %v", err)) | ||||
| 		ncutils.Log("running pull on " + commsCfg.Node.Network + " to reconnect") | ||||
| 		logger.Log(0, fmt.Sprintf("error publishing ping, %v", err)) | ||||
| 		logger.Log(0, "running pull on "+commsCfg.Node.Network+" to reconnect") | ||||
| 		_, err := Pull(commsCfg.Node.Network, true) | ||||
| 		if err != nil { | ||||
| 			ncutils.Log("could not run pull on " + commsCfg.Node.Network + ", error: " + err.Error()) | ||||
| 			logger.Log(0, "could not run pull on "+commsCfg.Node.Network+", error: "+err.Error()) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -8,6 +8,7 @@ import ( | ||||
| 	"runtime" | ||||
|  | ||||
| 	nodepb "github.com/gravitl/netmaker/grpc" | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/auth" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| @@ -43,7 +44,7 @@ func Pull(network string, manual bool) (*models.Node, error) { | ||||
| 		conn, err := grpc.Dial(cfg.Server.GRPCAddress, | ||||
| 			ncutils.GRPCRequestOpts(cfg.Server.GRPCSSL)) | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog("Cant dial GRPC server: "+err.Error(), 1) | ||||
| 			logger.Log(1, "Cant dial GRPC server: ", err.Error()) | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		defer conn.Close() | ||||
| @@ -51,12 +52,12 @@ func Pull(network string, manual bool) (*models.Node, error) { | ||||
|  | ||||
| 		ctx, err = auth.SetJWT(wcclient, network) | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog("Failed to authenticate: "+err.Error(), 1) | ||||
| 			logger.Log(1, "Failed to authenticate: ", err.Error()) | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		data, err := json.Marshal(&node) | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog("Failed to parse node config: "+err.Error(), 1) | ||||
| 			logger.Log(1, "Failed to parse node config: ", err.Error()) | ||||
| 			return nil, err | ||||
| 		} | ||||
|  | ||||
| @@ -80,7 +81,7 @@ func Pull(network string, manual bool) (*models.Node, error) { | ||||
| 		// check for interface change | ||||
| 		if cfg.Node.Interface != resNode.Interface { | ||||
| 			if err = DeleteInterface(cfg.Node.Interface, cfg.Node.PostDown); err != nil { | ||||
| 				ncutils.PrintLog("could not delete old interface "+cfg.Node.Interface, 1) | ||||
| 				logger.Log(1, "could not delete old interface ", cfg.Node.Interface) | ||||
| 			} | ||||
| 		} | ||||
| 		if err = config.ModConfig(&resNode); err != nil { | ||||
| @@ -119,7 +120,7 @@ func Pull(network string, manual bool) (*models.Node, error) { | ||||
| 	} | ||||
| 	var bkupErr = config.SaveBackup(network) | ||||
| 	if bkupErr != nil { | ||||
| 		ncutils.Log("unable to update backup file") | ||||
| 		logger.Log(0, "unable to update backup file") | ||||
| 	} | ||||
|  | ||||
| 	return &resNode, err | ||||
|   | ||||
| @@ -11,6 +11,7 @@ import ( | ||||
| 	"log" | ||||
| 	"os/exec" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| ) | ||||
| @@ -28,10 +29,10 @@ func SetDNSWithRetry(node models.Node, address string) bool { | ||||
| 		time.Sleep(time.Second << 1) | ||||
| 	} | ||||
| 	if !reachable { | ||||
| 		ncutils.Log("not setting dns (server unreachable), will try again later: " + address) | ||||
| 		logger.Log(0, "not setting dns (server unreachable), will try again later: "+address) | ||||
| 		return true | ||||
| 	} else if err := UpdateDNS(node.Interface, node.Network, address); err != nil { | ||||
| 		ncutils.Log("error applying dns" + err.Error()) | ||||
| 		logger.Log(0, "error applying dns"+err.Error()) | ||||
| 	} else if IsDNSWorking(node.Network, address) { | ||||
| 		return true | ||||
| 	} | ||||
|   | ||||
| @@ -3,6 +3,7 @@ package local | ||||
| import ( | ||||
| 	"net" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| 	"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||||
| ) | ||||
| @@ -20,14 +21,14 @@ func SetPeerRoutes(iface, currentNodeAddr string, oldPeers map[string][]net.IPNe | ||||
| 			for _, allowedIP := range peer.AllowedIPs { // compare new ones (if any) to old ones | ||||
| 				if !ncutils.IPNetSliceContains(currPeerAllowedIPs, allowedIP) { | ||||
| 					if err := setRoute(iface, &allowedIP, allowedIP.IP.String()); err != nil { | ||||
| 						ncutils.PrintLog(err.Error(), 1) | ||||
| 						logger.Log(1, err.Error()) | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 			for _, allowedIP := range currPeerAllowedIPs { // compare old ones (if any) to new ones | ||||
| 				if !ncutils.IPNetSliceContains(peer.AllowedIPs, allowedIP) { | ||||
| 					if err := deleteRoute(iface, &allowedIP, allowedIP.IP.String()); err != nil { | ||||
| 						ncutils.PrintLog(err.Error(), 1) | ||||
| 						logger.Log(1, err.Error()) | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| @@ -35,7 +36,7 @@ func SetPeerRoutes(iface, currentNodeAddr string, oldPeers map[string][]net.IPNe | ||||
| 		} else { | ||||
| 			for _, allowedIP := range peer.AllowedIPs { // add all routes as peer doesn't exist | ||||
| 				if err := setRoute(iface, &allowedIP, allowedIP.String()); err != nil { | ||||
| 					ncutils.PrintLog(err.Error(), 1) | ||||
| 					logger.Log(1, err.Error()) | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
|   | ||||
| @@ -20,6 +20,7 @@ import ( | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||||
| 	"google.golang.org/grpc" | ||||
| @@ -75,12 +76,6 @@ func SetVersion(ver string) { | ||||
| 	Version = ver | ||||
| } | ||||
|  | ||||
| // Log - logs a message | ||||
| func Log(message string) { | ||||
| 	log.SetFlags(log.Flags() &^ (log.Llongfile | log.Lshortfile)) | ||||
| 	log.Println("[netclient]", message) | ||||
| } | ||||
|  | ||||
| // IsWindows - checks if is windows | ||||
| func IsWindows() bool { | ||||
| 	return runtime.GOOS == "windows" | ||||
| @@ -335,7 +330,7 @@ func GetFileWithRetry(path string, retryCount int) ([]byte, error) { | ||||
| 		if err == nil { | ||||
| 			return data, err | ||||
| 		} else { | ||||
| 			PrintLog("failed to retrieve file "+path+", retrying...", 1) | ||||
| 			logger.Log(1, "failed to retrieve file ", path, ", retrying...") | ||||
| 			time.Sleep(time.Second >> 2) | ||||
| 		} | ||||
| 	} | ||||
| @@ -444,8 +439,8 @@ func RunCmds(commands []string, printerr bool) error { | ||||
| 		args := strings.Fields(command) | ||||
| 		out, err := exec.Command(args[0], args[1:]...).CombinedOutput() | ||||
| 		if err != nil && printerr { | ||||
| 			log.Println("error running command:", command) | ||||
| 			log.Println(strings.TrimSuffix(string(out), "\n")) | ||||
| 			logger.Log(0, "error running command:", command) | ||||
| 			logger.Log(0, strings.TrimSuffix(string(out), "\n")) | ||||
| 		} | ||||
| 	} | ||||
| 	return err | ||||
| @@ -461,19 +456,11 @@ func FileExists(f string) bool { | ||||
| 		return false | ||||
| 	} | ||||
| 	if err != nil { | ||||
| 		Log("error reading file: " + f + ", " + err.Error()) | ||||
| 		logger.Log(0, "error reading file: "+f+", "+err.Error()) | ||||
| 	} | ||||
| 	return !info.IsDir() | ||||
| } | ||||
|  | ||||
| // PrintLog - prints log | ||||
| func PrintLog(message string, loglevel int) { | ||||
| 	log.SetFlags(log.Flags() &^ (log.Llongfile | log.Lshortfile)) | ||||
| 	if loglevel < 2 { | ||||
| 		log.Println("[netclient]", message) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| // GetSystemNetworks - get networks locally | ||||
| func GetSystemNetworks() ([]string, error) { | ||||
| 	var networks []string | ||||
| @@ -519,7 +506,7 @@ func ShortenString(input string, length int) string { | ||||
| func DNSFormatString(input string) string { | ||||
| 	reg, err := regexp.Compile("[^a-zA-Z0-9-]+") | ||||
| 	if err != nil { | ||||
| 		Log("error with regex: " + err.Error()) | ||||
| 		logger.Log(0, "error with regex: "+err.Error()) | ||||
| 		return "" | ||||
| 	} | ||||
| 	return reg.ReplaceAllString(input, "") | ||||
| @@ -562,12 +549,12 @@ func CheckWG() { | ||||
| 	uspace := GetWireGuard() | ||||
| 	if err != nil { | ||||
| 		if uspace == "wg" { | ||||
| 			PrintLog(err.Error(), 0) | ||||
| 			logger.Log(0, err.Error()) | ||||
| 			log.Fatal("WireGuard not installed. Please install WireGuard (wireguard-tools) and try again.") | ||||
| 		} | ||||
| 		PrintLog("running with userspace wireguard: "+uspace, 0) | ||||
| 		logger.Log(0, "running with userspace wireguard: ", uspace) | ||||
| 	} else if uspace != "wg" { | ||||
| 		PrintLog("running userspace WireGuard with "+uspace, 0) | ||||
| 		logger.Log(0, "running userspace WireGuard with ", uspace) | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| package ncutils | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"os/exec" | ||||
| 	"strings" | ||||
| ) | ||||
| @@ -13,8 +13,8 @@ func RunCmd(command string, printerr bool) (string, error) { | ||||
| 	cmd.Wait() | ||||
| 	out, err := cmd.CombinedOutput() | ||||
| 	if err != nil && printerr { | ||||
| 		log.Println("error running command:", command) | ||||
| 		log.Println(strings.TrimSuffix(string(out), "\n")) | ||||
| 		logger.Log(0, "error running command:", command) | ||||
| 		logger.Log(0, strings.TrimSuffix(string(out), "\n")) | ||||
| 	} | ||||
| 	return string(out), err | ||||
| } | ||||
|   | ||||
| @@ -2,12 +2,12 @@ package ncutils | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"os/exec" | ||||
| 	"strings" | ||||
| 	"syscall" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| ) | ||||
|  | ||||
| // RunCmdFormatted - run a command formatted for freebsd | ||||
| @@ -19,8 +19,8 @@ func RunCmdFormatted(command string, printerr bool) (string, error) { | ||||
| 	cmd.Wait() | ||||
| 	out, err := cmd.CombinedOutput() | ||||
| 	if err != nil && printerr { | ||||
| 		Log(fmt.Sprintf("error running command: %s", command)) | ||||
| 		Log(strings.TrimSuffix(string(out), "\n")) | ||||
| 		logger.Log(0, "error running command: ", command) | ||||
| 		logger.Log(0, strings.TrimSuffix(string(out), "\n")) | ||||
| 	} | ||||
| 	return string(out), err | ||||
| } | ||||
| @@ -43,8 +43,8 @@ func RunCmd(command string, printerr bool) (string, error) { | ||||
| 	}() | ||||
| 	out, err := cmd.CombinedOutput() | ||||
| 	if err != nil && printerr { | ||||
| 		log.Println("error running command:", command) | ||||
| 		log.Println(strings.TrimSuffix(string(out), "\n")) | ||||
| 		logger.Log(0, "error running command:", command) | ||||
| 		logger.Log(0, strings.TrimSuffix(string(out), "\n")) | ||||
| 	} | ||||
| 	return string(out), err | ||||
| } | ||||
|   | ||||
| @@ -4,6 +4,8 @@ import ( | ||||
| 	"fmt" | ||||
| 	"os/exec" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| ) | ||||
|  | ||||
| // RunCmd - runs a local command | ||||
| @@ -13,8 +15,8 @@ func RunCmd(command string, printerr bool) (string, error) { | ||||
| 	cmd.Wait() | ||||
| 	out, err := cmd.CombinedOutput() | ||||
| 	if err != nil && printerr { | ||||
| 		Log(fmt.Sprintf("error running command: %s", command)) | ||||
| 		Log(strings.TrimSuffix(string(out), "\n")) | ||||
| 		logger.Log(0, fmt.Sprintf("error running command: %s", command)) | ||||
| 		logger.Log(0, strings.TrimSuffix(string(out), "\n")) | ||||
| 	} | ||||
| 	return string(out), err | ||||
| } | ||||
| @@ -28,5 +30,3 @@ func RunCmdFormatted(command string, printerr bool) (string, error) { | ||||
| func GetEmbedded() error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -3,11 +3,12 @@ package ncutils | ||||
| import ( | ||||
| 	"embed" | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"strings" | ||||
| 	"syscall" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| ) | ||||
|  | ||||
| //go:embed windowsdaemon/winsw.exe | ||||
| @@ -21,8 +22,8 @@ func RunCmd(command string, printerr bool) (string, error) { | ||||
| 	//cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: "/C \"" + command + "\""} | ||||
| 	out, err := cmd.CombinedOutput() | ||||
| 	if err != nil && printerr { | ||||
| 		log.Println("error running command:", command) | ||||
| 		log.Println(strings.TrimSuffix(string(out), "\n")) | ||||
| 		logger.Log(0, "error running command:", command) | ||||
| 		logger.Log(0, strings.TrimSuffix(string(out), "\n")) | ||||
| 	} | ||||
| 	return string(out), err | ||||
| } | ||||
| @@ -38,8 +39,8 @@ func RunCmdFormatted(command string, printerr bool) (string, error) { | ||||
| 	cmd.Wait() | ||||
| 	out, err := cmd.CombinedOutput() | ||||
| 	if err != nil && printerr { | ||||
| 		log.Println("error running command:", command) | ||||
| 		log.Println(strings.TrimSuffix(string(out), "\n")) | ||||
| 		logger.Log(0, "error running command:", command) | ||||
| 		logger.Log(0, strings.TrimSuffix(string(out), "\n")) | ||||
| 	} | ||||
| 	return string(out), err | ||||
| } | ||||
| @@ -53,7 +54,7 @@ func GetEmbedded() error { | ||||
| 	fileName := fmt.Sprintf("%swinsw.exe", GetNetclientPathSpecific()) | ||||
| 	err = os.WriteFile(fileName, data, 0700) | ||||
| 	if err != nil { | ||||
| 		Log("could not mount winsw.exe") | ||||
| 		logger.Log(0, "could not mount winsw.exe") | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
|   | ||||
| @@ -1,11 +1,13 @@ | ||||
| package ncutils | ||||
|  | ||||
| import ( | ||||
| 	"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||||
| 	"net" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"golang.zx2c4.com/wireguard/wgctrl/wgtypes" | ||||
| ) | ||||
|  | ||||
| func GetPeers(iface string) ([]wgtypes.Peer, error) { | ||||
| @@ -22,7 +24,7 @@ func GetPeers(iface string) ([]wgtypes.Peer, error) { | ||||
| 		var allowedIPs []net.IPNet | ||||
| 		fields := strings.Fields(line) | ||||
| 		if len(fields) < 4 { | ||||
| 			Log("error parsing peer: " + line) | ||||
| 			logger.Log(0, "error parsing peer: "+line) | ||||
| 			continue | ||||
| 		} | ||||
| 		pubkeystring := fields[0] | ||||
| @@ -36,7 +38,7 @@ func GetPeers(iface string) ([]wgtypes.Peer, error) { | ||||
|  | ||||
| 		pubkey, err := wgtypes.ParseKey(pubkeystring) | ||||
| 		if err != nil { | ||||
| 			Log("error parsing peer key " + pubkeystring) | ||||
| 			logger.Log(0, "error parsing peer key "+pubkeystring) | ||||
| 			continue | ||||
| 		} | ||||
| 		ipstrings := strings.Split(allowedipstring, ",") | ||||
| @@ -53,22 +55,22 @@ func GetPeers(iface string) ([]wgtypes.Peer, error) { | ||||
| 			} | ||||
| 		} | ||||
| 		if len(allowedIPs) == 0 { | ||||
| 			Log("error parsing peer " + pubkeystring + ", no allowedips found") | ||||
| 			logger.Log(0, "error parsing peer "+pubkeystring+", no allowedips found") | ||||
| 			continue | ||||
| 		} | ||||
| 		var endpointarr []string | ||||
| 		var endpointip net.IP | ||||
| 		if endpointarr = strings.Split(endpointstring, ":"); len(endpointarr) != 2 { | ||||
| 			Log("error parsing peer " + pubkeystring + ", could not parse endpoint: " + endpointstring) | ||||
| 			logger.Log(0, "error parsing peer "+pubkeystring+", could not parse endpoint: "+endpointstring) | ||||
| 			continue | ||||
| 		} | ||||
| 		if endpointip = net.ParseIP(endpointarr[0]); endpointip == nil { | ||||
| 			Log("error parsing peer " + pubkeystring + ", could not parse endpoint: " + endpointarr[0]) | ||||
| 			logger.Log(0, "error parsing peer "+pubkeystring+", could not parse endpoint: "+endpointarr[0]) | ||||
| 			continue | ||||
| 		} | ||||
| 		var port int | ||||
| 		if port, err = strconv.Atoi(endpointarr[1]); err != nil { | ||||
| 			Log("error parsing peer " + pubkeystring + ", could not parse port: " + err.Error()) | ||||
| 			logger.Log(0, "error parsing peer "+pubkeystring+", could not parse port: "+err.Error()) | ||||
| 			continue | ||||
| 		} | ||||
| 		var endpoint = net.UDPAddr{ | ||||
| @@ -78,7 +80,7 @@ func GetPeers(iface string) ([]wgtypes.Peer, error) { | ||||
| 		var dur time.Duration | ||||
| 		if pkeepalivestring != "" { | ||||
| 			if dur, err = time.ParseDuration(pkeepalivestring + "s"); err != nil { | ||||
| 				Log("error parsing peer " + pubkeystring + ", could not parse keepalive: " + err.Error()) | ||||
| 				logger.Log(0, "error parsing peer "+pubkeystring+", could not parse keepalive: "+err.Error()) | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
|   | ||||
| @@ -3,6 +3,8 @@ package ncutils | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| ) | ||||
|  | ||||
| // BackOff - back off any function while there is an error | ||||
| @@ -18,7 +20,7 @@ func BackOff(isExponential bool, maxTime int, f interface{}) (interface{}, error | ||||
| 		if isExponential { | ||||
| 			sleepTime = sleepTime << 1 | ||||
| 		} | ||||
| 		PrintLog("retrying...", 1) | ||||
| 		logger.Log(1, "retrying...") | ||||
| 	} | ||||
| 	return nil, fmt.Errorf("could not find result") | ||||
| } | ||||
|   | ||||
| @@ -4,6 +4,7 @@ import ( | ||||
| 	"log" | ||||
| 	"os" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| ) | ||||
|  | ||||
| @@ -25,7 +26,7 @@ func InitWindows() { | ||||
| 	_, currentNetclientErr := os.Stat(currentPath) | ||||
|  | ||||
| 	if currentPath == dataPath && currentNetclientErr == nil { | ||||
| 		ncutils.Log("netclient.exe is in proper location, " + currentPath) | ||||
| 		logger.Log(0, "netclient.exe is in proper location, "+currentPath) | ||||
| 	} else if os.IsNotExist(dataNetclientErr) { // check and see if netclient.exe is in appdata | ||||
| 		if currentNetclientErr == nil { // copy it if it exists locally | ||||
| 			input, err := os.ReadFile(currentPath) | ||||
|   | ||||
| @@ -9,6 +9,7 @@ import ( | ||||
| 	"time" | ||||
|  | ||||
| 	nodepb "github.com/gravitl/netmaker/grpc" | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/auth" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| @@ -103,7 +104,7 @@ func GetPeers(macaddress string, network string, server string, dualstack bool, | ||||
|  | ||||
| 		nodeData, err := json.Marshal(&nodecfg) | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog("could not parse node data from config during peer fetch for network "+network, 1) | ||||
| 			logger.Log(1, "could not parse node data from config during peer fetch for network ", network) | ||||
| 			return peers, hasGateway, gateways, err | ||||
| 		} | ||||
|  | ||||
| @@ -185,16 +186,16 @@ func GetPeers(macaddress string, network string, server string, dualstack bool, | ||||
| 			for _, iprange := range ranges { // go through each cidr for egress gateway | ||||
| 				_, ipnet, err := net.ParseCIDR(iprange) // confirming it's valid cidr | ||||
| 				if err != nil { | ||||
| 					ncutils.PrintLog("could not parse gateway IP range. Not adding "+iprange, 1) | ||||
| 					logger.Log(1, "could not parse gateway IP range. Not adding ", iprange) | ||||
| 					continue // if can't parse CIDR | ||||
| 				} | ||||
| 				nodeEndpointArr := strings.Split(node.Endpoint, ":") // getting the public ip of node | ||||
| 				if ipnet.Contains(net.ParseIP(nodeEndpointArr[0])) { // ensuring egress gateway range does not contain public ip of node | ||||
| 					ncutils.PrintLog("egress IP range of "+iprange+" overlaps with "+node.Endpoint+", omitting", 2) | ||||
| 					logger.Log(2, "egress IP range of ", iprange, " overlaps with ", node.Endpoint, ", omitting") | ||||
| 					continue // skip adding egress range if overlaps with node's ip | ||||
| 				} | ||||
| 				if ipnet.Contains(net.ParseIP(nodecfg.LocalAddress)) { // ensuring egress gateway range does not contain public ip of node | ||||
| 					ncutils.PrintLog("egress IP range of "+iprange+" overlaps with "+nodecfg.LocalAddress+", omitting", 2) | ||||
| 					logger.Log(2, "egress IP range of ", iprange, " overlaps with ", nodecfg.LocalAddress, ", omitting") | ||||
| 					continue // skip adding egress range if overlaps with node's local ip | ||||
| 				} | ||||
| 				gateways = append(gateways, iprange) | ||||
| @@ -282,7 +283,7 @@ func GetExtPeers(macaddress string, network string, server string, dualstack boo | ||||
|  | ||||
| 		nodeData, err := json.Marshal(&nodecfg) | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog("could not parse node data from config during peer fetch for network "+network, 1) | ||||
| 			logger.Log(1, "could not parse node data from config during peer fetch for network ", network) | ||||
| 			return peers, err | ||||
| 		} | ||||
|  | ||||
|   | ||||
| @@ -9,6 +9,7 @@ import ( | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| 	"github.com/gravitl/netmaker/netclient/local" | ||||
| @@ -38,19 +39,19 @@ func SetPeers(iface string, node *models.Node, peers []wgtypes.PeerConfig) error | ||||
| 	} else { | ||||
| 		client, err := wgctrl.New() | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog("failed to start wgctrl", 0) | ||||
| 			logger.Log(0, "failed to start wgctrl") | ||||
| 			return err | ||||
| 		} | ||||
| 		defer client.Close() | ||||
| 		device, err := client.Device(iface) | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog("failed to parse interface", 0) | ||||
| 			logger.Log(0, "failed to parse interface") | ||||
| 			return err | ||||
| 		} | ||||
| 		devicePeers = device.Peers | ||||
| 	} | ||||
| 	if len(devicePeers) > 1 && len(peers) == 0 { | ||||
| 		ncutils.PrintLog("no peers pulled", 1) | ||||
| 		logger.Log(1, "no peers pulled") | ||||
| 		return err | ||||
| 	} | ||||
| 	for _, peer := range peers { | ||||
| @@ -153,7 +154,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig | ||||
| 		node.ListenPort = 0 | ||||
| 	} | ||||
| 	if err := WriteWgConfig(&modcfg.Node, key.String(), peers); err != nil { | ||||
| 		ncutils.PrintLog("error writing wg conf file: "+err.Error(), 1) | ||||
| 		logger.Log(1, "error writing wg conf file: ", err.Error()) | ||||
| 		return err | ||||
| 	} | ||||
| 	// spin up userspace / windows interface + apply the conf file | ||||
| @@ -167,8 +168,8 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig | ||||
| 	} | ||||
| 	// ensure you clear any existing interface first | ||||
| 	RemoveConfGraceful(deviceiface) | ||||
| 	ApplyConf(node, ifacename, confPath)            // Apply initially | ||||
| 	ncutils.PrintLog("waiting for interface...", 1) // ensure interface is created | ||||
| 	ApplyConf(node, ifacename, confPath)      // Apply initially | ||||
| 	logger.Log(1, "waiting for interface...") // ensure interface is created | ||||
| 	output, _ := ncutils.RunCmd("wg", false) | ||||
| 	starttime := time.Now() | ||||
| 	ifaceReady := strings.Contains(output, deviceiface) | ||||
| @@ -196,7 +197,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig | ||||
| 			return fmt.Errorf("could not reliably create interface, please check wg installation and retry") | ||||
| 		} | ||||
| 	} | ||||
| 	ncutils.PrintLog("interface ready - netclient.. ENGAGE", 1) | ||||
| 	logger.Log(1, "interface ready - netclient.. ENGAGE") | ||||
| 	if syncconf { // should never be called really. | ||||
| 		fmt.Println("why here") | ||||
| 		err = SyncWGQuickConf(ifacename, confPath) | ||||
| @@ -204,7 +205,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig | ||||
| 	if !ncutils.HasWgQuick() && ncutils.IsLinux() { | ||||
| 		err = SetPeers(ifacename, node, peers) | ||||
| 		if err != nil { | ||||
| 			ncutils.PrintLog("error setting peers: "+err.Error(), 1) | ||||
| 			logger.Log(1, "error setting peers: ", err.Error()) | ||||
| 		} | ||||
| 		time.Sleep(time.Second) | ||||
| 	} | ||||
| @@ -212,7 +213,7 @@ func InitWireguard(node *models.Node, privkey string, peers []wgtypes.PeerConfig | ||||
| 	if cidrErr == nil { | ||||
| 		local.SetCIDRRoute(ifacename, node.Address, cidr) | ||||
| 	} else { | ||||
| 		ncutils.PrintLog("could not set cidr route properly: "+cidrErr.Error(), 1) | ||||
| 		logger.Log(1, "could not set cidr route properly: ", cidrErr.Error()) | ||||
| 	} | ||||
| 	local.SetCurrentPeerRoutes(ifacename, node.Address, peers) | ||||
|  | ||||
| @@ -495,7 +496,7 @@ func RemoveConfGraceful(ifacename string) { | ||||
| 	// ensure you clear any existing interface first | ||||
| 	wgclient, err := wgctrl.New() | ||||
| 	if err != nil { | ||||
| 		ncutils.PrintLog("could not create wgclient", 0) | ||||
| 		logger.Log(0, "could not create wgclient") | ||||
| 		return | ||||
| 	} | ||||
| 	defer wgclient.Close() | ||||
|   | ||||
| @@ -8,6 +8,7 @@ import ( | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| ) | ||||
| @@ -44,13 +45,13 @@ func WgQuickUpMac(node *models.Node, iface string, confPath string) error { | ||||
| 	} | ||||
| 	realIface, err = addInterface(iface) | ||||
| 	if err != nil { | ||||
| 		ncutils.PrintLog("error creating wg interface", 1) | ||||
| 		logger.Log(1, "error creating wg interface") | ||||
| 		return err | ||||
| 	} | ||||
| 	time.Sleep(time.Second / 2) | ||||
| 	err = setConfig(realIface, confPath) | ||||
| 	if err != nil { | ||||
| 		ncutils.PrintLog("error setting config for "+realIface, 1) | ||||
| 		logger.Log(1, "error setting config for ", realIface) | ||||
| 		return err | ||||
| 	} | ||||
| 	var ips = append(node.AllowedIPs, node.Address, node.Address6) | ||||
| @@ -62,7 +63,7 @@ func WgQuickUpMac(node *models.Node, iface string, confPath string) error { | ||||
| 		if i != "" { | ||||
| 			err = addAddress(realIface, i) | ||||
| 			if err != nil { | ||||
| 				ncutils.PrintLog("error adding address "+i+" on interface "+realIface, 1) | ||||
| 				logger.Log(1, "error adding address ", i, " on interface ", realIface) | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
| @@ -70,14 +71,14 @@ func WgQuickUpMac(node *models.Node, iface string, confPath string) error { | ||||
| 	setMTU(realIface, int(node.MTU)) | ||||
| 	err = upInterface(realIface) | ||||
| 	if err != nil { | ||||
| 		ncutils.PrintLog("error turning on interface "+iface, 1) | ||||
| 		logger.Log(1, "error turning on interface ", iface) | ||||
| 		return err | ||||
| 	} | ||||
| 	for _, i := range ips { | ||||
| 		if i != "" { | ||||
| 			err = addRoute(i, realIface) | ||||
| 			if err != nil { | ||||
| 				ncutils.PrintLog("error adding route to "+realIface+" for "+i, 1) | ||||
| 				logger.Log(1, "error adding route to ", realIface, " for ", i) | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
| @@ -237,7 +238,7 @@ func SetMacPeerRoutes(realIface string) error { | ||||
| 		if i != "" { | ||||
| 			err = addRoute(i, realIface) | ||||
| 			if err != nil { | ||||
| 				ncutils.PrintLog("error adding route to "+realIface+" for "+i, 1) | ||||
| 				logger.Log(1, "error adding route to ", realIface, " for ", i) | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
|   | ||||
| @@ -7,6 +7,7 @@ import ( | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| @@ -67,11 +68,11 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e | ||||
| 	err = wgclient.ConfigureDevice(ifacename, conf) | ||||
| 	if err != nil { | ||||
| 		if os.IsNotExist(err) { | ||||
| 			ncutils.PrintLog("Could not configure device: "+err.Error(), 0) | ||||
| 			logger.Log(0, "Could not configure device: ", err.Error()) | ||||
| 		} | ||||
| 	} | ||||
| 	if _, err := ncutils.RunCmd(ipExec+" link set down dev "+ifacename, false); err != nil { | ||||
| 		ncutils.PrintLog("attempted to remove interface before editing", 1) | ||||
| 		logger.Log(1, "attempted to remove interface before editing") | ||||
| 		return err | ||||
| 	} | ||||
| 	if node.PostDown != "" { | ||||
| @@ -80,7 +81,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e | ||||
| 	} | ||||
| 	// set MTU of node interface | ||||
| 	if _, err := ncutils.RunCmd(ipExec+" link set mtu "+strconv.Itoa(int(node.MTU))+" up dev "+ifacename, true); err != nil { | ||||
| 		ncutils.PrintLog("failed to create interface with mtu "+strconv.Itoa(int(node.MTU))+"-"+ifacename, 1) | ||||
| 		logger.Log(1, "failed to create interface with mtu ", strconv.Itoa(int(node.MTU)), "-", ifacename) | ||||
| 		return err | ||||
| 	} | ||||
| 	if node.PostUp != "" { | ||||
| @@ -88,7 +89,7 @@ func ApplyWithoutWGQuick(node *models.Node, ifacename string, confPath string) e | ||||
| 		_ = ncutils.RunCmds(runcmds, true) | ||||
| 	} | ||||
| 	if node.Address6 != "" && node.IsDualStack == "yes" { | ||||
| 		ncutils.PrintLog("adding address: "+node.Address6, 1) | ||||
| 		logger.Log(1, "adding address: ", node.Address6) | ||||
| 		_, _ = ncutils.RunCmd(ipExec+" address add dev "+ifacename+" "+node.Address6+"/64", true) | ||||
| 	} | ||||
| 	return nil | ||||
| @@ -103,8 +104,8 @@ func RemoveWithoutWGQuick(ifacename string) error { | ||||
| 	out, err := ncutils.RunCmd(ipExec+" link del "+ifacename, false) | ||||
| 	dontprint := strings.Contains(out, "does not exist") || strings.Contains(out, "Cannot find device") | ||||
| 	if err != nil && !dontprint { | ||||
| 		ncutils.PrintLog("error running command: "+ipExec+" link del "+ifacename, 1) | ||||
| 		ncutils.PrintLog(out, 1) | ||||
| 		logger.Log(1, "error running command: ", ipExec, " link del ", ifacename) | ||||
| 		logger.Log(1, out) | ||||
| 	} | ||||
| 	network := strings.ReplaceAll(ifacename, "nm-", "") | ||||
| 	nodeconf, err := config.ReadConfig(network) | ||||
| @@ -114,7 +115,7 @@ func RemoveWithoutWGQuick(ifacename string) error { | ||||
| 			_ = ncutils.RunCmds(runcmds, false) | ||||
| 		} | ||||
| 	} else if err != nil { | ||||
| 		ncutils.PrintLog("error retrieving config: "+err.Error(), 1) | ||||
| 		logger.Log(1, "error retrieving config: ", err.Error()) | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
|   | ||||
| @@ -6,6 +6,7 @@ import ( | ||||
| 	"os" | ||||
| 	"regexp" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/gravitl/netmaker/netclient/config" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| @@ -58,7 +59,7 @@ func ApplyWGQuickConf(confPath string, ifacename string) error { | ||||
| 	} else { | ||||
| 		_, err := os.Stat(confPath) | ||||
| 		if err != nil { | ||||
| 			ncutils.Log(confPath + " does not exist " + err.Error()) | ||||
| 			logger.Log(0, confPath+" does not exist "+err.Error()) | ||||
| 			return err | ||||
| 		} | ||||
| 		if ncutils.IfaceExists(ifacename) { | ||||
| @@ -98,12 +99,12 @@ func SyncWGQuickConf(iface string, confPath string) error { | ||||
| 	_, err = ncutils.RunCmd("wg syncconf "+iface+" "+tmpConf, true) | ||||
| 	if err != nil { | ||||
| 		log.Println(err.Error()) | ||||
| 		ncutils.Log("error syncing conf, resetting") | ||||
| 		logger.Log(0, "error syncing conf, resetting") | ||||
| 		err = ApplyWGQuickConf(confPath, iface) | ||||
| 	} | ||||
| 	errN := os.Remove(tmpConf) | ||||
| 	if errN != nil { | ||||
| 		ncutils.Log(errN.Error()) | ||||
| 		logger.Log(0, errN.Error()) | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
|   | ||||
| @@ -3,6 +3,7 @@ package wireguard | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/logger" | ||||
| 	"github.com/gravitl/netmaker/netclient/ncutils" | ||||
| ) | ||||
|  | ||||
| @@ -14,7 +15,7 @@ func ApplyWindowsConf(confPath string) error { | ||||
| 			copyConfPath := fmt.Sprintf("%s\\%s", ncutils.WINDOWS_WG_DPAPI_PATH, pathStrings[1]) | ||||
| 			err := ncutils.Copy(confPath, copyConfPath) | ||||
| 			if err != nil { | ||||
| 				ncutils.PrintLog(err.Error(), 1) | ||||
| 				logger.Log(err.Error(), 1) | ||||
| 			} | ||||
| 		} | ||||
| 	*/ | ||||
| @@ -28,7 +29,7 @@ func ApplyWindowsConf(confPath string) error { | ||||
| // RemoveWindowsConf - removes the WireGuard configuration file on Windows and dpapi file | ||||
| func RemoveWindowsConf(ifacename string, printlog bool) error { | ||||
| 	if _, err := ncutils.RunCmd("wireguard.exe /uninstalltunnelservice "+ifacename, printlog); err != nil { | ||||
| 		ncutils.PrintLog(err.Error(), 1) | ||||
| 		logger.Log(1, err.Error()) | ||||
| 	} | ||||
| 	/* | ||||
| 		dpapipath := fmt.Sprintf("%s\\%s.conf.dpapi", ncutils.WINDOWS_WG_DPAPI_PATH, ifacename) | ||||
| @@ -36,14 +37,14 @@ func RemoveWindowsConf(ifacename string, printlog bool) error { | ||||
| 		if ncutils.FileExists(confpath) { | ||||
| 			err := os.Remove(confpath) | ||||
| 			if err != nil { | ||||
| 				ncutils.PrintLog(err.Error(), 1) | ||||
| 				logger.Log(err.Error(), 1) | ||||
| 			} | ||||
| 		} | ||||
| 		time.Sleep(time.Second >> 2) | ||||
| 		if ncutils.FileExists(dpapipath) { | ||||
| 			err := os.Remove(dpapipath) | ||||
| 			if err != nil { | ||||
| 				ncutils.PrintLog(err.Error(), 1) | ||||
| 				logger.Log(err.Error(), 1) | ||||
| 			} | ||||
| 		} | ||||
| 	*/ | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 dcarns
					dcarns