mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-10 23:24:32 +08:00
fix address range check
This commit is contained in:
parent
a81de819d2
commit
131f70b2a4
2 changed files with 3 additions and 18 deletions
|
@ -50,7 +50,7 @@ func GetNetworkNodes(network string) ([]models.Node, error) {
|
||||||
func UpdateNode(currentNode *models.Node, newNode *models.Node) error {
|
func UpdateNode(currentNode *models.Node, newNode *models.Node) error {
|
||||||
if newNode.Address.IP.String() != currentNode.Address.IP.String() {
|
if newNode.Address.IP.String() != currentNode.Address.IP.String() {
|
||||||
if network, err := GetParentNetwork(newNode.Network); err == nil {
|
if network, err := GetParentNetwork(newNode.Network); err == nil {
|
||||||
if !IsAddressInCIDR(newNode.Address.IP.String(), network.AddressRange) {
|
if !IsAddressInCIDR(newNode.Address.IP, network.AddressRange) {
|
||||||
return fmt.Errorf("invalid address provided; out of network range for node %s", newNode.ID)
|
return fmt.Errorf("invalid address provided; out of network range for node %s", newNode.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
@ -40,26 +39,12 @@ func FileExists(f string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAddressInCIDR - util to see if an address is in a cidr or not
|
// IsAddressInCIDR - util to see if an address is in a cidr or not
|
||||||
func IsAddressInCIDR(address, cidr string) bool {
|
func IsAddressInCIDR(address net.IP, cidr string) bool {
|
||||||
var _, currentCIDR, cidrErr = net.ParseCIDR(cidr)
|
var _, currentCIDR, cidrErr = net.ParseCIDR(cidr)
|
||||||
if cidrErr != nil {
|
if cidrErr != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
var addrParts = strings.Split(address, ".")
|
return currentCIDR.Contains(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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetNetworkNodesLastModified - sets the network nodes last modified
|
// SetNetworkNodesLastModified - sets the network nodes last modified
|
||||||
|
|
Loading…
Add table
Reference in a new issue