prevent nodes from changing address out of range or to .0 or .255 addresses

This commit is contained in:
0xdcarns
2022-01-19 10:44:00 -05:00
parent de8c4d782d
commit 2430eb0a47
6 changed files with 43 additions and 18 deletions

View File

@@ -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