Initial upload to Gitea

This commit is contained in:
antonn committed 2026-11-30 16:32:37 +00:00
1 parent 78193254ad
commit 8478998cae
1485 files changed
+461025 -42348

No files matched your search

+194 -11
View File
@@ -2,7 +2,20 @@
(function () {
"use strict";
// Shared (localStorage, all tabs): window geometry plus a "last live
// session" hint (sessionId/live) so a tab that has never opened a terminal
// here can attach to another tab's still-live SSH session — the server
// explicitly supports several viewers sharing one session.
const STORAGE_KEY = "adpanel:ssh-terminal-window:v1";
// Per-tab (sessionStorage, dies with the tab): whether THIS tab's window is
// open, its minimized state, and the session id THIS tab's frame is
// attached to. A close in one tab must not flip another tab's restore
// state — the shared record used to carry `open`/`sessionId` globally, so
// closing here silently killed every other tab's auto-restore.
const TAB_STORAGE_KEY = "adpanel:ssh-terminal-window:tab:v1";
// Fields routed to the per-tab store by writeState(); everything else
// (geometry) stays shared.
const TAB_STATE_FIELDS = new Set(["open", "minimized", "sessionId", "live"]);
const DEFAULT_WIDTH = 960;
const DEFAULT_HEIGHT = 640;
const MIN_WIDTH = 360;
@@ -37,9 +50,13 @@
let launcherVisibilityRaf = 0;
let geometry = { x: 24, y: 24, w: DEFAULT_WIDTH, h: DEFAULT_HEIGHT };
function readState() {
// Storage access is resolved inside the try: sessionStorage/localStorage
// themselves can throw (blocked storage, privacy modes), and a throwing
// store must degrade to "no state" exactly like a parse failure does.
function readStore(store, key) {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!store) return {};
const raw = store.getItem(key);
if (!raw) return {};
const data = JSON.parse(raw);
return data && typeof data === "object" ? data : {};
@@ -48,13 +65,103 @@
}
}
function writeState(patch) {
function writeStore(store, key, patch) {
try {
const next = Object.assign({}, readState(), patch, { updatedAt: Date.now() });
localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
if (!store) return;
const next = Object.assign({}, readStore(store, key), patch, { updatedAt: Date.now() });
store.setItem(key, JSON.stringify(next));
} catch { }
}
function sessionStore() {
try { return window.sessionStorage; } catch { return null; }
}
function localStore() {
try { return window.localStorage; } catch { return null; }
}
function readTabState() {
return readStore(sessionStore(), TAB_STORAGE_KEY);
}
function writeTabState(patch) {
writeStore(sessionStore(), TAB_STORAGE_KEY, patch);
}
function readSharedState() {
return readStore(localStore(), STORAGE_KEY);
}
function writeSharedState(patch) {
writeStore(localStore(), STORAGE_KEY, patch);
}
function deleteSharedKeys(keys) {
try {
const store = localStore();
if (!store) return;
const raw = store.getItem(STORAGE_KEY);
if (!raw) return;
const data = JSON.parse(raw);
if (!data || typeof data !== "object") return;
let changed = false;
for (const key of keys) {
if (key in data) {
delete data[key];
changed = true;
}
}
if (changed) store.setItem(STORAGE_KEY, JSON.stringify(data));
} catch { }
}
// One-time migration from the pre-per-tab layout, which stored
// open/minimized/sessionId/live in the shared key. Adopt the legacy record
// into this tab's own storage (so an in-flight terminal survives the
// upgrade) and strip the ownership fields from the shared record — from
// then on the shared sessionId/live pair is only the last-live-session
// hint. Every not-yet-migrated open tab adopts once (each into its own
// sessionStorage); the server's multi-viewer attach makes that harmless.
function migrateLegacySharedState() {
const shared = readSharedState();
if (!("open" in shared) && !("minimized" in shared)) return;
const tab = readTabState();
if (shared.open && Object.keys(tab).length === 0) {
writeTabState({
open: true,
minimized: !!shared.minimized,
sessionId: typeof shared.sessionId === "string" ? shared.sessionId : "",
live: shared.live !== false,
});
}
deleteSharedKeys(["open", "minimized"]);
}
// Merged view for geometry consumers and existing call sites: per-tab
// ownership fields win over the shared record (whose sessionId/live are
// then just the hint).
function readState() {
return Object.assign({}, readSharedState(), readTabState());
}
// Routes each field to the store that owns it: ownership fields to the
// per-tab record, geometry to the shared one.
function writeState(patch) {
const tabPatch = {};
const sharedPatch = {};
for (const [key, value] of Object.entries(patch)) {
(TAB_STATE_FIELDS.has(key) ? tabPatch : sharedPatch)[key] = value;
}
if (Object.keys(tabPatch).length) writeTabState(tabPatch);
if (Object.keys(sharedPatch).length) writeSharedState(sharedPatch);
}
// Valid terminal session id, same shape the server normalizes to.
function isValidSessionId(value) {
return typeof value === "string" && /^[A-Za-z0-9._:-]{8,128}$/.test(value);
}
function viewport() {
return {
w: Math.max(320, window.innerWidth || document.documentElement.clientWidth || 1280),
@@ -292,7 +399,12 @@
// check is handled by the panel's upgrade proxy (it rewrites the Origin
// header upstream). The page receives its session id via the URL
// fragment and uses no storage at all; the sandbox must keep excluding
// allow-same-origin.
// allow-same-origin. allow-modals is required for the terminal page's
// multi-line paste confirmation: in a sandboxed frame without it,
// window.confirm() is a silent no-op returning false, which would block
// every multi-line paste. The frame hosts only first-party, token-gated,
// nonce-CSP content, so modals carry no additional exposure.
// allow-same-origin stays excluded.
win.innerHTML = `
<div class="adp-ssh-terminal-titlebar" data-adp-ssh-drag>
<div class="adp-ssh-terminal-title">
@@ -310,7 +422,7 @@
</div>
</div>
<div class="adp-ssh-terminal-frame-wrap">
<iframe class="adp-ssh-terminal-frame" title="ADPanel SSH Terminal" loading="eager" referrerpolicy="no-referrer" sandbox="allow-scripts"></iframe>
<iframe class="adp-ssh-terminal-frame" title="ADPanel SSH Terminal" loading="eager" referrerpolicy="no-referrer" sandbox="allow-scripts allow-modals"></iframe>
</div>
<div class="adp-ssh-terminal-resize adp-ssh-terminal-resize-n" data-adp-ssh-resize="n"></div>
<div class="adp-ssh-terminal-resize adp-ssh-terminal-resize-e" data-adp-ssh-resize="e"></div>
@@ -383,8 +495,24 @@
// sees. A minted id is flagged fresh so the page skips a pointless
// attach attempt; a reused id lets the page re-attach to the
// still-live SSH session.
let sessionId = (typeof state.sessionId === "string" && /^[A-Za-z0-9._:-]{8,128}$/.test(state.sessionId)) ? state.sessionId : "";
// Resolution order: (1) this tab's own session id (its window was open
// here — navigation restore or re-open); (2) the shared last-live hint
// for a tab that has NEVER had a terminal here, so opening in a second
// tab attaches to the other tab's live session; (3) mint fresh. A tab
// that closed its own window keeps an empty sessionId in its record,
// so it deliberately starts clean instead of re-attaching. Both stores
// are read explicitly — the merged readState() would leak the shared
// hint into state.sessionId for a record-less tab and bypass the
// live !== false guard below.
const tab = readTabState();
let sessionId = isValidSessionId(tab.sessionId) ? tab.sessionId : "";
let minted = false;
if (!sessionId && !("sessionId" in tab)) {
const shared = readSharedState();
if (shared.live !== false && isValidSessionId(shared.sessionId)) {
sessionId = shared.sessionId;
}
}
if (!sessionId) {
sessionId = (window.crypto && typeof window.crypto.randomUUID === "function")
? window.crypto.randomUUID()
@@ -483,6 +611,21 @@
// Invalidate any in-flight open: after a close, a pending openTerminal
// promise is stale and must neither be reused nor write state/frame.src.
openEpoch += 1;
// A close can land mid-interaction: on touch/pen devices a second
// pointer taps the close button while the first is still dragging the
// titlebar (or a resize handle). The pending pointerup would otherwise
// hit endInteraction's !win guard and strand the page-wide grabbing
// cursor and the interaction state until some later drag completes, so
// unwind them here, before the window node goes away.
if (interaction) {
if (raf) {
cancelAnimationFrame(raf);
raf = 0;
}
if (win) win.classList.remove("is-interacting", "is-dragging", "is-resizing");
clearInteractionCursor();
interaction = null;
}
if (win && win.classList.contains("is-liquid-motion")) cancelWindowMotion(win);
if (frame && frame.contentWindow) {
try {
@@ -499,9 +642,19 @@
}
// The child receives the close signal and replies with a 'disconnect'
// message, destroying the server-side session; clearing the id here
// means the next open is a genuinely new terminal session.
// message; the server tears the shared session down when the closing
// tab was the last attached viewer, and otherwise only detaches this
// tab (other tabs keep the session live). Clearing the id here means
// the next open in this window is a genuinely new terminal session
// either way.
// All four fields are per-tab (routed to sessionStorage): closing here
// must leave every other tab's open flag and stored session id intact so
// their auto-restore keeps working. The shared hint is only marked
// not-live: this tab's frame is disconnecting, and if it was the last
// viewer the session really is gone — a still-attached tab refreshes the
// hint via its own frame's next live report.
writeState({ open: false, minimized: false, live: false, sessionId: "" });
writeSharedState({ live: false });
const old = win;
const from = old ? old.getBoundingClientRect() : null;
@@ -608,7 +761,18 @@
}
function endInteraction(event) {
if (!interaction || !win || event.pointerId !== interaction.pointerId) return;
if (!interaction) return;
if (!win) {
// Defense in depth for a window torn down mid-interaction by a path
// that did not unwind first: the geometry has nothing left to apply
// to, but the page-wide interaction cursor must still unwind or the
// page stays stuck in "grabbing"/resize cursor until a later drag
// completes.
clearInteractionCursor();
interaction = null;
return;
}
if (event.pointerId !== interaction.pointerId) return;
const minimized = win.classList.contains("is-minimized");
const next = interaction.next || clampGeometry(geometry.x, geometry.y, geometry.w, geometry.h, minimized);
applyGeometry(next);
@@ -630,9 +794,17 @@
});
});
// Adopt/strip the pre-per-tab shared record before the restore read
// below, so an in-flight terminal survives this deploy exactly once.
migrateLegacySharedState();
// Invariant: `open: true` is only persisted after a successful launch
// (see openTerminal), so a true state here means the last open fully
// succeeded — restoring it can never resurrect a failed/ghost window.
// The flag is per-tab (sessionStorage): a close in another tab no longer
// suppresses this tab's restore, and this tab's close no longer
// suppresses theirs. The merged read still falls back to the shared
// record for geometry.
const state = readState();
if (state.open) {
// The frame reports liveness via postMessage (live on ws connect, false
@@ -709,6 +881,17 @@
const data = event.data;
if (!data || data.type !== "adpanel-ssh-terminal-live" || typeof data.live !== "boolean") return;
writeState({ live: data.live });
// Mirror liveness into the shared hint so a tab that never opened a
// terminal here can discover and attach to this tab's live session.
// live:false clears only the flag (the id stays for diagnostics); the
// frame-null guard above keeps a stale report from a dying frame out —
// closeTerminal nulls the frame before any late report can land.
if (data.live) {
const sid = readTabState().sessionId;
if (isValidSessionId(sid)) writeSharedState({ sessionId: sid, live: true });
} else {
writeSharedState({ live: false });
}
});
window.addEventListener("resize", () => {
if (!win) return;