279 lines
12 KiB
PHP
279 lines
12 KiB
PHP
<?php
|
|
$pdo = module_fn('pi_control', 'pdo');
|
|
module_fn('pi_control', 'ensure_schema');
|
|
$table = fn(string $name) => module_fn('pi_control', 'table', $name);
|
|
|
|
$notice = null;
|
|
$error = null;
|
|
$terminalNotice = null;
|
|
$terminalError = null;
|
|
$terminalUrl = null;
|
|
$terminalToken = null;
|
|
|
|
$settings = modules()->settings('pi_control');
|
|
$ttydUrl = trim((string)($settings['ttyd_url'] ?? '/ttyd'));
|
|
$defaultProvider = 'ttyd';
|
|
$defaultTimeout = (int)($settings['exec_default_timeout'] ?? 300);
|
|
$defaultTimeout = $defaultTimeout > 0 ? $defaultTimeout : 300;
|
|
$queueName = (string)($settings['redis']['queue'] ?? 'pi_control:queue');
|
|
$tokenTtl = (int)($settings['terminal_token_ttl'] ?? 10);
|
|
$tokenTtl = $tokenTtl > 0 ? $tokenTtl : 10;
|
|
|
|
$hosts = $pdo->query('SELECT * FROM ' . $table('hosts') . ' ORDER BY name ASC')->fetchAll(PDO::FETCH_ASSOC);
|
|
$commands = $pdo->query('SELECT * FROM ' . $table('commands') . ' ORDER BY label ASC')->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$action = (string)($_POST['action'] ?? '');
|
|
|
|
if ($action === 'open_console') {
|
|
$hostId = (int)($_POST['terminal_host_id'] ?? 0);
|
|
$provider = 'ttyd';
|
|
|
|
if ($hostId <= 0) {
|
|
$terminalError = 'Bitte einen Host wählen.';
|
|
} elseif ($ttydUrl === '') {
|
|
$terminalError = 'ttyd URL fehlt. Bitte im Setup setzen.';
|
|
} else {
|
|
$driver = (string)$pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
|
|
$expiresSql = $driver === 'pgsql'
|
|
? "NOW() + INTERVAL '{$tokenTtl} minutes'"
|
|
: "DATETIME('now', '+{$tokenTtl} minutes')";
|
|
|
|
$token = bin2hex(random_bytes(24));
|
|
$terminalToken = $token;
|
|
$stmt = $pdo->prepare(
|
|
'INSERT INTO ' . $table('sessions') . ' (token, host_id, provider, created_by, expires_at)
|
|
VALUES (:token, :host_id, :provider, :created_by, ' . $expiresSql . ')'
|
|
);
|
|
$stmt->execute([
|
|
'token' => $token,
|
|
'host_id' => $hostId,
|
|
'provider' => $provider,
|
|
'created_by' => auth_display_name() ?: null,
|
|
]);
|
|
|
|
$sep = str_contains($ttydUrl, '?') ? '&' : '?';
|
|
$terminalUrl = rtrim($ttydUrl, '/') . '/' . $sep . 'arg=' . rawurlencode($token);
|
|
}
|
|
} else {
|
|
$hostId = (int)($_POST['host_id'] ?? 0);
|
|
$commandId = (int)($_POST['command_id'] ?? 0);
|
|
$rawCommand = trim((string)($_POST['command_text'] ?? ''));
|
|
$timeoutSec = $defaultTimeout;
|
|
|
|
if ($hostId <= 0) {
|
|
$error = 'Bitte einen Host wählen.';
|
|
} elseif ($commandId <= 0 && $rawCommand === '') {
|
|
$error = 'Bitte einen Befehl wählen oder einen Befehl eingeben.';
|
|
} else {
|
|
$selectedCommand = '';
|
|
if ($commandId > 0) {
|
|
foreach ($commands as $c) {
|
|
if ((int)$c['id'] === $commandId) {
|
|
if (!auth_is_admin() && !empty($c['admin_only'])) {
|
|
$error = 'Dieser Befehl ist nur für Admins.';
|
|
} else {
|
|
$selectedCommand = (string)$c['command'];
|
|
if (!empty($c['timeout_sec'])) {
|
|
$timeoutSec = (int)$c['timeout_sec'];
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$error) {
|
|
$commandText = $selectedCommand !== '' ? $selectedCommand : $rawCommand;
|
|
$driver = (string)$pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
|
|
if ($driver === 'pgsql') {
|
|
$stmt = $pdo->prepare(
|
|
'INSERT INTO ' . $table('runs') . ' (host_id, command_id, command_text, status, timeout_sec, created_by)
|
|
VALUES (:host_id, :command_id, :command_text, :status, :timeout_sec, :created_by)
|
|
RETURNING id'
|
|
);
|
|
$stmt->execute([
|
|
'host_id' => $hostId,
|
|
'command_id' => $commandId > 0 ? $commandId : null,
|
|
'command_text' => $commandText,
|
|
'status' => 'queued',
|
|
'timeout_sec' => $timeoutSec,
|
|
'created_by' => auth_display_name() ?: null,
|
|
]);
|
|
$runId = (int)$stmt->fetchColumn();
|
|
} else {
|
|
$stmt = $pdo->prepare(
|
|
'INSERT INTO ' . $table('runs') . ' (host_id, command_id, command_text, status, timeout_sec, created_by)
|
|
VALUES (:host_id, :command_id, :command_text, :status, :timeout_sec, :created_by)'
|
|
);
|
|
$stmt->execute([
|
|
'host_id' => $hostId,
|
|
'command_id' => $commandId > 0 ? $commandId : null,
|
|
'command_text' => $commandText,
|
|
'status' => 'queued',
|
|
'timeout_sec' => $timeoutSec,
|
|
'created_by' => auth_display_name() ?: null,
|
|
]);
|
|
$runId = (int)$pdo->lastInsertId();
|
|
}
|
|
try {
|
|
$redis = module_fn('pi_control', 'redis');
|
|
$payload = json_encode(['run_id' => $runId], JSON_THROW_ON_ERROR);
|
|
$redis->command(['RPUSH', $queueName, $payload]);
|
|
$notice = 'Befehl wurde in die Queue gestellt.';
|
|
} catch (\Throwable $e) {
|
|
$pdo->exec('UPDATE ' . $table('runs') . ' SET status = \'queue_error\' WHERE id = ' . (int)$runId);
|
|
$notice = 'Befehl gespeichert, aber Queue nicht erreichbar.';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$runs = $pdo->query(
|
|
'SELECT r.*, h.name AS host_name, h.host AS host_addr
|
|
FROM ' . $table('runs') . ' r
|
|
LEFT JOIN ' . $table('hosts') . ' h ON h.id = r.host_id
|
|
ORDER BY r.id DESC LIMIT 20'
|
|
)->fetchAll(PDO::FETCH_ASSOC);
|
|
?>
|
|
<div class="card">
|
|
<div class="pill">Pi Control</div>
|
|
<h1 style="margin-top:.75rem;">Konsole</h1>
|
|
<p class="muted">Wähle einen Host und führe einen Befehl aus.</p>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="card" style="margin-top:1rem; border-color:#ffb4a8; background:#fff5f3; color:#7a2114;">
|
|
<?= e($error) ?>
|
|
</div>
|
|
<?php elseif ($notice): ?>
|
|
<div class="card" style="margin-top:1rem; border-color:var(--accent-2);">
|
|
<?= e($notice) ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="grid" style="margin-top:1rem;">
|
|
<div class="card form-card" style="background:var(--panel-2);">
|
|
<strong>Live-Konsole</strong>
|
|
<?php if ($terminalError): ?>
|
|
<div class="card" style="margin-top:1rem; border-color:#ffb4a8; background:#fff5f3; color:#7a2114;">
|
|
<?= e($terminalError) ?>
|
|
</div>
|
|
<?php elseif ($terminalNotice): ?>
|
|
<div class="card" style="margin-top:1rem; border-color:var(--accent-2);">
|
|
<?= e($terminalNotice) ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form method="post" class="form-grid" style="margin-top:.75rem;">
|
|
<input type="hidden" name="action" value="open_console">
|
|
<label class="form-field">
|
|
<span class="muted">Host</span>
|
|
<select name="terminal_host_id" required title="Pi auswählen, zu dem die Konsole verbindet.">
|
|
<option value="">Host wählen</option>
|
|
<?php foreach ($hosts as $h): ?>
|
|
<option value="<?= e((string)$h['id']) ?>"><?= e($h['name'] ?? $h['host']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="muted">Konsole</span>
|
|
<input type="text" value="ttyd" disabled>
|
|
</label>
|
|
<button class="cta-button" type="submit">Konsole öffnen</button>
|
|
</form>
|
|
|
|
<?php if ($terminalUrl): ?>
|
|
<div class="card" style="margin-top:1rem;">
|
|
<div style="display:flex; align-items:center; justify-content:space-between; gap:12px; flex-wrap:wrap;">
|
|
<strong>Aktive Konsole</strong>
|
|
<a class="nav-link" href="<?= e($terminalUrl) ?>" target="_blank" rel="noopener">In neuem Tab öffnen</a>
|
|
</div>
|
|
<?php if ($terminalToken): ?>
|
|
<div class="muted" style="margin-top:.5rem;">
|
|
Token: <code><?= e($terminalToken) ?></code>
|
|
</div>
|
|
<?php endif; ?>
|
|
<div style="margin-top:.75rem; border-radius:12px; overflow:hidden; border:1px solid var(--line); background:#0b0f17;">
|
|
<iframe src="<?= e($terminalUrl) ?>" title="Pi Konsole" style="width:100%; height:520px; border:0;"></iframe>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<div class="card form-card" style="background:var(--panel-2);">
|
|
<strong>Befehl ausführen</strong>
|
|
<form method="post" class="form-grid" style="margin-top:.75rem;">
|
|
<input type="hidden" name="action" value="run_command">
|
|
<label class="form-field">
|
|
<span class="muted">Host</span>
|
|
<select name="host_id" required>
|
|
<option value="">Host wählen</option>
|
|
<?php foreach ($hosts as $h): ?>
|
|
<option value="<?= e((string)$h['id']) ?>"><?= e($h['name'] ?? $h['host']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="muted">Preset</span>
|
|
<select name="command_id">
|
|
<option value="">Preset auswählen (optional)</option>
|
|
<?php foreach ($commands as $c): ?>
|
|
<?php if (!auth_is_admin() && !empty($c['admin_only'])) { continue; } ?>
|
|
<option value="<?= e((string)$c['id']) ?>"><?= e($c['label'] ?? '') ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</label>
|
|
<label class="form-field">
|
|
<span class="muted">Direkter Command</span>
|
|
<textarea name="command_text" rows="3" placeholder="Oder Command direkt eingeben"></textarea>
|
|
</label>
|
|
<button class="cta-button" type="submit">Befehl senden</button>
|
|
</form>
|
|
<p class="muted" style="margin-top:.5rem;">Befehle werden über die Queue ausgeführt.</p>
|
|
</div>
|
|
|
|
<div class="card form-card" style="background:var(--panel-2);">
|
|
<strong>Letzte Runs</strong>
|
|
<div style="margin-top:.75rem; overflow:auto;">
|
|
<table class="table" style="min-width:560px;">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Status</th>
|
|
<th>Host</th>
|
|
<th>Command</th>
|
|
<th>Von</th>
|
|
<th>Output</th>
|
|
<th>Timeout</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (!$runs): ?>
|
|
<tr><td colspan="7" class="muted">Noch keine Runs.</td></tr>
|
|
<?php endif; ?>
|
|
<?php foreach ($runs as $r): ?>
|
|
<?php
|
|
$out = (string)($r['output'] ?? '');
|
|
$err = (string)($r['error'] ?? '');
|
|
$snippet = $out !== '' ? $out : $err;
|
|
if (strlen($snippet) > 140) {
|
|
$snippet = substr($snippet, 0, 140) . '…';
|
|
}
|
|
?>
|
|
<tr>
|
|
<td><?= e((string)$r['id']) ?></td>
|
|
<td><?= e($r['status'] ?? '') ?></td>
|
|
<td><?= e($r['host_name'] ?? $r['host_addr'] ?? '') ?></td>
|
|
<td class="muted" style="max-width:360px;"><code><?= e($r['command_text'] ?? '') ?></code></td>
|
|
<td><?= e($r['created_by'] ?? '') ?></td>
|
|
<td class="muted" style="max-width:240px;"><code><?= e($snippet) ?></code></td>
|
|
<td><?= !empty($r['timeout_sec']) ? e((string)$r['timeout_sec']) . 's' : 'default' ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|