netmaker/netclient/functions/pull.go

88 lines
2.3 KiB
Go
Raw Normal View History

2022-02-18 23:15:21 +08:00
package functions
import (
"encoding/json"
"errors"
"fmt"
2022-04-26 07:28:56 +08:00
"io"
"net/http"
2022-02-18 23:15:21 +08:00
"os"
"runtime"
"github.com/gravitl/netmaker/logger"
2022-02-18 23:15:21 +08:00
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/local"
"github.com/gravitl/netmaker/netclient/ncutils"
"github.com/gravitl/netmaker/netclient/wireguard"
2022-04-26 07:33:31 +08:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2022-02-18 23:15:21 +08:00
//homedir "github.com/mitchellh/go-homedir"
)
// Pull - pulls the latest config from the server, if manual it will overwrite
2022-04-26 02:30:51 +08:00
func Pull(network string, iface bool) (*models.Node, error) {
2022-02-18 23:15:21 +08:00
cfg, err := config.ReadConfig(network)
if err != nil {
return nil, err
}
if cfg.Node.IPForwarding == "yes" && !ncutils.IsWindows() {
if err = local.SetIPForwarding(); err != nil {
return nil, err
}
}
token, err := Authenticate(cfg)
if err != nil {
return nil, err
}
url := "https://" + cfg.Server.API + "/api/nodes/" + cfg.Network + "/" + cfg.Node.ID
response, err := API("", http.MethodGet, url, token)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
2022-04-26 07:28:56 +08:00
bytes, err := io.ReadAll(response.Body)
2022-02-18 23:15:21 +08:00
if err != nil {
fmt.Println(err)
2022-02-18 23:15:21 +08:00
}
return nil, (fmt.Errorf("%s %w", string(bytes), err))
}
defer response.Body.Close()
2022-04-26 04:30:18 +08:00
var nodeGET models.NodeGet
if err := json.NewDecoder(response.Body).Decode(&nodeGET); err != nil {
return nil, fmt.Errorf("error decoding node %w", err)
2022-02-18 23:15:21 +08:00
}
2022-04-26 04:30:18 +08:00
resNode := nodeGET.Node
2022-02-18 23:15:21 +08:00
// ensure that the OS never changes
resNode.OS = runtime.GOOS
2022-04-26 07:33:31 +08:00
if nodeGET.Peers == nil {
nodeGET.Peers = []wgtypes.PeerConfig{}
}
2022-06-01 00:07:56 +08:00
if nodeGET.ServerConfig.API != "" && nodeGET.ServerConfig.MQPort != "" {
if err = config.ModServerConfig(&nodeGET.ServerConfig, resNode.Network); err != nil {
logger.Log(0, "unable to update server config: "+err.Error())
}
}
2022-04-26 02:04:36 +08:00
if iface {
2022-06-01 00:07:56 +08:00
if err = config.ModNodeConfig(&resNode); err != nil {
2022-02-18 23:15:21 +08:00
return nil, err
}
2022-04-26 04:30:18 +08:00
if err = wireguard.SetWGConfig(network, false, nodeGET.Peers[:]); err != nil {
2022-02-18 23:15:21 +08:00
return nil, err
}
} else {
2022-04-26 04:30:18 +08:00
if err = wireguard.SetWGConfig(network, true, nodeGET.Peers[:]); err != nil {
2022-02-18 23:15:21 +08:00
if errors.Is(err, os.ErrNotExist) && !ncutils.IsFreeBSD() {
2022-04-26 02:30:51 +08:00
return Pull(network, true)
2022-02-18 23:15:21 +08:00
} else {
return nil, err
}
}
}
var bkupErr = config.SaveBackup(network)
if bkupErr != nil {
logger.Log(0, "unable to update backup file")
2022-02-18 23:15:21 +08:00
}
2022-05-31 20:42:12 +08:00
2022-02-18 23:15:21 +08:00
return &resNode, err
}