fix UpdateWgPeers

This commit is contained in:
Matthew R Kasun
2022-01-19 15:00:03 -05:00
parent fc86015c29
commit 353c92e333
3 changed files with 54 additions and 16 deletions

View File

@@ -61,8 +61,16 @@ func MessageQueue(ctx context.Context, network string) {
if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil { if token := client.Subscribe("#", 0, nil); token.Wait() && token.Error() != nil {
log.Fatal(token.Error()) log.Fatal(token.Error())
} }
client.AddRoute("update/"+cfg.Node.ID, NodeUpdate) if token := client.Subscribe("update/"+cfg.Node.ID, 0, NodeUpdate); token.Wait() && token.Error() != nil {
client.AddRoute("update/peers/"+cfg.Node.ID, UpdatePeers) 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 //handle key updates in node update
//client.AddRoute("update/keys/"+cfg.Node.ID, UpdateKeys) //client.AddRoute("update/keys/"+cfg.Node.ID, UpdateKeys)
defer client.Disconnect(250) defer client.Disconnect(250)
@@ -73,8 +81,9 @@ func MessageQueue(ctx context.Context, network string) {
// All -- mqtt message hander for all ('#') topics // All -- mqtt message hander for all ('#') topics
var All mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { 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("Topic: " + string(msg.Topic()))
ncutils.Log("Message: " + string(msg.Payload())) //ncutils.Log("Message: " + string(msg.Payload()))
} }
// NodeUpdate -- mqtt message handler for /update/<NodeID> topic // NodeUpdate -- mqtt message handler for /update/<NodeID> topic
@@ -137,7 +146,6 @@ var NodeUpdate mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message)
// UpdatePeers -- mqtt message handler for /update/peers/<NodeID> topic // UpdatePeers -- mqtt message handler for /update/peers/<NodeID> topic
var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { var UpdatePeers mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
ncutils.Log("received message to update peers " + string(msg.Payload()))
go func() { go func() {
var peerUpdate models.PeerUpdate var peerUpdate models.PeerUpdate
err := json.Unmarshal(msg.Payload(), &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") ncutils.Log("error unmarshalling peer data")
return 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 var cfg config.ClientConfig
cfg.Network = peerUpdate.Network cfg.Network = peerUpdate.Network
cfg.ReadConfig() 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()) ncutils.Log("error updating wireguard peers" + err.Error())
return return
} }
// path hardcoded for now... should be updated file := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
err = wireguard.ApplyWGQuickConf("/etc/netclient/config/" + cfg.Node.Interface + ".conf") ncutils.Log("applyWGQuickConf to " + file)
err = wireguard.ApplyWGQuickConf(file)
if err != nil { if err != nil {
ncutils.Log("error restarting wg after peer update " + err.Error()) ncutils.Log("error restarting wg after peer update " + err.Error())
return return

View File

@@ -327,8 +327,12 @@ func WriteWgConfig(cfg config.ClientConfig, privateKey string, peers []wgtypes.P
} }
if peer.AllowedIPs != nil { if peer.AllowedIPs != nil {
var allowedIPs string var allowedIPs string
for _, ip := range peer.AllowedIPs { for i, ip := range peer.AllowedIPs {
allowedIPs = allowedIPs + ", " + ip.String() if i == 0 {
allowedIPs = ip.String()
} else {
allowedIPs = allowedIPs + ", " + ip.String()
}
} }
wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs) 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 // UpdateWgPeers - updates the peers of a network
func UpdateWgPeers(wgInterface string, peers []wgtypes.PeerConfig) error { func UpdateWgPeers(wgInterface string, peers []wgtypes.PeerConfig) error {
//update to get path properly
file := ncutils.GetNetclientPathSpecific() + wgInterface + ".conf" file := ncutils.GetNetclientPathSpecific() + wgInterface + ".conf"
ncutils.Log("updating " + file)
wireguard, err := ini.ShadowLoad(file) wireguard, err := ini.ShadowLoad(file)
if err != nil { if err != nil {
return err return err
} }
//delete the peers sections as they are going to be replaced
wireguard.DeleteSection(section_peers)
for i, peer := range peers { for i, peer := range peers {
wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String()) wireguard.SectionWithIndex(section_peers, i).Key("PublicKey").SetValue(peer.PublicKey.String())
if peer.PresharedKey.String() != "" { //if peer.PresharedKey.String() != "" {
wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String()) //wireguard.SectionWithIndex(section_peers, i).Key("PreSharedKey").SetValue(peer.PresharedKey.String())
} //}
if peer.AllowedIPs != nil { if peer.AllowedIPs != nil {
var allowedIPs string var allowedIPs string
for _, ip := range peer.AllowedIPs { for i, ip := range peer.AllowedIPs {
allowedIPs = allowedIPs + ", " + ip.String() if i == 0 {
allowedIPs = ip.String()
} else {
allowedIPs = allowedIPs + ", " + ip.String()
}
} }
wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs) wireguard.SectionWithIndex(section_peers, i).Key("AllowedIps").SetValue(allowedIPs)
} }

View File

@@ -53,8 +53,19 @@ func SetWGKeyConfig(network string, serveraddr string) error {
// ApplyWGQuickConf - applies wg-quick commands if os supports // ApplyWGQuickConf - applies wg-quick commands if os supports
func ApplyWGQuickConf(confPath string) error { func ApplyWGQuickConf(confPath string) error {
_, _ = ncutils.RunCmd("wg-quick down "+confPath, false) _, err := os.Stat(confPath)
_, err := ncutils.RunCmd("wg-quick up "+confPath, false) 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 return err
} }