mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-16 13:51:42 +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" {
|
||||
network, err := GetNetwork(networkName)
|
||||
if err == nil {
|
||||
|
@@ -218,6 +218,7 @@ func setupMQTT(cfg *config.ClientConfig, server string, publish bool) mqtt.Clien
|
||||
opts.SetConnectRetryInterval(time.Second << 2)
|
||||
opts.SetKeepAlive(time.Minute >> 1)
|
||||
opts.SetWriteTimeout(time.Minute)
|
||||
|
||||
opts.SetOnConnectHandler(func(client mqtt.Client) {
|
||||
if !publish {
|
||||
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")
|
||||
})
|
||||
|
||||
client := mqtt.NewClient(opts)
|
||||
|
||||
tperiod := time.Now().Add(12 * time.Second)
|
||||
for {
|
||||
//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)
|
||||
}
|
||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||
|
||||
logger.Log(0, "unable to connect to broker, retrying ...")
|
||||
if time.Now().After(tperiod) {
|
||||
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())
|
||||
// }
|
||||
}
|
||||
_ = UpdateLocalListenPort(&nodeCfg)
|
||||
}
|
||||
|
||||
// UpdatePeers -- mqtt message handler for peers/<Network>/<NodeID> topic
|
||||
@@ -213,6 +214,7 @@ func UpdatePeers(client mqtt.Client, msg mqtt.Message) {
|
||||
return
|
||||
}
|
||||
}
|
||||
_ = UpdateLocalListenPort(&cfg)
|
||||
}
|
||||
|
||||
func setHostDNS(dns, iface string, windows bool) error {
|
||||
|
@@ -6,14 +6,12 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gravitl/netmaker/logger"
|
||||
"github.com/gravitl/netmaker/netclient/auth"
|
||||
"github.com/gravitl/netmaker/netclient/config"
|
||||
"github.com/gravitl/netmaker/netclient/local"
|
||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||
"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")
|
||||
}
|
||||
}
|
||||
var deviceiface = nodeCfg.Node.Interface
|
||||
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")
|
||||
}
|
||||
}
|
||||
_ = UpdateLocalListenPort(&nodeCfg)
|
||||
|
||||
} else if nodeCfg.Node.IsLocal == "yes" && nodeCfg.Node.LocalRange != "" {
|
||||
localIP, err := ncutils.GetLocalIP(nodeCfg.Node.LocalRange)
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
logger.Log(0, "sent a node update to server for node", nodeCfg.Node.Name, ", ", nodeCfg.Node.ID)
|
||||
return nil
|
||||
}
|
||||
@@ -139,7 +123,6 @@ func publish(nodeCfg *config.ClientConfig, dest string, msg []byte, qos byte) er
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serverPubKey, err := ncutils.ConvertBytesToKey(nodeCfg.Node.TrafficKeys.Server)
|
||||
if err != nil {
|
||||
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 {
|
||||
return token.Error()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -8,7 +8,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gravitl/netmaker/netclient/ncutils"
|
||||
@@ -122,19 +121,3 @@ func GetMacIface(ipstring string) (string, error) {
|
||||
func HasNetwork(network string) bool {
|
||||
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,7 +233,6 @@ func SetWGConfig(network string, peerupdate bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if peerupdate && !ncutils.IsFreeBSD() && !(ncutils.IsLinux() && !ncutils.IsKernel()) {
|
||||
var iface string
|
||||
iface = nodecfg.Interface
|
||||
if ncutils.IsMac() {
|
||||
@@ -241,7 +240,6 @@ func SetWGConfig(network string, peerupdate bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err = SetPeers(iface, &nodecfg, []wgtypes.PeerConfig{})
|
||||
} else if peerupdate {
|
||||
err = InitWireguard(&nodecfg, privkey, []wgtypes.PeerConfig{}, true)
|
||||
|
Reference in New Issue
Block a user