From c0824921038d0f28ea03ff67a807c030d60f998d Mon Sep 17 00:00:00 2001 From: Max Ma Date: Fri, 15 Nov 2024 12:54:31 +0100 Subject: [PATCH] forbid duplicated ip in Edit node --- controllers/node.go | 5 +++++ logic/nodes.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/controllers/node.go b/controllers/node.go index d7f2e2569..e96e2a443 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -679,6 +679,11 @@ func updateNode(w http.ResponseWriter, r *http.Request) { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) return } + err = logic.ValidateNodeIp(¤tNode, &newData) + if err != nil { + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } if !servercfg.IsPro { newData.AdditionalRagIps = []string{} } diff --git a/logic/nodes.go b/logic/nodes.go index 34eebe2e4..63bbe5dce 100644 --- a/logic/nodes.go +++ b/logic/nodes.go @@ -179,6 +179,16 @@ func UpdateNode(currentNode *models.Node, newNode *models.Node) error { } if servercfg.CacheEnabled() { storeNodeInCache(*newNode) + if _, ok := allocatedIpMap[newNode.Network]; ok { + if newNode.Address.IP != nil && !newNode.Address.IP.Equal(currentNode.Address.IP) { + AddIpToAllocatedIpMap(newNode.Network, newNode.Address.IP) + RemoveIpFromAllocatedIpMap(currentNode.Network, currentNode.Address.IP.String()) + } + if newNode.Address6.IP != nil && !newNode.Address6.IP.Equal(currentNode.Address6.IP) { + AddIpToAllocatedIpMap(newNode.Network, newNode.Address6.IP) + RemoveIpFromAllocatedIpMap(currentNode.Network, currentNode.Address6.IP.String()) + } + } } return nil } @@ -663,6 +673,26 @@ func ValidateParams(nodeid, netid string) (models.Node, error) { return node, nil } +func ValidateNodeIp(currentNode *models.Node, newNode *models.ApiNode) error { + + if currentNode.Address.IP != nil && currentNode.Address.String() != newNode.Address { + newIp, _, _ := net.ParseCIDR(newNode.Address) + ipAllocated := allocatedIpMap[currentNode.Network] + if _, ok := ipAllocated[newIp.String()]; ok { + return errors.New("ip specified is already allocated: " + newNode.Address) + } + } + if currentNode.Address6.IP != nil && currentNode.Address6.String() != newNode.Address6 { + newIp, _, _ := net.ParseCIDR(newNode.Address6) + ipAllocated := allocatedIpMap[currentNode.Network] + if _, ok := ipAllocated[newIp.String()]; ok { + return errors.New("ip specified is already allocated: " + newNode.Address6) + } + } + + return nil +} + func ValidateEgressRange(gateway models.EgressGatewayRequest) error { network, err := GetNetworkSettings(gateway.NetID) if err != nil {