feat: implement full network configuration handling in Proxmox firewall, allowing dynamic modification of firewall parameters and improved error reporting for adapter states
This commit is contained in:
@@ -20,6 +20,7 @@ class Firewall extends Component
|
||||
// Network Adapters
|
||||
public array $networkAdapters = [];
|
||||
public array $adapterFirewallStates = [];
|
||||
public array $adapterConfigs = []; // Store full network config strings
|
||||
|
||||
// Firewall Options (policies, filters)
|
||||
public string $policyIn = 'DROP';
|
||||
@@ -259,6 +260,9 @@ class Firewall extends Component
|
||||
$model = explode('=', $parts[0])[0] ?? 'unknown';
|
||||
$mac = null;
|
||||
|
||||
// Store the full configuration string for later modification
|
||||
$this->adapterConfigs['net' . $matches[1]] = $value;
|
||||
|
||||
foreach ($parts as $part) {
|
||||
if (str_contains($part, '=')) {
|
||||
[$k, $v] = explode('=', $part, 2);
|
||||
@@ -294,17 +298,52 @@ class Firewall extends Component
|
||||
$currentState = $this->adapterFirewallStates[$adapter] ?? false;
|
||||
$newState = !$currentState;
|
||||
|
||||
// Get the full network configuration string
|
||||
$configString = $this->adapterConfigs[$adapter] ?? '';
|
||||
|
||||
if (empty($configString)) {
|
||||
$this->error = 'Network adapter configuration not found';
|
||||
$this->showErrorModal = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse the configuration string and modify the firewall parameter
|
||||
$parts = explode(',', $configString);
|
||||
$firewallFound = false;
|
||||
|
||||
foreach ($parts as $key => $part) {
|
||||
$part = trim($part);
|
||||
if (str_contains($part, '=')) {
|
||||
[$k, $v] = explode('=', $part, 2);
|
||||
if (trim($k) === 'firewall') {
|
||||
$parts[$key] = 'firewall=' . ($newState ? '1' : '0');
|
||||
$firewallFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If firewall parameter doesn't exist, add it
|
||||
if (!$firewallFound) {
|
||||
$parts[] = 'firewall=' . ($newState ? '1' : '0');
|
||||
}
|
||||
|
||||
// Reconstruct the configuration string
|
||||
$newConfigString = implode(',', $parts);
|
||||
|
||||
// Send the full modified configuration back to Proxmox
|
||||
$response = $this->firewallRequest(
|
||||
'/nodes/' . $this->server->node->name . '/qemu/' . $this->server->vm_id . '/config',
|
||||
'put',
|
||||
[$adapter . '.firewall' => $newState ? 1 : 0]
|
||||
[$adapter => $newConfigString]
|
||||
);
|
||||
|
||||
if ($response->successful()) {
|
||||
$this->adapterFirewallStates[$adapter] = $newState;
|
||||
$this->adapterConfigs[$adapter] = $newConfigString; // Update stored config
|
||||
$this->notify('Adapter firewall ' . ($newState ? 'enabled' : 'disabled'), 'success');
|
||||
} else {
|
||||
$this->error = 'Failed to toggle adapter firewall';
|
||||
$this->error = 'Failed to toggle adapter firewall: ' . $response->body();
|
||||
$this->showErrorModal = true;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
@@ -359,7 +398,10 @@ class Firewall extends Component
|
||||
$this->firewallEnabled = $newState;
|
||||
$this->notify('Firewall ' . ($newState ? 'enabled' : 'disabled'), 'success');
|
||||
} else {
|
||||
$this->error = 'Failed to toggle firewall';
|
||||
// Show detailed error message from API
|
||||
$errorBody = $response->json();
|
||||
$errorMsg = $errorBody['errors'] ?? $response->body();
|
||||
$this->error = 'Failed to toggle firewall (Status ' . $response->status() . '): ' . (is_array($errorMsg) ? json_encode($errorMsg) : $errorMsg);
|
||||
$this->showErrorModal = true;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
|
||||
Reference in New Issue
Block a user