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