Skip to content

Commit

Permalink
forbid duplicated ip in Edit node
Browse files Browse the repository at this point in the history
  • Loading branch information
yabinma committed Nov 15, 2024
1 parent e721552 commit c082492
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,11 @@ func updateNode(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
err = logic.ValidateNodeIp(&currentNode, &newData)
if err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
if !servercfg.IsPro {
newData.AdditionalRagIps = []string{}
}
Expand Down
30 changes: 30 additions & 0 deletions logic/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c082492

Please sign in to comment.