mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 16:57:51 +08:00
prevent nodes from changing address out of range or to .0 or .255 addresses
This commit is contained in:
@@ -4,7 +4,9 @@ package logic
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -39,6 +41,29 @@ func FileExists(f string) bool {
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
// IsAddressInCIDR - util to see if an address is in a cidr or not
|
||||
func IsAddressInCIDR(address, cidr string) bool {
|
||||
var _, currentCIDR, cidrErr = net.ParseCIDR(cidr)
|
||||
if cidrErr != nil {
|
||||
return false
|
||||
}
|
||||
var addrParts = strings.Split(address, ".")
|
||||
var addrPartLength = len(addrParts)
|
||||
if addrPartLength != 4 {
|
||||
return false
|
||||
} else {
|
||||
if addrParts[addrPartLength-1] == "0" ||
|
||||
addrParts[addrPartLength-1] == "255" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
ip, _, err := net.ParseCIDR(fmt.Sprintf("%s/32", address))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return currentCIDR.Contains(ip)
|
||||
}
|
||||
|
||||
// DeleteNodeByMacAddress - deletes a node from database or moves into delete nodes table
|
||||
func DeleteNodeByMacAddress(node *models.Node, exterminate bool) error {
|
||||
var err error
|
||||
|
Reference in New Issue
Block a user