mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-11 23:54:22 +08:00
initial commit
This commit is contained in:
parent
59685731c1
commit
d6554ef081
7 changed files with 123 additions and 10 deletions
|
@ -67,6 +67,11 @@ func (s *NodeServiceServer) CreateNode(ctx context.Context, req *nodepb.Object)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var serverNodes = logic.GetServerNodes(node.Network)
|
||||||
|
for _, server := range serverNodes {
|
||||||
|
node.NetworkSettings.DefaultServerAddrs = append(node.NetworkSettings.DefaultServerAddrs, server.Address)
|
||||||
|
}
|
||||||
|
|
||||||
err = logic.CreateNode(&node)
|
err = logic.CreateNode(&node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -58,6 +58,21 @@ func GetSortedNetworkServerNodes(network string) ([]models.Node, error) {
|
||||||
return nodes, nil
|
return nodes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetServerNodes - gets the server nodes of a network
|
||||||
|
func GetServerNodes(network string) []models.Node {
|
||||||
|
var nodes, err = GetNetworkNodes(network)
|
||||||
|
var serverNodes = make([]models.Node, 0)
|
||||||
|
if err != nil {
|
||||||
|
return serverNodes
|
||||||
|
}
|
||||||
|
for _, node := range nodes {
|
||||||
|
if node.IsServer == "yes" {
|
||||||
|
serverNodes = append(serverNodes, node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return serverNodes
|
||||||
|
}
|
||||||
|
|
||||||
// UncordonNode - approves a node to join a network
|
// UncordonNode - approves a node to join a network
|
||||||
func UncordonNode(nodeid string) (models.Node, error) {
|
func UncordonNode(nodeid string) (models.Node, error) {
|
||||||
node, err := GetNodeByID(nodeid)
|
node, err := GetNodeByID(nodeid)
|
||||||
|
|
|
@ -20,6 +20,7 @@ func GetPeerUpdate(node *models.Node) (models.PeerUpdate, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return models.PeerUpdate{}, err
|
return models.PeerUpdate{}, err
|
||||||
}
|
}
|
||||||
|
var serverNodeAddresses = []string{}
|
||||||
for _, peer := range networkNodes {
|
for _, peer := range networkNodes {
|
||||||
if peer.ID == node.ID {
|
if peer.ID == node.ID {
|
||||||
//skip yourself
|
//skip yourself
|
||||||
|
@ -55,9 +56,13 @@ func GetPeerUpdate(node *models.Node) (models.PeerUpdate, error) {
|
||||||
PersistentKeepaliveInterval: &keepalive,
|
PersistentKeepaliveInterval: &keepalive,
|
||||||
}
|
}
|
||||||
peers = append(peers, peerData)
|
peers = append(peers, peerData)
|
||||||
|
if peer.IsServer == "yes" {
|
||||||
|
serverNodeAddresses = append(serverNodeAddresses, peer.Address)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
peerUpdate.Network = node.Network
|
peerUpdate.Network = node.Network
|
||||||
peerUpdate.Peers = peers
|
peerUpdate.Peers = peers
|
||||||
|
peerUpdate.ServerAddrs = serverNodeAddresses
|
||||||
return peerUpdate, nil
|
return peerUpdate, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,15 @@ package models
|
||||||
|
|
||||||
import "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
import "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
|
|
||||||
|
// PeerUpdate - struct
|
||||||
type PeerUpdate struct {
|
type PeerUpdate struct {
|
||||||
Network string
|
Network string `json:"network" bson:"network"`
|
||||||
Peers []wgtypes.PeerConfig
|
ServerAddrs []string `json:"serversaddrs" bson:"serversaddrs"`
|
||||||
|
Peers []wgtypes.PeerConfig `json:"peers" bson:"peers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// KeyUpdate - key update struct
|
||||||
type KeyUpdate struct {
|
type KeyUpdate struct {
|
||||||
Network string
|
Network string `json:"network" bson:"network"`
|
||||||
Interface string
|
Interface string `json:"interface" bson:"interface"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,10 +34,11 @@ type Network struct {
|
||||||
LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
|
LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
|
||||||
|
|
||||||
// checkin interval is depreciated at the network level. Set on server with CHECKIN_INTERVAL
|
// checkin interval is depreciated at the network level. Set on server with CHECKIN_INTERVAL
|
||||||
DefaultCheckInInterval int32 `json:"checkininterval,omitempty" bson:"checkininterval,omitempty" validate:"omitempty,numeric,min=2,max=100000"`
|
DefaultCheckInInterval int32 `json:"checkininterval,omitempty" bson:"checkininterval,omitempty" validate:"omitempty,numeric,min=2,max=100000"`
|
||||||
DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
|
DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
|
||||||
DefaultExtClientDNS string `json:"defaultextclientdns" bson:"defaultextclientdns"`
|
DefaultExtClientDNS string `json:"defaultextclientdns" bson:"defaultextclientdns"`
|
||||||
DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
|
DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
|
||||||
|
DefaultServerAddrs []string `json:"defaultserveraddrs" bson:"defaultserveraddrs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveData - sensitive fields of a network that should be kept the same
|
// SaveData - sensitive fields of a network that should be kept the same
|
||||||
|
|
|
@ -3,10 +3,12 @@ package functions
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -19,6 +21,22 @@ import (
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var messageCache = make(map[string]string, 20)
|
||||||
|
|
||||||
|
const lastNodeUpdate = "lnu"
|
||||||
|
const lastPeerUpdate = "lpu"
|
||||||
|
|
||||||
|
func insert(network, which, cache string) {
|
||||||
|
var mu sync.Mutex
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
messageCache[fmt.Sprintf("%s%s", network, which)] = cache
|
||||||
|
}
|
||||||
|
|
||||||
|
func read(network, which string) string {
|
||||||
|
return messageCache[fmt.Sprintf("%s%s", network, which)]
|
||||||
|
}
|
||||||
|
|
||||||
// Daemon runs netclient daemon from command line
|
// Daemon runs netclient daemon from command line
|
||||||
func Daemon() error {
|
func Daemon() error {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
@ -41,8 +59,12 @@ func Daemon() error {
|
||||||
// SetupMQTT creates a connection to broker and return client
|
// SetupMQTT creates a connection to broker and return client
|
||||||
func SetupMQTT(cfg *config.ClientConfig) mqtt.Client {
|
func SetupMQTT(cfg *config.ClientConfig) mqtt.Client {
|
||||||
opts := mqtt.NewClientOptions()
|
opts := mqtt.NewClientOptions()
|
||||||
ncutils.Log("setting broker to " + cfg.Server.CoreDNSAddr + ":1883")
|
for i, addr := range cfg.Node.NetworkSettings.DefaultServerAddrs {
|
||||||
opts.AddBroker(cfg.Server.CoreDNSAddr + ":1883")
|
if addr != "" {
|
||||||
|
ncutils.Log(fmt.Sprintf("adding server (%d) to listen on network %s \n", (i + 1), cfg.Node.Network))
|
||||||
|
opts.AddBroker(addr + ":1883")
|
||||||
|
}
|
||||||
|
}
|
||||||
opts.SetDefaultPublishHandler(All)
|
opts.SetDefaultPublishHandler(All)
|
||||||
client := mqtt.NewClient(opts)
|
client := mqtt.NewClient(opts)
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
@ -102,6 +124,12 @@ var NodeUpdate mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message)
|
||||||
ncutils.Log("error unmarshalling node update data" + err.Error())
|
ncutils.Log("error unmarshalling node update data" + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// see if cache hit, if so skip
|
||||||
|
var currentMessage = read(newNode.Network, lastNodeUpdate)
|
||||||
|
if currentMessage == string(msg.Payload()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
insert(newNode.Network, lastNodeUpdate, string(msg.Payload()))
|
||||||
cfg.Network = newNode.Network
|
cfg.Network = newNode.Network
|
||||||
cfg.ReadConfig()
|
cfg.ReadConfig()
|
||||||
//check if interface name has changed if so delete.
|
//check if interface name has changed if so delete.
|
||||||
|
@ -177,10 +205,24 @@ var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message)
|
||||||
ncutils.Log("error unmarshalling peer data")
|
ncutils.Log("error unmarshalling peer data")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// see if cache hit, if so skip
|
||||||
|
var currentMessage = read(peerUpdate.Network, lastPeerUpdate)
|
||||||
|
if currentMessage == string(msg.Payload()) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
insert(peerUpdate.Network, lastPeerUpdate, string(msg.Payload()))
|
||||||
ncutils.Log("update peer handler")
|
ncutils.Log("update peer handler")
|
||||||
var cfg config.ClientConfig
|
var cfg config.ClientConfig
|
||||||
cfg.Network = peerUpdate.Network
|
cfg.Network = peerUpdate.Network
|
||||||
cfg.ReadConfig()
|
cfg.ReadConfig()
|
||||||
|
var shouldReSub = shouldResub(cfg.Node.NetworkSettings.DefaultServerAddrs, peerUpdate.ServerAddrs)
|
||||||
|
if shouldReSub {
|
||||||
|
client.Disconnect(250) // kill client
|
||||||
|
// un sub, re sub.. how?
|
||||||
|
client.Unsubscribe("update/"+cfg.Node.ID, "update/peers/"+cfg.Node.ID)
|
||||||
|
cfg.Node.NetworkSettings.DefaultServerAddrs = peerUpdate.ServerAddrs
|
||||||
|
|
||||||
|
}
|
||||||
file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
|
file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
|
||||||
err = wireguard.UpdateWgPeers(file, peerUpdate.Peers)
|
err = wireguard.UpdateWgPeers(file, peerUpdate.Peers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -196,6 +238,26 @@ var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Resubscribe --- handles resubscribing if needed
|
||||||
|
func Resubscribe(client mqtt.Client, cfg *config.ClientConfig) {
|
||||||
|
if err := config.ModConfig(&cfg.Node); err == nil {
|
||||||
|
client.Disconnect(250)
|
||||||
|
client = SetupMQTT(cfg)
|
||||||
|
if token := client.Subscribe("update/"+cfg.Node.ID, 0, NodeUpdate); token.Wait() && token.Error() != nil {
|
||||||
|
log.Fatal(token.Error())
|
||||||
|
}
|
||||||
|
if cfg.DebugOn {
|
||||||
|
ncutils.Log("subscribed to node updates for node " + cfg.Node.Name + " update/" + cfg.Node.ID)
|
||||||
|
}
|
||||||
|
if token := client.Subscribe("update/peers/"+cfg.Node.ID, 0, UpdatePeers); token.Wait() && token.Error() != nil {
|
||||||
|
log.Fatal(token.Error())
|
||||||
|
}
|
||||||
|
ncutils.Log("finished re subbing")
|
||||||
|
} else {
|
||||||
|
ncutils.Log("could not mod config when re-subbing")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// UpdateKeys -- updates private key and returns new publickey
|
// UpdateKeys -- updates private key and returns new publickey
|
||||||
func UpdateKeys(cfg *config.ClientConfig, client mqtt.Client) error {
|
func UpdateKeys(cfg *config.ClientConfig, client mqtt.Client) error {
|
||||||
ncutils.Log("received message to update keys")
|
ncutils.Log("received message to update keys")
|
||||||
|
@ -291,3 +353,15 @@ func Hello(cfg *config.ClientConfig, network string) {
|
||||||
}
|
}
|
||||||
client.Disconnect(250)
|
client.Disconnect(250)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func shouldResub(currentServers, newServers []string) bool {
|
||||||
|
if len(currentServers) != len(newServers) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, srv := range currentServers {
|
||||||
|
if !ncutils.StringSliceContains(newServers, srv) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -532,3 +532,13 @@ func CheckWG() {
|
||||||
log.Println("running userspace WireGuard with " + uspace)
|
log.Println("running userspace WireGuard with " + uspace)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StringSliceContains - sees if a string slice contains a string element
|
||||||
|
func StringSliceContains(slice []string, item string) bool {
|
||||||
|
for _, s := range slice {
|
||||||
|
if s == item {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue