diff --git a/netclient/config/config.go b/netclient/config/config.go index 5120d6d3..d7d39d2f 100644 --- a/netclient/config/config.go +++ b/netclient/config/config.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "io/ioutil" "log" "os" @@ -208,6 +209,44 @@ func ModConfig(node *models.Node) error { return err } +// ModConfig - overwrites the node inside client config on disk +func SaveBackup(network string) error { + + var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network + var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network + if FileExists(configPath) { + input, err := ioutil.ReadFile(configPath) + if err != nil { + ncutils.Log("failed to read " + configPath + " to make a backup") + return err + } + if err = ioutil.WriteFile(backupPath, input, 0644); err != nil { + ncutils.Log("failed to copy backup to " + backupPath) + return err + } + } + return nil +} + +// ReplaceWithBackup - replaces netconfig file with backup +func ReplaceWithBackup(network string) error { + var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network + var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network + if FileExists(backupPath) { + input, err := ioutil.ReadFile(backupPath) + if err != nil { + ncutils.Log("failed to read file " + backupPath + " to backup network: " + network) + return err + } + if err = ioutil.WriteFile(configPath, input, 0644); err != nil { + ncutils.Log("failed backup " + backupPath + " to " + configPath) + return err + } + } + ncutils.Log("used backup file for network: " + network) + return nil +} + // GetCLIConfig - gets the cli flags as a config func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) { var cfg ClientConfig @@ -332,7 +371,13 @@ func ReadConfig(network string) (*ClientConfig, error) { f, err := os.Open(file) if err != nil { - nofile = true + if err = ReplaceWithBackup(network); err != nil { + nofile = true + } + f, err = os.Open(file) + if err != nil { + nofile = true + } } defer f.Close() diff --git a/netclient/functions/checkin.go b/netclient/functions/checkin.go index e56788af..bcc720ac 100644 --- a/netclient/functions/checkin.go +++ b/netclient/functions/checkin.go @@ -151,10 +151,11 @@ func CheckConfig(cliconf config.ClientConfig) error { // Pull - pulls the latest config from the server, if manual it will overwrite func Pull(network string, manual bool) (*models.Node, error) { cfg, err := config.ReadConfig(network) - node := cfg.Node if err != nil { return nil, err } + + node := cfg.Node servercfg := cfg.Server if cfg.Node.IPForwarding == "yes" && !ncutils.IsWindows() { @@ -243,6 +244,10 @@ func Pull(network string, manual bool) (*models.Node, error) { if ncutils.IsLinux() { setDNS(&resNode, servercfg, &cfg.Node) } + var bkupErr = config.SaveBackup(network) + if bkupErr != nil { + ncutils.Log("unable to update backup file") + } return &resNode, err } diff --git a/netclient/functions/join.go b/netclient/functions/join.go index fe12433c..f475d75e 100644 --- a/netclient/functions/join.go +++ b/netclient/functions/join.go @@ -5,6 +5,9 @@ import ( "encoding/json" "errors" "fmt" + "log" + "os/exec" + nodepb "github.com/gravitl/netmaker/grpc" "github.com/gravitl/netmaker/models" "github.com/gravitl/netmaker/netclient/auth" @@ -16,8 +19,6 @@ import ( "github.com/gravitl/netmaker/netclient/wireguard" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "google.golang.org/grpc" - "log" - "os/exec" ) // JoinNetwork - helps a client join a network @@ -185,6 +186,10 @@ func JoinNetwork(cfg config.ClientConfig, privateKey string) error { if err != nil { return err } + // 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") + } } ncutils.Log("retrieving peers") diff --git a/netclient/netclient.syso b/netclient/windowsdata/netclient.syso similarity index 100% rename from netclient/netclient.syso rename to netclient/windowsdata/netclient.syso