diff --git a/netclient/functions/daemon.go b/netclient/functions/daemon.go index dcb16e1a..b339e1b4 100644 --- a/netclient/functions/daemon.go +++ b/netclient/functions/daemon.go @@ -61,8 +61,16 @@ func MessageQueue(ctx context.Context, network string) { if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil { log.Fatal(token.Error()) } - client.AddRoute("update/"+cfg.Node.ID, NodeUpdate) - client.AddRoute("update/peers/"+cfg.Node.ID, UpdatePeers) + if token := client.Subscribe("update/"+cfg.Node.ID, 0, NodeUpdate); token.Wait() && token.Error() != nil { + log.Fatal(token.Error()) + } + if token := client.Subscribe("/update/peers/"+cfg.Node.ID, 0, UpdatePeers); token.Wait() && token.Error() != nil { + log.Fatal(token.Error()) + } + + //addroute doesn't seem to work consistently + //client.AddRoute("update/"+cfg.Node.ID, NodeUpdate) + //client.AddRoute("update/peers/"+cfg.Node.ID, UpdatePeers) //handle key updates in node update //client.AddRoute("update/keys/"+cfg.Node.ID, UpdateKeys) defer client.Disconnect(250) @@ -73,8 +81,9 @@ func MessageQueue(ctx context.Context, network string) { // All -- mqtt message hander for all ('#') topics var All mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { + ncutils.Log("default message handler -- received message but not handling") ncutils.Log("Topic: " + string(msg.Topic())) - ncutils.Log("Message: " + string(msg.Payload())) + //ncutils.Log("Message: " + string(msg.Payload())) } // NodeUpdate -- mqtt message handler for /update/ topic @@ -137,7 +146,6 @@ var NodeUpdate mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) // UpdatePeers -- mqtt message handler for /update/peers/ topic var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { - ncutils.Log("received message to update peers " + string(msg.Payload())) go func() { var peerUpdate models.PeerUpdate err := json.Unmarshal(msg.Payload(), &peerUpdate) @@ -145,6 +153,14 @@ var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) ncutils.Log("error unmarshalling peer data") return } + ncutils.Log("update peer handler") + ncutils.Log("recieved " + string(len(peerUpdate.Peers)) + "peers to update") + ncutils.Log(string(msg.Payload())) + ncutils.Log(peerUpdate.Network) + for _, peer := range peerUpdate.Peers { + key := peer.PublicKey.String() + ncutils.Log(key) + } var cfg config.ClientConfig cfg.Network = peerUpdate.Network cfg.ReadConfig() @@ -153,8 +169,9 @@ var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) ncutils.Log("error updating wireguard peers" + err.Error()) return } - // path hardcoded for now... should be updated - err = wireguard.ApplyWGQuickConf("/etc/netclient/config/" + cfg.Node.Interface + ".conf") + file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf" + ncutils.Log("applyWGQuickConf to " + file) + err = wireguard.ApplyWGQuickConf(file) if err != nil { ncutils.Log("error restarting wg after peer update " + err.Error()) return diff --git a/netclient/wireguard/common.go b/netclient/wireguard/common.go index 3f11fd36..123705ad 100644 --- a/netclient/wireguard/common.go +++ b/netclient/wireguard/common.go @@ -327,8 +327,12 @@ func WriteWgConfig(cfg config.ClientConfig, privateKey string, peers []wgtypes.P } if peer.AllowedIPs != nil { var allowedIPs string - for _, ip := range peer.AllowedIPs { - allowedIPs = allowedIPs + ", " + ip.String() + for i, ip := range peer.AllowedIPs { + if i == 0 { + allowedIPs = ip.String() + } else { + allowedIPs = allowedIPs + ", " + ip.String() + } } wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs) } @@ -344,21 +348,27 @@ func WriteWgConfig(cfg config.ClientConfig, privateKey string, peers []wgtypes.P // UpdateWgPeers - updates the peers of a network func UpdateWgPeers(wgInterface string, peers []wgtypes.PeerConfig) error { - //update to get path properly file := ncutils.GetNetclientPathSpecific() + wgInterface + ".conf" + ncutils.Log("updating " + file) wireguard, err := ini.ShadowLoad(file) if err != nil { return err } + //delete the peers sections as they are going to be replaced + wireguard.DeleteSection(section_peers) for i, peer := range peers { wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String()) - if peer.PresharedKey.String() != "" { - wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String()) - } + //if peer.PresharedKey.String() != "" { + //wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String()) + //} if peer.AllowedIPs != nil { var allowedIPs string - for _, ip := range peer.AllowedIPs { - allowedIPs = allowedIPs + ", " + ip.String() + for i, ip := range peer.AllowedIPs { + if i == 0 { + allowedIPs = ip.String() + } else { + allowedIPs = allowedIPs + ", " + ip.String() + } } wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs) } diff --git a/netclient/wireguard/unix.go b/netclient/wireguard/unix.go index 142aa920..303d1636 100644 --- a/netclient/wireguard/unix.go +++ b/netclient/wireguard/unix.go @@ -53,8 +53,19 @@ func SetWGKeyConfig(network string, serveraddr string) error { // ApplyWGQuickConf - applies wg-quick commands if os supports func ApplyWGQuickConf(confPath string) error { - _, _ = ncutils.RunCmd("wg-quick down "+confPath, false) - _, err := ncutils.RunCmd("wg-quick up "+confPath, false) + _, err := os.Stat(confPath) + if err != nil { + ncutils.Log(confPath + " does not exist " + err.Error()) + return err + } + _, err = ncutils.RunCmd("wg-quick down "+confPath, false) + if err != nil { + ncutils.Log("err runing wg-quick down " + confPath + err.Error()) + } + _, err = ncutils.RunCmd("wg-quick up "+confPath, false) + if err != nil { + ncutils.Log("err runing wg-quick up " + confPath + err.Error()) + } return err }