From 35544e108183408424d0e1c9b15c4c954db9637e Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 5 Dec 2025 12:05:48 -0500 Subject: [PATCH] Fix changing alias Former-commit-id: 039110647705b9a23fdc1fed7d3b02a75d2a3739 --- olm/olm.go | 18 ++++++++++-------- peers/manager.go | 22 +++++++++++++++++----- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/olm/olm.go b/olm/olm.go index 7f52ce9..853bac9 100644 --- a/olm/olm.go +++ b/olm/olm.go @@ -670,20 +670,22 @@ func StartTunnel(config TunnelConfig) { } } - // Remove old aliases - for _, alias := range updateSubnetsData.OldAliases { - if err := peerManager.RemoveAlias(updateSubnetsData.SiteId, alias.Alias); err != nil { - logger.Error("Failed to remove alias %s: %v", alias.Alias, err) - } - } - - // Add new aliases + // Add new aliases BEFORE removing old ones to preserve shared IP addresses + // This ensures that if an old and new alias share the same IP, the IP won't be + // temporarily removed from the allowed IPs list for _, alias := range updateSubnetsData.NewAliases { if err := peerManager.AddAlias(updateSubnetsData.SiteId, alias); err != nil { logger.Error("Failed to add alias %s: %v", alias.Alias, err) } } + // Remove old aliases after new ones are added + for _, alias := range updateSubnetsData.OldAliases { + if err := peerManager.RemoveAlias(updateSubnetsData.SiteId, alias.Alias); err != nil { + logger.Error("Failed to remove alias %s: %v", alias.Alias, err) + } + } + logger.Info("Successfully updated remote subnets and aliases for peer %d", updateSubnetsData.SiteId) }) diff --git a/peers/manager.go b/peers/manager.go index 78681e1..f21d117 100644 --- a/peers/manager.go +++ b/peers/manager.go @@ -661,14 +661,26 @@ func (pm *PeerManager) RemoveAlias(siteId int, aliasName string) error { } } - // remove the allowed IP for the alias - if err := pm.removeAllowedIp(siteId, aliasToRemove.AliasAddress+"/32"); err != nil { - return err - } - peer.Aliases = newAliases pm.peers[siteId] = peer + // Check if any other alias is still using this IP address before removing from allowed IPs + ipStillInUse := false + aliasIP := aliasToRemove.AliasAddress + "/32" + for _, a := range newAliases { + if a.AliasAddress+"/32" == aliasIP { + ipStillInUse = true + break + } + } + + // Only remove the allowed IP if no other alias is using it + if !ipStillInUse { + if err := pm.removeAllowedIp(siteId, aliasIP); err != nil { + return err + } + } + return nil }