mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-29 08:24:23 +08:00
moved peer determination to server
This commit is contained in:
parent
7c1e672693
commit
2f0f0f9c4d
2 changed files with 5 additions and 181 deletions
|
@ -28,7 +28,7 @@ func Daemon() error {
|
|||
return err
|
||||
}
|
||||
for _, network := range networks {
|
||||
go Netclient(ctx, network)
|
||||
go MessageQueue(ctx, network)
|
||||
}
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGTERM, os.Interrupt)
|
||||
|
@ -51,8 +51,8 @@ func SetupMQTT(cfg config.ClientConfig) mqtt.Client {
|
|||
return client
|
||||
}
|
||||
|
||||
// Netclient sets up Message Queue and subsribes/publishes updates to/from server
|
||||
func Netclient(ctx context.Context, network string) {
|
||||
// MessageQueue sets up Message Queue and subsribes/publishes updates to/from server
|
||||
func MessageQueue(ctx context.Context, network string) {
|
||||
ncutils.Log("netclient go routine started for " + network)
|
||||
var cfg config.ClientConfig
|
||||
cfg.Network = network
|
||||
|
@ -150,17 +150,7 @@ var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message)
|
|||
var cfg config.ClientConfig
|
||||
cfg.Network = peerUpdate.Network
|
||||
cfg.ReadConfig()
|
||||
peers, err := CalculatePeers(cfg.Node, peerUpdate.Nodes, cfg.Node.IsDualStack, cfg.Node.IsEgressGateway, cfg.Node.IsServer)
|
||||
if err != nil {
|
||||
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)
|
||||
err = wireguard.UpdateWgPeers(cfg.Node.Interface, peerUpdate.Peers)
|
||||
if err != nil {
|
||||
ncutils.Log("error updating wireguard peers" + err.Error())
|
||||
return
|
||||
|
@ -315,6 +305,7 @@ func Metrics(ctx context.Context, cfg config.ClientConfig, network string) {
|
|||
ncutils.Log("error publishing metrics " + token.Error().Error())
|
||||
}
|
||||
ncutils.Log("metrics collection complete")
|
||||
client.Disconnect(250)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,167 +0,0 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Reference in a new issue