Files
Joseph.Rawlings d8b76233c0 Add nodes management and initial setup pages
- Implemented nodes management functionality in `nodes.php` including create, update, and delete actions.
- Added form validation and error handling for node operations.
- Created a new setup page in `setup.php` for initial administrator account creation.
- Included user feedback messages for successful operations and errors.
- Designed user interface for both nodes management and setup processes.
2025-12-14 01:33:12 -05:00

587 lines
24 KiB
PHP

<?php
/**
* Nodes Management Page
*/
$asnManager = new ASN($db);
$nodeManager = new Node($db);
$validator = new Validator();
$action = $_GET['action'] ?? 'list';
$id = $_GET['id'] ?? null;
$message = '';
$error = '';
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!$auth->verifyCsrfToken($_POST['csrf_token'] ?? '')) {
$error = 'Invalid security token. Please try again.';
} else {
$postAction = $_POST['action'] ?? '';
switch ($postAction) {
case 'create':
if (!hasPermission('create_peers')) {
$error = 'You do not have permission to create nodes.';
break;
}
$asnId = $_POST['asn_id'] ?? '';
$name = trim($_POST['name'] ?? '');
$description = trim($_POST['description'] ?? '');
if (empty($asnId)) {
$error = 'Please select an ASN.';
break;
}
if (empty($name)) {
$error = 'Name is required.';
break;
}
$config = buildNodeConfigFromForm($_POST);
$result = $nodeManager->create($asnId, $name, $description, $config);
if ($result['success']) {
$logger->log('node', 'Created node', ['name' => $name, 'asn_id' => $asnId]);
header('Location: ?page=nodes&message=Node created successfully');
exit;
} else {
$error = $result['message'];
}
break;
case 'update':
if (!hasPermission('edit_peers')) {
$error = 'You do not have permission to edit nodes.';
break;
}
$id = $_POST['id'] ?? '';
$name = trim($_POST['name'] ?? '');
$description = trim($_POST['description'] ?? '');
if (empty($name)) {
$error = 'Name is required.';
break;
}
$config = buildNodeConfigFromForm($_POST);
$result = $nodeManager->update($id, [
'name' => $name,
'description' => $description,
'config' => $config,
]);
if ($result['success']) {
$logger->log('node', 'Updated node', ['id' => $id, 'name' => $name]);
header('Location: ?page=nodes&message=Node updated successfully');
exit;
} else {
$error = $result['message'];
}
break;
case 'delete':
if (!hasPermission('delete_peers')) {
$error = 'You do not have permission to delete nodes.';
break;
}
$id = $_POST['id'] ?? '';
$node = $nodeManager->get($id);
if (!$node) {
$error = 'Node not found.';
break;
}
$result = $nodeManager->delete($id);
if ($result['success']) {
$logger->log('node', 'Deleted node', ['name' => $node['name']]);
header('Location: ?page=nodes&message=Node deleted successfully');
exit;
} else {
$error = $result['message'];
}
break;
}
}
}
// Get message from query string
if (isset($_GET['message'])) {
$message = $_GET['message'];
}
// Helper function to build config from form
function buildNodeConfigFromForm(array $post): array {
$config = [];
// String fields
$stringFields = [
'router_id', 'source4', 'source6', 'hostname', 'bird_directory',
'bird_socket', 'cache_directory', 'irr_server', 'rtr_server',
];
foreach ($stringFields as $field) {
if (isset($post[$field]) && $post[$field] !== '') {
$config[$field] = trim($post[$field]);
}
}
// Boolean fields
$booleanFields = [
'keep_filtered', 'rpki_filter', 'irr_filter',
];
foreach ($booleanFields as $field) {
if (isset($post[$field])) {
$config[$field] = $post[$field] === '1';
}
}
// Prefixes
if (!empty($post['prefixes4'])) {
$config['prefixes'] = array_filter(array_map('trim', explode(',', $post['prefixes4'])));
}
if (!empty($post['prefixes6'])) {
$config['prefixes6'] = array_filter(array_map('trim', explode(',', $post['prefixes6'])));
}
// Templates
if (!empty($post['templates'])) {
$config['templates'] = array_filter(array_map('trim', explode(',', $post['templates'])));
}
return $config;
}
// Get node for edit
$editNode = null;
if ($action === 'edit' && $id) {
$editNode = $nodeManager->get($id);
if (!$editNode) {
$error = 'Node not found.';
$action = 'list';
}
}
// Get all ASNs and nodes
$asns = $asnManager->getAll();
$nodes = $nodeManager->getAll();
// Filter by ASN if specified
$filterAsnId = $_GET['asn_id'] ?? '';
if ($filterAsnId) {
$nodes = array_filter($nodes, fn($n) => $n['asn_id'] === $filterAsnId);
}
?>
<?php if ($action === 'list'): ?>
<!-- List View -->
<div class="page-header">
<div class="d-flex flex-items-center flex-justify-between">
<div>
<h1 class="h2 mb-1">Nodes</h1>
<p class="color-fg-muted mb-0">Manage your router nodes</p>
</div>
<?php if (hasPermission('create_peers')): ?>
<a href="?page=nodes&action=create" class="btn btn-primary">
<svg class="octicon mr-1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.75 2a.75.75 0 01.75.75V7h4.25a.75.75 0 110 1.5H8.5v4.25a.75.75 0 11-1.5 0V8.5H2.75a.75.75 0 010-1.5H7V2.75A.75.75 0 017.75 2z"/></svg>
New Node
</a>
<?php endif; ?>
</div>
</div>
<div class="page-content">
<?php if ($message): ?>
<div class="flash flash-success mb-3">
<?= e($message) ?>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="flash flash-error mb-3">
<?= e($error) ?>
</div>
<?php endif; ?>
<!-- Filter -->
<?php if (!empty($asns)): ?>
<div class="mb-3">
<form method="GET" class="d-flex flex-items-center" style="gap: 8px;">
<input type="hidden" name="page" value="nodes">
<select name="asn_id" class="form-select" onchange="this.form.submit()">
<option value="">All ASNs</option>
<?php foreach ($asns as $asn): ?>
<option value="<?= e($asn['id']) ?>" <?= $filterAsnId === $asn['id'] ? 'selected' : '' ?>>
AS<?= e($asn['asn']) ?> - <?= e($asn['name']) ?>
</option>
<?php endforeach; ?>
</select>
</form>
</div>
<?php endif; ?>
<?php if (empty($nodes)): ?>
<div class="blankslate">
<svg class="octicon blankslate-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="24" height="24"><path fill-rule="evenodd" d="M1.75 1A1.75 1.75 0 000 2.75v4c0 .372.116.717.314 1a1.742 1.742 0 00-.314 1v4c0 .966.784 1.75 1.75 1.75h12.5A1.75 1.75 0 0016 12.75v-4c0-.372-.116-.717-.314-1 .198-.283.314-.628.314-1v-4A1.75 1.75 0 0014.25 1H1.75z"/></svg>
<h3 class="blankslate-heading">No nodes configured</h3>
<p>Get started by creating your first node.</p>
<?php if (hasPermission('create_peers')): ?>
<a href="?page=nodes&action=create" class="btn btn-primary">Create Node</a>
<?php endif; ?>
</div>
<?php else: ?>
<div class="Box">
<div class="overflow-auto">
<table class="data-table">
<thead>
<tr>
<th>Name</th>
<th>ASN</th>
<th>Router ID</th>
<th>Description</th>
<th>Peers</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$peerManager = new Peer($db);
foreach ($nodes as $node):
$nodeAsn = $asnManager->get($node['asn_id']);
$nodePeers = $peerManager->getByNode($node['id']);
?>
<tr>
<td class="text-bold"><?= e($node['name']) ?></td>
<td>
<?php if ($nodeAsn): ?>
<span class="Label Label--secondary">AS<?= e($nodeAsn['asn']) ?></span>
<?php else: ?>
<span class="color-fg-muted">-</span>
<?php endif; ?>
</td>
<td>
<code><?= e($node['config']['router_id'] ?? '-') ?></code>
</td>
<td class="color-fg-muted"><?= e($node['description'] ?? '-') ?></td>
<td>
<span class="Counter"><?= count($nodePeers) ?></span>
</td>
<td class="text-right">
<a href="?page=peers&node_id=<?= e($node['id']) ?>" class="btn btn-sm">View Peers</a>
<a href="?page=nodes&action=edit&id=<?= e($node['id']) ?>" class="btn btn-sm">Edit</a>
<?php if (hasPermission('delete_peers')): ?>
<form method="POST" class="d-inline" onsubmit="return confirmDelete('Are you sure you want to delete <?= e($node['name']) ?>?')">
<?= csrfField() ?>
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= e($node['id']) ?>">
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php endif; ?>
</div>
<?php elseif ($action === 'create' || $action === 'edit'): ?>
<!-- Create/Edit Form -->
<div class="page-header">
<div class="d-flex flex-items-center">
<a href="?page=nodes" class="btn-octicon mr-2">
<svg class="octicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.78 12.53a.75.75 0 01-1.06 0L2.47 8.28a.75.75 0 010-1.06l4.25-4.25a.75.75 0 011.06 1.06L4.81 7h7.44a.75.75 0 010 1.5H4.81l2.97 2.97a.75.75 0 010 1.06z"/></svg>
</a>
<div>
<h1 class="h2 mb-1"><?= $action === 'edit' ? 'Edit Node' : 'New Node' ?></h1>
<p class="color-fg-muted mb-0">
<?= $action === 'edit' ? e($editNode['name']) : 'Configure a new router node' ?>
</p>
</div>
</div>
</div>
<div class="page-content">
<?php if (empty($asns)): ?>
<div class="flash flash-warn mb-3">
<strong>No ASNs found.</strong> You need to create an ASN before creating nodes.
<a href="?page=asns&action=create" class="Link--primary">Create ASN</a>
</div>
<?php else: ?>
<?php if ($error): ?>
<div class="flash flash-error mb-3">
<?= e($error) ?>
</div>
<?php endif; ?>
<form method="POST" action="">
<?= csrfField() ?>
<input type="hidden" name="action" value="<?= $action === 'edit' ? 'update' : 'create' ?>">
<?php if ($editNode): ?>
<input type="hidden" name="id" value="<?= e($editNode['id']) ?>">
<?php endif; ?>
<div class="Box mb-4">
<div class="Box-header">
<h3 class="Box-title">Basic Information</h3>
</div>
<div class="Box-body">
<div class="form-group">
<label class="form-label" for="asn_id">ASN *</label>
<select id="asn_id"
name="asn_id"
class="form-select"
style="max-width: 400px;"
<?= $editNode ? 'disabled' : 'required' ?>>
<option value="">Select an ASN</option>
<?php foreach ($asns as $asn): ?>
<option value="<?= e($asn['id']) ?>" <?= ($editNode['asn_id'] ?? '') === $asn['id'] ? 'selected' : '' ?>>
AS<?= e($asn['asn']) ?> - <?= e($asn['name']) ?>
</option>
<?php endforeach; ?>
</select>
<?php if ($editNode): ?>
<input type="hidden" name="asn_id" value="<?= e($editNode['asn_id']) ?>">
<?php endif; ?>
</div>
<div class="form-group">
<label class="form-label" for="name">Name *</label>
<input type="text"
id="name"
name="name"
class="form-control form-input-wide"
value="<?= e($editNode['name'] ?? '') ?>"
placeholder="e.g., router1.example.com"
required>
<p class="form-hint">A unique name for this node (usually the hostname)</p>
</div>
<div class="form-group">
<label class="form-label" for="description">Description</label>
<textarea id="description"
name="description"
class="form-control form-input-wide"
rows="2"><?= e($editNode['description'] ?? '') ?></textarea>
</div>
</div>
</div>
<div class="Box mb-4">
<div class="Box-header">
<h3 class="Box-title">Network Configuration</h3>
</div>
<div class="Box-body">
<p class="color-fg-muted mb-3">These settings override the ASN defaults for this specific node.</p>
<div class="d-flex flex-wrap" style="gap: 16px;">
<div class="form-group flex-1" style="min-width: 200px;">
<label class="form-label" for="router_id">Router ID</label>
<input type="text"
id="router_id"
name="router_id"
class="form-control"
value="<?= e($editNode['config']['router_id'] ?? '') ?>"
placeholder="e.g., 192.0.2.1">
</div>
<div class="form-group flex-1" style="min-width: 200px;">
<label class="form-label" for="hostname">Hostname</label>
<input type="text"
id="hostname"
name="hostname"
class="form-control"
value="<?= e($editNode['config']['hostname'] ?? '') ?>"
placeholder="router1">
</div>
</div>
<div class="d-flex flex-wrap" style="gap: 16px;">
<div class="form-group flex-1" style="min-width: 200px;">
<label class="form-label" for="source4">Source IPv4</label>
<input type="text"
id="source4"
name="source4"
class="form-control"
value="<?= e($editNode['config']['source4'] ?? '') ?>"
placeholder="e.g., 192.0.2.1">
</div>
<div class="form-group flex-1" style="min-width: 200px;">
<label class="form-label" for="source6">Source IPv6</label>
<input type="text"
id="source6"
name="source6"
class="form-control"
value="<?= e($editNode['config']['source6'] ?? '') ?>"
placeholder="e.g., 2001:db8::1">
</div>
</div>
<div class="form-group">
<label class="form-label" for="prefixes4">IPv4 Prefixes</label>
<input type="text"
id="prefixes4"
name="prefixes4"
class="form-control form-input-wide"
value="<?= e(implode(', ', $editNode['config']['prefixes'] ?? [])) ?>"
placeholder="192.0.2.0/24, 198.51.100.0/24">
<p class="form-hint">Node-specific IPv4 prefixes (overrides ASN defaults)</p>
</div>
<div class="form-group">
<label class="form-label" for="prefixes6">IPv6 Prefixes</label>
<input type="text"
id="prefixes6"
name="prefixes6"
class="form-control form-input-wide"
value="<?= e(implode(', ', $editNode['config']['prefixes6'] ?? [])) ?>"
placeholder="2001:db8::/32">
<p class="form-hint">Node-specific IPv6 prefixes (overrides ASN defaults)</p>
</div>
</div>
</div>
<div class="Box mb-4">
<div class="Box-header">
<h3 class="Box-title">BIRD Configuration</h3>
</div>
<div class="Box-body">
<div class="d-flex flex-wrap" style="gap: 16px;">
<div class="form-group flex-1" style="min-width: 200px;">
<label class="form-label" for="bird_directory">BIRD Directory</label>
<input type="text"
id="bird_directory"
name="bird_directory"
class="form-control"
value="<?= e($editNode['config']['bird_directory'] ?? '') ?>"
placeholder="/etc/bird">
</div>
<div class="form-group flex-1" style="min-width: 200px;">
<label class="form-label" for="bird_socket">BIRD Socket</label>
<input type="text"
id="bird_socket"
name="bird_socket"
class="form-control"
value="<?= e($editNode['config']['bird_socket'] ?? '') ?>"
placeholder="/run/bird/bird.ctl">
</div>
</div>
<div class="form-group">
<label class="form-label" for="cache_directory">Cache Directory</label>
<input type="text"
id="cache_directory"
name="cache_directory"
class="form-control"
style="max-width: 400px;"
value="<?= e($editNode['config']['cache_directory'] ?? '') ?>"
placeholder="/var/cache/pathvector">
</div>
</div>
</div>
<div class="Box mb-4">
<div class="Box-header">
<h3 class="Box-title">Filtering Options</h3>
</div>
<div class="Box-body">
<div class="d-flex flex-wrap" style="gap: 16px;">
<div class="form-group flex-1" style="min-width: 200px;">
<label class="form-label" for="irr_server">IRR Server</label>
<input type="text"
id="irr_server"
name="irr_server"
class="form-control"
value="<?= e($editNode['config']['irr_server'] ?? '') ?>"
placeholder="rr.ntt.net">
</div>
<div class="form-group flex-1" style="min-width: 200px;">
<label class="form-label" for="rtr_server">RTR Server (RPKI)</label>
<input type="text"
id="rtr_server"
name="rtr_server"
class="form-control"
value="<?= e($editNode['config']['rtr_server'] ?? '') ?>"
placeholder="rtr.example.com:8282">
</div>
</div>
<div class="d-flex flex-wrap" style="gap: 24px;">
<div class="form-checkbox">
<label>
<input type="checkbox"
name="rpki_filter"
value="1"
<?= ($editNode['config']['rpki_filter'] ?? false) ? 'checked' : '' ?>>
Enable RPKI filtering
</label>
</div>
<div class="form-checkbox">
<label>
<input type="checkbox"
name="irr_filter"
value="1"
<?= ($editNode['config']['irr_filter'] ?? false) ? 'checked' : '' ?>>
Enable IRR filtering
</label>
</div>
<div class="form-checkbox">
<label>
<input type="checkbox"
name="keep_filtered"
value="1"
<?= ($editNode['config']['keep_filtered'] ?? false) ? 'checked' : '' ?>>
Keep filtered routes
</label>
</div>
</div>
</div>
</div>
<div class="Box mb-4">
<div class="Box-header">
<h3 class="Box-title">Templates</h3>
</div>
<div class="Box-body">
<div class="form-group">
<label class="form-label" for="templates">Default Templates</label>
<input type="text"
id="templates"
name="templates"
class="form-control form-input-wide"
value="<?= e(implode(', ', $editNode['config']['templates'] ?? [])) ?>"
placeholder="upstream, peer, downstream">
<p class="form-hint">Comma-separated list of default templates to use for this node</p>
</div>
</div>
</div>
<div class="d-flex flex-items-center" style="gap: 8px;">
<button type="submit" class="btn btn-primary">
<?= $action === 'edit' ? 'Update Node' : 'Create Node' ?>
</button>
<a href="?page=nodes" class="btn">Cancel</a>
</div>
</form>
<?php endif; ?>
</div>
<?php endif; ?>