This commit is contained in:
Adrien MALINGREY 2025-05-27 18:06:36 +02:00
parent 0b3bd9108c
commit bf81566b88
6 changed files with 216 additions and 88 deletions

46
common.php Normal file
View File

@ -0,0 +1,46 @@
<?php
$namePtn = "[a-zA-Z_][\w-]*";
$ipPtn = "\d+\.\d+\.\d+\.\d+";
$macPtn = "[\da-f]{2}(?:[:-][\da-f]{2}){5}";
$domainPtn = "[\w-]+(\.[\w-]+)*";
$ifacePtn = "[\w,]+";
$modesPtn = "static|proxy|ra-only|slaac|ra-names|ra-stateless|ra-advrouter|off-link";
$timePtn = "\d+[mhds]";
$commentPtn = "[ \t]*(?:# ?(?<comment>.*))?";
$long_mask = [
0b00000000000000000000000000000000,
0b10000000000000000000000000000000,
0b11000000000000000000000000000000,
0b11100000000000000000000000000000,
0b11110000000000000000000000000000,
0b11111000000000000000000000000000,
0b11111100000000000000000000000000,
0b11111110000000000000000000000000,
0b11111111000000000000000000000000,
0b11111111100000000000000000000000,
0b11111111110000000000000000000000,
0b11111111111000000000000000000000,
0b11111111111100000000000000000000,
0b11111111111110000000000000000000,
0b11111111111111000000000000000000,
0b11111111111111100000000000000000,
0b11111111111111110000000000000000,
0b11111111111111111000000000000000,
0b11111111111111111100000000000000,
0b11111111111111111110000000000000,
0b11111111111111111111000000000000,
0b11111111111111111111100000000000,
0b11111111111111111111110000000000,
0b11111111111111111111111000000000,
0b11111111111111111111111100000000,
0b11111111111111111111111110000000,
0b11111111111111111111111111000000,
0b11111111111111111111111111100000,
0b11111111111111111111111111110000,
0b11111111111111111111111111111000,
0b11111111111111111111111111111100,
0b11111111111111111111111111111110,
0b11111111111111111111111111111111,
];

View File

@ -1,4 +1,5 @@
<?php
require_once "common.php";
$conf_paths = [
"/etc/dnsmasq.d/ipam.conf",
@ -17,13 +18,4 @@ foreach($conf_paths as $path) {
$conf = array_merge($default, parse_ini_file($path, true, INI_SCANNER_TYPED));
break;
}
}
$namePtn = "[a-zA-Z_][\w-]*";
$ipPtn = "\d+\.\d+\.\d+\.\d+";
$macPtn = "[\da-f]{2}(?:[:-][\da-f]{2}){5}";
$domainPtn = "[\w-]+(\.[\w-]+)*";
$ifacePtn = "[\w,]+";
$modesPtn = "static|proxya-only|slaac|ra-names|ra-stateless|ra-advrouter|off-link";
$timePtn = "\d+[mhds]";
$commentPtn = "[ \t]*(?:# ?(?<comment>.*))?";
}

View File

@ -1,78 +0,0 @@
<?php
require_once "load-conf.php";
$networks = [];
if (isset($conf["dhcp-optsfile"]) && file_exists($conf["dhcp-optsfile"])) {
foreach(file($conf["dhcp-optsfile"]) as $line) {
if (preg_match(
"/^(?:tag:(?<tag>$namePtn),)?option:(?<option>$namePtn),(?<value>[^#\r\n]*)$commentPtn$/",
$line, $option
)) {
if (!isset($networks[$option["tag"] ?? "default"])) $networks[$option["tag"] ?? "default"] = [];
$networks[$option["tag"] ?? "default"][$option["option"]] = $option["value"];
}
}
}
if (isset($conf["conf-file"]) && file_exists($conf["conf-file"])) {
foreach(file($conf["conf-file"]) as $line) {
if (preg_match(
"/^dhcp-range=(?:(?:tag|set):(?<tag>$namePtn),)?(?<start_addr>$ipPtn),(?:(?<end_addr>$ipPtn)|$modesPtn)(?:,(?<netmask>$ipPtn)(?:,$ipPtn)?)?(?:,(?<lease_time>$timePtn|infinite))?$commentPtn$/",
$line, $range, PREG_UNMATCHED_AS_NULL
)) {
if (!isset($networks[$range["tag"] ?? "default"])) $networks[$range["tag"] ?? "default"] = [];
$networks[$range["tag"] ?? "default"]["dhcp-range"] = [
"start_addr" => $range["start_addr"],
"end_addr" => $range["end_addr"],
"netmask" => $range["netmask"],
"lease_time" => $range["lease_time"],
];
}
}
}
$long_mask = [
0b00000000000000000000000000000000,
0b10000000000000000000000000000000,
0b11000000000000000000000000000000,
0b11100000000000000000000000000000,
0b11110000000000000000000000000000,
0b11111000000000000000000000000000,
0b11111100000000000000000000000000,
0b11111110000000000000000000000000,
0b11111111000000000000000000000000,
0b11111111100000000000000000000000,
0b11111111110000000000000000000000,
0b11111111111000000000000000000000,
0b11111111111100000000000000000000,
0b11111111111110000000000000000000,
0b11111111111111000000000000000000,
0b11111111111111100000000000000000,
0b11111111111111110000000000000000,
0b11111111111111111000000000000000,
0b11111111111111111100000000000000,
0b11111111111111111110000000000000,
0b11111111111111111111000000000000,
0b11111111111111111111100000000000,
0b11111111111111111111110000000000,
0b11111111111111111111111000000000,
0b11111111111111111111111100000000,
0b11111111111111111111111110000000,
0b11111111111111111111111111000000,
0b11111111111111111111111111100000,
0b11111111111111111111111111110000,
0b11111111111111111111111111111000,
0b11111111111111111111111111111100,
0b11111111111111111111111111111110,
0b11111111111111111111111111111111,
];
foreach ($networks as $tag => &$options) {
$ip_addr = ip2long($network["router"] ?? $options["dhcp-range"]["start_addr"]);
$netmask = ip2long($network["netmask"] ?? $options["dhcp-range"]["netmask"]);
$hostmask = 0xFFFFFFFF & ~$netmask;
$options["network-addr"] = $ip_addr & $netmask;
$options["broadcast"] = $ip_addr | $hostmask;
$options["cidr_mask"] = array_search($netmask, $long_mask);
}

17
load-options.php Normal file
View File

@ -0,0 +1,17 @@
<?php
require_once "common.php";
require_once "load-conf.php";
$options = [];
if (isset($conf["dhcp-optsfile"]) && file_exists($conf["dhcp-optsfile"])) {
foreach(file($conf["dhcp-optsfile"]) as $line) {
if (preg_match(
"/^(?:tag:(?<tag>$namePtn),)?option:(?<option>$namePtn),(?<value>[^#\r\n]*)$commentPtn$/",
$line, $option
)) {
if (!isset($option["tag"])) $option["tag"] = "default";
if (!isset($options[$option["tag"]])) $options[$option["tag"]] = [];
$options[$option["tag"]][$option["option"]] = $option["value"];
}
}
}

22
load-ranges.php Normal file
View File

@ -0,0 +1,22 @@
<?php
require_once "common.php";
require_once "load-conf.php";
$ranges = [];
if (isset($conf["conf-file"]) && file_exists($conf["conf-file"])) {
foreach(file($conf["conf-file"]) as $line) {
if (preg_match(
"/^dhcp-range=(?:(?:tag|set):(?<tag>$namePtn),)?(?<start_addr>$ipPtn),(?:(?<end_addr>$ipPtn)|$modesPtn)(?:,(?<netmask>$ipPtn)(?:,$ipPtn)?)?(?:,(?<lease_time>$timePtn|infinite))?$commentPtn$/",
$line, $range, PREG_UNMATCHED_AS_NULL
)) {
if (!isset($range["tag"])) $range["tag"] = "default";
if (!isset($ranges[$range["tag"]])) $ranges[$range["tag"]] = [];
$ranges[$range["tag"]]["dhcp-range"] = [
"start-addr" => $range["start_addr"],
"end-addr" => $range["end_addr"],
"netmask" => $range["netmask"],
"lease_time" => $range["lease_time"],
];
}
}
}

129
network-options.php Normal file
View File

@ -0,0 +1,129 @@
<?php
require_once "common.php";
if (preg_match("/^$namePtn$/", $_SERVER["QUERY_STRING"])) {
include_once "load-options.php";
include_once "load-ranges.php";
$tag = $_SERVER["QUERY_STRING"];
$option = $options[$tag];
$range = $ranges[$tag];
} else {
$tag = "";
$option = [];
$range = [];
}
$placeholder = [];
if (preg_match(
"/\n\d+: (?P<iface>[a-z0-9]+) inet (?<ipaddr>$ipPtn)\/(?<cidr_mask>\d+) brd (?<broadcast>$ipPtn)/",
`ip -o -4 a`,
$this_server
)) {
$placeholder["netmask"] = long2ip($long_mask[(int)$this_server["cidr_mask"]]);
}
if (preg_match(
"/default via ($ipPtn)/",
`ip -4 route`,
$router
)) {
$placeholder["router"] = $router[1];
}
$placeholder["dns-server"] = $_SERVER['SERVER_ADDR'];
preg_match_all("/\d+ (?<name>$namePtn)/", `dnsmasq --help dhcp`, $option_names, PREG_SET_ORDER);
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IPAM</title>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/fomantic-ui@2.9.4/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/fomantic-ui@2.9.4/dist/semantic.min.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<nav class="ui inverted teal menu">
<a class="header item" href=".">IP<em>AM</em></a>
</nav>
<main class="ui text container">
<h1>Éditer un réseau</h1>
<form action="update-network.php" method="post" class="ui form">
<input type="hidden" name="previous-tag" value="<?=$tag?>"/>
<div class="field">
<label for="tag" class="required">Nom</label>
<input id="tag" name="tag" type="text" pattern="[a-zA-Z_][\w\-]*" placeholder="Reseau-local" required value="<?=$tag?>"
title="une lettre suivie par des lettres, des chiffres ou des tirets"/>
</div>
<h4 class="ui header">Paramètres</h4>
<div id="optionsDiv" class="three fields" style="flex-wrap: wrap">
<?php foreach(array_merge($option, $placeholder) as $name => $value) : ?>
<div class="field">
<label for="<?=$name?>"><?=$name?></label>
<input id="<?=$name?>" name="<?=$name?>" type="text"
<?=isset($option[$name]) ? "value='{$option[$name]}'": ""?>
<?=isset($placeholder[$name]) ? "placeholder='{$placeholder[$name]}'": ""?>
<?=($name == "netmask") || ($name == "router") || (strpos($name, "server") !== false) ? 'pattern="^(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})$"' : "" ?>
/>
</div>
<?php endforeach; ?>
</div>
<div class="field">
<label for="addOptionSelect">Ajouter un paramètre</label>
<select id="addOptionSelect" class="ui search selection dropdown">
<?php foreach ($option_names as $option) :?>
<option value="<?=$option["name"]?>"><?=$option["name"]?></option>
<?php endforeach; ?>
</select>
<script>
addOptionSelect.oninput = function(event) {
var name = addOptionSelect.value
var field = document.createElement("div")
field.className = "field"
var label = document.createElement("label")
label.setAttribute("for", name)
label.textContent = name
field.appendChild(label)
var input = document.createElement("input")
input.setAttribute("id", name)
input.setAttribute("name", name)
input.setAttribute("type", "text")
if (name == "netmask" || name == "router" || name.includes("server")) {
input.setAttribute("pattern", "^(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})$")
}
field.appendChild(input)
optionsDiv.appendChild(field)
}
</script>
</div>
<h4 class="ui header">Plage DHCP dynamique</h4>
<div class="two fields">
<div class="field">
<label for="start-addr">Première adresse</label>
<div class="ui labeled input">
<input id="start-addr" name="start-addr" type="text" title="xxx.xxx.xxx.xxx"
pattern="(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)"
placeholder="192.168.1.100"
value="<?=$range["start-addr"] ?? "" ?>"/>
</div>
</div>
<div class="field">
<label for="end-addr">Dernière adresse ou mode</label>
<select id="end-addr" name="end-addr" class="ui search dropdown">
<?php if (isset($range["end-addr"])): ?>
<option value="<?=$range["end-addr"]?>"><?=$range["end-addr"]?></option>
<?php endif; ?>
<option value="">Adresse IP</option>
<option value="static">static</option>
<option value="proxya-only">proxy</option>
</select>
<script>$('#end-addr').dropdown({allowAdditions: true})</script>
</div>
</div>
<button type="submit" class="ui button">Enregistrer</button>
</form>
</main>
</body>
</html>