mirror of
https://github.com/gravitl/netmaker.git
synced 2025-11-03 02:53:29 +08:00
replace ncutil.Log/PrintLog with logger.Log
This commit is contained in:
committed by
0xdcarns
parent
28efa45419
commit
11b1098390
@@ -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