mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 10:21:52 +00:00
better validation
This commit is contained in:
@@ -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++) {
|
||||
|
||||
@@ -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]) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user