mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 08:47:35 +08:00
WIP commit to enable rebasing to uuid feature
This commit is contained in:
@@ -154,9 +154,19 @@ var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message)
|
||||
var cfg config.ClientConfig
|
||||
cfg.Network = peerUpdate.Network
|
||||
cfg.ReadConfig()
|
||||
err = wireguard.UpdateWgPeers(cfg.Node.Interface, peerUpdate.Peers)
|
||||
peers, err := CalculatePeers(cfg.Node, peerUpdate.Nodes, cfg.Node.IsDualStack, cfg.Node.IsEgressGateway, cfg.Node.IsServer)
|
||||
if err != nil {
|
||||
ncutils.Log("error updating peers" + err.Error())
|
||||
ncutils.Log("error calculating Peers " + err.Error())
|
||||
return
|
||||
}
|
||||
extpeers, err := CalculateExtPeers(cfg.Node, peerUpdate.ExtPeers)
|
||||
if err != nil {
|
||||
ncutils.Log("error updated external wireguard peers " + err.Error())
|
||||
}
|
||||
peers = append(peers, extpeers...)
|
||||
err = wireguard.UpdateWgPeers(cfg.Node.Interface, peers)
|
||||
if err != nil {
|
||||
ncutils.Log("error updating wireguard peers" + err.Error())
|
||||
return
|
||||
}
|
||||
// path hardcoded for now... should be updated
|
||||
@@ -187,7 +197,9 @@ func UpdateKeys(cfg *config.ClientConfig, client mqtt.Client) (*config.ClientCon
|
||||
client.Disconnect(250)
|
||||
return cfg, err
|
||||
}
|
||||
client.Disconnect(250)
|
||||
if err := config.ModConfig(&cfg.Node); err != nil {
|
||||
ncutils.Log("error updating local config " + err.Error())
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
@@ -202,6 +214,11 @@ func Checkin(ctx context.Context, cfg config.ClientConfig, network string) {
|
||||
//delay should be configuraable -> use cfg.Node.NetworkSettings.DefaultCheckInInterval ??
|
||||
case <-time.After(time.Second * 10):
|
||||
ncutils.Log("Checkin running")
|
||||
//read latest config
|
||||
cfg.ReadConfig()
|
||||
//fix NodeID to remove ### so NodeID can be used as message topic
|
||||
//remove with GRA-73
|
||||
cfg.Node.ID = strings.Replace(cfg.Node.ID, "###", "-", 1)
|
||||
if cfg.Node.Roaming == "yes" && cfg.Node.IsStatic != "yes" {
|
||||
extIP, err := ncutils.GetPublicIP()
|
||||
if err != nil {
|
||||
@@ -242,6 +259,10 @@ func UpdateEndpoint(cfg config.ClientConfig, network, ip string) {
|
||||
if token := client.Publish("update/ip/"+cfg.Node.ID, 0, false, ip); token.Wait() && token.Error() != nil {
|
||||
ncutils.Log("error publishing endpoint update " + token.Error().Error())
|
||||
}
|
||||
cfg.Node.Endpoint = ip
|
||||
if err := config.Write(&cfg, cfg.Network); err != nil {
|
||||
ncutils.Log("error updating local config " + err.Error())
|
||||
}
|
||||
client.Disconnect(250)
|
||||
}
|
||||
|
||||
@@ -252,13 +273,19 @@ func UpdateLocalAddress(cfg config.ClientConfig, network, ip string) {
|
||||
if token := client.Publish("update/localaddress/"+cfg.Node.ID, 0, false, ip); token.Wait() && token.Error() != nil {
|
||||
ncutils.Log("error publishing local address update " + token.Error().Error())
|
||||
}
|
||||
cfg.Node.LocalAddress = ip
|
||||
ncutils.Log("updating local address in local config to: " + cfg.Node.LocalAddress)
|
||||
if err := config.Write(&cfg, cfg.Network); err != nil {
|
||||
ncutils.Log("error updating local config " + err.Error())
|
||||
}
|
||||
client.Disconnect(250)
|
||||
}
|
||||
|
||||
// Hello -- ping the broker to let server know node is alive and doing fine
|
||||
func Hello(cfg config.ClientConfig, network string) {
|
||||
client := SetupMQTT(cfg)
|
||||
if token := client.Publish("ping/"+cfg.Node.ID, 0, false, "hello world!"); token.Wait() && token.Error() != nil {
|
||||
ncutils.Log("sending ping " + cfg.Node.ID)
|
||||
if token := client.Publish("ping/"+cfg.Node.ID, 2, false, "hello world!"); token.Wait() && token.Error() != nil {
|
||||
ncutils.Log("error publishing ping " + token.Error().Error())
|
||||
}
|
||||
client.Disconnect(250)
|
||||
|
167
netclient/functions/peers.go
Normal file
167
netclient/functions/peers.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package functions
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gravitl/netmaker/models"
|
||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
)
|
||||
|
||||
func CalculatePeers(thisNode models.Node, peernodes []models.Node, dualstack, egressgateway, server string) ([]wgtypes.Peer, error) {
|
||||
//hasGateway := false
|
||||
var gateways []string
|
||||
var peers []wgtypes.Peer
|
||||
|
||||
keepalive := thisNode.PersistentKeepalive
|
||||
keepalivedur, _ := time.ParseDuration(strconv.FormatInt(int64(keepalive), 10) + "s")
|
||||
keepaliveserver, err := time.ParseDuration(strconv.FormatInt(int64(5), 10) + "s")
|
||||
if err != nil {
|
||||
log.Fatalf("Issue with format of keepalive value. Please update netconfig: %v", err)
|
||||
}
|
||||
for _, node := range peernodes {
|
||||
pubkey, err := wgtypes.ParseKey(node.PublicKey)
|
||||
if err != nil {
|
||||
log.Println("error parsing key")
|
||||
//return peers, hasGateway, gateways, err
|
||||
}
|
||||
|
||||
if thisNode.PublicKey == node.PublicKey {
|
||||
continue
|
||||
}
|
||||
if thisNode.Endpoint == node.Endpoint {
|
||||
if thisNode.LocalAddress != node.LocalAddress && node.LocalAddress != "" {
|
||||
node.Endpoint = node.LocalAddress
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
var peer wgtypes.Peer
|
||||
var peeraddr = net.IPNet{
|
||||
IP: net.ParseIP(node.Address),
|
||||
Mask: net.CIDRMask(32, 32),
|
||||
}
|
||||
var allowedips []net.IPNet
|
||||
allowedips = append(allowedips, peeraddr)
|
||||
// handle manually set peers
|
||||
for _, allowedIp := range node.AllowedIPs {
|
||||
if _, ipnet, err := net.ParseCIDR(allowedIp); err == nil {
|
||||
nodeEndpointArr := strings.Split(node.Endpoint, ":")
|
||||
if !ipnet.Contains(net.IP(nodeEndpointArr[0])) && ipnet.IP.String() != node.Address { // don't need to add an allowed ip that already exists..
|
||||
allowedips = append(allowedips, *ipnet)
|
||||
}
|
||||
} else if appendip := net.ParseIP(allowedIp); appendip != nil && allowedIp != node.Address {
|
||||
ipnet := net.IPNet{
|
||||
IP: net.ParseIP(allowedIp),
|
||||
Mask: net.CIDRMask(32, 32),
|
||||
}
|
||||
allowedips = append(allowedips, ipnet)
|
||||
}
|
||||
}
|
||||
// handle egress gateway peers
|
||||
if node.IsEgressGateway == "yes" {
|
||||
//hasGateway = true
|
||||
ranges := node.EgressGatewayRanges
|
||||
for _, iprange := range ranges { // go through each cidr for egress gateway
|
||||
_, ipnet, err := net.ParseCIDR(iprange) // confirming it's valid cidr
|
||||
if err != nil {
|
||||
ncutils.PrintLog("could not parse gateway IP range. Not adding "+iprange, 1)
|
||||
continue // if can't parse CIDR
|
||||
}
|
||||
nodeEndpointArr := strings.Split(node.Endpoint, ":") // getting the public ip of node
|
||||
if ipnet.Contains(net.ParseIP(nodeEndpointArr[0])) { // ensuring egress gateway range does not contain public ip of node
|
||||
ncutils.PrintLog("egress IP range of "+iprange+" overlaps with "+node.Endpoint+", omitting", 2)
|
||||
continue // skip adding egress range if overlaps with node's ip
|
||||
}
|
||||
if ipnet.Contains(net.ParseIP(thisNode.LocalAddress)) { // ensuring egress gateway range does not contain public ip of node
|
||||
ncutils.PrintLog("egress IP range of "+iprange+" overlaps with "+thisNode.LocalAddress+", omitting", 2)
|
||||
continue // skip adding egress range if overlaps with node's local ip
|
||||
}
|
||||
gateways = append(gateways, iprange)
|
||||
if err != nil {
|
||||
log.Println("ERROR ENCOUNTERED SETTING GATEWAY")
|
||||
} else {
|
||||
allowedips = append(allowedips, *ipnet)
|
||||
}
|
||||
}
|
||||
}
|
||||
if node.Address6 != "" && dualstack == "yes" {
|
||||
var addr6 = net.IPNet{
|
||||
IP: net.ParseIP(node.Address6),
|
||||
Mask: net.CIDRMask(128, 128),
|
||||
}
|
||||
allowedips = append(allowedips, addr6)
|
||||
}
|
||||
if thisNode.IsServer == "yes" && !(node.IsServer == "yes") {
|
||||
peer = wgtypes.Peer{
|
||||
PublicKey: pubkey,
|
||||
PersistentKeepaliveInterval: keepaliveserver,
|
||||
AllowedIPs: allowedips,
|
||||
}
|
||||
} else if keepalive != 0 {
|
||||
peer = wgtypes.Peer{
|
||||
PublicKey: pubkey,
|
||||
PersistentKeepaliveInterval: keepalivedur,
|
||||
Endpoint: &net.UDPAddr{
|
||||
IP: net.ParseIP(node.Endpoint),
|
||||
Port: int(node.ListenPort),
|
||||
},
|
||||
AllowedIPs: allowedips,
|
||||
}
|
||||
} else {
|
||||
peer = wgtypes.Peer{
|
||||
PublicKey: pubkey,
|
||||
Endpoint: &net.UDPAddr{
|
||||
IP: net.ParseIP(node.Endpoint),
|
||||
Port: int(node.ListenPort),
|
||||
},
|
||||
AllowedIPs: allowedips,
|
||||
}
|
||||
}
|
||||
peers = append(peers, peer)
|
||||
}
|
||||
return peers, nil
|
||||
}
|
||||
|
||||
func CalculateExtPeers(thisNode models.Node, extPeers []models.ExtPeersResponse) ([]wgtypes.Peer, error) {
|
||||
var peers []wgtypes.Peer
|
||||
var err error
|
||||
for _, extPeer := range extPeers {
|
||||
pubkey, err := wgtypes.ParseKey(extPeer.PublicKey)
|
||||
if err != nil {
|
||||
log.Println("error parsing key")
|
||||
return peers, err
|
||||
}
|
||||
|
||||
if thisNode.PublicKey == extPeer.PublicKey {
|
||||
continue
|
||||
}
|
||||
|
||||
var peer wgtypes.Peer
|
||||
var peeraddr = net.IPNet{
|
||||
IP: net.ParseIP(extPeer.Address),
|
||||
Mask: net.CIDRMask(32, 32),
|
||||
}
|
||||
var allowedips []net.IPNet
|
||||
allowedips = append(allowedips, peeraddr)
|
||||
|
||||
if extPeer.Address6 != "" && thisNode.IsDualStack == "yes" {
|
||||
var addr6 = net.IPNet{
|
||||
IP: net.ParseIP(extPeer.Address6),
|
||||
Mask: net.CIDRMask(128, 128),
|
||||
}
|
||||
allowedips = append(allowedips, addr6)
|
||||
}
|
||||
peer = wgtypes.Peer{
|
||||
PublicKey: pubkey,
|
||||
AllowedIPs: allowedips,
|
||||
}
|
||||
peers = append(peers, peer)
|
||||
}
|
||||
return peers, err
|
||||
}
|
Reference in New Issue
Block a user