mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 08:47:35 +08:00
publishing port changes
This commit is contained in:
@@ -79,6 +79,10 @@ func GetNodePeers(networkName, nodeid string, excludeRelayed bool, isP2S bool) (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// if udp hole punching is on, but port is still set to default (e.g. 51821), use the LocalListenPort
|
||||||
|
if node.UDPHolePunch == "yes" && node.IsStatic != "yes" && peer.ListenPort == node.ListenPort {
|
||||||
|
peer.ListenPort = node.LocalListenPort
|
||||||
|
}
|
||||||
if node.IsRelay == "yes" {
|
if node.IsRelay == "yes" {
|
||||||
network, err := GetNetwork(networkName)
|
network, err := GetNetwork(networkName)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@@ -218,6 +218,7 @@ func setupMQTT(cfg *config.ClientConfig, server string, publish bool) mqtt.Clien
|
|||||||
opts.SetConnectRetryInterval(time.Second << 2)
|
opts.SetConnectRetryInterval(time.Second << 2)
|
||||||
opts.SetKeepAlive(time.Minute >> 1)
|
opts.SetKeepAlive(time.Minute >> 1)
|
||||||
opts.SetWriteTimeout(time.Minute)
|
opts.SetWriteTimeout(time.Minute)
|
||||||
|
|
||||||
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
||||||
if !publish {
|
if !publish {
|
||||||
networks, err := ncutils.GetSystemNetworks()
|
networks, err := ncutils.GetSystemNetworks()
|
||||||
@@ -243,8 +244,8 @@ func setupMQTT(cfg *config.ClientConfig, server string, publish bool) mqtt.Clien
|
|||||||
}
|
}
|
||||||
logger.Log(0, "connection re-established with mqtt server")
|
logger.Log(0, "connection re-established with mqtt server")
|
||||||
})
|
})
|
||||||
|
|
||||||
client := mqtt.NewClient(opts)
|
client := mqtt.NewClient(opts)
|
||||||
|
|
||||||
tperiod := time.Now().Add(12 * time.Second)
|
tperiod := time.Now().Add(12 * time.Second)
|
||||||
for {
|
for {
|
||||||
//if after 12 seconds, try a pull on the last try
|
//if after 12 seconds, try a pull on the last try
|
||||||
@@ -258,6 +259,7 @@ func setupMQTT(cfg *config.ClientConfig, server string, publish bool) mqtt.Clien
|
|||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
}
|
}
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
|
||||||
logger.Log(0, "unable to connect to broker, retrying ...")
|
logger.Log(0, "unable to connect to broker, retrying ...")
|
||||||
if time.Now().After(tperiod) {
|
if time.Now().After(tperiod) {
|
||||||
logger.Log(0, "could not connect to broker, exiting ", cfg.Node.Network, " setup: ", token.Error().Error())
|
logger.Log(0, "could not connect to broker, exiting ", cfg.Node.Network, " setup: ", token.Error().Error())
|
||||||
|
62
netclient/functions/localport.go
Normal file
62
netclient/functions/localport.go
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
package functions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/logger"
|
||||||
|
"github.com/gravitl/netmaker/netclient/config"
|
||||||
|
"github.com/gravitl/netmaker/netclient/local"
|
||||||
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Get LocalListenPort - Gets the port running on the local interface
|
||||||
|
func GetLocalListenPort(ifacename string) (int32, error) {
|
||||||
|
portstring, err := ncutils.RunCmd("wg show "+ifacename+" listen-port", false)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
portstring = strings.TrimSuffix(portstring, "\n")
|
||||||
|
i, err := strconv.ParseInt(portstring, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
} else if i == 0 {
|
||||||
|
return 0, errors.New("parsed port is unset or invalid")
|
||||||
|
}
|
||||||
|
return int32(i), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateLocalListenPort(nodeCfg *config.ClientConfig) error {
|
||||||
|
var err error
|
||||||
|
ifacename := getRealIface(nodeCfg.Node.Interface, nodeCfg.Node.Address)
|
||||||
|
localPort, err := GetLocalListenPort(ifacename)
|
||||||
|
if err != nil {
|
||||||
|
logger.Log(1, "error encountered checking local listen port: ", err.Error())
|
||||||
|
} else if nodeCfg.Node.LocalListenPort != localPort && localPort != 0 {
|
||||||
|
logger.Log(1, "local port has changed from ", strconv.Itoa(int(nodeCfg.Node.LocalListenPort)), " to ", strconv.Itoa(int(localPort)))
|
||||||
|
nodeCfg.Node.LocalListenPort = localPort
|
||||||
|
err = config.ModConfig(&nodeCfg.Node)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Println("server: " + nodeCfg.Server.Server)
|
||||||
|
if err := PublishNodeUpdate(nodeCfg); err != nil {
|
||||||
|
logger.Log(0, "could not publish local port change")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRealIface(ifacename string, address string) string {
|
||||||
|
var deviceiface = ifacename
|
||||||
|
var err error
|
||||||
|
if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
|
||||||
|
deviceiface, err = local.GetMacIface(address)
|
||||||
|
if err != nil || deviceiface == "" {
|
||||||
|
deviceiface = ifacename
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return deviceiface
|
||||||
|
}
|
@@ -155,6 +155,7 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) {
|
|||||||
// logger.Log(0, "error applying dns" + err.Error())
|
// logger.Log(0, "error applying dns" + err.Error())
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
_ = UpdateLocalListenPort(&nodeCfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdatePeers -- mqtt message handler for peers/<Network>/<NodeID> topic
|
// UpdatePeers -- mqtt message handler for peers/<Network>/<NodeID> topic
|
||||||
@@ -213,6 +214,7 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ = UpdateLocalListenPort(&cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setHostDNS(dns, iface string, windows bool) error {
|
func setHostDNS(dns, iface string, windows bool) error {
|
||||||
|
@@ -6,14 +6,12 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/logger"
|
"github.com/gravitl/netmaker/logger"
|
||||||
"github.com/gravitl/netmaker/netclient/auth"
|
"github.com/gravitl/netmaker/netclient/auth"
|
||||||
"github.com/gravitl/netmaker/netclient/config"
|
"github.com/gravitl/netmaker/netclient/config"
|
||||||
"github.com/gravitl/netmaker/netclient/local"
|
|
||||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
"github.com/gravitl/netmaker/tls"
|
"github.com/gravitl/netmaker/tls"
|
||||||
)
|
)
|
||||||
@@ -62,23 +60,8 @@ func Checkin(ctx context.Context, wg *sync.WaitGroup) {
|
|||||||
logger.Log(0, "could not publish local address change")
|
logger.Log(0, "could not publish local address change")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var deviceiface = nodeCfg.Node.Interface
|
_ = UpdateLocalListenPort(&nodeCfg)
|
||||||
if ncutils.IsMac() { // if node is Mac (Darwin) get the tunnel name first
|
|
||||||
deviceiface, err = local.GetMacIface(nodeCfg.Node.Address)
|
|
||||||
if err != nil || deviceiface == "" {
|
|
||||||
deviceiface = nodeCfg.Node.Interface
|
|
||||||
}
|
|
||||||
}
|
|
||||||
localPort, errN := local.GetLocalListenPort(deviceiface)
|
|
||||||
if errN != nil {
|
|
||||||
logger.Log(1, "error encountered checking local listen port: ", err.Error())
|
|
||||||
} else if nodeCfg.Node.LocalListenPort != localPort && localPort != 0 {
|
|
||||||
logger.Log(1, "local port has changed from ", strconv.Itoa(int(nodeCfg.Node.LocalListenPort)), " to ", strconv.Itoa(int(localPort)))
|
|
||||||
nodeCfg.Node.LocalListenPort = localPort
|
|
||||||
if err := PublishNodeUpdate(&nodeCfg); err != nil {
|
|
||||||
logger.Log(0, "could not publish local port change")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if nodeCfg.Node.IsLocal == "yes" && nodeCfg.Node.LocalRange != "" {
|
} else if nodeCfg.Node.IsLocal == "yes" && nodeCfg.Node.LocalRange != "" {
|
||||||
localIP, err := ncutils.GetLocalIP(nodeCfg.Node.LocalRange)
|
localIP, err := ncutils.GetLocalIP(nodeCfg.Node.LocalRange)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -115,6 +98,7 @@ func PublishNodeUpdate(nodeCfg *config.ClientConfig) error {
|
|||||||
if err = publish(nodeCfg, fmt.Sprintf("update/%s", nodeCfg.Node.ID), data, 1); err != nil {
|
if err = publish(nodeCfg, fmt.Sprintf("update/%s", nodeCfg.Node.ID), data, 1); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log(0, "sent a node update to server for node", nodeCfg.Node.Name, ", ", nodeCfg.Node.ID)
|
logger.Log(0, "sent a node update to server for node", nodeCfg.Node.Name, ", ", nodeCfg.Node.ID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -139,7 +123,6 @@ func publish(nodeCfg *config.ClientConfig, dest string, msg []byte, qos byte) er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
serverPubKey, err := ncutils.ConvertBytesToKey(nodeCfg.Node.TrafficKeys.Server)
|
serverPubKey, err := ncutils.ConvertBytesToKey(nodeCfg.Node.TrafficKeys.Server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -155,6 +138,7 @@ func publish(nodeCfg *config.ClientConfig, dest string, msg []byte, qos byte) er
|
|||||||
if token := client.Publish(dest, qos, false, encrypted); token.Wait() && token.Error() != nil {
|
if token := client.Publish(dest, qos, false, encrypted); token.Wait() && token.Error() != nil {
|
||||||
return token.Error()
|
return token.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -8,7 +8,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||||
@@ -122,19 +121,3 @@ func GetMacIface(ipstring string) (string, error) {
|
|||||||
func HasNetwork(network string) bool {
|
func HasNetwork(network string) bool {
|
||||||
return ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network)
|
return ncutils.FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get LocalListenPort - Gets the port running on the local interface
|
|
||||||
func GetLocalListenPort(ifacename string) (int32, error) {
|
|
||||||
portstring, err := ncutils.RunCmd("wg show "+ifacename+" listen-port", false)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
portstring = strings.TrimSuffix(portstring, "\n")
|
|
||||||
i, err := strconv.ParseInt(portstring, 10, 32)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
} else if i == 0 {
|
|
||||||
return 0, errors.New("parsed port is unset or invalid")
|
|
||||||
}
|
|
||||||
return int32(i), nil
|
|
||||||
}
|
|
||||||
|
@@ -233,14 +233,12 @@ func SetWGConfig(network string, peerupdate bool) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if peerupdate && !ncutils.IsFreeBSD() && !(ncutils.IsLinux() && !ncutils.IsKernel()) {
|
var iface string
|
||||||
var iface string
|
iface = nodecfg.Interface
|
||||||
iface = nodecfg.Interface
|
if ncutils.IsMac() {
|
||||||
if ncutils.IsMac() {
|
iface, err = local.GetMacIface(nodecfg.Address)
|
||||||
iface, err = local.GetMacIface(nodecfg.Address)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
err = SetPeers(iface, &nodecfg, []wgtypes.PeerConfig{})
|
err = SetPeers(iface, &nodecfg, []wgtypes.PeerConfig{})
|
||||||
} else if peerupdate {
|
} else if peerupdate {
|
||||||
|
Reference in New Issue
Block a user