- Neues, eigenständiges Frontend (Sidebar, zentriertes Logo in der Topbar, Dark/Light-Theme, Karten-Dashboard, Toasts/Modals statt Bootstrap) - Oeffentliches Kurz-Dashboard ohne Login (Status-Uebersicht) - Browser-SSH-Terminal (paramiko, plattformunabhaengig) zum Testen von Switch-Zugangsdaten inkl. interaktiver Host-Key-Bestaetigung - Granulares Rechtesystem mit Gruppen (Devices/Switches-Berechtigungen) - Aufgeraeumtes Backend mit konfigurierbaren Pfaden, auto-generierten Secrets statt hart codierter Werte im Original Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
335 lines
15 KiB
HTML
335 lines
15 KiB
HTML
{% extends "base.html" %}
|
|
{% set active_page = "switches" %}
|
|
{% block extra_head %}
|
|
<link rel="stylesheet" href="{{ url_for('static', filename='css/vendor/xterm.css') }}">
|
|
{% endblock %}
|
|
{% set can_create = current_user.has_permission('switches.create') %}
|
|
{% set can_edit = current_user.has_permission('switches.edit') %}
|
|
{% set can_delete = current_user.has_permission('switches.delete') %}
|
|
{% block page_title %}Switches{% endblock %}
|
|
{% block page_sub %}<div class="topbar-sub">{{ switches|length }} Switche</div>{% endblock %}
|
|
{% block topbar_right %}
|
|
{% if can_create %}
|
|
<button type="button" class="btn btn-primary" data-open-modal="addSwitchModal">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M12 5v14M5 12h14"/></svg>
|
|
Neuer Switch
|
|
</button>
|
|
{% endif %}
|
|
{% endblock %}
|
|
|
|
{% block content %}
|
|
|
|
<div class="table-wrap">
|
|
<div class="table-toolbar">
|
|
<div class="search-input">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="11" cy="11" r="7"/><path d="M21 21l-4.3-4.3"/></svg>
|
|
<input type="text" id="switchSearch" placeholder="Switches durchsuchen…" oninput="filterTable('switchSearch','switchesTable')">
|
|
</div>
|
|
</div>
|
|
<div style="overflow-x:auto;">
|
|
<table class="data-table" id="switchesTable">
|
|
<thead>
|
|
<tr><th>Hostname</th><th>IP-Adresse</th><th>Username</th><th style="width:1%;">Aktionen</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
{% for s in switches %}
|
|
<tr>
|
|
<td class="cell-name">{{ s['hostname'] }}</td>
|
|
<td class="mono">{{ s['ip'] }}</td>
|
|
<td>{{ s['username'] }}</td>
|
|
<td>
|
|
<div class="row-actions">
|
|
{% if can_edit %}
|
|
<button class="icon-btn" title="Bearbeiten" data-open-modal="editSwitchModal{{ loop.index }}">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h9"/><path d="M16.5 3.5a2.1 2.1 0 013 3L7 19l-4 1 1-4z"/></svg>
|
|
</button>
|
|
{% endif %}
|
|
{% if can_delete %}
|
|
<form method="post" action="{{ url_for('delete_switch', hostname=s['hostname']) }}" data-confirm="Willst du den Switch „{{ s['hostname'] }}“ wirklich löschen?">
|
|
<button type="submit" class="icon-btn" style="color:var(--danger);" title="Löschen">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18M8 6V4a2 2 0 012-2h4a2 2 0 012 2v2m3 0l-1 14a2 2 0 01-2 2H7a2 2 0 01-2-2L4 6"/></svg>
|
|
</button>
|
|
</form>
|
|
{% endif %}
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
|
|
{% if can_edit %}
|
|
<div class="modal-overlay" id="editSwitchModal{{ loop.index }}">
|
|
<div class="modal">
|
|
<form method="post" onsubmit="return validateSwitchForm(this, '{{ loop.index }}');">
|
|
<input type="hidden" name="edit_switch" value="1">
|
|
<input type="hidden" name="old_hostname" value="{{ s['hostname'] }}">
|
|
<div class="modal-header">
|
|
<h3>Switch bearbeiten</h3>
|
|
<button type="button" class="modal-close" data-close-modal>×</button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="field"><label>Hostname</label>
|
|
<input type="text" name="hostname" value="{{ s['hostname'] }}" required>
|
|
</div>
|
|
<div class="field"><label>IP-Adresse</label>
|
|
<input type="text" name="ip" value="{{ s['ip'] }}" required placeholder="z.B. 192.168.1.100">
|
|
<div class="invalid-feedback">Bitte eine gültige IP-Adresse eingeben.</div>
|
|
</div>
|
|
<div class="field"><label>Username</label>
|
|
<input type="text" name="username" value="{{ s['username'] }}" required>
|
|
</div>
|
|
<div class="field"><label>Neues Passwort</label>
|
|
<input type="password" id="password_edit_{{ loop.index }}" name="password" placeholder="Nur bei Änderung ausfüllen">
|
|
</div>
|
|
<div class="field"><label>Passwort bestätigen</label>
|
|
<input type="password" id="password_confirm_edit_{{ loop.index }}" name="password_confirm" placeholder="Bestätigen">
|
|
<div class="invalid-feedback">Passwörter stimmen nicht überein!</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" style="margin-right:auto;" onclick="openTerminal(this.closest('form'))">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="M6 9l4 3-4 3M13 15h5"/></svg>
|
|
Verbindung testen
|
|
</button>
|
|
<button type="button" class="btn btn-secondary" data-close-modal>Abbrechen</button>
|
|
<button type="submit" class="btn btn-primary">Speichern</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
{% endif %}
|
|
{% else %}
|
|
<tr class="empty-row"><td colspan="4">Noch keine Switche vorhanden.</td></tr>
|
|
{% endfor %}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
{% if can_create %}
|
|
<!-- Modal: Neuer Switch -->
|
|
<div class="modal-overlay" id="addSwitchModal">
|
|
<div class="modal">
|
|
<form method="post" onsubmit="return validateSwitchForm(this, 'add');">
|
|
<div class="modal-header">
|
|
<h3>Neuen Switch hinzufügen</h3>
|
|
<button type="button" class="modal-close" data-close-modal>×</button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" name="add_switch" value="1">
|
|
<div class="field"><label>Hostname</label>
|
|
<input type="text" name="hostname" required placeholder="z.B. Switch01">
|
|
</div>
|
|
<div class="field"><label>IP-Adresse</label>
|
|
<input type="text" name="ip" required placeholder="z.B. 192.168.1.100">
|
|
<div class="invalid-feedback">Bitte eine gültige IP-Adresse eingeben.</div>
|
|
</div>
|
|
<div class="field"><label>Username</label>
|
|
<input type="text" name="username" required placeholder="z.B. admin">
|
|
</div>
|
|
<div class="field"><label>Passwort</label>
|
|
<input type="password" id="password_add" name="password" required>
|
|
</div>
|
|
<div class="field"><label>Passwort bestätigen</label>
|
|
<input type="password" id="password_confirm_add" name="password_confirm" required>
|
|
<div class="invalid-feedback">Passwörter stimmen nicht überein!</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-secondary" style="margin-right:auto;" onclick="openTerminal(this.closest('form'))">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="M6 9l4 3-4 3M13 15h5"/></svg>
|
|
Verbindung testen
|
|
</button>
|
|
<button type="button" class="btn btn-secondary" data-close-modal>Abbrechen</button>
|
|
<button type="submit" class="btn btn-primary">Hinzufügen</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
{% endif %}
|
|
|
|
{% if can_create or can_edit %}
|
|
<!-- Modal: SSH-Verbindungstest (echtes Terminal, zum Akzeptieren von Host-Keys
|
|
und Prüfen der Zugangsdaten, bevor der Switch gespeichert wird) -->
|
|
<div class="modal-overlay" id="terminalModal">
|
|
<div class="modal" style="max-width:720px;">
|
|
<div class="modal-header">
|
|
<h3>SSH-Verbindungstest</h3>
|
|
<button type="button" class="modal-close" data-close-modal onclick="closeTerminal()">×</button>
|
|
</div>
|
|
<div class="modal-body" style="padding:0;">
|
|
<div class="term-toolbar">
|
|
<div class="flex gap-2" style="align-items:center;">
|
|
<span id="termStatus" class="pill unknown">Bereit</span>
|
|
<span id="termTarget" class="text-faint mono" style="font-size:12px;"></span>
|
|
</div>
|
|
<button type="button" class="btn btn-sm btn-secondary" id="termPastePassword">
|
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="11" width="18" height="10" rx="2"/><path d="M7 11V7a5 5 0 0110 0v4"/></svg>
|
|
Passwort einfügen
|
|
</button>
|
|
</div>
|
|
<div id="terminal" class="xterm-container"></div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<p class="text-faint" style="font-size:11.5px; margin-right:auto;">
|
|
Erste Verbindung? Bestätige den Host-Key mit „yes“, gib danach das Passwort ein — direkt hier im Terminal.
|
|
</p>
|
|
<button type="button" class="btn btn-secondary" data-close-modal onclick="closeTerminal()">Schließen</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{% endif %}
|
|
|
|
{% endblock %}
|
|
|
|
{% block scripts %}
|
|
<script src="{{ url_for('static', filename='js/vendor/xterm.js') }}"></script>
|
|
<script src="{{ url_for('static', filename='js/vendor/xterm-addon-fit.js') }}"></script>
|
|
<script>
|
|
// -------------------------------------------------------------------------
|
|
// SSH-Verbindungstest (Web-Terminal via /ws/ssh_terminal)
|
|
// -------------------------------------------------------------------------
|
|
let term = null, fitAddon = null, termSocket = null, activePasswordInput = null;
|
|
|
|
function ensureTerminal() {
|
|
if (term) return;
|
|
term = new Terminal({
|
|
convertEol: true,
|
|
fontSize: 13,
|
|
fontFamily: "ui-monospace, SFMono-Regular, Consolas, monospace",
|
|
cursorBlink: true,
|
|
theme: { background: "#0a0c10", foreground: "#c7ccd6" },
|
|
});
|
|
fitAddon = new FitAddon.FitAddon();
|
|
term.loadAddon(fitAddon);
|
|
term.open(document.getElementById("terminal"));
|
|
fitAddon.fit();
|
|
term.onData((data) => {
|
|
if (termSocket && termSocket.readyState === WebSocket.OPEN) {
|
|
termSocket.send(JSON.stringify({ type: "input", data }));
|
|
}
|
|
});
|
|
term.onResize(({ cols, rows }) => {
|
|
if (termSocket && termSocket.readyState === WebSocket.OPEN) {
|
|
termSocket.send(JSON.stringify({ type: "resize", cols, rows }));
|
|
}
|
|
});
|
|
window.addEventListener("resize", () => fitAddon && fitAddon.fit());
|
|
}
|
|
|
|
function setTermStatus(text, cls) {
|
|
const el = document.getElementById("termStatus");
|
|
el.className = "pill " + cls;
|
|
el.innerText = text;
|
|
}
|
|
|
|
function openTerminal(form) {
|
|
const host = (form.querySelector("input[name='ip']") || {}).value?.trim();
|
|
const username = (form.querySelector("input[name='username']") || {}).value?.trim();
|
|
activePasswordInput = form.querySelector("input[name='password']");
|
|
|
|
if (!host || !username) {
|
|
showToast("Bitte IP-Adresse und Username ausfüllen, bevor du die Verbindung testest.", "danger");
|
|
return;
|
|
}
|
|
|
|
PoeUI.openModal("terminalModal");
|
|
ensureTerminal();
|
|
term.reset();
|
|
document.getElementById("termTarget").innerText = `${username}@${host}`;
|
|
setTermStatus("Verbinde…", "unknown");
|
|
setTimeout(() => fitAddon && fitAddon.fit(), 60);
|
|
|
|
if (termSocket) { try { termSocket.close(); } catch (e) {} }
|
|
|
|
const proto = location.protocol === "https:" ? "wss:" : "ws:";
|
|
termSocket = new WebSocket(`${proto}//${location.host}/ws/ssh_terminal`);
|
|
|
|
termSocket.onopen = () => {
|
|
termSocket.send(JSON.stringify({ host, username, port: 22 }));
|
|
setTermStatus("Verbunden", "online");
|
|
};
|
|
termSocket.onmessage = (event) => term.write(event.data);
|
|
termSocket.onclose = () => setTermStatus("Getrennt", "offline");
|
|
// Bewusst kein Toast bei "error": manche Browser melden beim Schließen
|
|
// einer WebSocket-Verbindung ein error-Event, obwohl die Sitzung
|
|
// inhaltlich erfolgreich war. Terminal-Inhalt + Status-Pill genügen als
|
|
// Feedback — der Verbindungsstatus wird zuverlässig über onclose gepflegt.
|
|
termSocket.onerror = () => setTermStatus("Fehler", "offline");
|
|
}
|
|
|
|
function closeTerminal() {
|
|
if (termSocket) {
|
|
try { termSocket.close(); } catch (e) {}
|
|
termSocket = null;
|
|
}
|
|
}
|
|
|
|
const termPasteBtn = document.getElementById("termPastePassword");
|
|
if (termPasteBtn) {
|
|
termPasteBtn.addEventListener("click", () => {
|
|
if (!activePasswordInput || !activePasswordInput.value) {
|
|
showToast("Kein Passwort im Formular eingetragen.", "danger");
|
|
return;
|
|
}
|
|
if (!termSocket || termSocket.readyState !== WebSocket.OPEN) {
|
|
showToast("Keine aktive Terminal-Verbindung.", "danger");
|
|
return;
|
|
}
|
|
termSocket.send(JSON.stringify({ type: "input", data: activePasswordInput.value + "\n" }));
|
|
});
|
|
}
|
|
|
|
// Verbindung auch beim Schließen per Klick auf Backdrop / Escape sauber trennen
|
|
const terminalModalEl = document.getElementById("terminalModal");
|
|
if (terminalModalEl) {
|
|
terminalModalEl.addEventListener("click", (e) => {
|
|
if (e.target.id === "terminalModal") closeTerminal();
|
|
});
|
|
document.addEventListener("keydown", (e) => {
|
|
if (e.key === "Escape" && terminalModalEl.classList.contains("open")) closeTerminal();
|
|
});
|
|
}
|
|
|
|
const ipPattern = /^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/;
|
|
|
|
function validateIP(input) {
|
|
const ok = ipPattern.test(input.value);
|
|
input.classList.toggle("is-invalid", !ok);
|
|
return ok;
|
|
}
|
|
|
|
function validatePassword(id, isEdit) {
|
|
const pass = document.getElementById(isEdit ? `password_edit_${id}` : "password_add");
|
|
const confirm = document.getElementById(isEdit ? `password_confirm_edit_${id}` : "password_confirm_add");
|
|
if (!pass || !confirm) return true;
|
|
if (pass.value !== confirm.value) { confirm.classList.add("is-invalid"); return false; }
|
|
confirm.classList.remove("is-invalid");
|
|
return true;
|
|
}
|
|
|
|
function validateSwitchForm(form, id) {
|
|
const ipInput = form.querySelector("input[name='ip']");
|
|
let valid = true;
|
|
if (ipInput) valid = validateIP(ipInput) && valid;
|
|
valid = validatePassword(id, id !== "add") && valid;
|
|
return valid;
|
|
}
|
|
|
|
document.addEventListener("input", (e) => {
|
|
if (e.target.name === "ip") validateIP(e.target);
|
|
if (e.target.id.startsWith("password_confirm")) {
|
|
const id = e.target.id.replace("password_confirm_", "");
|
|
const pass = document.getElementById("password_" + id);
|
|
if (pass) e.target.classList.toggle("is-invalid", pass.value !== e.target.value);
|
|
}
|
|
});
|
|
|
|
function filterTable(inputId, tableId) {
|
|
const q = document.getElementById(inputId).value.trim().toLowerCase();
|
|
document.querySelectorAll(`#${tableId} tbody tr`).forEach(row => {
|
|
if (row.classList.contains("empty-row")) return;
|
|
row.style.display = row.innerText.toLowerCase().includes(q) ? "" : "none";
|
|
});
|
|
}
|
|
</script>
|
|
{% endblock %}
|