2021-05-26 00:48:04 +08:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import (
|
2021-10-06 03:02:09 +08:00
|
|
|
"context"
|
2021-08-03 06:06:26 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2021-08-12 07:20:25 +08:00
|
|
|
"os"
|
2021-08-31 03:58:23 +08:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2021-10-06 03:02:09 +08:00
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
nodepb "github.com/gravitl/netmaker/grpc"
|
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
"github.com/gravitl/netmaker/netclient/auth"
|
|
|
|
"github.com/gravitl/netmaker/netclient/config"
|
|
|
|
"github.com/gravitl/netmaker/netclient/local"
|
2021-09-20 02:03:47 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
2021-08-03 06:06:26 +08:00
|
|
|
"github.com/gravitl/netmaker/netclient/wireguard"
|
2021-08-06 11:46:40 +08:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2021-08-03 06:06:26 +08:00
|
|
|
"google.golang.org/grpc"
|
2021-05-26 00:48:04 +08:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
//homedir "github.com/mitchellh/go-homedir"
|
|
|
|
)
|
|
|
|
|
2021-08-10 22:55:48 +08:00
|
|
|
func isDeleteError(err error) bool {
|
2021-08-10 23:59:16 +08:00
|
|
|
return err != nil && strings.Contains(err.Error(), models.NODE_DELETE)
|
2021-08-10 22:55:48 +08:00
|
|
|
}
|
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
func checkIP(node *models.Node, servercfg config.ServerConfig, cliconf config.ClientConfig, network string) bool {
|
2021-05-26 00:48:04 +08:00
|
|
|
ipchange := false
|
2021-08-03 06:06:26 +08:00
|
|
|
var err error
|
2021-08-10 02:13:19 +08:00
|
|
|
if node.Roaming == "yes" && node.IsStatic != "yes" {
|
2021-08-03 06:06:26 +08:00
|
|
|
if node.IsLocal == "no" {
|
2021-09-20 02:03:47 +08:00
|
|
|
extIP, err := ncutils.GetPublicIP()
|
2021-08-03 06:06:26 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
|
|
|
if node.Endpoint != extIP && extIP != "" {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("endpoint has changed from "+
|
|
|
|
node.Endpoint+" to "+extIP, 1)
|
|
|
|
ncutils.PrintLog("updating address", 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
node.Endpoint = extIP
|
|
|
|
ipchange = true
|
|
|
|
}
|
|
|
|
intIP, err := getPrivateAddr()
|
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
|
|
|
if node.LocalAddress != intIP && intIP != "" {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("local Address has changed from "+
|
|
|
|
node.LocalAddress+" to "+intIP, 1)
|
|
|
|
ncutils.PrintLog("updating address", 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
node.LocalAddress = intIP
|
|
|
|
ipchange = true
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-20 02:03:47 +08:00
|
|
|
localIP, err := ncutils.GetLocalIP(node.LocalRange)
|
2021-08-03 06:06:26 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error encountered checking ip addresses: "+err.Error(), 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
|
|
|
if node.Endpoint != localIP && localIP != "" {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("endpoint has changed from "+
|
|
|
|
node.Endpoint+" to "+localIP, 1)
|
|
|
|
ncutils.PrintLog("updating address", 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
node.Endpoint = localIP
|
|
|
|
node.LocalAddress = localIP
|
|
|
|
ipchange = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ipchange {
|
|
|
|
err = config.ModConfig(node)
|
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error modifying config file: "+err.Error(), 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
err = wireguard.SetWGConfig(network, false)
|
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error setting wireguard config: "+err.Error(), 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipchange && err == nil
|
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2022-01-02 23:30:07 +08:00
|
|
|
// DEPRECATED
|
|
|
|
// func setDNS(node *models.Node, servercfg config.ServerConfig, nodecfg *models.Node) {
|
|
|
|
// if nodecfg.DNSOn == "yes" {
|
|
|
|
// ifacename := node.Interface
|
|
|
|
// nameserver := servercfg.CoreDNSAddr
|
|
|
|
// network := node.Network
|
|
|
|
// local.UpdateDNS(ifacename, network, nameserver)
|
|
|
|
// }
|
|
|
|
// }
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2021-08-10 22:55:48 +08:00
|
|
|
func checkNodeActions(node *models.Node, networkName string, servercfg config.ServerConfig, localNode *models.Node, cfg *config.ClientConfig) string {
|
2021-08-10 10:31:01 +08:00
|
|
|
if (node.Action == models.NODE_UPDATE_KEY || localNode.Action == models.NODE_UPDATE_KEY) &&
|
2021-08-10 02:13:19 +08:00
|
|
|
node.IsStatic != "yes" {
|
2021-08-10 22:55:48 +08:00
|
|
|
err := wireguard.SetWGKeyConfig(networkName, servercfg.GRPCAddress)
|
2021-05-26 00:48:04 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("unable to process reset keys request: "+err.Error(), 1)
|
2021-08-03 06:06:26 +08:00
|
|
|
return ""
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-06 09:22:59 +08:00
|
|
|
if node.Action == models.NODE_DELETE || localNode.Action == models.NODE_DELETE {
|
2021-08-10 22:55:48 +08:00
|
|
|
err := RemoveLocalInstance(cfg, networkName)
|
2021-05-26 00:48:04 +08:00
|
|
|
if err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("error deleting locally: "+err.Error(), 1)
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
return models.NODE_DELETE
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
return ""
|
|
|
|
}
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// CheckConfig - checks if current config of client needs update, see flow below
|
2021-08-03 06:06:26 +08:00
|
|
|
/**
|
|
|
|
* Pull changes if any (interface refresh)
|
|
|
|
* - Save it
|
|
|
|
* Check local changes for (ipAddress, publickey, configfile changes) (interface refresh)
|
|
|
|
* - Save it
|
|
|
|
* - Push it
|
|
|
|
* Pull Peers (sync)
|
|
|
|
*/
|
|
|
|
func CheckConfig(cliconf config.ClientConfig) error {
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
network := cliconf.Network
|
|
|
|
cfg, err := config.ReadConfig(network)
|
|
|
|
if err != nil {
|
2021-05-26 00:48:04 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
servercfg := cfg.Server
|
2021-08-06 09:22:59 +08:00
|
|
|
currentNode := cfg.Node
|
2021-05-26 00:48:04 +08:00
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
newNode, err := Pull(network, false)
|
2021-08-10 22:55:48 +08:00
|
|
|
if isDeleteError(err) {
|
|
|
|
return RemoveLocalInstance(cfg, network)
|
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
if err != nil {
|
2021-05-26 00:48:04 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
if newNode.IsPending == "yes" {
|
|
|
|
return errors.New("node is pending")
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
2021-08-10 22:55:48 +08:00
|
|
|
actionCompleted := checkNodeActions(newNode, network, servercfg, ¤tNode, cfg)
|
2021-08-03 06:06:26 +08:00
|
|
|
if actionCompleted == models.NODE_DELETE {
|
|
|
|
return errors.New("node has been removed")
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
// Check if ip changed and push if so
|
2021-08-06 09:22:59 +08:00
|
|
|
checkIP(newNode, servercfg, cliconf, network)
|
|
|
|
return Push(network)
|
2021-05-26 00:48:04 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// Pull - pulls the latest config from the server, if manual it will overwrite
|
2021-08-03 06:06:26 +08:00
|
|
|
func Pull(network string, manual bool) (*models.Node, error) {
|
2021-06-03 12:55:39 +08:00
|
|
|
cfg, err := config.ReadConfig(network)
|
2021-08-03 06:06:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-04 03:43:10 +08:00
|
|
|
|
|
|
|
node := cfg.Node
|
2022-01-01 06:45:50 +08:00
|
|
|
//servercfg := cfg.Server
|
2021-06-01 08:36:08 +08:00
|
|
|
|
2021-09-20 02:03:47 +08:00
|
|
|
if cfg.Node.IPForwarding == "yes" && !ncutils.IsWindows() {
|
2021-08-03 06:06:26 +08:00
|
|
|
if err = local.SetIPForwarding(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 03:02:09 +08:00
|
|
|
var resNode models.Node // just need to fill this with either server calls or client calls
|
2021-10-08 21:09:54 +08:00
|
|
|
|
|
|
|
var header metadata.MD
|
|
|
|
var wcclient nodepb.NodeServiceClient
|
2021-10-06 03:02:09 +08:00
|
|
|
var ctx context.Context
|
2021-10-08 21:09:54 +08:00
|
|
|
|
2021-10-06 03:02:09 +08:00
|
|
|
if cfg.Node.IsServer != "yes" {
|
|
|
|
conn, err := grpc.Dial(cfg.Server.GRPCAddress,
|
|
|
|
ncutils.GRPCRequestOpts(cfg.Server.GRPCSSL))
|
|
|
|
if err != nil {
|
|
|
|
ncutils.PrintLog("Cant dial GRPC server: "+err.Error(), 1)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
2021-10-08 21:09:54 +08:00
|
|
|
wcclient = nodepb.NewNodeServiceClient(conn)
|
2021-06-01 08:36:08 +08:00
|
|
|
|
2021-10-08 21:09:54 +08:00
|
|
|
ctx, err = auth.SetJWT(wcclient, network)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
|
|
|
ncutils.PrintLog("Failed to authenticate: "+err.Error(), 1)
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-03 12:55:39 +08:00
|
|
|
|
2022-01-11 08:36:13 +08:00
|
|
|
data, err := json.Marshal(&node)
|
|
|
|
if err != nil {
|
|
|
|
ncutils.PrintLog("Failed to parse node config: "+err.Error(), 1)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-06 03:02:09 +08:00
|
|
|
req := &nodepb.Object{
|
2022-01-11 08:36:13 +08:00
|
|
|
Data: string(data),
|
|
|
|
Type: nodepb.NODE_TYPE,
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2021-10-08 21:09:54 +08:00
|
|
|
|
2021-10-06 03:02:09 +08:00
|
|
|
readres, err := wcclient.ReadNode(ctx, req, grpc.Header(&header))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal([]byte(readres.Data), &resNode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
2021-08-31 03:58:23 +08:00
|
|
|
// ensure that the OS never changes
|
|
|
|
resNode.OS = runtime.GOOS
|
2021-08-03 06:06:26 +08:00
|
|
|
if resNode.PullChanges == "yes" || manual {
|
2021-08-06 02:45:27 +08:00
|
|
|
// check for interface change
|
|
|
|
if cfg.Node.Interface != resNode.Interface {
|
|
|
|
if err = DeleteInterface(cfg.Node.Interface, cfg.Node.PostDown); err != nil {
|
2021-09-20 02:03:47 +08:00
|
|
|
ncutils.PrintLog("could not delete old interface "+cfg.Node.Interface, 1)
|
2021-08-06 02:45:27 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-06 10:35:23 +08:00
|
|
|
resNode.PullChanges = "no"
|
2021-08-03 06:06:26 +08:00
|
|
|
if err = config.ModConfig(&resNode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = wireguard.SetWGConfig(network, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nodeData, err := json.Marshal(&resNode)
|
|
|
|
if err != nil {
|
|
|
|
return &resNode, err
|
|
|
|
}
|
2021-10-06 03:02:09 +08:00
|
|
|
if resNode.IsServer != "yes" {
|
2021-10-08 21:09:54 +08:00
|
|
|
if wcclient == nil || ctx == nil {
|
|
|
|
return &cfg.Node, errors.New("issue initializing gRPC client")
|
|
|
|
}
|
2021-10-06 03:02:09 +08:00
|
|
|
req := &nodepb.Object{
|
|
|
|
Data: string(nodeData),
|
|
|
|
Type: nodepb.NODE_TYPE,
|
|
|
|
Metadata: "",
|
|
|
|
}
|
|
|
|
_, err = wcclient.UpdateNode(ctx, req, grpc.Header(&header))
|
|
|
|
if err != nil {
|
|
|
|
return &resNode, err
|
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
2021-08-06 08:53:47 +08:00
|
|
|
} else {
|
|
|
|
if err = wireguard.SetWGConfig(network, true); err != nil {
|
2021-11-13 02:46:55 +08:00
|
|
|
if errors.Is(err, os.ErrNotExist) && !ncutils.IsFreeBSD() {
|
2021-08-12 07:20:25 +08:00
|
|
|
return Pull(network, true)
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-06 08:53:47 +08:00
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
2022-01-01 06:45:50 +08:00
|
|
|
//if ncutils.IsLinux() {
|
|
|
|
// setDNS(&resNode, servercfg, &cfg.Node)
|
|
|
|
//}
|
2021-11-04 03:43:10 +08:00
|
|
|
var bkupErr = config.SaveBackup(network)
|
|
|
|
if bkupErr != nil {
|
|
|
|
ncutils.Log("unable to update backup file")
|
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
|
2021-08-06 03:18:24 +08:00
|
|
|
return &resNode, err
|
2021-06-01 08:36:08 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 03:07:12 +08:00
|
|
|
// Push - pushes current client configuration to server
|
2021-08-03 06:06:26 +08:00
|
|
|
func Push(network string) error {
|
2021-10-13 03:44:19 +08:00
|
|
|
|
2021-08-03 06:06:26 +08:00
|
|
|
cfg, err := config.ReadConfig(network)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-31 03:58:23 +08:00
|
|
|
postnode := cfg.Node
|
|
|
|
// always set the OS on client
|
|
|
|
postnode.OS = runtime.GOOS
|
2021-10-06 03:02:09 +08:00
|
|
|
postnode.SetLastCheckIn()
|
2021-06-01 08:36:08 +08:00
|
|
|
|
2021-10-13 03:44:19 +08:00
|
|
|
var header metadata.MD
|
|
|
|
var wcclient nodepb.NodeServiceClient
|
|
|
|
conn, err := grpc.Dial(cfg.Server.GRPCAddress,
|
|
|
|
ncutils.GRPCRequestOpts(cfg.Server.GRPCSSL))
|
|
|
|
if err != nil {
|
|
|
|
ncutils.PrintLog("Cant dial GRPC server: "+err.Error(), 1)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
wcclient = nodepb.NewNodeServiceClient(conn)
|
2021-06-01 08:36:08 +08:00
|
|
|
|
2021-10-13 03:44:19 +08:00
|
|
|
ctx, err := auth.SetJWT(wcclient, network)
|
|
|
|
if err != nil {
|
|
|
|
ncutils.PrintLog("Failed to authenticate with server: "+err.Error(), 1)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if postnode.IsPending != "yes" {
|
|
|
|
privateKey, err := wireguard.RetrievePrivKey(network)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-13 03:44:19 +08:00
|
|
|
privateKeyWG, err := wgtypes.ParseKey(privateKey)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-13 03:44:19 +08:00
|
|
|
if postnode.PublicKey != privateKeyWG.PublicKey().String() {
|
|
|
|
postnode.PublicKey = privateKeyWG.PublicKey().String()
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
}
|
2021-10-13 03:44:19 +08:00
|
|
|
nodeData, err := json.Marshal(&postnode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &nodepb.Object{
|
|
|
|
Data: string(nodeData),
|
|
|
|
Type: nodepb.NODE_TYPE,
|
|
|
|
Metadata: "",
|
|
|
|
}
|
|
|
|
data, err := wcclient.UpdateNode(ctx, req, grpc.Header(&header))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(data.Data), &postnode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-03 06:06:26 +08:00
|
|
|
err = config.ModConfig(&postnode)
|
|
|
|
return err
|
2021-06-01 08:36:08 +08:00
|
|
|
}
|