update isInSubnet function
This commit is contained in:
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user