better validation

This commit is contained in:
Aiden McClelland
2025-11-05 17:05:56 -07:00
parent 7e888b825c
commit 18b659653d
3 changed files with 5 additions and 5 deletions

View File

@@ -31,7 +31,7 @@ export class IpAddress {
octets = address.split(".").map(Number)
if (octets.length !== 4) throw new Error("invalid ipv4 address")
}
if (octets.some((o) => o > 255)) {
if (octets.some((o) => isNaN(o) || o > 255)) {
throw new Error("invalid ip address")
}
return new IpAddress(octets, address)
@@ -175,7 +175,7 @@ export class IpNet extends IpAddress {
return IpAddress.fromOctets(octets)
}
last(): IpAddress {
broadcast(): IpAddress {
let octets: number[] = []
let prefix = this.prefix
for (let idx = 0; idx < this.octets.length; idx++) {

View File

@@ -40,7 +40,7 @@ export const ipInSubnetValidator = (subnet: string | null = null) => {
return { invalidIp: 'Not a valid IP Address' }
}
if (!ipnet) return null
return ipnet.contains(ip)
return ipnet.zero().cmp(ip) === -1 && ipnet.broadcast().cmp(ip) === 1
? null
: { notInSubnet: `Address is not part of ${subnet}` }
}
@@ -48,7 +48,7 @@ export const ipInSubnetValidator = (subnet: string | null = null) => {
export function getIp({ clients, range }: MappedSubnet) {
const net = IpNet.parse(range)
const last = net.last()
const last = net.broadcast()
for (let ip = net.add(1); ip.cmp(last) === -1; ip.add(1)) {
if (!clients[ip.address]) {

View File

@@ -135,7 +135,7 @@ export default class Subnets {
this.subnets()
.map(s => utils.IpNet.parse(s.range))
.sort((a, b) => -1 * a.cmp(b))[0] ?? utils.IpNet.parse('10.58.255.0/24')
const next = utils.IpNet.fromIpPrefix(last.last().add(2), 24)
const next = utils.IpNet.fromIpPrefix(last.broadcast().add(2), 24)
if (!next.isPublic()) {
return next.ipnet
}