From 7bfbf06eb511db2cb398dcc34ff36dcdd405d80e Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Mon, 11 Jul 2022 08:15:46 -0400 Subject: [PATCH 1/3] update logic for determing when to check for free port --- netclient/functions/localport.go | 4 ++-- netclient/functions/localport_freebsd.go | 2 +- netclient/functions/mqhandlers.go | 15 ++++++++++----- netclient/functions/pull.go | 5 ++++- netclient/wireguard/common.go | 2 +- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/netclient/functions/localport.go b/netclient/functions/localport.go index 97a65f4f..976515a1 100644 --- a/netclient/functions/localport.go +++ b/netclient/functions/localport.go @@ -23,7 +23,7 @@ func GetLocalListenPort(ifacename string) (int32, error) { defer client.Close() device, err := client.Device(ifacename) if err != nil { - logger.Log(0, "failed to parse interface") + logger.Log(0, "failed to parse interface", ifacename) return 0, err } return int32(device.ListenPort), nil @@ -35,7 +35,7 @@ func UpdateLocalListenPort(nodeCfg *config.ClientConfig) 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()) + logger.Log(1, "error encountered checking local listen port: ", ifacename, 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 diff --git a/netclient/functions/localport_freebsd.go b/netclient/functions/localport_freebsd.go index ab02fc9e..a24491a8 100644 --- a/netclient/functions/localport_freebsd.go +++ b/netclient/functions/localport_freebsd.go @@ -34,7 +34,7 @@ func UpdateLocalListenPort(nodeCfg *config.ClientConfig) error { var err error localPort, err := GetLocalListenPort(nodeCfg.Node.Interface) if err != nil { - logger.Log(1, "error encountered checking local listen port: ", err.Error()) + logger.Log(1, "error encountered checking local listen port for interface : ",nodeCfg.Node.Interface 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 diff --git a/netclient/functions/mqhandlers.go b/netclient/functions/mqhandlers.go index 2912fa58..34ed633e 100644 --- a/netclient/functions/mqhandlers.go +++ b/netclient/functions/mqhandlers.go @@ -106,12 +106,17 @@ func NodeUpdate(client mqtt.Client, msg mqtt.Message) { file := ncutils.GetNetclientPathSpecific() + nodeCfg.Node.Interface + ".conf" if ifaceDelta { // if a change caused an ifacedelta we need to notify the server to update the peers - err = ncutils.ModPort(&newNode) - if err != nil { - logger.Log(0, "error modifying node port on", newNode.Name, "-", err.Error()) - return + if newNode.ListenPort != nodeCfg.Node.LocalListenPort { + if err := wireguard.RemoveConf(newNode.Interface, false); err != nil { + logger.Log(0, "error remove interface", newNode.Interface, err.Error()) + } + err = ncutils.ModPort(&newNode) + if err != nil { + logger.Log(0, "error modifying node port on", newNode.Name, "-", err.Error()) + return + } + informPortChange(&newNode) } - informPortChange(&newNode) if err := wireguard.UpdateWgInterface(file, privateKey, nameserver, newNode); err != nil { logger.Log(0, "error updating wireguard config "+err.Error()) return diff --git a/netclient/functions/pull.go b/netclient/functions/pull.go index b9457b23..87886a3f 100644 --- a/netclient/functions/pull.go +++ b/netclient/functions/pull.go @@ -62,7 +62,10 @@ func Pull(network string, iface bool) (*models.Node, error) { logger.Log(0, "unable to update server config: "+err.Error()) } } - if nodeGET.Node.ListenPort != cfg.Node.ListenPort { + if nodeGET.Node.ListenPort != cfg.Node.LocalListenPort { + if err := wireguard.RemoveConf(resNode.Interface, false); err != nil { + logger.Log(0, "error remove interface", resNode.Interface, err.Error()) + } err = ncutils.ModPort(&resNode) if err != nil { return nil, err diff --git a/netclient/wireguard/common.go b/netclient/wireguard/common.go index 67f2cfc7..79473fd9 100644 --- a/netclient/wireguard/common.go +++ b/netclient/wireguard/common.go @@ -548,7 +548,7 @@ func GetDevicePeers(iface string) ([]wgtypes.Peer, error) { defer client.Close() device, err := client.Device(iface) if err != nil { - logger.Log(0, "failed to parse interface") + logger.Log(0, "failed to parse interface", iface) return nil, err } return device.Peers, nil From 665863d83df87c547d18995d9ddf1dbbbb7284e2 Mon Sep 17 00:00:00 2001 From: afeiszli Date: Mon, 11 Jul 2022 10:20:49 -0400 Subject: [PATCH 2/3] changing traffic policy --- k8s/server/mosquitto.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s/server/mosquitto.yaml b/k8s/server/mosquitto.yaml index abdc7337..010c42f7 100644 --- a/k8s/server/mosquitto.yaml +++ b/k8s/server/mosquitto.yaml @@ -138,7 +138,7 @@ metadata: labels: name: 'netmaker-mqtt' spec: - externalTrafficPolicy: Local + externalTrafficPolicy: Cluster type: NodePort selector: app.kubernetes.io/instance: mosquitto From 70aa4d717fb214c47f182accb1d294841e19dd0c Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Mon, 11 Jul 2022 11:59:05 -0400 Subject: [PATCH 3/3] fixed missing comma --- netclient/functions/localport_freebsd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netclient/functions/localport_freebsd.go b/netclient/functions/localport_freebsd.go index a24491a8..726dbb21 100644 --- a/netclient/functions/localport_freebsd.go +++ b/netclient/functions/localport_freebsd.go @@ -34,7 +34,7 @@ func UpdateLocalListenPort(nodeCfg *config.ClientConfig) error { var err error localPort, err := GetLocalListenPort(nodeCfg.Node.Interface) if err != nil { - logger.Log(1, "error encountered checking local listen port for interface : ",nodeCfg.Node.Interface err.Error()) + logger.Log(1, "error encountered checking local listen port for interface : ", nodeCfg.Node.Interface, 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