Move failover command to monitor

Former-commit-id: 23e7b173c9
This commit is contained in:
Owen
2025-12-01 20:21:05 -05:00
parent a497f0873f
commit 3b2ffe006a
4 changed files with 51 additions and 64 deletions

View File

@@ -3,6 +3,7 @@ package peers
import (
"fmt"
"net"
"strings"
"sync"
"github.com/fosrl/newt/logger"
@@ -594,3 +595,34 @@ func (pm *PeerManager) RemoveAlias(siteId int, aliasName string) error {
return nil
}
// HandleFailover handles failover to the relay server when a peer is disconnected
func (pm *PeerManager) HandleFailover(siteId int, relayEndpoint string) {
pm.mu.RLock()
peer, exists := pm.peers[siteId]
pm.mu.RUnlock()
if !exists {
logger.Error("Cannot handle failover: peer with site ID %d not found", siteId)
return
}
// Check for IPv6 and format the endpoint correctly
formattedEndpoint := relayEndpoint
if strings.Contains(relayEndpoint, ":") {
formattedEndpoint = fmt.Sprintf("[%s]", relayEndpoint)
}
// Update only the endpoint for this peer (update_only preserves other settings)
wgConfig := fmt.Sprintf(`public_key=%s
update_only=true
endpoint=%s:21820`, peer.PublicKey, formattedEndpoint)
err := pm.device.IpcSet(wgConfig)
if err != nil {
logger.Error("Failed to configure WireGuard device: %v\n", err)
return
}
logger.Info("Adjusted peer %d to point to relay!\n", siteId)
}