update isInSubnet function

This commit is contained in:
zefie
2025-03-09 08:53:43 -04:00
parent 832297e51d
commit 3e957a9b95

View File

@@ -105,15 +105,17 @@ class WTVAdmin {
}
isInSubnet(ip, subnet) {
if (subnet.indexOf('/') == -1) {
var mask, base_ip, long_ip = this.ip2long(ip);
var mask2, base_ip2, long_ip2 = this.ip2long(ip);
return (long_ip == long_ip2);
// If subnet is given as a single IP address (no CIDR notation)
if (subnet.indexOf('/') === -1) {
return this.ip2long(ip) === this.ip2long(subnet);
} else {
var mask, base_ip, long_ip = this.ip2long(ip);
if ((mask = subnet.match(/^(.*?)\/(\d{1,2})$/)) && ((base_ip = this.ip2long(mask[1])) >= 0)) {
var freedom = Math.pow(2, 32 - parseInt(mask[2]));
return (long_ip > base_ip) && (long_ip < base_ip + freedom - 1);
// Expect subnet in format "base_ip/prefix_length"
let parts = subnet.match(/^(.*?)\/(\d{1,2})$/);
if (parts && (this.ip2long(parts[1]) >= 0)) {
let base_ip = this.ip2long(parts[1]);
let prefixLength = parseInt(parts[2]);
let freedom = Math.pow(2, 32 - prefixLength);
return (this.ip2long(ip) >= base_ip) && (this.ip2long(ip) < base_ip + freedom);
}
}
return false;