2022-02-19 00:34:18 +08:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-03-02 19:28:53 +08:00
|
|
|
"os"
|
2022-02-19 00:34:18 +08:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
"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-03-02 19:28:53 +08:00
|
|
|
"github.com/guumaster/hostctl/pkg/file"
|
|
|
|
"github.com/guumaster/hostctl/pkg/parser"
|
|
|
|
"github.com/guumaster/hostctl/pkg/types"
|
2022-02-20 23:08:42 +08:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2022-02-19 00:34:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeUpdate -- mqtt message handler for /update/<NodeID> topic
|
|
|
|
func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
|
|
|
|
var newNode models.Node
|
2022-02-19 23:45:25 +08:00
|
|
|
var nodeCfg config.ClientConfig
|
2022-02-19 00:34:18 +08:00
|
|
|
var network = parseNetworkFromTopic(msg.Topic())
|
2022-02-19 23:45:25 +08:00
|
|
|
nodeCfg.Network = network
|
|
|
|
nodeCfg.ReadConfig()
|
|
|
|
var commsCfg = getCommsCfgByNode(&nodeCfg.Node)
|
2022-02-19 00:34:18 +08:00
|
|
|
|
2022-02-19 23:45:25 +08:00
|
|
|
data, dataErr := decryptMsg(&nodeCfg, msg.Payload())
|
2022-02-19 00:34:18 +08:00
|
|
|
if dataErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := json.Unmarshal([]byte(data), &newNode)
|
|
|
|
if err != nil {
|
|
|
|
ncutils.Log("error unmarshalling node update data" + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ncutils.Log("received message to update node " + newNode.Name)
|
|
|
|
// see if cache hit, if so skip
|
|
|
|
var currentMessage = read(newNode.Network, lastNodeUpdate)
|
|
|
|
if currentMessage == string(data) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
insert(newNode.Network, lastNodeUpdate, string(data)) // store new message in cache
|
|
|
|
|
2022-02-19 23:45:25 +08:00
|
|
|
// ensure that OS never changes
|
2022-02-19 00:34:18 +08:00
|
|
|
newNode.OS = runtime.GOOS
|
|
|
|
// check if interface needs to delta
|
2022-02-19 23:45:25 +08:00
|
|
|
ifaceDelta := ncutils.IfaceDelta(&nodeCfg.Node, &newNode)
|
|
|
|
shouldDNSChange := nodeCfg.Node.DNSOn != newNode.DNSOn
|
2022-02-21 22:31:31 +08:00
|
|
|
hubChange := nodeCfg.Node.IsHub != newNode.IsHub
|
2022-02-19 00:34:18 +08:00
|
|
|
|
2022-02-19 23:45:25 +08:00
|
|
|
nodeCfg.Node = newNode
|
2022-02-19 00:34:18 +08:00
|
|
|
switch newNode.Action {
|
|
|
|
case models.NODE_DELETE:
|
2022-02-19 23:45:25 +08:00
|
|
|
ncutils.PrintLog(fmt.Sprintf("received delete request for %s", nodeCfg.Node.Name), 0)
|
|
|
|
unsubscribeNode(client, &nodeCfg)
|
2022-02-21 07:09:32 +08:00
|
|
|
if err = LeaveNetwork(nodeCfg.Node.Network, true); err != nil {
|
2022-02-19 00:34:18 +08:00
|
|
|
if !strings.Contains("rpc error", err.Error()) {
|
2022-02-19 23:45:25 +08:00
|
|
|
ncutils.PrintLog(fmt.Sprintf("failed to leave, please check that local files for network %s were removed", nodeCfg.Node.Network), 0)
|
|
|
|
return
|
2022-02-19 00:34:18 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-19 23:45:25 +08:00
|
|
|
ncutils.PrintLog(fmt.Sprintf("%s was removed", nodeCfg.Node.Name), 0)
|
2022-02-19 00:34:18 +08:00
|
|
|
return
|
|
|
|
case models.NODE_UPDATE_KEY:
|
2022-02-20 23:08:42 +08:00
|
|
|
// == get the current key for node ==
|
|
|
|
oldPrivateKey, retErr := wireguard.RetrievePrivKey(nodeCfg.Network)
|
|
|
|
if retErr != nil {
|
|
|
|
break
|
|
|
|
}
|
2022-02-19 23:45:25 +08:00
|
|
|
if err := UpdateKeys(&nodeCfg, client); err != nil {
|
2022-02-20 23:08:42 +08:00
|
|
|
ncutils.PrintLog("err updating wireguard keys, reusing last key\n"+err.Error(), 0)
|
|
|
|
if key, parseErr := wgtypes.ParseKey(oldPrivateKey); parseErr == nil {
|
|
|
|
wireguard.StorePrivKey(key.String(), nodeCfg.Network)
|
|
|
|
nodeCfg.Node.PublicKey = key.PublicKey().String()
|
|
|
|
}
|
2022-02-19 00:34:18 +08:00
|
|
|
}
|
2022-02-20 23:08:42 +08:00
|
|
|
ifaceDelta = true
|
2022-02-19 00:34:18 +08:00
|
|
|
case models.NODE_NOOP:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
// Save new config
|
2022-02-19 23:45:25 +08:00
|
|
|
nodeCfg.Node.Action = models.NODE_NOOP
|
|
|
|
if err := config.Write(&nodeCfg, nodeCfg.Network); err != nil {
|
2022-02-19 00:34:18 +08:00
|
|
|
ncutils.PrintLog("error updating node configuration: "+err.Error(), 0)
|
|
|
|
}
|
2022-02-19 23:45:25 +08:00
|
|
|
nameserver := nodeCfg.Server.CoreDNSAddr
|
2022-02-19 00:34:18 +08:00
|
|
|
privateKey, err := wireguard.RetrievePrivKey(newNode.Network)
|
|
|
|
if err != nil {
|
|
|
|
ncutils.Log("error reading PrivateKey " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-02-19 23:45:25 +08:00
|
|
|
file := ncutils.GetNetclientPathSpecific() + nodeCfg.Node.Interface + ".conf"
|
2022-02-19 00:34:18 +08:00
|
|
|
|
|
|
|
if err := wireguard.UpdateWgInterface(file, privateKey, nameserver, newNode); err != nil {
|
|
|
|
ncutils.Log("error updating wireguard config " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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)
|
2022-02-19 23:45:25 +08:00
|
|
|
err = wireguard.ApplyConf(&nodeCfg.Node, nodeCfg.Node.Interface, file)
|
2022-02-19 00:34:18 +08:00
|
|
|
if err != nil {
|
|
|
|
ncutils.Log("error restarting wg after node update " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second >> 0)
|
2022-03-04 04:07:27 +08:00
|
|
|
// if newNode.DNSOn == "yes" {
|
|
|
|
// for _, server := range newNode.NetworkSettings.DefaultServerAddrs {
|
|
|
|
// if server.IsLeader {
|
|
|
|
// go local.SetDNSWithRetry(newNode, server.Address)
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2022-02-19 23:45:25 +08:00
|
|
|
doneErr := publishSignal(&commsCfg, &nodeCfg, ncutils.DONE)
|
2022-02-19 00:34:18 +08:00
|
|
|
if doneErr != nil {
|
|
|
|
ncutils.Log("could not notify server to update peers after interface change")
|
|
|
|
} else {
|
2022-02-19 23:45:25 +08:00
|
|
|
ncutils.Log("signalled finished interface update to server")
|
2022-02-19 00:34:18 +08:00
|
|
|
}
|
2022-02-21 22:31:31 +08:00
|
|
|
} else if hubChange {
|
|
|
|
doneErr := publishSignal(&commsCfg, &nodeCfg, ncutils.DONE)
|
|
|
|
if doneErr != nil {
|
|
|
|
ncutils.Log("could not notify server to update peers after hub change")
|
|
|
|
} else {
|
|
|
|
ncutils.Log("signalled finished hub update to server")
|
|
|
|
}
|
2022-02-19 00:34:18 +08:00
|
|
|
}
|
|
|
|
//deal with DNS
|
2022-02-19 23:45:25 +08:00
|
|
|
if newNode.DNSOn != "yes" && shouldDNSChange && nodeCfg.Node.Interface != "" {
|
2022-02-19 00:34:18 +08:00
|
|
|
ncutils.Log("settng DNS off")
|
2022-03-04 04:07:27 +08:00
|
|
|
if err := removeHostDNS(ncutils.IsWindows()); err != nil {
|
2022-03-08 19:22:02 +08:00
|
|
|
ncutils.Log("error removing netmaker profile from /etc/hosts " + err.Error())
|
2022-02-19 00:34:18 +08:00
|
|
|
}
|
2022-03-04 04:07:27 +08:00
|
|
|
// _, err := ncutils.RunCmd("/usr/bin/resolvectl revert "+nodeCfg.Node.Interface, true)
|
|
|
|
// if err != nil {
|
|
|
|
// ncutils.Log("error applying dns" + err.Error())
|
|
|
|
// }
|
2022-02-19 00:34:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePeers -- mqtt message handler for peers/<Network>/<NodeID> topic
|
|
|
|
func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
|
|
|
|
var peerUpdate models.PeerUpdate
|
|
|
|
var network = parseNetworkFromTopic(msg.Topic())
|
|
|
|
var cfg = config.ClientConfig{}
|
|
|
|
cfg.Network = network
|
|
|
|
cfg.ReadConfig()
|
|
|
|
|
|
|
|
data, dataErr := decryptMsg(&cfg, msg.Payload())
|
|
|
|
if dataErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := json.Unmarshal([]byte(data), &peerUpdate)
|
|
|
|
if err != nil {
|
|
|
|
ncutils.Log("error unmarshalling peer data")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// see if cached hit, if so skip
|
|
|
|
var currentMessage = read(peerUpdate.Network, lastPeerUpdate)
|
|
|
|
if currentMessage == string(data) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
insert(peerUpdate.Network, lastPeerUpdate, string(data))
|
|
|
|
|
|
|
|
file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
|
|
|
|
err = wireguard.UpdateWgPeers(file, peerUpdate.Peers)
|
|
|
|
if err != nil {
|
|
|
|
ncutils.Log("error updating wireguard peers" + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//err = wireguard.SyncWGQuickConf(cfg.Node.Interface, file)
|
|
|
|
var iface = cfg.Node.Interface
|
|
|
|
if ncutils.IsMac() {
|
|
|
|
iface, err = local.GetMacIface(cfg.Node.Address)
|
|
|
|
if err != nil {
|
|
|
|
ncutils.Log("error retrieving mac iface: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 22:45:42 +08:00
|
|
|
err = wireguard.SetPeers(iface, &cfg.Node, peerUpdate.Peers)
|
2022-02-19 00:34:18 +08:00
|
|
|
if err != nil {
|
|
|
|
ncutils.Log("error syncing wg after peer update: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-03-09 06:15:13 +08:00
|
|
|
//skip dns updates if this is a peer update for comms network
|
|
|
|
if cfg.Node.NetworkSettings.IsComms == "yes" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ncutils.Log("DNS updating /etc/hosts")
|
2022-03-02 20:54:15 +08:00
|
|
|
if cfg.Node.DNSOn == "yes" {
|
2022-03-04 04:07:27 +08:00
|
|
|
if err := setHostDNS(peerUpdate.DNS, ncutils.IsWindows()); err != nil {
|
2022-03-02 20:54:15 +08:00
|
|
|
ncutils.Log("error updating /etc/hosts " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-03-02 22:07:52 +08:00
|
|
|
} else {
|
|
|
|
if err := removeHostDNS(ncutils.IsWindows()); err != nil {
|
2022-03-08 19:02:50 +08:00
|
|
|
ncutils.Log("error removing netmaker profile from /etc/hosts " + err.Error())
|
2022-03-02 22:07:52 +08:00
|
|
|
return
|
|
|
|
}
|
2022-03-02 19:28:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setHostDNS(dns []byte, windows bool) error {
|
|
|
|
etchosts := "/etc/hosts"
|
|
|
|
if windows {
|
|
|
|
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
|
|
|
|
}
|
2022-03-05 02:29:56 +08:00
|
|
|
tmpfile := "/tmp/dnsdata"
|
|
|
|
if windows {
|
|
|
|
tmpfile = "c:\\windows\\temp\\dnsdata"
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(tmpfile, dns, 0600); err != nil {
|
2022-03-02 19:28:53 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-03-05 02:29:56 +08:00
|
|
|
dnsdata, err := os.Open(tmpfile)
|
2022-03-02 19:28:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
profile, err := parser.ParseProfile(dnsdata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-05 03:00:05 +08:00
|
|
|
hosts, err := file.NewFile(etchosts)
|
2022-03-02 19:28:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
profile.Name = "netmaker"
|
|
|
|
profile.Status = types.Enabled
|
|
|
|
if err := hosts.ReplaceProfile(profile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-02 22:07:52 +08:00
|
|
|
if err := hosts.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeHostDNS(windows bool) error {
|
|
|
|
etchosts := "/etc/hosts"
|
|
|
|
if windows {
|
|
|
|
etchosts = "c:\\windows\\system32\\drivers\\etc\\hosts"
|
|
|
|
}
|
|
|
|
hosts, err := file.NewFile(etchosts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := hosts.RemoveProfile("netmaker"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := hosts.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-02 19:28:53 +08:00
|
|
|
return nil
|
2022-02-19 00:34:18 +08:00
|
|
|
}
|