feat: gestion réseau DHCP/statique depuis l'interface web

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jules
2026-05-06 01:23:38 +02:00
parent 29da0a8380
commit 9b7cf851c2
3 changed files with 296 additions and 0 deletions

91
web/api/network.php Normal file
View File

@@ -0,0 +1,91 @@
<?php
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';
admin_check();
header('Content-Type: application/json');
// Lit la config réseau actuelle via nmcli
function get_network_info(): array {
// Connexion active
$conName = trim(shell_exec("nmcli -t -f NAME,TYPE con show --active 2>/dev/null | grep ethernet | cut -d: -f1 | head -1") ?? '');
if (!$conName) $conName = 'Wired connection 1';
// Méthode (dhcp ou static)
$method = trim(shell_exec("nmcli -g ipv4.method con show " . escapeshellarg($conName) . " 2>/dev/null") ?? 'auto');
// IP actuelle
$ip4 = trim(shell_exec("nmcli -g IP4.ADDRESS dev show eth0 2>/dev/null | head -1") ?? '');
// Gateway actuelle
$gw = trim(shell_exec("nmcli -g IP4.GATEWAY dev show eth0 2>/dev/null | head -1") ?? '');
// DNS
$dns = trim(shell_exec("nmcli -g IP4.DNS dev show eth0 2>/dev/null | head -3 | tr '\n' ','") ?? '');
$dns = rtrim($dns, ',');
// Config statique configurée (pas forcément active)
$staticIp = trim(shell_exec("nmcli -g ipv4.addresses con show " . escapeshellarg($conName) . " 2>/dev/null") ?? '');
$staticGw = trim(shell_exec("nmcli -g ipv4.gateway con show " . escapeshellarg($conName) . " 2>/dev/null") ?? '');
$staticDns = trim(shell_exec("nmcli -g ipv4.dns con show " . escapeshellarg($conName) . " 2>/dev/null") ?? '');
return [
'connection' => $conName,
'method' => ($method === 'auto' || $method === 'dhcp') ? 'dhcp' : 'static',
'current_ip' => $ip4,
'current_gw' => $gw,
'current_dns'=> $dns,
'static_ip' => $staticIp,
'static_gw' => $staticGw,
'static_dns' => $staticDns,
'hostname' => gethostname(),
];
}
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo json_encode(get_network_info());
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = json_decode(file_get_contents('php://input'), true) ?? [];
$mode = $data['mode'] ?? 'dhcp';
if ($mode === 'dhcp') {
$out = shell_exec('sudo /opt/copymail/scripts/apply_network.sh dhcp 2>&1');
echo json_encode(['success' => true, 'msg' => trim($out)]);
exit;
}
if ($mode === 'static') {
$ip = trim($data['ip'] ?? '');
$prefix = trim($data['prefix'] ?? '24');
$gateway = trim($data['gateway'] ?? '');
$dns = trim($data['dns'] ?? '8.8.8.8,1.1.1.1');
// Validation basique
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
http_response_code(400);
echo json_encode(['error' => 'Adresse IP invalide.']);
exit;
}
if (!filter_var($gateway, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
http_response_code(400);
echo json_encode(['error' => 'Passerelle invalide.']);
exit;
}
$prefix = max(1, min(30, (int)$prefix));
$out = shell_exec(sprintf(
'sudo /opt/copymail/scripts/apply_network.sh static %s %s %s 2>&1',
escapeshellarg("{$ip}/{$prefix}"),
escapeshellarg($gateway),
escapeshellarg($dns)
));
echo json_encode(['success' => true, 'msg' => trim($out), 'new_ip' => $ip]);
exit;
}
http_response_code(400);
echo json_encode(['error' => 'Mode invalide.']);
}