добавил валидацию подсетей
This commit is contained in:
parent
656e7c03ba
commit
59a461eab2
|
|
@ -35,6 +35,10 @@ App.auth = async function () {
|
||||||
this.fillNetworks();
|
this.fillNetworks();
|
||||||
|
|
||||||
$("#panel").show();
|
$("#panel").show();
|
||||||
|
|
||||||
|
let invalidNetworks = await JSONRPC.__invoke("getInvalidNetworks");
|
||||||
|
|
||||||
|
$("body").append(`<span>There are invalid networks ` +invalidNetworks.join("<BR>")+` </span>`);
|
||||||
}
|
}
|
||||||
|
|
||||||
App.fillNetworks = function () {
|
App.fillNetworks = function () {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class IPv4Subnet
|
||||||
|
{
|
||||||
|
|
||||||
|
private string $address;
|
||||||
|
private int $prefix;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $subnetAddress
|
||||||
|
* @param int $prefix
|
||||||
|
*/
|
||||||
|
public function __construct(string $subnetAddress, int $prefix)
|
||||||
|
{
|
||||||
|
$this->address = $subnetAddress;
|
||||||
|
if (ip2long($subnetAddress) === false) {
|
||||||
|
throw new RuntimeException("Invalid subnet address: " . $subnetAddress);
|
||||||
|
}
|
||||||
|
$this->prefix = $prefix;
|
||||||
|
if ($prefix < 0 or $prefix > 32) {
|
||||||
|
throw new RuntimeException("Invalid subnet prefix: " . $prefix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFirstAddress()
|
||||||
|
{
|
||||||
|
$a = ip2long($this->address);
|
||||||
|
$mask = ip2long($this->getNetMask());
|
||||||
|
return long2ip($a & $mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLastAddress()
|
||||||
|
{
|
||||||
|
return long2ip(ip2long($this->getFirstAddress()) + $this->getAddressCount() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAddressCount()
|
||||||
|
{
|
||||||
|
return pow(2, 32 - $this->prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getNetMask()
|
||||||
|
{
|
||||||
|
return long2ip(-1 << (32 - $this->prefix));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -88,6 +88,20 @@ class RPC
|
||||||
return (new NetworkConfigReader())->getConfigs();
|
return (new NetworkConfigReader())->getConfigs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getInvalidNetworks(): array
|
||||||
|
{
|
||||||
|
$this->checkAuth();
|
||||||
|
$invalid = [];
|
||||||
|
foreach ((new NetworkConfigReader())->getConfigs() as $config) {
|
||||||
|
foreach ($config["networks"] as $network) {
|
||||||
|
if (!RouteUtil::validateSubnet($network)) {
|
||||||
|
$invalid[] = $network;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $invalid;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function logout(): void
|
public function logout(): void
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class RouteUtil
|
||||||
|
{
|
||||||
|
public static function validateSubnet($subnet)
|
||||||
|
{
|
||||||
|
$parts = explode("/", $subnet);
|
||||||
|
if (count($parts) != 2) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$ipv4subnet = new IPv4Subnet($parts[0], $parts[1]);
|
||||||
|
return $parts[0] == $ipv4subnet->getFirstAddress();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue