merge: full Titano Server (auth, users, uploads, sync, jobs, render nodes, events, admin panel, Windows service)
# Conflicts: # i18n/en/Server.resx # i18n/it/Server.it.resx # src/Titano.Data/Repositories/FrameRepository.cs
This commit is contained in:
104 files changed
+8879
-250
No files matched your search
@@ -1,4 +1,4 @@
|
||||
/* Titano Server admin — DESIGN.md light tokens (the web admin is light-only). */
|
||||
/* Titano Server admin — DESIGN.md light tokens (the web admin is light-only), mockups Admin.html and AdminUsers.html. */
|
||||
:root {
|
||||
--bg: #f3f3f3;
|
||||
--chrome: #ffffff;
|
||||
@@ -15,12 +15,14 @@
|
||||
--on-accent: #ffffff;
|
||||
--accent-selected: #e8f1fa;
|
||||
--sun: #e08a1e;
|
||||
--sun-soft: #fff4e5;
|
||||
--success: #0f7b4f;
|
||||
--warning: #9a6700;
|
||||
--error: #c42b1c;
|
||||
--radius: 6px;
|
||||
--radius-card: 8px;
|
||||
--font: "Segoe UI Variable Text", "Segoe UI", system-ui, -apple-system, "Noto Sans", sans-serif;
|
||||
--mono: Consolas, "Cascadia Mono", ui-monospace, monospace;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
@@ -28,73 +30,190 @@ html, body { height: 100%; }
|
||||
body { margin: 0; font: 13px/1.45 var(--font); color: var(--text); background: var(--bg); }
|
||||
button, input, select { font: inherit; color: inherit; }
|
||||
a { color: var(--accent); }
|
||||
a:hover { color: #004c8c; }
|
||||
h1, h2 { margin: 0; }
|
||||
h2 { font-size: 14px; font-weight: 600; }
|
||||
.num { font-variant-numeric: tabular-nums; }
|
||||
.mono { font-family: var(--mono); }
|
||||
.muted { color: var(--text-muted); }
|
||||
.right { text-align: right; justify-self: end; }
|
||||
.nowrap { white-space: nowrap; }
|
||||
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
|
||||
.spacer { flex: 1; }
|
||||
[hidden] { display: none !important; }
|
||||
|
||||
/* Shell */
|
||||
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
|
||||
.sidebar { background: var(--chrome); border-right: 1px solid var(--stroke); display: flex; flex-direction: column; padding: 16px 12px; }
|
||||
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; }
|
||||
.brand img { width: 28px; height: 28px; }
|
||||
.sidebar { background: var(--chrome); border-right: 1px solid var(--stroke); display: flex; flex-direction: column; padding: 16px 12px; position: sticky; top: 0; height: 100vh; }
|
||||
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; }
|
||||
.brand img { width: 22px; height: 22px; }
|
||||
.brand strong { display: block; font-size: 14px; }
|
||||
.brand span { display: block; font-size: 11px; color: var(--text-muted); }
|
||||
.nav a { display: flex; align-items: center; gap: 12px; height: 36px; padding: 0 12px; border-radius: var(--radius); color: var(--text); text-decoration: none; margin-bottom: 2px; }
|
||||
.brand.large img { width: 40px; height: 40px; }
|
||||
.brand.large strong { font-size: 18px; }
|
||||
.nav { display: grid; gap: 2px; }
|
||||
.nav a { display: flex; align-items: center; gap: 10px; height: 36px; padding: 0 10px; border-radius: var(--radius); color: var(--text); text-decoration: none; }
|
||||
.nav a:hover { background: var(--card-hover); }
|
||||
.nav a[aria-current="page"] { background: var(--accent-selected); color: var(--accent); font-weight: 600; }
|
||||
.nav svg { width: 16px; height: 16px; flex: none; }
|
||||
.nav .badge { margin-left: auto; min-width: 18px; height: 16px; padding: 0 5px; border-radius: 8px; background: var(--accent); color: var(--on-accent); font-size: 10px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; }
|
||||
.sidebar .user { margin-top: auto; border-top: 1px solid var(--stroke); padding: 12px 8px 0; display: flex; align-items: center; gap: 10px; }
|
||||
.avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--accent-selected); color: var(--accent); display: grid; place-items: center; font-weight: 600; }
|
||||
.nav svg { flex: none; }
|
||||
.badge { margin-left: auto; min-width: 18px; padding: 0 6px; border-radius: 8px; background: var(--accent); color: var(--on-accent); font-size: 11px; font-weight: 600; text-align: center; }
|
||||
.badge.quiet { background: none; color: var(--text-muted); font-weight: 400; }
|
||||
.sidebar .user { margin-top: auto; border-top: 1px solid var(--stroke); padding: 8px 10px 0; display: flex; align-items: center; gap: 10px; }
|
||||
.user .who { flex: 1; min-width: 0; }
|
||||
.user .who strong { display: block; }
|
||||
.user .who span { color: var(--text-muted); font-size: 11px; }
|
||||
|
||||
.main { display: flex; flex-direction: column; min-width: 0; }
|
||||
.header { height: 60px; display: flex; align-items: center; gap: 12px; padding: 0 24px; border-bottom: 1px solid var(--stroke); background: var(--chrome); }
|
||||
.header h1 { font-size: 18px; font-weight: 600; margin: 0; }
|
||||
.header { min-height: 60px; display: flex; align-items: center; gap: 14px; padding: 0 24px; border-bottom: 1px solid var(--stroke); background: var(--chrome); flex-wrap: wrap; }
|
||||
.header h1 { font-size: 18px; font-weight: 600; }
|
||||
.header .meta { color: var(--text-muted); }
|
||||
.header .spacer { flex: 1; }
|
||||
.content { padding: 24px; display: grid; gap: 20px; }
|
||||
.header .actions { display: flex; align-items: center; gap: 8px; }
|
||||
.header .language { width: auto; }
|
||||
.content { padding: 24px; display: grid; gap: 20px; align-content: start; }
|
||||
|
||||
/* Controls */
|
||||
.btn { height: 32px; min-width: 44px; padding: 0 12px; border: 1px solid var(--stroke); border-radius: var(--radius); background: var(--card); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
|
||||
.btn { height: 32px; min-width: 44px; padding: 0 12px; border: 1px solid var(--stroke); border-radius: var(--radius); background: var(--card); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none; color: var(--text); white-space: nowrap; }
|
||||
.btn:hover { background: var(--card-hover); border-color: var(--stroke-strong); }
|
||||
.btn:focus-visible, .input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
|
||||
.btn:focus-visible, .input:focus-visible, .chip:focus-visible, .link:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
|
||||
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }
|
||||
.btn.primary:hover { background: var(--accent-hover); }
|
||||
.btn.destructive { border-color: var(--error); color: var(--error); background: transparent; }
|
||||
.btn.subtle { border-color: transparent; background: transparent; }
|
||||
.btn.icon { width: 32px; padding: 0; }
|
||||
.btn.icon { width: 32px; min-width: 32px; padding: 0; }
|
||||
.btn.small { height: 28px; padding: 0 10px; font-size: 12px; }
|
||||
.btn:disabled { opacity: .4; cursor: default; }
|
||||
.btn[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); }
|
||||
.link { border: 0; background: none; padding: 0; color: var(--text); cursor: pointer; text-align: left; }
|
||||
.link:hover { color: var(--accent); }
|
||||
.input { height: 32px; width: 100%; padding: 0 10px; border: 1px solid var(--stroke); border-radius: var(--radius); background: var(--card); }
|
||||
.input:hover { border-color: var(--stroke-strong); }
|
||||
.input:focus { border-color: var(--accent); outline: none; }
|
||||
.input[aria-invalid="true"] { border-color: var(--error); }
|
||||
select.input { padding-right: 24px; }
|
||||
.input:disabled, .input[readonly] { background: var(--bg); color: var(--text-muted); }
|
||||
.input.small { height: 28px; width: auto; font-size: 12px; }
|
||||
.input.narrow { width: 120px; }
|
||||
.input.search { width: 220px; }
|
||||
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); margin: 0; }
|
||||
.chips, .button-row { display: flex; gap: 8px; flex-wrap: wrap; }
|
||||
.chip { height: 24px; padding: 0 10px; border: 1px solid var(--stroke); border-radius: 12px; background: var(--card); font-size: 11px; cursor: pointer; }
|
||||
.chip[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
|
||||
.chip.warn { border-color: var(--warning); color: var(--warning); cursor: default; display: inline-flex; align-items: center; }
|
||||
.segmented { display: inline-flex; }
|
||||
.segmented .btn { border-radius: 0; margin-left: -1px; }
|
||||
.segmented .btn:first-child { border-radius: var(--radius) 0 0 var(--radius); }
|
||||
.segmented .btn:last-child { border-radius: 0 var(--radius) var(--radius) 0; }
|
||||
|
||||
.panel { background: var(--surface); border: 1px solid var(--stroke); border-radius: var(--radius-card); padding: 18px 20px; }
|
||||
.panel h2 { font-size: 14px; font-weight: 600; margin: 0 0 12px; }
|
||||
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; }
|
||||
.tile .label { color: var(--text-muted); }
|
||||
.tile .value { font-size: 28px; font-weight: 600; margin-top: 4px; }
|
||||
.tile .value small { font-size: 13px; font-weight: 400; color: var(--text-muted); margin-left: 6px; }
|
||||
.tile .sub { color: var(--text-muted); margin-top: 6px; }
|
||||
.progress { height: 6px; border-radius: 3px; background: var(--stroke); overflow: hidden; margin-top: 10px; }
|
||||
.progress > div { height: 6px; background: var(--accent); }
|
||||
/* Panels and tiles */
|
||||
.panel { background: var(--surface); border: 1px solid var(--stroke); border-radius: var(--radius-card); padding: 16px 18px; display: grid; gap: 10px; align-content: start; min-width: 0; }
|
||||
.panel.flush { padding: 0; gap: 0; overflow-x: auto; }
|
||||
.panel-head { display: flex; align-items: center; gap: 12px; }
|
||||
.panel-head h2 { flex: 1; }
|
||||
.panel-head.padded { padding: 12px 18px; border-bottom: 1px solid var(--stroke); }
|
||||
.empty { padding: 24px 18px; color: var(--text-muted); }
|
||||
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 20px; }
|
||||
.tile { gap: 8px; }
|
||||
.tile .label, .tile .sub { font-size: 12px; color: var(--text-muted); }
|
||||
.tile .value { font-size: 26px; font-weight: 600; display: flex; align-items: baseline; gap: 6px; }
|
||||
.tile .value small { font-size: 13px; font-weight: 400; color: var(--text-muted); }
|
||||
.grid { display: grid; gap: 20px; }
|
||||
.grid.two { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
|
||||
.grid.wide-right { grid-template-columns: minmax(300px, 1fr) minmax(400px, 1.4fr); }
|
||||
|
||||
.status-ok { color: var(--success); }
|
||||
.status-warn { color: var(--warning); }
|
||||
.status-err { color: var(--error); }
|
||||
/* Bars and charts (SVG attributes only: the panel runs under a strict CSP) */
|
||||
svg.bar { width: 100%; height: 6px; display: block; }
|
||||
svg.bar .track { fill: var(--stroke); }
|
||||
svg.bar .fill { fill: var(--accent); }
|
||||
svg.bar.muted .fill { fill: var(--text-faint); }
|
||||
svg.chart { width: 100%; height: auto; max-height: 220px; }
|
||||
svg.chart .axis { stroke: var(--stroke); }
|
||||
svg.chart .column { fill: var(--accent); }
|
||||
svg.chart.sun .column { fill: var(--sun); }
|
||||
svg.chart text { font: 10px var(--font); text-anchor: middle; }
|
||||
svg.chart .value { fill: var(--text); }
|
||||
svg.chart .label { fill: var(--text-muted); }
|
||||
.progress-cell { display: flex; align-items: center; gap: 8px; min-width: 0; }
|
||||
.progress-cell svg.bar { flex: 1; min-width: 40px; }
|
||||
|
||||
/* Tables (grid rows, 52 px in the admin) */
|
||||
.table { display: grid; min-width: 0; }
|
||||
.table .thead, .table .row { display: grid; gap: 12px; align-items: center; padding: 0 18px; }
|
||||
.table .thead { font-size: 11px; color: var(--text-muted); padding-top: 10px; padding-bottom: 10px; border-bottom: 1px solid var(--stroke); }
|
||||
.table .row { min-height: 52px; border-bottom: 1px solid var(--stroke); }
|
||||
.table .row:hover { background: var(--card-hover); }
|
||||
.table .row.dim { color: var(--text-muted); }
|
||||
.table.compact .row { min-height: 32px; padding: 0; grid-template-columns: 1fr auto auto auto; border-bottom: 0; }
|
||||
.table.compact .row:hover { background: none; }
|
||||
.table.users .thead, .table.users .row { grid-template-columns: minmax(110px, 1.4fr) 90px minmax(100px, 1.2fr) minmax(80px, 1fr) 70px 70px 230px; }
|
||||
.table.jobs .thead, .table.jobs .row { grid-template-columns: 110px minmax(140px, 1.5fr) 100px minmax(110px, 1fr) 100px 80px 280px; }
|
||||
.table.jobs-mini .thead, .table.jobs-mini .row { grid-template-columns: 96px minmax(120px, 1.5fr) 92px minmax(100px, 1fr) 90px 80px; padding: 0; min-height: 40px; }
|
||||
.table.jobs-mini .row { border-bottom: 0; }
|
||||
.table.nodes .thead, .table.nodes .row { grid-template-columns: minmax(140px, 1fr) 120px minmax(140px, 1fr) 170px 100px 100px; }
|
||||
.table.audit .thead, .table.audit .row { grid-template-columns: 170px 120px 170px minmax(160px, 1fr) 130px; min-height: 40px; }
|
||||
.table-foot { display: flex; align-items: center; gap: 16px; padding: 10px 18px; font-size: 12px; }
|
||||
.row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
|
||||
.person { display: flex; align-items: center; gap: 10px; min-width: 0; }
|
||||
.stack { display: flex; flex-direction: column; min-width: 0; }
|
||||
.stack small { font-size: 11px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.avatar { width: 30px; height: 30px; flex: none; border-radius: 50%; background: var(--accent-selected); color: var(--accent); display: grid; place-items: center; font-weight: 600; }
|
||||
.avatar.uploader { background: var(--sun-soft); color: var(--warning); }
|
||||
.row.dim .avatar { background: #eeeeee; color: var(--text-muted); }
|
||||
.state { white-space: nowrap; }
|
||||
.state.running { color: var(--accent); }
|
||||
.state.done { color: var(--success); }
|
||||
.state.failed { color: var(--error); }
|
||||
.state.paused { color: var(--warning); }
|
||||
.state.queued, .state.cancelled { color: var(--text-muted); }
|
||||
.activity { display: grid; grid-template-columns: 64px 1fr; column-gap: 12px; row-gap: 9px; font-size: 12px; }
|
||||
pre.log { margin: 0; padding: 12px 18px; background: var(--bg); border-top: 1px solid var(--stroke); font: 12px/1.5 var(--mono); white-space: pre-wrap; overflow: auto; max-height: 260px; }
|
||||
pre.log.tall { max-height: 60vh; border-top: 0; }
|
||||
|
||||
/* Forms */
|
||||
.split { display: grid; grid-template-columns: 1fr auto; gap: 20px; align-items: start; }
|
||||
.side { width: 340px; }
|
||||
.form { display: grid; gap: 12px; }
|
||||
.form label { display: grid; gap: 6px; color: var(--text-muted); }
|
||||
.form label.check, label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
|
||||
.form fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 8px; }
|
||||
.form legend { color: var(--text-muted); margin-bottom: 8px; }
|
||||
.form h2 { font-size: 16px; }
|
||||
.inline { display: flex; align-items: center; gap: 8px; }
|
||||
.buttons { display: flex; gap: 8px; justify-content: flex-end; }
|
||||
.error { color: var(--error); min-height: 18px; }
|
||||
.settings { display: grid; gap: 20px; }
|
||||
.field { display: grid; grid-template-columns: 180px minmax(0, 420px); align-items: center; gap: 12px; color: var(--text-muted); }
|
||||
.settings-form .field { grid-template-columns: 140px minmax(0, 1fr); }
|
||||
.fingerprint { font: 12px var(--mono); color: var(--text); overflow-wrap: anywhere; }
|
||||
|
||||
/* Dialogs and toasts */
|
||||
dialog.dialog { border: 1px solid var(--stroke); border-radius: 12px; padding: 20px 22px; width: 420px; max-width: calc(100vw - 32px); box-shadow: 0 8px 24px rgba(0, 0, 0, .18); }
|
||||
dialog.dialog::backdrop { background: rgba(0, 0, 0, .25); }
|
||||
dialog.dialog form { display: grid; gap: 14px; }
|
||||
dialog.dialog h2 { font-size: 16px; }
|
||||
dialog.dialog p { margin: 0; color: var(--text-muted); }
|
||||
#toasts { position: fixed; right: 24px; bottom: 24px; display: grid; gap: 8px; z-index: 10; }
|
||||
.toast { width: 420px; max-width: calc(100vw - 48px); padding: 12px 14px; background: var(--card); border: 1px solid var(--stroke); border-left: 3px solid var(--accent); border-radius: var(--radius-card); box-shadow: 0 4px 16px rgba(0, 0, 0, .12); }
|
||||
.toast.error { border-left-color: var(--error); }
|
||||
|
||||
/* About */
|
||||
.about dl { display: grid; grid-template-columns: 180px 1fr; gap: 8px 16px; margin: 0; }
|
||||
.about dt { color: var(--text-muted); }
|
||||
.about dd { margin: 0; overflow-wrap: anywhere; }
|
||||
|
||||
/* Login */
|
||||
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
|
||||
.login form { width: 360px; max-width: 100%; display: grid; gap: 14px; }
|
||||
.login .brand { padding: 0 0 8px; }
|
||||
.login h1 { font-size: 16px; font-weight: 600; }
|
||||
.login label { display: grid; gap: 6px; color: var(--text-muted); }
|
||||
.login .error { color: var(--error); min-height: 18px; }
|
||||
.login .row { display: flex; gap: 8px; }
|
||||
|
||||
@media (max-width: 800px) {
|
||||
@media (max-width: 900px) {
|
||||
.shell { grid-template-columns: 1fr; }
|
||||
.sidebar { flex-direction: row; overflow-x: auto; padding: 8px; }
|
||||
.sidebar { flex-direction: row; overflow-x: auto; padding: 8px; position: static; height: auto; }
|
||||
.sidebar .brand, .sidebar .user { display: none; }
|
||||
.nav { display: flex; gap: 4px; }
|
||||
.nav a span { display: none; }
|
||||
.nav a span:not(.badge) { display: none; }
|
||||
.split, .grid.wide-right { grid-template-columns: 1fr; }
|
||||
.side { width: auto; }
|
||||
.field { grid-template-columns: 1fr; }
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
// Same API as the desktop clients: cookie session + double-submit CSRF token for the web admin (BRIEF §14.2).
|
||||
// Same API as the desktop clients: cookie session + CSRF header for the web admin (BRIEF §14.2).
|
||||
import { culture } from "./i18n.js";
|
||||
|
||||
export class ApiFailure extends Error {
|
||||
constructor(status, code, message) {
|
||||
constructor(status, code, message, details) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
|
||||
const SESSION_KEY = "titano.admin.user";
|
||||
let currentUser = null;
|
||||
|
||||
function csrfToken() {
|
||||
const m = document.cookie.match(/(?:^|; )titano_csrf=([^;]+)/);
|
||||
@@ -15,15 +18,27 @@ function csrfToken() {
|
||||
}
|
||||
|
||||
async function request(method, url, body) {
|
||||
const headers = { Accept: "application/json" };
|
||||
const headers = { Accept: "application/json", "Accept-Language": culture() };
|
||||
if (body !== undefined) headers["Content-Type"] = "application/json";
|
||||
if (method !== "GET") headers["X-CSRF-Token"] = csrfToken();
|
||||
const res = await fetch(url, { method, headers, credentials: "same-origin", body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
if (res.status === 401 && url !== "/api/v1/auth/login") sessionStorage.removeItem(SESSION_KEY);
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(url, { method, headers, credentials: "same-origin", body: body === undefined ? undefined : JSON.stringify(body) });
|
||||
} catch {
|
||||
throw new ApiFailure(0, "unreachable", "");
|
||||
}
|
||||
if (res.status === 401 && url !== "/api/v1/auth/login") {
|
||||
currentUser = null;
|
||||
window.dispatchEvent(new Event("titano:signed-out"));
|
||||
}
|
||||
if (!res.ok) {
|
||||
let err = { code: "http_" + res.status, message: res.statusText };
|
||||
try { err = await res.json(); } catch { /* non-JSON error */ }
|
||||
throw new ApiFailure(res.status, err.code, err.message);
|
||||
try {
|
||||
err = await res.json();
|
||||
} catch {
|
||||
// Not a JSON error body.
|
||||
}
|
||||
throw new ApiFailure(res.status, err.code, err.message, err.details);
|
||||
}
|
||||
return res.status === 204 ? null : res.json();
|
||||
}
|
||||
@@ -31,17 +46,71 @@ async function request(method, url, body) {
|
||||
export const api = {
|
||||
get: (url) => request("GET", url),
|
||||
post: (url, body) => request("POST", url, body ?? {}),
|
||||
put: (url, body) => request("PUT", url, body),
|
||||
patch: (url, body) => request("PATCH", url, body),
|
||||
del: (url) => request("DELETE", url),
|
||||
isSignedIn: () => sessionStorage.getItem(SESSION_KEY) !== null,
|
||||
user: () => JSON.parse(sessionStorage.getItem(SESSION_KEY) ?? "null"),
|
||||
user: () => currentUser,
|
||||
|
||||
/** Restores the session from the cookie; null when signed out. */
|
||||
async resume() {
|
||||
try {
|
||||
currentUser = await request("GET", "/api/v1/auth/me");
|
||||
} catch {
|
||||
currentUser = null;
|
||||
}
|
||||
return currentUser;
|
||||
},
|
||||
|
||||
async signIn(username, password) {
|
||||
const r = await request("POST", "/api/v1/auth/login", { username, password, clientName: "admin-web", cookie: true });
|
||||
sessionStorage.setItem(SESSION_KEY, JSON.stringify(r.user));
|
||||
currentUser = r.user;
|
||||
return r;
|
||||
},
|
||||
|
||||
async changePassword(currentPassword, newPassword) {
|
||||
await request("POST", "/api/v1/auth/password", { currentPassword, newPassword });
|
||||
currentUser = await request("GET", "/api/v1/auth/me");
|
||||
},
|
||||
|
||||
async signOut() {
|
||||
try { await request("POST", "/api/v1/auth/logout", {}); } catch { /* already gone */ }
|
||||
sessionStorage.removeItem(SESSION_KEY);
|
||||
try {
|
||||
await request("POST", "/api/v1/auth/logout", {});
|
||||
} catch {
|
||||
// Already signed out.
|
||||
}
|
||||
currentUser = null;
|
||||
},
|
||||
|
||||
/** Server events over WebSocket (cookie session), reconnecting with back-off until stop() is called. */
|
||||
events(onEvent) {
|
||||
let socket = null;
|
||||
let stopped = false;
|
||||
let delay = 1000;
|
||||
const connect = () => {
|
||||
if (stopped) return;
|
||||
socket = new WebSocket(`${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/api/v1/events`);
|
||||
socket.onopen = () => {
|
||||
delay = 1000;
|
||||
};
|
||||
socket.onmessage = (m) => {
|
||||
try {
|
||||
onEvent(JSON.parse(m.data));
|
||||
} catch {
|
||||
// Ignore malformed or unknown events.
|
||||
}
|
||||
};
|
||||
socket.onclose = () => {
|
||||
if (stopped) return;
|
||||
setTimeout(connect, delay);
|
||||
delay = Math.min(delay * 2, 30000);
|
||||
};
|
||||
};
|
||||
connect();
|
||||
return {
|
||||
stop() {
|
||||
stopped = true;
|
||||
socket?.close();
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -1,42 +1,53 @@
|
||||
// Titano Server admin panel — vanilla ES modules, no build step (BRIEF §14.8).
|
||||
import { api, ApiFailure } from "./api.js";
|
||||
import { t, loadStrings, applyStrings } from "./i18n.js";
|
||||
import { t, loadStrings, applyStrings, setLanguage, languages, culture } from "./i18n.js";
|
||||
import { icons } from "./icons.js";
|
||||
import { escapeHtml, attr, errorText, nativeName } from "./ui.js";
|
||||
|
||||
const app = document.getElementById("app");
|
||||
const pages = ["dashboard", "users", "archive", "jobs", "settings", "logs", "about"];
|
||||
const pageIcon = { dashboard: "dashboard", users: "users", archive: "archive", jobs: "jobs", settings: "settings", logs: "logs", about: "about" };
|
||||
|
||||
let info = null;
|
||||
let events = null;
|
||||
const listeners = new Set();
|
||||
|
||||
async function start() {
|
||||
await loadStrings(navigator.language);
|
||||
await loadStrings();
|
||||
info = await api.get("/api/v1/server/info").catch(() => null);
|
||||
await api.resume();
|
||||
window.addEventListener("hashchange", render);
|
||||
window.addEventListener("titano:signed-out", () => {
|
||||
events?.stop();
|
||||
events = null;
|
||||
render();
|
||||
});
|
||||
render();
|
||||
}
|
||||
|
||||
function currentPage() {
|
||||
const p = location.hash.replace(/^#\/?/, "");
|
||||
const p = location.hash.replace(/^#\/?/, "").split("?")[0];
|
||||
return pages.includes(p) ? p : "dashboard";
|
||||
}
|
||||
|
||||
function render() {
|
||||
if (!api.isSignedIn()) {
|
||||
renderLogin();
|
||||
return;
|
||||
}
|
||||
const user = api.user();
|
||||
if (!user) return renderLogin();
|
||||
if (user.mustChangePassword) return renderPasswordChange();
|
||||
renderShell(currentPage());
|
||||
}
|
||||
|
||||
function renderLogin() {
|
||||
function brand() {
|
||||
return `<div class="brand"><img src="favicon.svg" alt=""><div><strong>Titano Server</strong><span class="num">${escapeHtml(info?.name ?? "")} · v${escapeHtml(info?.version ?? "")}</span></div></div>`;
|
||||
}
|
||||
|
||||
function renderLogin(message = "") {
|
||||
app.innerHTML = `
|
||||
<main class="login">
|
||||
<form novalidate>
|
||||
<div class="brand"><img src="favicon.svg" alt=""><div><strong>Titano Server</strong><span class="num">${escapeHtml(info?.name ?? "")} · v${escapeHtml(info?.version ?? "")}</span></div></div>
|
||||
<label><span data-i18n="Admin.Login.User"></span><input class="input" name="username" autocomplete="username" required></label>
|
||||
<label><span data-i18n="Admin.Login.Password"></span><input class="input" name="password" type="password" autocomplete="current-password" required></label>
|
||||
<div class="error" role="alert"></div>
|
||||
${brand()}
|
||||
<label><span data-i18n="Admin.Login.User"></span><input class="input" name="username" autocomplete="username" required data-i18n-title="Admin.Login.User.Tip"></label>
|
||||
<label><span data-i18n="Admin.Login.Password"></span><input class="input" name="password" type="password" autocomplete="current-password" required data-i18n-title="Admin.Login.Password.Tip"></label>
|
||||
<div class="error" role="alert">${escapeHtml(message)}</div>
|
||||
<button class="btn primary" type="submit" data-i18n="Admin.Login.SignIn" data-i18n-title="Admin.Login.SignIn.Tip"></button>
|
||||
</form>
|
||||
</main>`;
|
||||
@@ -48,10 +59,56 @@ function renderLogin() {
|
||||
error.textContent = "";
|
||||
const data = new FormData(form);
|
||||
try {
|
||||
await api.signIn(String(data.get("username")), String(data.get("password")));
|
||||
const r = await api.signIn(String(data.get("username")), String(data.get("password")));
|
||||
if (r.user.role !== "admin") {
|
||||
await api.signOut();
|
||||
error.textContent = t("Admin.Login.NotAdmin");
|
||||
return;
|
||||
}
|
||||
render();
|
||||
} catch (err) {
|
||||
error.textContent = err instanceof ApiFailure && err.status === 401 ? t("Admin.Login.Failed") : t("Admin.Error.Unreachable");
|
||||
error.textContent = err instanceof ApiFailure && err.status === 401 ? t("Admin.Login.Failed") : errorText(err);
|
||||
}
|
||||
});
|
||||
form.querySelector("input").focus();
|
||||
}
|
||||
|
||||
function renderPasswordChange() {
|
||||
app.innerHTML = `
|
||||
<main class="login">
|
||||
<form novalidate>
|
||||
${brand()}
|
||||
<h1 data-i18n="Admin.Password.Title"></h1>
|
||||
<label><span data-i18n="Admin.Password.Current"></span><input class="input" name="current" type="password" autocomplete="current-password" required data-i18n-title="Admin.Password.Current.Tip"></label>
|
||||
<label><span data-i18n="Admin.Password.New"></span><input class="input" name="next" type="password" autocomplete="new-password" required data-i18n-title="Admin.Password.New.Tip"></label>
|
||||
<label><span data-i18n="Admin.Password.Repeat"></span><input class="input" name="repeat" type="password" autocomplete="new-password" required data-i18n-title="Admin.Password.Repeat.Tip"></label>
|
||||
<div class="error" role="alert"></div>
|
||||
<div class="row">
|
||||
<button class="btn" type="button" id="cancel" data-i18n="Admin.SignOut" data-i18n-title="Admin.SignOut.Tip"></button>
|
||||
<span class="spacer"></span>
|
||||
<button class="btn primary" type="submit" data-i18n="Admin.Password.Save" data-i18n-title="Admin.Password.Save.Tip"></button>
|
||||
</div>
|
||||
</form>
|
||||
</main>`;
|
||||
applyStrings(app);
|
||||
const form = app.querySelector("form");
|
||||
const error = form.querySelector(".error");
|
||||
app.querySelector("#cancel").addEventListener("click", async () => {
|
||||
await api.signOut();
|
||||
render();
|
||||
});
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const data = new FormData(form);
|
||||
if (data.get("next") !== data.get("repeat")) {
|
||||
error.textContent = t("Admin.Password.Mismatch");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await api.changePassword(String(data.get("current")), String(data.get("next")));
|
||||
render();
|
||||
} catch (err) {
|
||||
error.textContent = errorText(err);
|
||||
}
|
||||
});
|
||||
form.querySelector("input").focus();
|
||||
@@ -59,40 +116,82 @@ function renderLogin() {
|
||||
|
||||
function renderShell(page) {
|
||||
const nav = pages.map((p) => `
|
||||
<a href="#/${p}" ${p === page ? 'aria-current="page"' : ""} data-i18n-title="Admin.Nav.${cap(p)}.Tip">${icons[pageIcon[p]]}<span data-i18n="Admin.Nav.${cap(p)}"></span></a>`).join("");
|
||||
<a href="#/${p}" ${p === page ? 'aria-current="page"' : ""} data-i18n-title="Admin.Nav.${cap(p)}.Tip">${icons[p]}<span data-i18n="Admin.Nav.${cap(p)}"></span><span class="badge" id="badge-${p}" hidden></span></a>`).join("");
|
||||
const user = api.user();
|
||||
const options = languages().map((code) => `<option value="${attr(code)}" ${code === culture() ? "selected" : ""}>${escapeHtml(nativeName(code))}</option>`).join("");
|
||||
app.innerHTML = `
|
||||
<div class="shell">
|
||||
<aside class="sidebar">
|
||||
<div class="brand"><img src="favicon.svg" alt=""><div><strong>Titano Server</strong><span class="num">${escapeHtml(info?.name ?? "")} · v${escapeHtml(info?.version ?? "")}</span></div></div>
|
||||
${brand()}
|
||||
<nav class="nav">${nav}</nav>
|
||||
<div class="user">
|
||||
<div class="avatar">${escapeHtml((user?.username ?? "?").slice(0, 1).toUpperCase())}</div>
|
||||
<div class="who"><strong>${escapeHtml(user?.username ?? "")}</strong><span data-i18n="Admin.Role.${cap(user?.role ?? "viewer")}"></span></div>
|
||||
<button class="btn icon subtle" id="signout" data-i18n-title="Admin.SignOut.Tip" aria-label="${escapeHtml(t("Admin.SignOut.Tip"))}">${icons.power}</button>
|
||||
<div class="avatar">${escapeHtml(user.username.slice(0, 1).toUpperCase())}</div>
|
||||
<div class="who"><strong>${escapeHtml(user.username)}</strong><span data-i18n="Admin.Role.Admin"></span></div>
|
||||
<button class="btn icon" id="signout" data-i18n-title="Admin.SignOut.Tip">${icons.power}</button>
|
||||
</div>
|
||||
</aside>
|
||||
<div class="main">
|
||||
<header class="header"><h1 data-i18n="Admin.Nav.${cap(page)}"></h1><span class="meta" id="meta"></span><span class="spacer"></span></header>
|
||||
<header class="header">
|
||||
<h1 data-i18n="Admin.Nav.${cap(page)}"></h1>
|
||||
<span class="meta" id="meta"></span>
|
||||
<span class="spacer"></span>
|
||||
<span class="actions" id="actions"></span>
|
||||
<select class="input language" id="language" data-i18n-title="Admin.Language.Tip">${options}</select>
|
||||
</header>
|
||||
<section class="content" id="content"></section>
|
||||
</div>
|
||||
</div>`;
|
||||
applyStrings(app);
|
||||
app.querySelector("#signout").addEventListener("click", async () => {
|
||||
events?.stop();
|
||||
events = null;
|
||||
await api.signOut();
|
||||
render();
|
||||
});
|
||||
app.querySelector("#language").addEventListener("change", async (e) => {
|
||||
await setLanguage(e.target.value);
|
||||
render();
|
||||
});
|
||||
events ??= api.events((e) => listeners.forEach((fn) => fn(e)));
|
||||
listeners.clear();
|
||||
updateBadges();
|
||||
listeners.add((e) => {
|
||||
if (e.type === "jobState") updateBadges();
|
||||
});
|
||||
|
||||
const content = app.querySelector("#content");
|
||||
const context = {
|
||||
api,
|
||||
info,
|
||||
meta: app.querySelector("#meta"),
|
||||
actions: app.querySelector("#actions"),
|
||||
onEvent: (fn) => listeners.add(fn),
|
||||
};
|
||||
import(`./pages/${page}.js`)
|
||||
.then((m) => m.render(content, { api, t, info, meta: app.querySelector("#meta") }))
|
||||
.catch(() => {
|
||||
content.innerHTML = `<div class="panel"><span data-i18n="Admin.Error.Unreachable"></span></div>`;
|
||||
applyStrings(content);
|
||||
.then((m) => m.render(content, context))
|
||||
.catch((err) => {
|
||||
content.innerHTML = `<div class="panel empty"><span>${escapeHtml(errorText(err))}</span></div>`;
|
||||
});
|
||||
}
|
||||
|
||||
export function escapeHtml(s) {
|
||||
return String(s).replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
|
||||
let badgeTimer = null;
|
||||
|
||||
/** Running jobs and accounts, as nav badges (refreshed at most every two seconds). */
|
||||
function updateBadges() {
|
||||
if (badgeTimer) return;
|
||||
badgeTimer = setTimeout(() => {
|
||||
badgeTimer = null;
|
||||
}, 2000);
|
||||
api.get("/api/v1/jobs?state=running&limit=100").then((page) => setBadge("jobs", page.items.length, true)).catch(() => {});
|
||||
api.get("/api/v1/users").then((users) => setBadge("users", users.length, false)).catch(() => {});
|
||||
}
|
||||
|
||||
function setBadge(page, count, accent) {
|
||||
const badge = document.getElementById(`badge-${page}`);
|
||||
if (!badge) return;
|
||||
badge.hidden = count === 0;
|
||||
badge.textContent = String(count);
|
||||
badge.classList.toggle("quiet", !accent);
|
||||
}
|
||||
|
||||
function cap(s) {
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
// Strings come from /api/v1/i18n/{culture}, generated from the same .resx catalog as the desktop apps.
|
||||
const LANGUAGE_KEY = "titano.admin.language";
|
||||
let strings = {};
|
||||
|
||||
export async function loadStrings(culture) {
|
||||
/** Loads the strings of the chosen language (remembered per browser), else of the browser's language. */
|
||||
export async function loadStrings(requested) {
|
||||
let preferred = requested;
|
||||
try {
|
||||
const res = await fetch(`/api/v1/i18n/${encodeURIComponent(culture || "en")}`);
|
||||
preferred ??= localStorage.getItem(LANGUAGE_KEY);
|
||||
} catch {
|
||||
// Storage may be blocked; the browser language is used.
|
||||
}
|
||||
try {
|
||||
const res = await fetch(`/api/v1/i18n/${encodeURIComponent(preferred || navigator.language || "en")}`);
|
||||
strings = await res.json();
|
||||
} catch {
|
||||
strings = {};
|
||||
@@ -12,16 +20,47 @@ export async function loadStrings(culture) {
|
||||
document.documentElement.dir = strings.$rtl === "true" ? "rtl" : "ltr";
|
||||
}
|
||||
|
||||
export async function setLanguage(code) {
|
||||
try {
|
||||
localStorage.setItem(LANGUAGE_KEY, code);
|
||||
} catch {
|
||||
// Only this session uses the language then.
|
||||
}
|
||||
await loadStrings(code);
|
||||
}
|
||||
|
||||
/** The current culture (e.g. "it"), used for numbers, dates and the Accept-Language of API calls. */
|
||||
export function culture() {
|
||||
return strings.$culture || "en";
|
||||
}
|
||||
|
||||
/** Languages with translated strings, as codes. */
|
||||
export function languages() {
|
||||
return (strings.$languages || "en").split(",");
|
||||
}
|
||||
|
||||
/** Localized string for key with {0}, {1}… placeholders filled from args. */
|
||||
export function t(key, ...args) {
|
||||
const s = strings[key];
|
||||
if (s === undefined) return `⟦${key}⟧`;
|
||||
return s.replace(/\{(\d+)(?:[,:][^}]*)?\}/g, (_, i) => (args[Number(i)] ?? ""));
|
||||
return s.replace(/\{(\d+)(?:[,:][^}]*)?\}/g, (_, i) => String(args[Number(i)] ?? ""));
|
||||
}
|
||||
|
||||
/** Fills data-i18n (text), data-i18n-title (tooltip) and data-i18n-placeholder attributes. */
|
||||
/** True when the catalog has the key (for keys built from data, e.g. audit actions). */
|
||||
export function has(key) {
|
||||
return strings[key] !== undefined;
|
||||
}
|
||||
|
||||
/** Fills data-i18n (text), data-i18n-title (tooltip, also the accessible name of icon buttons) and data-i18n-placeholder. */
|
||||
export function applyStrings(root) {
|
||||
root.querySelectorAll("[data-i18n]").forEach((el) => { el.textContent = t(el.dataset.i18n); });
|
||||
root.querySelectorAll("[data-i18n-title]").forEach((el) => { el.title = t(el.dataset.i18nTitle); });
|
||||
root.querySelectorAll("[data-i18n-placeholder]").forEach((el) => { el.placeholder = t(el.dataset.i18nPlaceholder); });
|
||||
root.querySelectorAll("[data-i18n]").forEach((el) => {
|
||||
el.textContent = t(el.dataset.i18n);
|
||||
});
|
||||
root.querySelectorAll("[data-i18n-title]").forEach((el) => {
|
||||
el.title = t(el.dataset.i18nTitle);
|
||||
if (el.classList.contains("icon")) el.setAttribute("aria-label", el.title);
|
||||
});
|
||||
root.querySelectorAll("[data-i18n-placeholder]").forEach((el) => {
|
||||
el.placeholder = t(el.dataset.i18nPlaceholder);
|
||||
});
|
||||
}
|
||||
@@ -12,5 +12,6 @@ export const icons = {
|
||||
refresh: `<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path d="M10.75 3.24 A5.5 5.5 0 1 1 4.46 3.79 M13.2 2.6 L13.2 5.3 L10.5 5.3" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>`,
|
||||
close: `<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path d="M4 4 L12 12 M12 4 L4 12" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>`,
|
||||
plus: `<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path d="M8 3 V13 M3 8 H13" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>`,
|
||||
copy: `<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path d="M6.5 5 H13.0 A1.5 1.5 0 0 1 14.5 6.5 V13.0 A1.5 1.5 0 0 1 13.0 14.5 H6.5 A1.5 1.5 0 0 1 5 13.0 V6.5 A1.5 1.5 0 0 1 6.5 5 Z M11 5 V2.5 A1 1 0 0 0 10 1.5 H2.5 A1 1 0 0 0 1.5 2.5 V10 A1 1 0 0 0 2.5 11 H5" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>`,
|
||||
search: `<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path d="M2.5 7 A4.5 4.5 0 1 0 11.5 7 A4.5 4.5 0 1 0 2.5 7 Z M10.4 10.4 L14 14" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>`,
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
// Job and node labels shared by the dashboard and the Jobs page.
|
||||
import { t, has } from "./i18n.js";
|
||||
import { escapeHtml, attr, bar } from "./ui.js";
|
||||
|
||||
export function pascal(text) {
|
||||
return String(text).split(/[-_]/).map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join("");
|
||||
}
|
||||
|
||||
export function kindLabel(kind) {
|
||||
const key = `Admin.Job.Kind.${pascal(kind)}`;
|
||||
return has(key) ? t(key) : kind;
|
||||
}
|
||||
|
||||
export function stateLabel(state) {
|
||||
const key = `Admin.Job.State.${pascal(state)}`;
|
||||
return has(key) ? t(key) : state;
|
||||
}
|
||||
|
||||
/** Where a job runs: this server, a render node by name, or nowhere yet. */
|
||||
export function nodeLabel(job, nodes) {
|
||||
if (!job.nodeId) return "—";
|
||||
if (job.nodeId.startsWith("server:")) return t("Admin.Jobs.Server");
|
||||
return nodes.find((n) => n.id === job.nodeId)?.clientName ?? "—";
|
||||
}
|
||||
|
||||
export function progressCell(job) {
|
||||
if (job.progressTotal <= 0) return `<div class="progress-cell">${bar(0, true)}<span class="muted">—</span></div>`;
|
||||
const fraction = job.progressDone / job.progressTotal;
|
||||
return `<div class="progress-cell">${bar(fraction)}<span class="num">${Math.floor(fraction * 100)} %</span></div>`;
|
||||
}
|
||||
|
||||
export function stateCell(state) {
|
||||
const glyph = { running: "◐", queued: "○", paused: "❚❚", done: "●", failed: "✕", cancelled: "○" }[state] ?? "";
|
||||
return `<span class="state ${attr(state)}">${glyph} ${escapeHtml(stateLabel(state))}</span>`;
|
||||
}
|
||||
|
||||
/** Node encoders as "NVENC HEVC/H.264/AV1". */
|
||||
export function encoders(node) {
|
||||
const names = [...new Set(node.encoders.map((e) => e.codec.toUpperCase()))];
|
||||
return names.join("/");
|
||||
}
|
||||
@@ -1,12 +1,21 @@
|
||||
// About: version, API, database, uptime and the certificate fingerprint clients are asked to trust.
|
||||
import { applyStrings } from "../i18n.js";
|
||||
import { escapeHtml } from "../app.js";
|
||||
import { escapeHtml, formatDuration } from "../ui.js";
|
||||
|
||||
export async function render(root, { info }) {
|
||||
export async function render(root, { api, info }) {
|
||||
const dashboard = await api.get("/api/v1/server/dashboard").catch(() => null);
|
||||
const rows = [
|
||||
["Admin.About.Name", info?.name],
|
||||
["Admin.About.Version", info?.version],
|
||||
["Admin.About.Api", info?.apiVersion],
|
||||
["Admin.About.Database", dashboard?.database],
|
||||
["Admin.About.Uptime", dashboard ? formatDuration(dashboard.uptimeSeconds) : null],
|
||||
["Admin.About.Fingerprint", info?.tlsFingerprint],
|
||||
];
|
||||
root.innerHTML = `
|
||||
<div class="panel">
|
||||
<h2 data-i18n="Admin.About.Title"></h2>
|
||||
<p class="num">Titano Server ${escapeHtml(info?.version ?? "")} · API ${escapeHtml(String(info?.apiVersion ?? ""))}</p>
|
||||
<p class="num"><span data-i18n="Admin.About.Fingerprint"></span> ${escapeHtml(info?.tlsFingerprint ?? "–")}</p>
|
||||
<div class="panel about">
|
||||
<div class="brand large"><img src="favicon.svg" alt=""><strong data-i18n="Admin.About.Title"></strong></div>
|
||||
<dl>${rows.map(([key, value]) => `<dt data-i18n="${key}"></dt><dd class="${key.endsWith("Fingerprint") ? "mono" : "num"}">${escapeHtml(value ?? "—")}</dd>`).join("")}</dl>
|
||||
</div>`;
|
||||
applyStrings(root);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
// Archive (BRIEF §14.8): naming templates, proxy policy, scan/rebuild/verify/maintenance, statistics.
|
||||
import { t, applyStrings } from "../i18n.js";
|
||||
import { escapeHtml, attr, formatBytes, formatNumber, bar, columns, toast, errorText } from "../ui.js";
|
||||
|
||||
const PROXY_SIZES = [-1, 1280, 1920, 2560, 3840, 0];
|
||||
const ACTIONS = ["scan", "rebuild", "verify", "maintenance"];
|
||||
|
||||
export async function render(root, { api, meta }) {
|
||||
let settings;
|
||||
let stats;
|
||||
try {
|
||||
[settings, stats] = await Promise.all([api.get("/api/v1/archive/settings"), api.get("/api/v1/archive/stats")]);
|
||||
} catch (err) {
|
||||
root.innerHTML = `<div class="panel empty">${escapeHtml(errorText(err))}</div>`;
|
||||
return;
|
||||
}
|
||||
meta.textContent = settings.rootPath;
|
||||
const used = stats.capacityBytes > 0 ? (stats.capacityBytes - stats.freeBytes) / stats.capacityBytes : 0;
|
||||
const sizes = PROXY_SIZES.includes(settings.proxyLongEdge) ? PROXY_SIZES : [settings.proxyLongEdge, ...PROXY_SIZES];
|
||||
const proxyOptions = sizes.map((size) => `<option value="${size}" ${size === settings.proxyLongEdge ? "selected" : ""}>${escapeHtml(proxyLabel(size))}</option>`).join("");
|
||||
|
||||
root.innerHTML = `
|
||||
<div class="tiles">
|
||||
<div class="panel tile"><span class="label" data-i18n="Admin.Archive.Sequences"></span><span class="value num">${formatNumber(stats.sequences)}</span>
|
||||
<span class="sub">${escapeHtml(t("Admin.Dashboard.PhotosCameras", formatNumber(stats.photos), formatNumber(stats.cameras)))}</span></div>
|
||||
<div class="panel tile"><span class="label" data-i18n="Admin.Archive.Originals"></span><span class="value">${escapeHtml(formatBytes(stats.bytes))}</span>
|
||||
<span class="sub">${escapeHtml(stats.firstYear ? `${stats.firstYear}–${stats.lastYear}` : "—")}</span></div>
|
||||
<div class="panel tile"><span class="label" data-i18n="Admin.Dashboard.Storage"></span><span class="value">${escapeHtml(formatBytes(stats.freeBytes))}<small data-i18n="Admin.Archive.FreeShort"></small></span>
|
||||
${bar(used)}<span class="sub">${escapeHtml(t("Admin.Dashboard.Of", formatBytes(stats.capacityBytes)))}</span></div>
|
||||
<div class="panel tile"><span class="label" data-i18n="Admin.Archive.Health"></span><span class="value num">${formatNumber(stats.unverifiedSequences)}<small data-i18n="Admin.Archive.Unverified"></small></span>
|
||||
<span class="sub">${escapeHtml(t("Admin.Dashboard.NoProxies", formatNumber(stats.sequencesWithoutProxies)))}</span></div>
|
||||
</div>
|
||||
<div class="grid two">
|
||||
<form class="panel form settings-form" novalidate>
|
||||
<h2 data-i18n="Admin.Archive.Layout"></h2>
|
||||
<label class="field"><span data-i18n="Admin.Archive.Name"></span><input class="input" name="name" value="${attr(settings.name)}" data-i18n-title="Admin.Archive.Name.Tip"></label>
|
||||
<label class="field"><span data-i18n="Admin.Archive.FolderTemplate"></span><input class="input mono" name="folderTemplate" value="${attr(settings.folderTemplate)}" data-i18n-title="Admin.Archive.FolderTemplate.Tip"></label>
|
||||
<label class="field"><span data-i18n="Admin.Archive.FileTemplate"></span><input class="input mono" name="fileTemplate" value="${attr(settings.fileTemplate)}" data-i18n-title="Admin.Archive.FileTemplate.Tip"></label>
|
||||
<label class="field"><span data-i18n="Admin.Archive.Proxies"></span><select class="input" name="proxyLongEdge" data-i18n-title="Admin.Archive.Proxies.Tip">${proxyOptions}</select></label>
|
||||
<div class="buttons"><button class="btn primary" type="submit" data-i18n="Admin.Common.Save" data-i18n-title="Admin.Common.Save.Tip"></button></div>
|
||||
</form>
|
||||
<div class="panel">
|
||||
<h2 data-i18n="Admin.Archive.Maintenance"></h2>
|
||||
<div class="button-row">${ACTIONS.map((a) => `<button class="btn" data-action="${a}" data-i18n="Admin.Archive.${cap(a)}" data-i18n-title="Admin.Archive.${cap(a)}.Tip"></button>`).join("")}</div>
|
||||
<h2 data-i18n="Admin.Archive.ByCamera"></h2>
|
||||
<div class="table compact">
|
||||
${stats.byCamera.slice(0, 8).map((c) => `<div class="row"><span>${escapeHtml(c.key || "—")}</span><span class="num right">${formatNumber(c.count)}</span><span class="num right">${escapeHtml(formatBytes(c.bytes))}</span></div>`).join("")
|
||||
|| `<p class="muted" data-i18n="Admin.Dashboard.NoData"></p>`}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid two charts">
|
||||
<div class="panel"><h2 data-i18n="Admin.Dashboard.ByYear"></h2>
|
||||
${stats.byYear.length === 0 ? `<p class="muted" data-i18n="Admin.Dashboard.NoData"></p>`
|
||||
: columns(stats.byYear.map((y) => ({ label: y.key, value: y.bytes, title: `${y.key} · ${formatBytes(y.bytes)}` })), { valueLabel: (i) => formatBytes(i.value) })}</div>
|
||||
<div class="panel"><h2 data-i18n="Admin.Archive.ByFormat"></h2>
|
||||
<div class="table compact">
|
||||
${stats.byFormat.map((f) => `<div class="row"><span>${escapeHtml(f.key.toUpperCase())}</span><span class="num right">${formatNumber(f.count)}</span><span class="num right">${escapeHtml(formatBytes(f.bytes))}</span></div>`).join("")
|
||||
|| `<p class="muted" data-i18n="Admin.Dashboard.NoData"></p>`}
|
||||
</div></div>
|
||||
</div>`;
|
||||
applyStrings(root);
|
||||
|
||||
const form = root.querySelector("form");
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
form.querySelectorAll("[aria-invalid]").forEach((el) => el.removeAttribute("aria-invalid"));
|
||||
const data = new FormData(form);
|
||||
try {
|
||||
settings = await api.put("/api/v1/archive/settings", {
|
||||
...settings,
|
||||
name: String(data.get("name")).trim(),
|
||||
folderTemplate: String(data.get("folderTemplate")),
|
||||
fileTemplate: String(data.get("fileTemplate")),
|
||||
proxyLongEdge: Number(data.get("proxyLongEdge")),
|
||||
});
|
||||
toast(t("Admin.Common.Saved"));
|
||||
} catch (err) {
|
||||
// Errors name the field in details; the field gets the error border and the message as its tooltip.
|
||||
const field = err.details && form.elements[err.details];
|
||||
if (field) {
|
||||
field.setAttribute("aria-invalid", "true");
|
||||
field.title = errorText(err);
|
||||
}
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
});
|
||||
root.querySelectorAll("[data-action]").forEach((button) => button.addEventListener("click", async () => {
|
||||
button.disabled = true;
|
||||
try {
|
||||
await api.post(`/api/v1/archive/${button.dataset.action}`);
|
||||
toast(t("Admin.Archive.Queued"));
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
function proxyLabel(size) {
|
||||
if (size === -1) return t("Admin.Archive.ProxiesOff");
|
||||
if (size === 0) return t("Admin.Archive.ProxiesFull");
|
||||
return t("Admin.Archive.ProxiesEdge", size);
|
||||
}
|
||||
|
||||
function cap(s) {
|
||||
return s.charAt(0).toUpperCase() + s.slice(1);
|
||||
}
|
||||
@@ -1,18 +1,131 @@
|
||||
// Dashboard: storage, archive, jobs, render nodes (BRIEF §14.8). Data from the public API only.
|
||||
import { applyStrings } from "../i18n.js";
|
||||
// Dashboard (mockup Admin.html, BRIEF §14.8): storage, archive, jobs, render nodes, charts, activity, active jobs.
|
||||
import { t, has, applyStrings } from "../i18n.js";
|
||||
import { escapeHtml, attr, formatBytes, formatNumber, formatTime, formatDuration, bar, columns, toast, errorText } from "../ui.js";
|
||||
import { kindLabel, nodeLabel, progressCell, stateCell, stateLabel, encoders, pascal } from "../jobs.js";
|
||||
|
||||
export async function render(root, { api, t, info, meta }) {
|
||||
const health = await api.get("/health").catch(() => null);
|
||||
meta.textContent = info ? `v${info.version} · TLS ${info.tlsFingerprint ? "auto" : "proxy"}` : "";
|
||||
root.innerHTML = `
|
||||
<div class="tiles">
|
||||
<div class="panel tile"><div class="label" data-i18n="Admin.Dashboard.Server"></div>
|
||||
<div class="value">${health?.status === "ok" ? `<span class="status-ok">●</span>` : `<span class="status-err">✕</span>`}<small>${health?.status ?? ""}</small></div>
|
||||
<div class="sub num">${info?.name ?? ""}</div></div>
|
||||
<div class="panel tile"><div class="label" data-i18n="Admin.Dashboard.Archive"></div>
|
||||
<div class="value">${health?.checks?.archive === "ok" ? `<span class="status-ok">●</span>` : `<span class="status-warn">▲</span>`}<small>${health?.checks?.archive ?? ""}</small></div></div>
|
||||
<div class="panel tile"><div class="label" data-i18n="Admin.Dashboard.Api"></div>
|
||||
<div class="value num">${info?.apiVersion ?? "–"}</div><div class="sub">${(info?.capabilities ?? []).join(" · ")}</div></div>
|
||||
</div>`;
|
||||
applyStrings(root);
|
||||
export async function render(root, { api, meta, onEvent }) {
|
||||
let timer = null;
|
||||
const refresh = () => {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(load, 1500);
|
||||
};
|
||||
|
||||
async function load() {
|
||||
let d;
|
||||
try {
|
||||
d = await api.get("/api/v1/server/dashboard");
|
||||
} catch (err) {
|
||||
root.innerHTML = `<div class="panel empty">${escapeHtml(errorText(err))}</div>`;
|
||||
return;
|
||||
}
|
||||
meta.textContent = t("Admin.Dashboard.Meta", formatDuration(d.uptimeSeconds), d.database, d.tlsMode,
|
||||
t(d.discovery ? "Admin.Dashboard.DiscoveryOn" : "Admin.Dashboard.DiscoveryOff"));
|
||||
const a = d.archive;
|
||||
const used = a.capacityBytes > 0 ? (a.capacityBytes - a.freeBytes) / a.capacityBytes : 0;
|
||||
const online = d.nodes.filter((n) => n.state !== "offline");
|
||||
const uploads = lastThirtyDays(d.uploads);
|
||||
const totalUploads = uploads.reduce((s, u) => s + u.value, 0);
|
||||
const peak = Math.max(0, ...uploads.map((u) => u.value));
|
||||
|
||||
root.innerHTML = `
|
||||
<div class="tiles">
|
||||
<div class="panel tile">
|
||||
<span class="label" data-i18n="Admin.Dashboard.Storage"></span>
|
||||
<span class="value">${escapeHtml(formatBytes(a.capacityBytes - a.freeBytes))}<small>${escapeHtml(t("Admin.Dashboard.Of", formatBytes(a.capacityBytes)))}</small></span>
|
||||
${bar(used)}
|
||||
<span class="sub">${escapeHtml(t("Admin.Dashboard.Free", formatBytes(a.freeBytes)))} · ${escapeHtml(t("Admin.Dashboard.Originals", formatBytes(a.bytes)))}</span>
|
||||
</div>
|
||||
<div class="panel tile">
|
||||
<span class="label" data-i18n="Admin.Dashboard.Archive"></span>
|
||||
<span class="value num">${formatNumber(a.sequences)}<small data-i18n="Admin.Dashboard.Sequences"></small></span>
|
||||
<span class="sub">${escapeHtml(t("Admin.Dashboard.PhotosCameras", formatNumber(a.photos), formatNumber(a.cameras)))}</span>
|
||||
<span class="sub">${escapeHtml(t("Admin.Dashboard.NoProxies", formatNumber(a.sequencesWithoutProxies)))}</span>
|
||||
</div>
|
||||
<div class="panel tile">
|
||||
<span class="label" data-i18n="Admin.Dashboard.Jobs"></span>
|
||||
<span class="value num">${formatNumber(d.jobs.running)}<small data-i18n="Admin.Dashboard.Running"></small></span>
|
||||
<span class="sub">${escapeHtml(t("Admin.Dashboard.JobCounts", formatNumber(d.jobs.queued), formatNumber(d.jobs.doneToday), formatNumber(d.jobs.failedToday)))}</span>
|
||||
<span class="sub">${escapeHtml(t("Admin.Dashboard.Workers", formatNumber(d.workers)))}</span>
|
||||
</div>
|
||||
<div class="panel tile">
|
||||
<span class="label" data-i18n="Admin.Dashboard.Nodes"></span>
|
||||
<span class="value num">${formatNumber(online.length)}<small data-i18n="Admin.Dashboard.Online"></small></span>
|
||||
${online.slice(0, 2).map((n) => `<span class="sub">${escapeHtml(n.clientName)} · ${escapeHtml(encoders(n))} · ${escapeHtml(stateLabel(n.state))}</span>`).join("")}
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid two charts">
|
||||
<div class="panel">
|
||||
<div class="panel-head"><h2 data-i18n="Admin.Dashboard.ByYear"></h2></div>
|
||||
${a.byYear.length === 0 ? `<p class="muted" data-i18n="Admin.Dashboard.NoData"></p>`
|
||||
: columns(a.byYear.map((y) => ({ label: y.key, value: y.bytes, title: `${y.key} · ${formatBytes(y.bytes)} · ${formatNumber(y.count)}` })), { valueLabel: (i) => formatBytes(i.value) })}
|
||||
</div>
|
||||
<div class="panel">
|
||||
<div class="panel-head"><h2 data-i18n="Admin.Dashboard.Uploads"></h2>
|
||||
<span class="muted">${escapeHtml(t("Admin.Dashboard.UploadTotals", formatNumber(totalUploads), formatNumber(peak)))}</span></div>
|
||||
${columns(uploads, { sun: true })}
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid wide-right">
|
||||
<div class="panel">
|
||||
<div class="panel-head"><h2 data-i18n="Admin.Dashboard.Activity"></h2><a href="#/logs?view=audit" data-i18n="Admin.Dashboard.AllLogs" data-i18n-title="Admin.Dashboard.AllLogs.Tip"></a></div>
|
||||
<div class="activity">${d.activity.map((e) => `<span class="muted num">${escapeHtml(formatTime(e.at))}</span><span>${escapeHtml(activityText(e))}</span>`).join("")}</div>
|
||||
${d.activity.length === 0 ? `<p class="muted" data-i18n="Admin.Dashboard.NoActivity"></p>` : ""}
|
||||
</div>
|
||||
<div class="panel">
|
||||
<div class="panel-head"><h2 data-i18n="Admin.Dashboard.Jobs"></h2><a href="#/jobs" data-i18n="Admin.Dashboard.Queue" data-i18n-title="Admin.Dashboard.Queue.Tip"></a></div>
|
||||
${d.activeJobs.length === 0 ? `<p class="muted" data-i18n="Admin.Jobs.Empty"></p>` : `
|
||||
<div class="table jobs-mini">
|
||||
<div class="thead"><span data-i18n="Admin.Jobs.Kind"></span><span data-i18n="Admin.Jobs.Target"></span><span data-i18n="Admin.Jobs.Node"></span><span data-i18n="Admin.Jobs.Progress"></span><span data-i18n="Admin.Jobs.State"></span><span></span></div>
|
||||
${d.activeJobs.map((j) => `
|
||||
<div class="row" data-job="${attr(j.id)}">
|
||||
<span>${escapeHtml(kindLabel(j.kind))}</span><span class="ellipsis">${escapeHtml(j.targetLabel ?? "")}</span><span>${escapeHtml(nodeLabel(j, d.nodes))}</span>
|
||||
<span class="progress">${progressCell(j)}</span><span>${stateCell(j.state)}</span>
|
||||
<button class="btn small" data-cancel="${attr(j.id)}" data-i18n="${j.state === "queued" ? "Admin.Jobs.Remove" : "Admin.Jobs.Cancel"}" data-i18n-title="${j.state === "queued" ? "Admin.Jobs.Remove.Tip" : "Admin.Jobs.Cancel.Tip"}"></button>
|
||||
</div>`).join("")}
|
||||
</div>`}
|
||||
</div>
|
||||
</div>`;
|
||||
applyStrings(root);
|
||||
root.querySelectorAll("[data-cancel]").forEach((button) => button.addEventListener("click", async () => {
|
||||
button.disabled = true;
|
||||
try {
|
||||
await api.post(`/api/v1/jobs/${button.dataset.cancel}/cancel`);
|
||||
refresh();
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
button.disabled = false;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
onEvent((e) => {
|
||||
if (e.type === "jobProgress") {
|
||||
const cell = root.querySelector(`[data-job="${CSS.escape(e.jobId)}"] .progress`);
|
||||
if (cell) cell.innerHTML = progressCell({ progressDone: e.done, progressTotal: e.total });
|
||||
} else {
|
||||
refresh();
|
||||
}
|
||||
});
|
||||
await load();
|
||||
}
|
||||
|
||||
/** One sentence per audit entry, e.g. "alberto signed in" (unknown actions show their code). */
|
||||
function activityText(entry) {
|
||||
const key = `Admin.Activity.${pascal(entry.action)}`;
|
||||
return has(key) ? t(key, entry.username ?? "—", entry.details ?? "") : `${entry.username ?? ""} ${entry.action}`;
|
||||
}
|
||||
|
||||
/** Frames per UTC day for the last 30 days, zero-filled. */
|
||||
function lastThirtyDays(days) {
|
||||
const byDay = new Map(days.map((d) => [d.day, d.frames]));
|
||||
const items = [];
|
||||
const today = new Date();
|
||||
for (let i = 29; i >= 0; i--) {
|
||||
const day = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate() - i));
|
||||
const key = day.toISOString().slice(0, 10);
|
||||
const value = byDay.get(key) ?? 0;
|
||||
const label = i === 29 || i === 0 ? new Intl.DateTimeFormat(document.documentElement.lang, { day: "numeric", month: "short" }).format(day) : "";
|
||||
items.push({ value, label, title: `${key} · ${formatNumber(value)}` });
|
||||
}
|
||||
return items;
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
// Jobs (BRIEF §14.8): queue and history with cancel/pause/resume/retry/priority, log tails, render nodes.
|
||||
import { t, applyStrings } from "../i18n.js";
|
||||
import { escapeHtml, attr, formatTime, formatDateTime, toast, errorText, confirmDialog } from "../ui.js";
|
||||
import { kindLabel, nodeLabel, progressCell, stateCell, stateLabel, encoders } from "../jobs.js";
|
||||
|
||||
const FILTERS = ["all", "running", "queued", "paused", "failed", "done", "cancelled"];
|
||||
const PRIORITIES = ["background", "normal", "user"];
|
||||
|
||||
export async function render(root, { api, meta, onEvent }) {
|
||||
let filter = "all";
|
||||
let jobs = [];
|
||||
let nodes = [];
|
||||
let cursor = null;
|
||||
const expanded = new Set();
|
||||
let timer = null;
|
||||
|
||||
root.innerHTML = `
|
||||
<div class="chips" role="group">${FILTERS.map((f) => `<button class="chip" data-filter="${f}" aria-pressed="${f === filter}" data-i18n="Admin.Jobs.Filter.${cap(f)}" data-i18n-title="Admin.Jobs.Filter.Tip"></button>`).join("")}</div>
|
||||
<div class="panel flush">
|
||||
<div class="table jobs">
|
||||
<div class="thead"><span data-i18n="Admin.Jobs.Kind"></span><span data-i18n="Admin.Jobs.Target"></span><span data-i18n="Admin.Jobs.Node"></span>
|
||||
<span data-i18n="Admin.Jobs.Progress"></span><span data-i18n="Admin.Jobs.State"></span><span data-i18n="Admin.Jobs.Created"></span><span></span></div>
|
||||
<div id="rows"></div>
|
||||
</div>
|
||||
<div class="table-foot" id="foot" hidden><span class="spacer"></span><button class="btn small" id="more" data-i18n="Admin.Common.More" data-i18n-title="Admin.Common.More.Tip"></button></div>
|
||||
</div>
|
||||
<div class="panel flush">
|
||||
<div class="panel-head padded"><h2 data-i18n="Admin.Jobs.Nodes"></h2></div>
|
||||
<div class="table nodes">
|
||||
<div class="thead"><span data-i18n="Admin.Jobs.NodeName"></span><span data-i18n="Admin.Jobs.Owner"></span><span data-i18n="Admin.Jobs.Encoders"></span>
|
||||
<span data-i18n="Admin.Jobs.Heartbeat"></span><span data-i18n="Admin.Jobs.State"></span><span></span></div>
|
||||
<div id="nodes"></div>
|
||||
</div>
|
||||
</div>`;
|
||||
applyStrings(root);
|
||||
root.querySelectorAll("[data-filter]").forEach((chip) => chip.addEventListener("click", () => {
|
||||
filter = chip.dataset.filter;
|
||||
root.querySelectorAll("[data-filter]").forEach((c) => c.setAttribute("aria-pressed", String(c === chip)));
|
||||
load();
|
||||
}));
|
||||
root.querySelector("#more").addEventListener("click", () => load(true));
|
||||
|
||||
async function load(more = false) {
|
||||
try {
|
||||
const state = filter === "all" ? "" : `&state=${filter}`;
|
||||
const page = await api.get(`/api/v1/jobs?limit=50${state}${more && cursor ? `&cursor=${encodeURIComponent(cursor)}` : ""}`);
|
||||
jobs = more ? jobs.concat(page.items) : page.items;
|
||||
cursor = page.nextCursor;
|
||||
nodes = await api.get("/api/v1/nodes");
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
return;
|
||||
}
|
||||
const running = jobs.filter((j) => j.state === "running").length;
|
||||
meta.textContent = t("Admin.Jobs.Meta", running, nodes.filter((n) => n.state !== "offline").length);
|
||||
draw();
|
||||
}
|
||||
|
||||
function draw() {
|
||||
const host = root.querySelector("#rows");
|
||||
host.innerHTML = jobs.map((j) => `
|
||||
<div class="row" data-job="${attr(j.id)}">
|
||||
<button class="link" data-toggle="${attr(j.id)}" aria-expanded="${expanded.has(j.id)}" data-i18n-title="Admin.Jobs.Log.Tip">${escapeHtml(kindLabel(j.kind))}</button>
|
||||
<span class="ellipsis">${escapeHtml(j.targetLabel ?? "")}</span>
|
||||
<span>${escapeHtml(nodeLabel(j, nodes))}</span>
|
||||
<span class="progress">${progressCell(j)}</span>
|
||||
<span>${stateCell(j.state)}</span>
|
||||
<span class="muted">${escapeHtml(formatTime(j.created))}</span>
|
||||
<span class="row-actions">${buttons(j)}</span>
|
||||
</div>
|
||||
${expanded.has(j.id) ? `<pre class="log">${escapeHtml(j.error ? `${j.logTail ?? ""}\n${j.error}`.trim() : j.logTail ?? "")}</pre>` : ""}`).join("")
|
||||
|| `<div class="empty" data-i18n="Admin.Jobs.Empty"></div>`;
|
||||
applyStrings(host);
|
||||
host.querySelectorAll("[data-toggle]").forEach((b) => b.addEventListener("click", () => {
|
||||
const id = b.dataset.toggle;
|
||||
if (!expanded.delete(id)) expanded.add(id);
|
||||
draw();
|
||||
}));
|
||||
host.querySelectorAll("[data-act]").forEach((b) => b.addEventListener("click", () => act(b.closest(".row").dataset.job, b.dataset.act)));
|
||||
host.querySelectorAll("select[data-priority]").forEach((s) => s.addEventListener("change", () => setPriority(s.dataset.priority, s.value)));
|
||||
root.querySelector("#foot").hidden = !cursor;
|
||||
|
||||
const nodeHost = root.querySelector("#nodes");
|
||||
nodeHost.innerHTML = nodes.map((n) => `
|
||||
<div class="row" data-node="${attr(n.id)}">
|
||||
<span><strong>${escapeHtml(n.clientName)}</strong></span><span>${escapeHtml(n.username)}</span>
|
||||
<span class="ellipsis" title="${attr(n.encoders.map((e) => e.name).join(", "))}">${escapeHtml(encoders(n))}</span>
|
||||
<span class="muted">${escapeHtml(formatDateTime(n.lastHeartbeat))}</span>
|
||||
<span class="state ${n.state === "offline" ? "cancelled" : n.state === "busy" ? "running" : "done"}">${n.state === "offline" ? "○" : "●"} ${escapeHtml(stateLabel(n.state))}</span>
|
||||
<span class="row-actions"><button class="btn small destructive" data-forget="${attr(n.id)}" data-i18n="Admin.Jobs.Forget" data-i18n-title="Admin.Jobs.Forget.Tip"></button></span>
|
||||
</div>`).join("") || `<div class="empty" data-i18n="Admin.Jobs.NoNodes"></div>`;
|
||||
applyStrings(nodeHost);
|
||||
nodeHost.querySelectorAll("[data-forget]").forEach((b) => b.addEventListener("click", () => forget(nodes.find((n) => n.id === b.dataset.forget))));
|
||||
}
|
||||
|
||||
function buttons(job) {
|
||||
const button = (act, key) => `<button class="btn small" data-act="${act}" data-i18n="Admin.Jobs.${key}" data-i18n-title="Admin.Jobs.${key}.Tip"></button>`;
|
||||
const priority = `<select class="input small" data-priority="${attr(job.id)}" data-i18n-title="Admin.Jobs.Priority.Tip">${PRIORITIES.map((p) =>
|
||||
`<option value="${p}" ${p === job.priority ? "selected" : ""}>${escapeHtml(t(`Admin.Jobs.Priority.${cap(p)}`))}</option>`).join("")}</select>`;
|
||||
switch (job.state) {
|
||||
case "running": return button("pause", "Pause") + button("cancel", "Cancel");
|
||||
case "queued": return priority + button("pause", "Pause") + button("cancel", "Remove");
|
||||
case "paused": return priority + button("resume", "Resume") + button("cancel", "Cancel");
|
||||
case "failed":
|
||||
case "cancelled": return button("retry", "Retry");
|
||||
default: return "";
|
||||
}
|
||||
}
|
||||
|
||||
async function act(id, action) {
|
||||
try {
|
||||
await api.post(`/api/v1/jobs/${id}/${action}`);
|
||||
await load();
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
}
|
||||
|
||||
async function setPriority(id, priority) {
|
||||
try {
|
||||
await api.post(`/api/v1/jobs/${id}/priority`, { priority });
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
}
|
||||
|
||||
async function forget(node) {
|
||||
const confirmed = await confirmDialog({
|
||||
title: t("Admin.Jobs.ForgetTitle", node.clientName),
|
||||
confirm: t("Admin.Jobs.Forget"),
|
||||
confirmTip: t("Admin.Jobs.Forget.Tip"),
|
||||
cancel: t("Admin.Common.Keep"),
|
||||
cancelTip: t("Admin.Common.Keep.Tip"),
|
||||
});
|
||||
if (!confirmed) return;
|
||||
try {
|
||||
await api.del(`/api/v1/nodes/${node.id}`);
|
||||
await load();
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
}
|
||||
|
||||
onEvent((e) => {
|
||||
if (e.type === "jobProgress") {
|
||||
const job = jobs.find((j) => j.id === e.jobId);
|
||||
if (job) {
|
||||
job.progressDone = e.done;
|
||||
job.progressTotal = e.total;
|
||||
const cell = root.querySelector(`[data-job="${CSS.escape(e.jobId)}"] .progress`);
|
||||
if (cell) cell.innerHTML = progressCell(job);
|
||||
}
|
||||
} else if (e.type === "jobState" || e.type === "nodeState") {
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(load, 800);
|
||||
}
|
||||
});
|
||||
await load();
|
||||
}
|
||||
|
||||
function cap(s) {
|
||||
return s.charAt(0).toUpperCase() + s.slice(1);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// Logs (BRIEF §14.8): server log tail and downloads, sign-in log, audit log.
|
||||
import { t, has, applyStrings } from "../i18n.js";
|
||||
import { escapeHtml, attr, formatBytes, formatDateTime, toast, errorText, routeParams } from "../ui.js";
|
||||
import { pascal } from "../jobs.js";
|
||||
|
||||
const VIEWS = ["server", "signins", "audit"];
|
||||
const LINES = [100, 300, 1000];
|
||||
|
||||
export async function render(root, { api, meta, actions }) {
|
||||
let view = VIEWS.includes(routeParams().get("view")) ? routeParams().get("view") : "server";
|
||||
let lines = 300;
|
||||
let audit = [];
|
||||
let cursor = null;
|
||||
|
||||
actions.innerHTML = `<div class="segmented" role="group">${VIEWS.map((v) =>
|
||||
`<button class="btn" data-view="${v}" aria-pressed="${v === view}" data-i18n="Admin.Logs.View.${cap(v)}" data-i18n-title="Admin.Logs.View.${cap(v)}.Tip"></button>`).join("")}</div>`;
|
||||
applyStrings(actions);
|
||||
actions.querySelectorAll("[data-view]").forEach((b) => b.addEventListener("click", () => {
|
||||
view = b.dataset.view;
|
||||
actions.querySelectorAll("[data-view]").forEach((x) => x.setAttribute("aria-pressed", String(x === b)));
|
||||
history.replaceState(null, "", `#/logs?view=${view}`);
|
||||
show();
|
||||
}));
|
||||
|
||||
async function show() {
|
||||
meta.textContent = "";
|
||||
if (view === "server") await showServerLog();
|
||||
else await showAudit(false);
|
||||
}
|
||||
|
||||
async function showServerLog() {
|
||||
let tail;
|
||||
let files;
|
||||
try {
|
||||
[tail, files] = await Promise.all([api.get(`/api/v1/logs?lines=${lines}`), api.get("/api/v1/logs/files")]);
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
return;
|
||||
}
|
||||
meta.textContent = tail.file;
|
||||
root.innerHTML = `
|
||||
<div class="panel flush">
|
||||
<div class="panel-head padded">
|
||||
<select class="input small" id="lines" data-i18n-title="Admin.Logs.Lines.Tip">${LINES.map((n) => `<option value="${n}" ${n === lines ? "selected" : ""}>${escapeHtml(t("Admin.Logs.LineCount", n))}</option>`).join("")}</select>
|
||||
<span class="spacer"></span>
|
||||
<button class="btn small" id="refresh" data-i18n="Admin.Common.Refresh" data-i18n-title="Admin.Logs.Refresh.Tip"></button>
|
||||
</div>
|
||||
<pre class="log tall">${escapeHtml(tail.lines.join("\n"))}</pre>
|
||||
</div>
|
||||
<div class="panel">
|
||||
<h2 data-i18n="Admin.Logs.Files"></h2>
|
||||
<div class="table compact">${files.map((f) => `
|
||||
<div class="row"><span class="mono">${escapeHtml(f.name)}</span><span class="num right">${escapeHtml(formatBytes(f.size))}</span>
|
||||
<span class="muted">${escapeHtml(formatDateTime(f.modified))}</span>
|
||||
<a class="btn small" href="/api/v1/logs/files/${encodeURIComponent(f.name)}" download="${attr(f.name)}" data-i18n="Admin.Logs.Download" data-i18n-title="Admin.Logs.Download.Tip"></a></div>`).join("")}
|
||||
</div>
|
||||
</div>`;
|
||||
applyStrings(root);
|
||||
const pre = root.querySelector("pre");
|
||||
pre.scrollTop = pre.scrollHeight;
|
||||
root.querySelector("#lines").addEventListener("change", (e) => {
|
||||
lines = Number(e.target.value);
|
||||
showServerLog();
|
||||
});
|
||||
root.querySelector("#refresh").addEventListener("click", showServerLog);
|
||||
}
|
||||
|
||||
async function showAudit(more) {
|
||||
const action = view === "signins" ? "&action=login" : "";
|
||||
try {
|
||||
const page = await api.get(`/api/v1/audit?limit=100${action}${more && cursor ? `&cursor=${encodeURIComponent(cursor)}` : ""}`);
|
||||
audit = more ? audit.concat(page.items) : page.items;
|
||||
cursor = page.nextCursor;
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
return;
|
||||
}
|
||||
root.innerHTML = `
|
||||
<div class="panel flush">
|
||||
<div class="table audit">
|
||||
<div class="thead"><span data-i18n="Admin.Logs.When"></span><span data-i18n="Admin.Logs.Who"></span><span data-i18n="Admin.Logs.What"></span>
|
||||
<span data-i18n="Admin.Logs.Details"></span><span data-i18n="Admin.Logs.From"></span></div>
|
||||
${audit.map((e) => `
|
||||
<div class="row"><span class="num">${escapeHtml(formatDateTime(e.at))}</span><span>${escapeHtml(e.username ?? "—")}</span>
|
||||
<span>${escapeHtml(actionLabel(e.action))}</span><span class="ellipsis" title="${attr(e.details ?? "")}">${escapeHtml(e.details ?? "")}</span>
|
||||
<span class="mono">${escapeHtml(e.remoteAddress ?? "")}</span></div>`).join("") || `<div class="empty" data-i18n="Admin.Logs.Empty"></div>`}
|
||||
</div>
|
||||
${cursor ? `<div class="table-foot"><span class="spacer"></span><button class="btn small" id="more" data-i18n="Admin.Common.More" data-i18n-title="Admin.Common.More.Tip"></button></div>` : ""}
|
||||
</div>`;
|
||||
applyStrings(root);
|
||||
root.querySelector("#more")?.addEventListener("click", () => showAudit(true));
|
||||
}
|
||||
|
||||
await show();
|
||||
}
|
||||
|
||||
function actionLabel(action) {
|
||||
const key = `Admin.Logs.Action.${pascal(action)}`;
|
||||
return has(key) ? t(key) : action;
|
||||
}
|
||||
|
||||
function cap(s) {
|
||||
return s.charAt(0).toUpperCase() + s.slice(1);
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
// Settings (BRIEF §14.8): server name, ports, TLS, discovery, limits, language, retention. Values fixed by environment
|
||||
// variables are read-only here; ports, TLS, workers, chunk size and job history apply at the next restart.
|
||||
import { t, applyStrings, languages } from "../i18n.js";
|
||||
import { escapeHtml, attr, toast, errorText, nativeName } from "../ui.js";
|
||||
|
||||
/** Setting → environment variable that can fix it (BRIEF §14.9). */
|
||||
const VARIABLES = {
|
||||
serverName: "TITANO_SERVER_NAME", httpPort: "TITANO_HTTP_PORT", httpsPort: "TITANO_HTTPS_PORT", tlsMode: "TITANO_TLS_MODE",
|
||||
tlsCert: "TITANO_TLS_CERT", tlsKey: "TITANO_TLS_KEY", language: "TITANO_LANG", workers: "TITANO_WORKERS", discovery: "TITANO_DISCOVERY",
|
||||
};
|
||||
const MB = 1e6;
|
||||
const MIB = 1024 * 1024;
|
||||
|
||||
export async function render(root, { api, info, meta }) {
|
||||
let settings;
|
||||
try {
|
||||
settings = await api.get("/api/v1/server/settings");
|
||||
} catch (err) {
|
||||
root.innerHTML = `<div class="panel empty">${escapeHtml(errorText(err))}</div>`;
|
||||
return;
|
||||
}
|
||||
|
||||
const locked = (name) => settings.locked.some((l) => l.toLowerCase() === name.toLowerCase());
|
||||
const lock = (name) => locked(name) ? `disabled title="${attr(t("Admin.Settings.Locked.Tip", VARIABLES[name] ?? name))}"` : `data-i18n-title="Admin.Settings.${cap(name)}.Tip"`;
|
||||
const field = (name, input) => `<label class="field"><span data-i18n="Admin.Settings.${cap(name)}"></span>${input}</label>`;
|
||||
const number = (name, value, attrs = "") => field(name, `<input class="input narrow" type="number" name="${name}" value="${attr(value)}" ${attrs} ${lock(name)}>`);
|
||||
const text = (name, value) => field(name, `<input class="input" name="${name}" value="${attr(value ?? "")}" ${lock(name)}>`);
|
||||
|
||||
function draw() {
|
||||
meta.textContent = "";
|
||||
root.innerHTML = `
|
||||
<form class="settings" novalidate>
|
||||
<section class="panel">
|
||||
<div class="panel-head"><h2 data-i18n="Admin.Settings.Server"></h2>
|
||||
${settings.restartRequired ? `<span class="chip warn" data-i18n="Admin.Settings.Restart" data-i18n-title="Admin.Settings.Restart.Tip"></span>` : ""}</div>
|
||||
${text("serverName", settings.serverName)}
|
||||
${field("language", `<select class="input" name="language" ${lock("language")}>${languages().concat(languages().includes(settings.language) ? [] : [settings.language])
|
||||
.map((code) => `<option value="${attr(code)}" ${code === settings.language ? "selected" : ""}>${escapeHtml(nativeName(code))}</option>`).join("")}</select>`)}
|
||||
${number("workers", settings.workers, 'min="1" max="64"')}
|
||||
${field("discovery", `<span class="inline"><input type="checkbox" name="discovery" ${settings.discovery ? "checked" : ""} ${lock("discovery")}></span>`)}
|
||||
</section>
|
||||
<section class="panel">
|
||||
<h2 data-i18n="Admin.Settings.Network"></h2>
|
||||
${number("httpPort", settings.httpPort, 'min="1" max="65535"')}
|
||||
${number("httpsPort", settings.httpsPort, 'min="1" max="65535"')}
|
||||
${field("tlsMode", `<select class="input" name="tlsMode" ${lock("tlsMode")}>${["auto", "pem", "proxy"].map((m) =>
|
||||
`<option value="${m}" ${m === settings.tlsMode ? "selected" : ""}>${escapeHtml(t(`Admin.Settings.Tls.${cap(m)}`))}</option>`).join("")}</select>`)}
|
||||
${text("tlsCert", settings.tlsCert)}
|
||||
${text("tlsKey", settings.tlsKey)}
|
||||
${field("fingerprint", `<code class="fingerprint" data-i18n-title="Admin.Settings.Fingerprint.Tip">${escapeHtml(info?.tlsFingerprint ?? "—")}</code>`)}
|
||||
</section>
|
||||
<section class="panel">
|
||||
<h2 data-i18n="Admin.Settings.Limits"></h2>
|
||||
${number("maxUploadMb", Math.round(settings.maxUploadBytes / MB), 'min="1"')}
|
||||
${number("chunkSizeMib", settings.chunkSizeBytes / MIB, 'min="0.0625" max="64" step="0.0625"')}
|
||||
${number("tokenLifetimeDays", settings.tokenLifetimeDays, 'min="1" max="365"')}
|
||||
${number("uploadRetentionHours", settings.uploadRetentionHours, 'min="1" max="8760"')}
|
||||
${number("jobHistoryDays", settings.jobHistoryDays, 'min="1" max="3650"')}
|
||||
</section>
|
||||
<div class="buttons"><button class="btn primary" type="submit" data-i18n="Admin.Common.Save" data-i18n-title="Admin.Common.Save.Tip"></button></div>
|
||||
</form>`;
|
||||
applyStrings(root);
|
||||
root.querySelector("form").addEventListener("submit", save);
|
||||
}
|
||||
|
||||
async function save(e) {
|
||||
e.preventDefault();
|
||||
const form = e.target;
|
||||
const data = new FormData(form);
|
||||
const value = (name, fallback) => (form.elements[name]?.disabled ? fallback : data.get(name));
|
||||
const request = {
|
||||
...settings,
|
||||
serverName: String(value("serverName", settings.serverName)).trim(),
|
||||
language: String(value("language", settings.language)),
|
||||
workers: Number(value("workers", settings.workers)),
|
||||
discovery: form.elements.discovery.disabled ? settings.discovery : form.elements.discovery.checked,
|
||||
httpPort: Number(value("httpPort", settings.httpPort)),
|
||||
httpsPort: Number(value("httpsPort", settings.httpsPort)),
|
||||
tlsMode: String(value("tlsMode", settings.tlsMode)),
|
||||
tlsCert: String(value("tlsCert", settings.tlsCert ?? "")).trim() || null,
|
||||
tlsKey: String(value("tlsKey", settings.tlsKey ?? "")).trim() || null,
|
||||
maxUploadBytes: Math.round(Number(data.get("maxUploadMb")) * MB),
|
||||
chunkSizeBytes: Math.round(Number(data.get("chunkSizeMib")) * MIB),
|
||||
tokenLifetimeDays: Number(data.get("tokenLifetimeDays")),
|
||||
uploadRetentionHours: Number(data.get("uploadRetentionHours")),
|
||||
jobHistoryDays: Number(data.get("jobHistoryDays")),
|
||||
};
|
||||
try {
|
||||
settings = await api.put("/api/v1/server/settings", request);
|
||||
draw();
|
||||
toast(t("Admin.Common.Saved"));
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
const name = err.details && Object.keys(form.elements).find((k) => k.toLowerCase() === String(err.details).toLowerCase());
|
||||
const input = name ? form.elements[name] : null;
|
||||
if (input) {
|
||||
input.setAttribute("aria-invalid", "true");
|
||||
input.title = errorText(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
draw();
|
||||
}
|
||||
|
||||
function cap(s) {
|
||||
return s.charAt(0).toUpperCase() + s.slice(1);
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
// Users (mockup AdminUsers.html, BRIEF §14.8): accounts table and a side panel to create, edit and reset.
|
||||
import { t, applyStrings } from "../i18n.js";
|
||||
import { escapeHtml, attr, formatBytes, formatNumber, formatTime, bar, toast, confirmDialog, errorText } from "../ui.js";
|
||||
import { icons } from "../icons.js";
|
||||
|
||||
const ROLES = ["viewer", "uploader", "editor", "admin"];
|
||||
const TB = 1e12;
|
||||
const RENDER_NODE = 32;
|
||||
|
||||
export async function render(root, { api, info, meta, actions }) {
|
||||
let users = [];
|
||||
let filter = "";
|
||||
let freeBytes = 0;
|
||||
let tokenDays = 30;
|
||||
|
||||
actions.innerHTML = `
|
||||
<input class="input search" type="search" id="search" data-i18n-placeholder="Admin.Users.Search" data-i18n-title="Admin.Users.Search.Tip" aria-label="${attr(t("Admin.Users.Search"))}">
|
||||
<button class="btn primary" id="new" data-i18n="Admin.Users.New" data-i18n-title="Admin.Users.New.Tip"></button>`;
|
||||
applyStrings(actions);
|
||||
actions.querySelector("#search").addEventListener("input", (e) => {
|
||||
filter = e.target.value.trim().toLowerCase();
|
||||
drawTable();
|
||||
});
|
||||
actions.querySelector("#new").addEventListener("click", () => openForm(null));
|
||||
|
||||
root.innerHTML = `
|
||||
<div class="split">
|
||||
<div class="panel flush">
|
||||
<div class="table users">
|
||||
<div class="thead"><span data-i18n="Admin.Users.User"></span><span data-i18n="Admin.Users.Role"></span><span data-i18n="Admin.Users.Quota"></span>
|
||||
<span data-i18n="Admin.Users.LastSeen"></span><span class="right" data-i18n="Admin.Users.Uploads"></span><span data-i18n="Admin.Users.Status"></span><span></span></div>
|
||||
<div id="rows"></div>
|
||||
</div>
|
||||
<div class="table-foot"><span id="policy" class="muted"></span><span class="spacer"></span>
|
||||
<a href="#/logs?view=signins" data-i18n="Admin.Users.SignInLog" data-i18n-title="Admin.Users.SignInLog.Tip"></a></div>
|
||||
</div>
|
||||
<aside class="panel side" id="side" hidden></aside>
|
||||
</div>`;
|
||||
applyStrings(root);
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const [list, stats, settings] = await Promise.all([
|
||||
api.get("/api/v1/users"),
|
||||
api.get("/api/v1/archive/stats").catch(() => null),
|
||||
api.get("/api/v1/server/settings").catch(() => null),
|
||||
]);
|
||||
users = list;
|
||||
freeBytes = stats?.freeBytes ?? 0;
|
||||
tokenDays = settings?.tokenLifetimeDays ?? tokenDays;
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
return;
|
||||
}
|
||||
const active = users.filter((u) => !u.user.disabled).length;
|
||||
meta.textContent = t("Admin.Users.Meta", formatNumber(users.length), formatNumber(active));
|
||||
root.querySelector("#policy").textContent = t("Admin.Users.Policy", info?.limits?.passwordMinLength ?? 10, tokenDays);
|
||||
drawTable();
|
||||
}
|
||||
|
||||
function drawTable() {
|
||||
const rows = users.filter((u) => !filter || u.user.username.toLowerCase().includes(filter) || (u.user.displayName ?? "").toLowerCase().includes(filter));
|
||||
const host = root.querySelector("#rows");
|
||||
host.innerHTML = rows.map((row) => {
|
||||
const u = row.user;
|
||||
const subtitle = [u.displayName, u.mustChangePassword ? t("Admin.Users.MustChange") : null, u.disabled ? t("Admin.Users.Disabled") : null]
|
||||
.filter(Boolean).join(" · ");
|
||||
const quota = u.quotaBytes > 0
|
||||
? `<div class="progress-cell">${bar(u.usedBytes / u.quotaBytes, u.disabled)}<span class="num nowrap">${escapeHtml(t("Admin.Users.QuotaOf", formatBytes(u.usedBytes), formatBytes(u.quotaBytes)))}</span></div>`
|
||||
: `<span class="muted">${escapeHtml(u.usedBytes > 0 ? formatBytes(u.usedBytes) : "—")}</span>`;
|
||||
const seen = row.lastSeen ? `${formatTime(row.lastSeen)}${row.lastClient ? " · " + row.lastClient : ""}` : "—";
|
||||
const buttons = u.disabled
|
||||
? `<button class="btn small" data-act="enable" data-i18n="Admin.Users.Enable" data-i18n-title="Admin.Users.Enable.Tip"></button>
|
||||
<button class="btn small destructive" data-act="delete" data-i18n="Admin.Users.Delete" data-i18n-title="Admin.Users.Delete.Tip"></button>`
|
||||
: `<button class="btn small" data-act="edit" data-i18n="Admin.Users.Edit" data-i18n-title="Admin.Users.Edit.Tip"></button>
|
||||
<button class="btn small" data-act="reset" data-i18n="Admin.Users.Reset" data-i18n-title="Admin.Users.Reset.Tip"></button>
|
||||
${u.id === api.user().id ? "" : `<button class="btn small" data-act="disable" data-i18n="Admin.Users.Disable" data-i18n-title="Admin.Users.Disable.Tip"></button>`}`;
|
||||
return `
|
||||
<div class="row${u.disabled ? " dim" : ""}" data-id="${attr(u.id)}">
|
||||
<span class="person"><span class="avatar ${attr(u.role)}">${escapeHtml(u.username.slice(0, 1).toUpperCase())}</span>
|
||||
<span class="stack"><strong>${escapeHtml(u.username)}</strong><small>${escapeHtml(subtitle)}</small></span></span>
|
||||
<span data-i18n="Admin.Role.${cap(u.role)}"></span>
|
||||
<span>${quota}</span>
|
||||
<span class="ellipsis">${escapeHtml(seen)}</span>
|
||||
<span class="num right">${formatNumber(row.uploadsLast30Days)}</span>
|
||||
<span class="${u.disabled ? "muted" : "state done"}">${u.disabled ? "○" : "●"} <span data-i18n="${u.disabled ? "Admin.Users.Disabled" : "Admin.Users.Active"}"></span></span>
|
||||
<span class="row-actions">${buttons}</span>
|
||||
</div>`;
|
||||
}).join("") || `<div class="empty" data-i18n="Admin.Users.Empty"></div>`;
|
||||
applyStrings(host);
|
||||
host.querySelectorAll("[data-act]").forEach((button) => button.addEventListener("click", () => {
|
||||
const row = users.find((u) => u.user.id === button.closest(".row").dataset.id);
|
||||
act(button.dataset.act, row.user);
|
||||
}));
|
||||
}
|
||||
|
||||
async function act(action, user) {
|
||||
try {
|
||||
if (action === "edit") return openForm(user);
|
||||
if (action === "reset") {
|
||||
const r = await api.post(`/api/v1/users/${user.id}/reset-password`, {});
|
||||
showPassword(r);
|
||||
} else if (action === "disable" || action === "enable") {
|
||||
await api.patch(`/api/v1/users/${user.id}`, { disabled: action === "disable" });
|
||||
} else if (action === "delete") {
|
||||
const confirmed = await confirmDialog({
|
||||
title: t("Admin.Users.DeleteTitle", user.username),
|
||||
body: t("Admin.Users.DeleteBody"),
|
||||
confirm: t("Admin.Users.DeleteConfirm"),
|
||||
confirmTip: t("Admin.Users.Delete.Tip"),
|
||||
cancel: t("Admin.Common.Keep"),
|
||||
cancelTip: t("Admin.Common.Keep.Tip"),
|
||||
typed: user.username,
|
||||
});
|
||||
if (!confirmed) return;
|
||||
await api.del(`/api/v1/users/${user.id}`);
|
||||
}
|
||||
await load();
|
||||
} catch (err) {
|
||||
toast(errorText(err), true);
|
||||
}
|
||||
}
|
||||
|
||||
function openForm(user) {
|
||||
const side = root.querySelector("#side");
|
||||
const editing = user !== null;
|
||||
const quotaTb = editing && user.quotaBytes > 0 ? +(user.quotaBytes / TB).toFixed(3) : 0;
|
||||
const renderNode = editing ? (user.permissions & RENDER_NODE) !== 0 : false;
|
||||
side.hidden = false;
|
||||
side.innerHTML = `
|
||||
<form class="form" novalidate>
|
||||
<h2 data-i18n="${editing ? "Admin.Users.EditTitle" : "Admin.Users.New"}"></h2>
|
||||
<label><span data-i18n="Admin.Users.Username"></span>
|
||||
<input class="input" name="username" required autocomplete="off" value="${attr(user?.username ?? "")}" ${editing ? "disabled" : ""} data-i18n-title="Admin.Users.Username.Tip"></label>
|
||||
<label><span data-i18n="Admin.Users.DisplayName"></span>
|
||||
<input class="input" name="displayName" autocomplete="off" value="${attr(user?.displayName ?? "")}" data-i18n-title="Admin.Users.DisplayName.Tip"></label>
|
||||
<fieldset><legend data-i18n="Admin.Users.Role"></legend>
|
||||
${ROLES.map((r) => `<label class="check"><input type="radio" name="role" value="${r}" ${(user?.role ?? "editor") === r ? "checked" : ""} data-i18n-title="Admin.Users.Role.${cap(r)}.Tip"><span data-i18n="Admin.Role.${cap(r)}"></span></label>`).join("")}
|
||||
</fieldset>
|
||||
<label><span data-i18n="Admin.Users.Quota"></span>
|
||||
<span class="inline"><input class="input narrow" name="quota" type="number" min="0" step="0.1" value="${quotaTb}" data-i18n-title="Admin.Users.Quota.Tip"><span>TB</span>
|
||||
<small class="muted">${escapeHtml(t("Admin.Users.FreeOnServer", formatBytes(freeBytes)))}</small></span></label>
|
||||
${editing ? "" : `
|
||||
<label><span data-i18n="Admin.Users.TempPassword"></span>
|
||||
<span class="inline"><input class="input mono" name="password" autocomplete="off" value="${attr(generatePassword())}" data-i18n-title="Admin.Users.TempPassword.Tip">
|
||||
<button class="btn icon" type="button" id="regenerate" data-i18n-title="Admin.Users.Regenerate.Tip">${icons.refresh}</button></span></label>
|
||||
<label class="check"><input type="checkbox" checked disabled data-i18n-title="Admin.Users.MustChangeFlag.Tip"><span data-i18n="Admin.Users.MustChangeFlag"></span></label>`}
|
||||
<label class="check"><input type="checkbox" name="renderNode" ${renderNode ? "checked" : ""} data-i18n-title="Admin.Users.RenderNode.Tip"><span data-i18n="Admin.Users.RenderNode"></span></label>
|
||||
<div class="error" role="alert"></div>
|
||||
<div class="buttons">
|
||||
<button class="btn" type="button" id="close" data-i18n="Admin.Common.Cancel" data-i18n-title="Admin.Common.Cancel.Tip"></button>
|
||||
<button class="btn primary" type="submit" data-i18n="${editing ? "Admin.Common.Save" : "Admin.Users.Create"}" data-i18n-title="${editing ? "Admin.Common.Save.Tip" : "Admin.Users.Create.Tip"}"></button>
|
||||
</div>
|
||||
</form>`;
|
||||
applyStrings(side);
|
||||
const form = side.querySelector("form");
|
||||
side.querySelector("#close").addEventListener("click", () => {
|
||||
side.hidden = true;
|
||||
});
|
||||
side.querySelector("#regenerate")?.addEventListener("click", () => {
|
||||
form.elements.password.value = generatePassword();
|
||||
});
|
||||
form.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
const error = form.querySelector(".error");
|
||||
error.textContent = "";
|
||||
const data = new FormData(form);
|
||||
const quotaBytes = Math.round(Number(data.get("quota") || 0) * TB);
|
||||
try {
|
||||
if (editing) {
|
||||
await api.patch(`/api/v1/users/${user.id}`, {
|
||||
displayName: String(data.get("displayName")), role: data.get("role"), quotaBytes, allowRenderNode: data.get("renderNode") === "on",
|
||||
});
|
||||
side.hidden = true;
|
||||
} else {
|
||||
const r = await api.post("/api/v1/users", {
|
||||
username: String(data.get("username")).trim(), displayName: String(data.get("displayName")), role: data.get("role"), quotaBytes,
|
||||
password: String(data.get("password")), allowRenderNode: data.get("renderNode") === "on",
|
||||
});
|
||||
showPassword(r);
|
||||
}
|
||||
await load();
|
||||
} catch (err) {
|
||||
error.textContent = errorText(err);
|
||||
}
|
||||
});
|
||||
form.querySelector("input:not([disabled])").focus();
|
||||
}
|
||||
|
||||
/** The one-time password of a new or reset account, shown once in the side panel. */
|
||||
function showPassword(result) {
|
||||
const side = root.querySelector("#side");
|
||||
side.hidden = false;
|
||||
side.innerHTML = `
|
||||
<div class="form">
|
||||
<h2>${escapeHtml(result.user.username)}</h2>
|
||||
<label><span data-i18n="Admin.Users.TempPassword"></span>
|
||||
<span class="inline"><input class="input mono" readonly value="${attr(result.temporaryPassword)}" data-i18n-title="Admin.Users.TempPassword.Tip">
|
||||
<button class="btn icon" type="button" id="copy" data-i18n-title="Admin.Users.Copy.Tip">${icons.copy}</button></span></label>
|
||||
<div class="buttons"><button class="btn" type="button" id="close" data-i18n="Admin.Common.Close" data-i18n-title="Admin.Common.Close.Tip"></button></div>
|
||||
</div>`;
|
||||
applyStrings(side);
|
||||
side.querySelector("#copy").addEventListener("click", async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(result.temporaryPassword);
|
||||
toast(t("Admin.Users.Copied"));
|
||||
} catch {
|
||||
side.querySelector("input").select();
|
||||
}
|
||||
});
|
||||
side.querySelector("#close").addEventListener("click", () => {
|
||||
side.hidden = true;
|
||||
});
|
||||
}
|
||||
|
||||
await load();
|
||||
}
|
||||
|
||||
/** Three groups of four unambiguous characters, like the server's own temporary passwords. */
|
||||
function generatePassword() {
|
||||
const alphabet = "abcdefghjkmnpqrstuvwxyz23456789";
|
||||
const chars = [];
|
||||
while (chars.length < 12) {
|
||||
// Bytes at or above 248 (8 × 31) are skipped so every character is equally likely.
|
||||
for (const b of crypto.getRandomValues(new Uint8Array(16))) {
|
||||
if (b < 248 && chars.length < 12) chars.push(alphabet[b % alphabet.length]);
|
||||
}
|
||||
}
|
||||
return `${chars.slice(0, 4).join("")}-${chars.slice(4, 8).join("")}-${chars.slice(8).join("")}`;
|
||||
}
|
||||
|
||||
function cap(s) {
|
||||
return s.charAt(0).toUpperCase() + s.slice(1);
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
// Small DOM and formatting helpers shared by the admin pages. No inline styles: the panel runs under a strict CSP.
|
||||
import { t, culture } from "./i18n.js";
|
||||
|
||||
export function escapeHtml(s) {
|
||||
return String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
|
||||
}
|
||||
|
||||
/** Escaped attribute value for title="…" tooltips. */
|
||||
export const attr = escapeHtml;
|
||||
|
||||
export function formatNumber(n) {
|
||||
return new Intl.NumberFormat(culture()).format(n ?? 0);
|
||||
}
|
||||
|
||||
/** 9.4 TB, 610 GB … (decimal units, like disk vendors and the desktop client). */
|
||||
export function formatBytes(bytes) {
|
||||
const units = ["B", "KB", "MB", "GB", "TB", "PB"];
|
||||
let value = Number(bytes ?? 0);
|
||||
let unit = 0;
|
||||
while (value >= 1000 && unit < units.length - 1) {
|
||||
value /= 1000;
|
||||
unit++;
|
||||
}
|
||||
const digits = value >= 100 || unit === 0 ? 0 : 1;
|
||||
return `${new Intl.NumberFormat(culture(), { maximumFractionDigits: digits }).format(value)} ${units[unit]}`;
|
||||
}
|
||||
|
||||
export function formatTime(iso) {
|
||||
if (!iso) return "—";
|
||||
const date = new Date(iso);
|
||||
const sameDay = new Date().toDateString() === date.toDateString();
|
||||
return new Intl.DateTimeFormat(culture(), sameDay ? { timeStyle: "short" } : { dateStyle: "medium" }).format(date);
|
||||
}
|
||||
|
||||
export function formatDateTime(iso) {
|
||||
return iso ? new Intl.DateTimeFormat(culture(), { dateStyle: "short", timeStyle: "medium" }).format(new Date(iso)) : "—";
|
||||
}
|
||||
|
||||
export function formatDuration(seconds) {
|
||||
const s = Math.max(0, Math.round(seconds ?? 0));
|
||||
if (s >= 86400) return t("Common.Duration.Days", Math.floor(s / 86400), Math.floor((s % 86400) / 3600));
|
||||
if (s >= 3600) return t("Common.Duration.Hours", Math.floor(s / 3600), Math.floor((s % 3600) / 60));
|
||||
if (s >= 60) return t("Common.Duration.Minutes", Math.floor(s / 60));
|
||||
return t("Common.Duration.Seconds", s);
|
||||
}
|
||||
|
||||
/** 6 px progress bar drawn as SVG (attributes, not inline styles). */
|
||||
export function bar(fraction, muted = false) {
|
||||
const width = Math.max(0, Math.min(100, Math.round((fraction ?? 0) * 100)));
|
||||
return `<svg class="bar${muted ? " muted" : ""}" viewBox="0 0 100 6" preserveAspectRatio="none" aria-hidden="true">` +
|
||||
`<rect class="track" width="100" height="6" rx="3"/><rect class="fill" width="${width}" height="6" rx="3"/></svg>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Column chart: values with labels under each bar and, when given, the value above it.
|
||||
* Bars use the accent colour, or the sun colour for upload activity.
|
||||
*/
|
||||
export function columns(items, { sun = false, valueLabel = null, height = 190 } = {}) {
|
||||
const width = 400;
|
||||
const base = height - 30;
|
||||
const max = Math.max(1, ...items.map((i) => i.value));
|
||||
const step = width / Math.max(1, items.length);
|
||||
const barWidth = Math.max(2, Math.min(32, step * 0.65));
|
||||
let svg = `<svg class="chart${sun ? " sun" : ""}" viewBox="0 0 ${width} ${height}" role="img">`;
|
||||
svg += `<line class="axis" x1="0" y1="${base}" x2="${width}" y2="${base}"/>`;
|
||||
items.forEach((item, i) => {
|
||||
const h = item.value > 0 ? Math.max(2, (item.value / max) * (base - 18)) : 0;
|
||||
const x = i * step + (step - barWidth) / 2;
|
||||
svg += `<rect class="column" x="${x.toFixed(1)}" y="${(base - h).toFixed(1)}" width="${barWidth.toFixed(1)}" height="${h.toFixed(1)}" rx="2"><title>${escapeHtml(item.title ?? "")}</title></rect>`;
|
||||
if (valueLabel) svg += `<text class="value" x="${(x + barWidth / 2).toFixed(1)}" y="${(base - h - 5).toFixed(1)}">${escapeHtml(valueLabel(item))}</text>`;
|
||||
if (item.label) svg += `<text class="label" x="${(x + barWidth / 2).toFixed(1)}" y="${base + 16}">${escapeHtml(item.label)}</text>`;
|
||||
});
|
||||
return svg + "</svg>";
|
||||
}
|
||||
|
||||
/** Shows a toast at the bottom right for 8 s (hover pauses). */
|
||||
export function toast(message, isError = false) {
|
||||
const host = document.getElementById("toasts") ?? document.body.appendChild(Object.assign(document.createElement("div"), { id: "toasts" }));
|
||||
const item = document.createElement("div");
|
||||
item.className = `toast${isError ? " error" : ""}`;
|
||||
item.setAttribute("role", isError ? "alert" : "status");
|
||||
item.textContent = message;
|
||||
host.appendChild(item);
|
||||
let remaining = 8000;
|
||||
let started = Date.now();
|
||||
let timer = setTimeout(() => item.remove(), remaining);
|
||||
item.addEventListener("mouseenter", () => {
|
||||
clearTimeout(timer);
|
||||
remaining -= Date.now() - started;
|
||||
});
|
||||
item.addEventListener("mouseleave", () => {
|
||||
started = Date.now();
|
||||
timer = setTimeout(() => item.remove(), remaining);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Two-button confirmation dialog (safe action left and default). Resolves true when the destructive action is chosen;
|
||||
* with `typed`, that button stays disabled until the text is typed.
|
||||
*/
|
||||
export function confirmDialog({ title, body, confirm, confirmTip, cancel, cancelTip, typed }) {
|
||||
return new Promise((resolve) => {
|
||||
const dialog = document.createElement("dialog");
|
||||
dialog.className = "dialog";
|
||||
dialog.innerHTML = `
|
||||
<form method="dialog">
|
||||
<h2>${escapeHtml(title)}</h2>
|
||||
${body ? `<p>${escapeHtml(body)}</p>` : ""}
|
||||
${typed ? `<input class="input" name="typed" autocomplete="off" title="${attr(typed)}" aria-label="${attr(typed)}" placeholder="${attr(typed)}">` : ""}
|
||||
<div class="buttons">
|
||||
<button class="btn" value="cancel" title="${attr(cancelTip)}" autofocus>${escapeHtml(cancel)}</button>
|
||||
<button class="btn destructive" value="ok" title="${attr(confirmTip)}" ${typed ? "disabled" : ""}>${escapeHtml(confirm)}</button>
|
||||
</div>
|
||||
</form>`;
|
||||
document.body.appendChild(dialog);
|
||||
const ok = dialog.querySelector('button[value="ok"]');
|
||||
dialog.querySelector('input[name="typed"]')?.addEventListener("input", (e) => {
|
||||
ok.disabled = e.target.value !== typed;
|
||||
});
|
||||
dialog.addEventListener("close", () => {
|
||||
resolve(dialog.returnValue === "ok");
|
||||
dialog.remove();
|
||||
});
|
||||
dialog.showModal();
|
||||
});
|
||||
}
|
||||
|
||||
/** The localised message of an API failure, or the generic one when the server was not reached. */
|
||||
export function errorText(err) {
|
||||
return err?.message && err.status ? err.message : t("Admin.Error.Unreachable");
|
||||
}
|
||||
|
||||
/** Query parameters of the hash route (#/logs?view=signins). */
|
||||
export function routeParams() {
|
||||
return new URLSearchParams(location.hash.split("?")[1] ?? "");
|
||||
}
|
||||
|
||||
/** A language's own name ("Italiano"), for language menus. */
|
||||
export function nativeName(code) {
|
||||
try {
|
||||
const name = new Intl.DisplayNames([code], { type: "language" }).of(code) ?? code;
|
||||
return name.charAt(0).toLocaleUpperCase(code) + name.slice(1);
|
||||
} catch {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using System.Text;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Jobs;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>Administration (BRIEF §14.8): dashboard figures, server settings, log tail and downloads, audit log.</summary>
|
||||
public static class AdminEndpoints
|
||||
{
|
||||
private const int MaxTailLines = 2000;
|
||||
private const int TailWindowBytes = 1 << 20;
|
||||
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
api.MapGet("/server/dashboard", DashboardAsync).Requires(Permissions.ManageArchive | Permissions.ManageUsers);
|
||||
api.MapGet("/server/settings", (ServerSettingsStore store) => Results.Json(store.Current(), ProtocolJson.Default.ServerSettingsDto))
|
||||
.Requires(Permissions.ManageArchive);
|
||||
api.MapPut("/server/settings", SaveSettingsAsync).Requires(Permissions.ManageArchive);
|
||||
api.MapGet("/audit", AuditAsync).Requires(Permissions.ManageUsers | Permissions.ManageArchive);
|
||||
|
||||
var logs = api.MapGroup("/logs").Requires(Permissions.ManageArchive);
|
||||
logs.MapGet("", TailAsync);
|
||||
logs.MapGet("/files", (ServerOptions options) => Results.Json(LogFiles(options).Select(f => new LogFileDto(f.Name, f.Length, f.LastWriteTimeUtc)).ToList(),
|
||||
ProtocolJson.Default.ListLogFileDto));
|
||||
logs.MapGet("/files/{name}", (HttpContext context, string name, ServerOptions options) =>
|
||||
{
|
||||
// Only names of existing log files are served: the name never becomes part of a path.
|
||||
if (LogFiles(options).FirstOrDefault(f => f.Name == name) is not { } file) return ApiErrors.NotFound(context, "log");
|
||||
context.Response.Headers.CacheControl = "no-store";
|
||||
return Results.File(file.FullName, "text/plain; charset=utf-8", file.Name, file.LastWriteTimeUtc, enableRangeProcessing: true);
|
||||
});
|
||||
}
|
||||
|
||||
private static async Task<IResult> DashboardAsync(HttpContext context, TitanoDatabase db, ServerArchive archive, ServerOptions options, ServerState state,
|
||||
JobEngineHost jobs, TimeProvider time)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
var now = time.GetUtcNow();
|
||||
var today = new DateTimeOffset(now.UtcDateTime.Date, TimeSpan.Zero);
|
||||
var users = await db.Users.ListAsync(ct);
|
||||
var names = users.ToDictionary(u => u.Id, u => u.Username);
|
||||
var counts = await db.Jobs.CountAsync(today, ct);
|
||||
var running = await db.Jobs.ListAsync(JobState.Running, null, 10, null, ct);
|
||||
var queued = await db.Jobs.ListAsync(JobState.Queued, null, 10, null, ct);
|
||||
var nodes = await db.RenderNodes.ListAsync(ct);
|
||||
var uploads = await db.Uploads.CommittedPerDayAsync(today.AddDays(-29), ct);
|
||||
var activity = await db.Audit.ListAsync(null, null, 12, null, ct);
|
||||
return Results.Json(new DashboardDto(
|
||||
options.ServerName,
|
||||
SystemEndpoints.Version,
|
||||
(long)(now - state.StartedAt).TotalSeconds,
|
||||
state.Database,
|
||||
options.TlsMode.ToString().ToLowerInvariant(),
|
||||
options.Discovery,
|
||||
users.Count,
|
||||
await ArchiveEndpoints.StatsAsync(db, archive, ct),
|
||||
new JobCountsDto(counts.Queued, counts.Running, counts.Paused, counts.DoneSince, counts.FailedSince),
|
||||
jobs.Workers,
|
||||
[.. nodes.Select(n => Dto.Node(n, names.GetValueOrDefault(n.UserId, string.Empty)))],
|
||||
[.. uploads.Select(u => new UploadDayDto(u.Day, u.Count, u.Bytes))],
|
||||
[.. activity.Items.Select(Dto.Audit)],
|
||||
[.. running.Items.Concat(queued.Items.OrderByDescending(j => j.Priority).ThenBy(j => j.Created)).Take(10).Select(Dto.Job)]),
|
||||
ProtocolJson.Default.DashboardDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> SaveSettingsAsync(HttpContext context, Caller caller, ServerSettingsDto request, ServerSettingsStore store, AuditLog audit)
|
||||
{
|
||||
switch (store.Save(request))
|
||||
{
|
||||
case ("setting_locked", var setting):
|
||||
return ApiErrors.Conflict(context, "setting_locked", setting);
|
||||
case (var code, var setting):
|
||||
return ApiErrors.BadRequest(context, code, setting, setting);
|
||||
}
|
||||
await audit.WriteAsync(caller, context, "server_settings", "server", null, request.ServerName);
|
||||
return Results.Json(store.Current(), ProtocolJson.Default.ServerSettingsDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> AuditAsync(HttpContext context, TitanoDatabase db, Guid? user, string? action, string? cursor, int? limit)
|
||||
{
|
||||
var page = await db.Audit.ListAsync(user, string.IsNullOrWhiteSpace(action) ? null : action, limit ?? 100,
|
||||
string.IsNullOrEmpty(cursor) ? null : cursor, context.RequestAborted);
|
||||
return Results.Json(new Page<AuditEntryDto>([.. page.Items.Select(Dto.Audit)], page.NextCursor, null), ProtocolJson.Default.PageAuditEntryDto);
|
||||
}
|
||||
|
||||
/// <summary>The last <c>lines</c> lines (default 300) of the newest server log.</summary>
|
||||
private static async Task<IResult> TailAsync(HttpContext context, ServerOptions options, int? lines)
|
||||
{
|
||||
var take = Math.Clamp(lines ?? 300, 1, MaxTailLines);
|
||||
if (LogFiles(options).FirstOrDefault() is not { } file) return Results.Json(new LogTailDto(string.Empty, []), ProtocolJson.Default.LogTailDto);
|
||||
await using var stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||
var start = Math.Max(0, stream.Length - TailWindowBytes);
|
||||
stream.Position = start;
|
||||
using var reader = new StreamReader(stream, Encoding.UTF8);
|
||||
var text = await reader.ReadToEndAsync(context.RequestAborted);
|
||||
var all = text.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
||||
// A window that starts mid-file begins with a partial line.
|
||||
var complete = start > 0 && all.Length > 0 ? all[1..] : all;
|
||||
return Results.Json(new LogTailDto(file.Name, [.. complete.TakeLast(take).Select(l => l.TrimEnd('\r'))]), ProtocolJson.Default.LogTailDto);
|
||||
}
|
||||
|
||||
/// <summary>Server log files, newest first.</summary>
|
||||
private static List<FileInfo> LogFiles(ServerOptions options) =>
|
||||
Directory.Exists(options.LogPath)
|
||||
? [.. new DirectoryInfo(options.LogPath).EnumerateFiles("*.log").OrderByDescending(f => f.LastWriteTimeUtc).ThenByDescending(f => f.Name)]
|
||||
: [];
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
using Titano.Core.Localization;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Error responses of API v1: <c>{ code, message, details }</c> (BRIEF §14.3). <c>code</c> is a stable snake_case
|
||||
/// identifier for programs; <c>message</c> is one localised sentence (resource <c>Server.Error.<Code></c>, language
|
||||
/// from <c>Accept-Language</c>, else the server language); <c>details</c> is optional machine-readable context.
|
||||
/// </summary>
|
||||
public static class ApiErrors
|
||||
{
|
||||
public static IResult Result(HttpContext context, int status, string code, string? details = null, params object?[] args) =>
|
||||
Results.Json(new ApiError(code, Message(context, code, args), details), ProtocolJson.Default.ApiError, statusCode: status);
|
||||
|
||||
public static Task WriteAsync(HttpContext context, int status, string code, string? details = null) =>
|
||||
Result(context, status, code, details).ExecuteAsync(context);
|
||||
|
||||
public static IResult NotFound(HttpContext context, string? what = null) => Result(context, StatusCodes.Status404NotFound, "not_found", what);
|
||||
|
||||
public static IResult BadRequest(HttpContext context, string code, string? details = null, params object?[] args) =>
|
||||
Result(context, StatusCodes.Status400BadRequest, code, details, args);
|
||||
|
||||
public static IResult Conflict(HttpContext context, string code, string? details = null) => Result(context, StatusCodes.Status409Conflict, code, details);
|
||||
|
||||
public static IResult Forbidden(HttpContext context) => Result(context, StatusCodes.Status403Forbidden, "forbidden");
|
||||
|
||||
public static string Message(HttpContext context, string code, params object?[] args)
|
||||
{
|
||||
var culture = RequestCulture.Of(context);
|
||||
var template = Loc.Get("Server.Error." + Pascal(code), culture) ?? code;
|
||||
return args.Length == 0 ? template : string.Format(culture, template, args);
|
||||
}
|
||||
|
||||
private static string Pascal(string code)
|
||||
{
|
||||
var sb = new StringBuilder(code.Length);
|
||||
var upper = true;
|
||||
foreach (var c in code)
|
||||
{
|
||||
if (c == '_')
|
||||
{
|
||||
upper = true;
|
||||
continue;
|
||||
}
|
||||
sb.Append(upper ? char.ToUpperInvariant(c) : c);
|
||||
upper = false;
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The language of a request's messages: the best shipped match of <c>Accept-Language</c>, else the server language.</summary>
|
||||
public static class RequestCulture
|
||||
{
|
||||
public static CultureInfo Of(HttpContext context)
|
||||
{
|
||||
if (context.Items.TryGetValue(typeof(RequestCulture), out var cached) && cached is CultureInfo known) return known;
|
||||
var fallback = context.RequestServices.GetRequiredService<ServerOptions>().Language;
|
||||
var culture = FromHeader(context.Request.Headers.AcceptLanguage.ToString()) ?? Parse(fallback) ?? CultureInfo.GetCultureInfo("en");
|
||||
context.Items[typeof(RequestCulture)] = culture;
|
||||
return culture;
|
||||
}
|
||||
|
||||
private static CultureInfo? FromHeader(string header)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(header) || !StringWithQualityHeaderValue.TryParseList([header], out var values)) return null;
|
||||
foreach (var value in values.OrderByDescending(v => v.Quality ?? 1))
|
||||
{
|
||||
if (value.Value.Value is { } tag && tag != "*" && Parse(tag) is { } culture) return culture;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static CultureInfo? Parse(string tag)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Languages.BestMatch(CultureInfo.GetCultureInfo(tag));
|
||||
}
|
||||
catch (CultureNotFoundException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Core.Naming;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Repositories;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Jobs;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary><c>/api/v1/archive</c> (admin): templates and proxy policy, scan/rebuild/verify/maintenance jobs, statistics.</summary>
|
||||
public static class ArchiveEndpoints
|
||||
{
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var archive = api.MapGroup("/archive").Requires(Permissions.ManageArchive);
|
||||
archive.MapGet("/settings", (ServerArchive a) => Results.Json(Settings(a.Current), ProtocolJson.Default.ArchiveSettingsDto));
|
||||
archive.MapPut("/settings", UpdateAsync);
|
||||
archive.MapGet("/stats", async (HttpContext c, TitanoDatabase db, ServerArchive a) =>
|
||||
Results.Json(await StatsAsync(db, a, c.RequestAborted), ProtocolJson.Default.ArchiveStatsDto));
|
||||
foreach (var (path, kind) in new[] { ("scan", JobKind.Scan), ("rebuild", JobKind.Rebuild), ("verify", JobKind.Verify), ("maintenance", JobKind.Maintenance) })
|
||||
{
|
||||
archive.MapPost("/" + path, (HttpContext c, Caller caller, ServerArchive a, JobEngineHost host, AuditLog audit) => EnqueueAsync(c, caller, a, host, audit, kind));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Totals, distributions, free space and health of the archive (also shown on the dashboard).</summary>
|
||||
public static async Task<ArchiveStatsDto> StatsAsync(TitanoDatabase db, ServerArchive archive, CancellationToken cancellationToken)
|
||||
{
|
||||
var stats = await db.Sequences.GetStatsAsync(archive.Id, cancellationToken);
|
||||
var withoutProxies = await db.Sequences.CountAsync(
|
||||
new SequenceQuery { ArchiveId = archive.Id, WithoutStatus = SequenceStatus.ProxiesBuilt }, cancellationToken);
|
||||
long free = 0, capacity = 0;
|
||||
try
|
||||
{
|
||||
var drive = new DriveInfo(archive.Root);
|
||||
free = drive.AvailableFreeSpace;
|
||||
capacity = drive.TotalSize;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or ArgumentException or UnauthorizedAccessException)
|
||||
{
|
||||
// An offline archive volume reports no space figures.
|
||||
}
|
||||
return new ArchiveStatsDto(
|
||||
stats.Sequences,
|
||||
stats.Photos,
|
||||
stats.Bytes,
|
||||
free,
|
||||
capacity,
|
||||
stats.ByCamera.Count(c => c.Model is not null),
|
||||
stats.ByYear.Count > 0 ? stats.ByYear[0].Year : null,
|
||||
stats.ByYear.Count > 0 ? stats.ByYear[^1].Year : null,
|
||||
[.. stats.ByYear.Select(y => new KeyValueCount(y.Year.ToString(System.Globalization.CultureInfo.InvariantCulture), y.Photos, y.Bytes))],
|
||||
[.. stats.ByMonth.Select(m => new KeyValueCount($"{m.Year:D4}-{m.Month:D2}", m.Photos, 0))],
|
||||
[.. stats.ByCamera.Select(c => new KeyValueCount(string.Join(' ', new[] { c.Make, c.Model }.Where(v => !string.IsNullOrEmpty(v))), c.Photos, c.Bytes))],
|
||||
[.. stats.ByFormat.Select(f => new KeyValueCount(f.Format.ToString().ToLowerInvariant(), f.Photos, f.Bytes))],
|
||||
withoutProxies,
|
||||
stats.Health.Unverified);
|
||||
}
|
||||
|
||||
private static async Task<IResult> UpdateAsync(HttpContext context, Caller caller, ArchiveSettingsDto request, TitanoDatabase db, ServerArchive archive, AuditLog audit)
|
||||
{
|
||||
var name = request.Name?.Trim();
|
||||
if (string.IsNullOrEmpty(name) || name.Length > 100) return ApiErrors.BadRequest(context, "bad_request", "name");
|
||||
foreach (var (field, template) in new[] { ("folderTemplate", request.FolderTemplate), ("fileTemplate", request.FileTemplate) })
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(template) || !PathTemplate.Parse(template).IsValid) return ApiErrors.BadRequest(context, "invalid_template", field);
|
||||
}
|
||||
if (request.ProxyLongEdge is not (-1 or 0) and (< 256 or > 8192)) return ApiErrors.BadRequest(context, "bad_request", "proxyLongEdge");
|
||||
|
||||
var updated = archive.Current with
|
||||
{
|
||||
Name = name,
|
||||
FolderTemplate = request.FolderTemplate,
|
||||
FileTemplate = request.FileTemplate,
|
||||
ProxyLongEdge = request.ProxyLongEdge,
|
||||
};
|
||||
await db.Archives.UpsertAsync(updated, context.RequestAborted);
|
||||
archive.Current = updated;
|
||||
await audit.WriteAsync(caller, context, "archive_settings", "archive", updated.Id.ToString(), $"{updated.FolderTemplate} · {updated.FileTemplate}");
|
||||
return Results.Json(Settings(updated), ProtocolJson.Default.ArchiveSettingsDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> EnqueueAsync(HttpContext context, Caller caller, ServerArchive archive, JobEngineHost host, AuditLog audit, JobKind kind)
|
||||
{
|
||||
if (!host.CanRun(kind)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(kind));
|
||||
var job = await host.Engine.EnqueueAsync(kind, [archive.Id], archive.Current.Name, priority: JobPriority.User, createdBy: caller.Id,
|
||||
cancellationToken: context.RequestAborted);
|
||||
await audit.WriteAsync(caller, context, "job_created", "job", job.Id.ToString(), JobWire.Kind(kind));
|
||||
return Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted);
|
||||
}
|
||||
|
||||
private static ArchiveSettingsDto Settings(Archive a) => new(a.Id, a.Name, a.RootPath, a.FolderTemplate, a.FileTemplate, a.ProxyLongEdge);
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using System.Globalization;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Security;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary><c>/api/v1/auth</c>: sign-in (token or admin cookie), sign-out, password change, current account.</summary>
|
||||
public static class AuthEndpoints
|
||||
{
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
api.MapPost("/auth/login", LoginAsync).AllowAnonymousCaller();
|
||||
api.MapPost("/auth/logout", LogoutAsync).AllowDuringPasswordChange();
|
||||
api.MapPost("/auth/password", ChangePasswordAsync).AllowDuringPasswordChange();
|
||||
api.MapGet("/auth/me", (Caller caller) => Results.Json(Dto.User(caller.User), ProtocolJson.Default.UserDto)).AllowDuringPasswordChange();
|
||||
}
|
||||
|
||||
private static async Task<IResult> LoginAsync(HttpContext context, LoginRequest request, TitanoDatabase db, TokenService tokens, LoginGuard guard,
|
||||
AuditLog audit, TimeProvider time)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (!guard.TryAttempt(context.Connection.RemoteIpAddress?.ToString() ?? "unknown", out var wait)) return TooMany(context, "rate_limited", wait);
|
||||
var account = Accounts.Key(request.Username);
|
||||
if (!Accounts.IsValidUsername(account) || string.IsNullOrEmpty(request.Password))
|
||||
return ApiErrors.Result(context, StatusCodes.Status401Unauthorized, "invalid_credentials");
|
||||
if (guard.IsLocked(account, out wait)) return TooMany(context, "account_locked", wait);
|
||||
if (request.Cookie && !SessionCookies.IsSecureTransport(context)) return ApiErrors.BadRequest(context, "https_required");
|
||||
|
||||
var user = await db.Users.GetByUsernameAsync(account, ct);
|
||||
bool valid;
|
||||
if (user is null)
|
||||
{
|
||||
PasswordHasher.VerifyDummy(request.Password);
|
||||
valid = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = PasswordHasher.Verify(request.Password, user.PasswordHash) && !user.Disabled;
|
||||
}
|
||||
if (!valid)
|
||||
{
|
||||
guard.RecordFailure(account);
|
||||
await audit.WriteAsync(user?.Id, account, context, "login_failed", "user", user?.Id.ToString());
|
||||
return ApiErrors.Result(context, StatusCodes.Status401Unauthorized, "invalid_credentials");
|
||||
}
|
||||
|
||||
guard.RecordSuccess(account);
|
||||
var signedIn = user! with { LastLogin = time.GetUtcNow() };
|
||||
if (PasswordHasher.NeedsRehash(signedIn.PasswordHash))
|
||||
{
|
||||
signedIn = signedIn with { PasswordHash = PasswordHasher.Hash(request.Password) };
|
||||
await db.Users.UpdateAsync(signedIn, ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
await db.Users.RecordLoginAsync(signedIn.Id, signedIn.LastLogin.Value, ct);
|
||||
}
|
||||
var issued = await tokens.IssueAsync(signedIn, request.ClientName, ct);
|
||||
await audit.WriteAsync(signedIn.Id, signedIn.Username, context, "login", "user", signedIn.Id.ToString(), issued.Stored.ClientName);
|
||||
|
||||
string? secret = issued.Secret;
|
||||
if (request.Cookie)
|
||||
{
|
||||
// The admin panel never sees the token itself; it travels only in the HttpOnly cookie.
|
||||
SessionCookies.Issue(context, issued);
|
||||
secret = null;
|
||||
}
|
||||
return Results.Json(new LoginResponse(secret, Dto.User(signedIn), signedIn.MustChangePassword, issued.Stored.Expires), ProtocolJson.Default.LoginResponse);
|
||||
}
|
||||
|
||||
private static async Task<IResult> LogoutAsync(HttpContext context, Caller caller, TitanoDatabase db)
|
||||
{
|
||||
await db.Tokens.RevokeAsync(caller.TokenHash, context.RequestAborted);
|
||||
if (caller.Source == CredentialSource.Cookie) SessionCookies.Clear(context);
|
||||
return Results.NoContent();
|
||||
}
|
||||
|
||||
private static async Task<IResult> ChangePasswordAsync(HttpContext context, Caller caller, ChangePasswordRequest request, TitanoDatabase db,
|
||||
AuditLog audit, ServerOptions options)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (string.IsNullOrEmpty(request.CurrentPassword) || !PasswordHasher.Verify(request.CurrentPassword, caller.User.PasswordHash))
|
||||
return ApiErrors.BadRequest(context, "wrong_password");
|
||||
if (!PasswordHasher.IsAcceptable(request.NewPassword))
|
||||
return ApiErrors.BadRequest(context, "weak_password", PasswordHasher.MinimumLength.ToString(CultureInfo.InvariantCulture), PasswordHasher.MinimumLength);
|
||||
if (request.NewPassword == request.CurrentPassword) return ApiErrors.BadRequest(context, "same_password");
|
||||
|
||||
var updated = caller.User with { PasswordHash = PasswordHasher.Hash(request.NewPassword), MustChangePassword = false };
|
||||
await db.Users.UpdateAsync(updated, ct);
|
||||
await db.Tokens.RevokeAllExceptAsync(caller.Id, caller.TokenHash, ct);
|
||||
await audit.WriteAsync(caller, context, "password_changed", "user", caller.Id.ToString());
|
||||
if (caller.User.Role == UserRole.Admin) AdminBootstrap.ForgetInitialPassword(options);
|
||||
return Results.NoContent();
|
||||
}
|
||||
|
||||
private static IResult TooMany(HttpContext context, string code, TimeSpan wait)
|
||||
{
|
||||
var seconds = Math.Max(1, (int)Math.Ceiling(wait.TotalSeconds));
|
||||
context.Response.Headers.RetryAfter = seconds.ToString(CultureInfo.InvariantCulture);
|
||||
return ApiErrors.Result(context, StatusCodes.Status429TooManyRequests, code, seconds.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Protocol;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>Domain entities → API v1 contracts.</summary>
|
||||
public static class Dto
|
||||
{
|
||||
public static UserDto User(User u) => new(
|
||||
u.Id, u.Username, u.DisplayName, Role(u.Role), (long)u.Permissions, u.QuotaBytes, u.UsedBytes, u.Disabled, u.MustChangePassword,
|
||||
u.Created, u.LastLogin);
|
||||
|
||||
public static string Role(UserRole role) => role.ToString().ToLowerInvariant();
|
||||
|
||||
public static SequenceDto Sequence(Sequence s) => new(
|
||||
s.Id, s.Title, s.DisplayTitle, s.Folder, s.CameraMake, s.CameraModel, s.Lens, s.FirstCapture, s.LastCapture, s.FrameCount,
|
||||
s.MedianInterval, s.TotalBytes, (long)s.Status, s.RenderedCount, s.HeroFrameId, s.Tags, s.Rating, s.Notes, s.SettingsVersion,
|
||||
s.FramesVersion, s.WarningCount, s.OwnerUserId, s.UpdatedAt);
|
||||
|
||||
public static FrameDto Frame(Frame f) => new(
|
||||
f.Id, f.SequenceId, f.Index, f.FileName, f.Size, f.Sha256, f.CapturedAt, f.UtcOffsetMinutes, f.ExposureTime, f.FNumber, f.Iso,
|
||||
f.FocalLength, f.Orientation, f.Width, f.Height, f.Format.ToString().ToLowerInvariant(), (long)f.Flags);
|
||||
|
||||
public static RenderDto Render(Render r) => new(r.Id, r.PresetName, r.Codec, r.Width, r.Height, r.Fps, r.DurationSeconds, r.Bytes, r.Created, r.ProducedBy);
|
||||
|
||||
public static JobDto Job(Job j) => new(
|
||||
j.Id, JobWire.Kind(j.Kind), j.TargetIds, j.TargetLabel, JobWire.State(j.State), JobWire.Priority(j.Priority), j.ProgressDone,
|
||||
j.ProgressTotal, j.ItemsPerSecond, j.Eta?.TotalSeconds, j.LogTail, j.NodeId, j.Attempts, j.Error, j.Created, j.Started, j.Finished);
|
||||
|
||||
public static AuditEntryDto Audit(AuditEntry a) => new(a.Id, a.At, a.UserId, a.Username, a.Action, a.TargetKind, a.TargetId, a.Details, a.RemoteAddress);
|
||||
|
||||
public static RenderNodeDto Node(RenderNode node, string username) =>
|
||||
new(node.Id, node.ClientName, username, Capabilities(node)?.Encoders ?? [], node.LastHeartbeat, node.State);
|
||||
|
||||
/// <summary>What a node announced at registration (stored as its capabilities JSON).</summary>
|
||||
public static RegisterNodeRequest? Capabilities(RenderNode node)
|
||||
{
|
||||
if (node.CapabilitiesJson is null) return null;
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize(node.CapabilitiesJson, ProtocolJson.Default.RegisterNodeRequest);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Wire names of job kinds, states and priorities: kebab-case (<c>deep-analyse</c>) and lower-case (<c>running</c>).</summary>
|
||||
public static class JobWire
|
||||
{
|
||||
public static string Kind(JobKind kind)
|
||||
{
|
||||
var name = kind.ToString();
|
||||
var sb = new StringBuilder(name.Length + 4);
|
||||
for (var i = 0; i < name.Length; i++)
|
||||
{
|
||||
if (i > 0 && char.IsUpper(name[i])) sb.Append('-');
|
||||
sb.Append(char.ToLowerInvariant(name[i]));
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static bool TryParseKind(string? text, out JobKind kind) => TryParse(text, Kind, out kind);
|
||||
|
||||
public static string State(JobState state) => state.ToString().ToLowerInvariant();
|
||||
|
||||
public static bool TryParseState(string? text, out JobState state) => TryParse(text, State, out state);
|
||||
|
||||
public static string Priority(JobPriority priority) => priority.ToString().ToLowerInvariant();
|
||||
|
||||
public static bool TryParsePriority(string? text, out JobPriority priority) => TryParse(text, Priority, out priority);
|
||||
|
||||
private static bool TryParse<T>(string? text, Func<T, string> name, out T value) where T : struct, Enum
|
||||
{
|
||||
foreach (var candidate in Enum.GetValues<T>())
|
||||
{
|
||||
if (string.Equals(name(candidate), text, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
value = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Globalization;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// Streamed archive files with Range support (BRIEF §14.3): originals, proxies, thumbnails and exports. Paths come
|
||||
/// from the database by id and are resolved under the archive root only.
|
||||
/// </summary>
|
||||
public static class FileEndpoints
|
||||
{
|
||||
private const string Immutable = "private, max-age=31536000, immutable";
|
||||
private const string Regenerable = "private, max-age=3600";
|
||||
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
// Originals feed editing clients and render nodes; viewers browse proxies, thumbnails and exports.
|
||||
api.MapGet("/frames/{id:guid}/original", OriginalAsync).Requires(Permissions.Edit | Permissions.RenderNode);
|
||||
api.MapGet("/frames/{id:guid}/proxy", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Proxy, db, a, l))
|
||||
.Requires(Permissions.Read | Permissions.RenderNode);
|
||||
api.MapGet("/frames/{id:guid}/thumb", (HttpContext c, Guid id, TitanoDatabase db, ServerArchive a, IFrameAssetLocator l) => AssetAsync(c, id, FrameAsset.Thumbnail, db, a, l))
|
||||
.Requires(Permissions.Read);
|
||||
api.MapGet("/renders/{id:guid}/file", RenderAsync).Requires(Permissions.Read);
|
||||
}
|
||||
|
||||
private static async Task<IResult> OriginalAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive)
|
||||
{
|
||||
if (await db.Frames.GetAsync(id, context.RequestAborted) is not { } frame) return ApiErrors.NotFound(context, "frame");
|
||||
if (ArchivePaths.ExistingFile(archive.Root, frame.RelativePath) is not { } file) return ApiErrors.NotFound(context, "file_missing");
|
||||
return FileResponses.Serve(context, file, 0, file.Length, FileResponses.ContentTypeOf(frame.FileName), Immutable, frame.Sha256, frame.FileName)
|
||||
?? ApiErrors.NotFound(context, "file_missing");
|
||||
}
|
||||
|
||||
private static async Task<IResult> AssetAsync(HttpContext context, Guid id, FrameAsset kind, TitanoDatabase db, ServerArchive archive, IFrameAssetLocator locator)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.Frames.GetAsync(id, ct) is not { } frame || await db.Sequences.GetAsync(frame.SequenceId, ct) is not { } sequence)
|
||||
return ApiErrors.NotFound(context, "frame");
|
||||
var location = await locator.LocateAsync(archive.Current, sequence, frame, kind, ct);
|
||||
if (location is null || !ArchivePaths.IsInside(archive.Root, location.Path)) return ApiErrors.NotFound(context, "asset_missing");
|
||||
var file = new FileInfo(location.Path);
|
||||
if (!file.Exists || file.LinkTarget is not null) return ApiErrors.NotFound(context, "asset_missing");
|
||||
var etag = string.Create(CultureInfo.InvariantCulture, $"{kind}-{frame.Id:N}-{file.LastWriteTimeUtc.Ticks}-{location.Offset}");
|
||||
return FileResponses.Serve(context, file, location.Offset, location.Length, location.ContentType, Regenerable, etag)
|
||||
?? ApiErrors.NotFound(context, "asset_missing");
|
||||
}
|
||||
|
||||
private static async Task<IResult> RenderAsync(HttpContext context, Guid id, TitanoDatabase db, ServerArchive archive)
|
||||
{
|
||||
if (await db.Renders.GetAsync(id, context.RequestAborted) is not { } render) return ApiErrors.NotFound(context, "render");
|
||||
if (ArchivePaths.ExistingFile(archive.Root, render.OutputPath) is not { } file) return ApiErrors.NotFound(context, "file_missing");
|
||||
var etag = string.Create(CultureInfo.InvariantCulture, $"r-{render.Id:N}-{file.Length}-{file.LastWriteTimeUtc.Ticks}");
|
||||
return FileResponses.Serve(context, file, 0, file.Length, FileResponses.ContentTypeOf(file.Name), Immutable, etag, file.Name)
|
||||
?? ApiErrors.NotFound(context, "file_missing");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
using System.Text.Json;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Jobs;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// <c>/api/v1/jobs</c> (BRIEF §14.6): queue and history, creation, cancel, pause, resume, retry and priority. Changes
|
||||
/// reach WebSocket clients as <c>jobState</c> and <c>jobProgress</c> events.
|
||||
/// </summary>
|
||||
public static class JobEndpoints
|
||||
{
|
||||
/// <summary>Kinds that work on sequences (targets are sequence ids) and need the edit right.</summary>
|
||||
private static readonly HashSet<JobKind> SequenceKinds = [JobKind.LightAnalyse, JobKind.DeepAnalyse, JobKind.Proxies, JobKind.Thumbs, JobKind.Verify];
|
||||
|
||||
/// <summary>Archive-wide maintenance (target: the archive) for administrators.</summary>
|
||||
private static readonly HashSet<JobKind> ArchiveKinds = [JobKind.Scan, JobKind.Rebuild, JobKind.Maintenance, JobKind.Duplicates, JobKind.Orphans];
|
||||
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var jobs = api.MapGroup("/jobs").Requires(Permissions.Read);
|
||||
jobs.MapGet("", ListAsync);
|
||||
jobs.MapPost("", CreateAsync);
|
||||
jobs.MapGet("/{id:guid}", async (HttpContext context, Guid id, TitanoDatabase db) =>
|
||||
await db.Jobs.GetAsync(id, context.RequestAborted) is { } job
|
||||
? Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto)
|
||||
: ApiErrors.NotFound(context, "job"));
|
||||
jobs.MapPost("/{id:guid}/cancel", (HttpContext c, Caller caller, Guid id, TitanoDatabase db, JobEngineHost host) =>
|
||||
ActAsync(c, caller, id, db, [JobState.Queued, JobState.Running, JobState.Paused], (job, ct) => host.Engine.CancelAsync(job.Id, ct)));
|
||||
jobs.MapPost("/{id:guid}/pause", (HttpContext c, Caller caller, Guid id, TitanoDatabase db, JobEngineHost host) =>
|
||||
ActAsync(c, caller, id, db, [JobState.Queued, JobState.Running], (job, ct) => host.Engine.PauseAsync(job.Id, ct)));
|
||||
jobs.MapPost("/{id:guid}/resume", (HttpContext c, Caller caller, Guid id, TitanoDatabase db, JobEngineHost host) =>
|
||||
ActAsync(c, caller, id, db, [JobState.Paused], (job, ct) => host.Engine.ResumeAsync(job.Id, ct)));
|
||||
jobs.MapPost("/{id:guid}/retry", (HttpContext c, Caller caller, Guid id, TitanoDatabase db, JobEngineHost host) =>
|
||||
ActAsync(c, caller, id, db, [JobState.Failed, JobState.Cancelled], (job, ct) => host.Engine.ResumeAsync(job.Id, ct)));
|
||||
jobs.MapPost("/{id:guid}/priority", PriorityAsync);
|
||||
}
|
||||
|
||||
private static async Task<IResult> ListAsync(HttpContext context, TitanoDatabase db, string? state, string? kind, string? cursor, int? limit)
|
||||
{
|
||||
JobState? stateFilter = null;
|
||||
JobKind? kindFilter = null;
|
||||
if (!string.IsNullOrEmpty(state))
|
||||
{
|
||||
if (!JobWire.TryParseState(state, out var s)) return ApiErrors.BadRequest(context, "invalid_filter", "state");
|
||||
stateFilter = s;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(kind))
|
||||
{
|
||||
if (!JobWire.TryParseKind(kind, out var k)) return ApiErrors.BadRequest(context, "invalid_filter", "kind");
|
||||
kindFilter = k;
|
||||
}
|
||||
var page = await db.Jobs.ListAsync(stateFilter, kindFilter, limit ?? 50, string.IsNullOrEmpty(cursor) ? null : cursor, context.RequestAborted);
|
||||
return Results.Json(new Page<JobDto>([.. page.Items.Select(Dto.Job)], page.NextCursor, null), ProtocolJson.Default.PageJobDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> CreateAsync(HttpContext context, Caller caller, CreateJobRequest request, TitanoDatabase db, JobEngineHost host,
|
||||
ServerArchive archive, AuditLog audit)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (!JobWire.TryParseKind(request.Kind, out var kind) || !(kind == JobKind.Render || SequenceKinds.Contains(kind) || ArchiveKinds.Contains(kind)))
|
||||
return ApiErrors.BadRequest(context, "invalid_job_kind");
|
||||
var needed = kind == JobKind.Render ? Permissions.Render : SequenceKinds.Contains(kind) ? Permissions.Edit : Permissions.ManageArchive;
|
||||
if (!caller.Has(needed)) return ApiErrors.Forbidden(context);
|
||||
if (!host.CanRun(kind)) return ApiErrors.Conflict(context, "job_unavailable", JobWire.Kind(kind));
|
||||
|
||||
IReadOnlyList<Guid> targets;
|
||||
string label;
|
||||
if (ArchiveKinds.Contains(kind))
|
||||
{
|
||||
targets = [archive.Id];
|
||||
label = archive.Current.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
var ids = request.Target ?? [];
|
||||
if (ids.Count == 0 || (kind == JobKind.Render && ids.Count != 1)) return ApiErrors.BadRequest(context, "invalid_parameters", "target");
|
||||
var titles = new List<string>(ids.Count);
|
||||
foreach (var id in ids)
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
|
||||
titles.Add(sequence.DisplayTitle);
|
||||
}
|
||||
targets = ids;
|
||||
label = titles.Count == 1 ? titles[0] : string.Join(", ", titles.Take(3)) + (titles.Count > 3 ? " …" : string.Empty);
|
||||
}
|
||||
if (kind == JobKind.Render)
|
||||
{
|
||||
if (ParseRender(request.ParametersJson) is not { } render) return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
|
||||
label = $"{label} → {render.PresetName}";
|
||||
}
|
||||
else if (request.ParametersJson is not null && !IsJsonObject(request.ParametersJson))
|
||||
{
|
||||
return ApiErrors.BadRequest(context, "invalid_parameters", "parametersJson");
|
||||
}
|
||||
|
||||
var job = await host.Engine.EnqueueAsync(kind, targets, label, request.ParametersJson, JobPriority.User, caller.Id, ct);
|
||||
await audit.WriteAsync(caller, context, "job_created", "job", job.Id.ToString(), $"{JobWire.Kind(kind)} · {label}");
|
||||
return Results.Json(Dto.Job(job), ProtocolJson.Default.JobDto, statusCode: StatusCodes.Status202Accepted);
|
||||
}
|
||||
|
||||
private static async Task<IResult> PriorityAsync(HttpContext context, Caller caller, Guid id, JobPriorityRequest request, TitanoDatabase db, JobEngineHost host)
|
||||
{
|
||||
if (!JobWire.TryParsePriority(request.Priority, out var priority)) return ApiErrors.BadRequest(context, "invalid_priority");
|
||||
return await ActAsync(context, caller, id, db, [JobState.Queued, JobState.Paused], async (job, ct) =>
|
||||
{
|
||||
await db.Jobs.SetPriorityAsync(job.Id, priority, ct);
|
||||
host.Publish(job with { Priority = priority });
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>Applies <paramref name="action"/> when the job is in one of <paramref name="allowed"/> states and the caller created it or administers the archive.</summary>
|
||||
private static async Task<IResult> ActAsync(HttpContext context, Caller caller, Guid id, TitanoDatabase db, JobState[] allowed, Func<Job, CancellationToken, Task> action)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.Jobs.GetAsync(id, ct) is not { } job) return ApiErrors.NotFound(context, "job");
|
||||
if (job.CreatedBy != caller.Id && !caller.Has(Permissions.ManageArchive)) return ApiErrors.Forbidden(context);
|
||||
if (!allowed.Contains(job.State)) return ApiErrors.Conflict(context, "invalid_state", JobWire.State(job.State));
|
||||
await action(job, ct);
|
||||
var updated = await db.Jobs.GetAsync(id, ct) ?? job;
|
||||
return Results.Json(Dto.Job(updated), ProtocolJson.Default.JobDto);
|
||||
}
|
||||
|
||||
/// <summary>Render parameters, or null when they are missing or incomplete.</summary>
|
||||
public static RenderJobParameters? ParseRender(string? json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(json)) return null;
|
||||
try
|
||||
{
|
||||
var p = JsonSerializer.Deserialize(json, ProtocolJson.Default.RenderJobParameters);
|
||||
return p is { PresetName.Length: > 0, PresetJson.Length: > 0, Codec.Length: > 0, Width: > 0, Height: > 0 } && IsJsonObject(p.PresetJson) ? p : null;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsJsonObject(string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var document = JsonDocument.Parse(json);
|
||||
return document.RootElement.ValueKind == JsonValueKind.Object;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Events;
|
||||
using Titano.Server.Jobs;
|
||||
using Titano.Server.Security;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary><c>/api/v1/nodes</c> (BRIEF §14.7): render node registration, heartbeat, claim, progress, failure and output upload.</summary>
|
||||
public static class NodeEndpoints
|
||||
{
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
api.MapGet("/nodes", ListAsync).Requires(Permissions.Read | Permissions.RenderNode);
|
||||
api.MapDelete("/nodes/{id:guid}", DeleteAsync).Requires(Permissions.RenderNode | Permissions.ManageArchive);
|
||||
|
||||
var nodes = api.MapGroup("/nodes").Requires(Permissions.RenderNode);
|
||||
nodes.MapPost("/register", async (HttpContext c, Caller caller, RegisterNodeRequest request, RenderNodeService service) =>
|
||||
(await service.RegisterAsync(caller, request, c.RequestAborted)).ToResult(c, n => Results.Json(n, ProtocolJson.Default.RenderNodeDto)));
|
||||
nodes.MapPost("/{id:guid}/heartbeat", async (HttpContext c, Caller caller, Guid id, RenderNodeService service) =>
|
||||
(await service.HeartbeatAsync(caller, id, c.RequestAborted)).ToResult(c, _ => Results.NoContent()));
|
||||
nodes.MapPost("/{id:guid}/claim", async (HttpContext c, Caller caller, Guid id, RenderNodeService service) =>
|
||||
(await service.ClaimAsync(caller, id, c.RequestAborted)).ToResult(c, job => job is null ? Results.NoContent() : Results.Json(job, ProtocolJson.Default.ClaimedJob)));
|
||||
nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/progress", async (HttpContext c, Caller caller, Guid id, Guid jobId, NodeProgress progress, RenderNodeService service) =>
|
||||
(await service.ProgressAsync(caller, id, jobId, progress, c.RequestAborted)).ToResult(c, _ => Results.NoContent()));
|
||||
nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/failed", async (HttpContext c, Caller caller, Guid id, Guid jobId, NodeJobFailure failure, RenderNodeService service) =>
|
||||
(await service.FailAsync(caller, id, jobId, failure, c.RequestAborted)).ToResult(c, _ => Results.NoContent()));
|
||||
nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/result", async (HttpContext c, Caller caller, Guid id, Guid jobId, RenderResultRequest request, RenderNodeService service) =>
|
||||
(await service.StartResultAsync(caller, id, jobId, request, c.RequestAborted))
|
||||
.ToResult(c, r => Results.Json(r, ProtocolJson.Default.StartUploadResponse, statusCode: StatusCodes.Status201Created)));
|
||||
nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/result/complete", async (HttpContext c, Caller caller, Guid id, Guid jobId, RenderNodeService service,
|
||||
IHostApplicationLifetime lifetime) =>
|
||||
// The commit runs to the end even if the node disconnects meanwhile.
|
||||
(await service.CompleteResultAsync(caller, id, jobId, lifetime.ApplicationStopping)).ToResult(c, r => Results.Json(r, ProtocolJson.Default.RenderDto)));
|
||||
}
|
||||
|
||||
private static async Task<IResult> ListAsync(HttpContext context, TitanoDatabase db)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
var users = (await db.Users.ListAsync(ct)).ToDictionary(u => u.Id, u => u.Username);
|
||||
var nodes = await db.RenderNodes.ListAsync(ct);
|
||||
return Results.Json(nodes.Select(n => Dto.Node(n, users.GetValueOrDefault(n.UserId, string.Empty))).ToList(), ProtocolJson.Default.ListRenderNodeDto);
|
||||
}
|
||||
|
||||
/// <summary>Forgets a node (its owner or an archive admin); its running jobs go back to the queue.</summary>
|
||||
private static async Task<IResult> DeleteAsync(HttpContext context, Caller caller, Guid id, TitanoDatabase db, EventHub events)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.RenderNodes.GetAsync(id, ct) is not { } node) return ApiErrors.NotFound(context, "node");
|
||||
if (node.UserId != caller.Id && !caller.Has(Permissions.ManageArchive)) return ApiErrors.Forbidden(context);
|
||||
await db.Jobs.RequeueRunningAsync(RenderNodeService.NodeKey(node), ct);
|
||||
await db.RenderNodes.DeleteAsync(id, ct);
|
||||
events.Publish(new NodeStateEvent(node.Id, node.ClientName, RenderNodeService.Offline));
|
||||
return Results.NoContent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>Result of a server operation: a value, or an HTTP status with an API error code (and optional details).</summary>
|
||||
public readonly record struct Outcome<T>(T? Value, int Status, string? Code, string? Details)
|
||||
{
|
||||
public bool Succeeded => Code is null;
|
||||
|
||||
public static Outcome<T> Ok(T value) => new(value, StatusCodes.Status200OK, null, null);
|
||||
|
||||
public static Outcome<T> Fail(int status, string code, string? details = null) => new(default, status, code, details);
|
||||
|
||||
/// <summary>The API response: <paramref name="ok"/> for a value, the error body otherwise.</summary>
|
||||
public IResult ToResult(HttpContext context, Func<T, IResult> ok) =>
|
||||
Succeeded ? ok(Value!) : ApiErrors.Result(context, Status, Code!, Details);
|
||||
|
||||
public static implicit operator Outcome<T>(T value) => Ok(value);
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
using System.Buffers.Text;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Repositories;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Events;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// <c>/api/v1/sequences</c>: library listing with filters and keyset paging, detail with ETag, edits with
|
||||
/// <c>settings_version</c> optimistic concurrency, the sync manifest, frames, analysis, statistics and renders.
|
||||
/// </summary>
|
||||
public static class SequenceEndpoints
|
||||
{
|
||||
private const int MaxTitle = 200;
|
||||
private const int MaxNotes = 2000;
|
||||
private const int MaxTags = 50;
|
||||
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
// Uploaders list sequences to pick an upload target.
|
||||
var sequences = api.MapGroup("/sequences").Requires(Permissions.Read | Permissions.Upload);
|
||||
sequences.MapGet("", ListAsync);
|
||||
sequences.MapPost("", CreateAsync).Requires(Permissions.Upload | Permissions.Edit);
|
||||
sequences.MapGet("/{id:guid}", GetAsync);
|
||||
sequences.MapPatch("/{id:guid}", UpdateAsync).Requires(Permissions.Edit);
|
||||
sequences.MapDelete("/{id:guid}", DeleteAsync).Requires(Permissions.Delete);
|
||||
sequences.MapGet("/{id:guid}/manifest", ManifestAsync).Requires(Permissions.Read);
|
||||
sequences.MapGet("/{id:guid}/frames", FramesAsync).Requires(Permissions.Read);
|
||||
sequences.MapGet("/{id:guid}/analysis", AnalysisAsync).Requires(Permissions.Read);
|
||||
sequences.MapGet("/{id:guid}/stats", StatsAsync).Requires(Permissions.Read);
|
||||
sequences.MapGet("/{id:guid}/renders", RendersAsync).Requires(Permissions.Read);
|
||||
}
|
||||
|
||||
private static async Task<IResult> ListAsync(HttpContext context, TitanoDatabase db, ServerArchive archive, int? year, string? camera, string? status,
|
||||
string? tag, string? q, string? owner, string? sort, string? cursor, int? limit, Caller caller)
|
||||
{
|
||||
if (!TryParseStatus(status, out var with, out var without)) return ApiErrors.BadRequest(context, "invalid_filter", "status");
|
||||
if (!TryParseSort(sort, out var order)) return ApiErrors.BadRequest(context, "invalid_filter", "sort");
|
||||
Guid? ownerId = null;
|
||||
if (owner == "me") ownerId = caller.Id;
|
||||
else if (owner is not null && Guid.TryParse(owner, out var parsed)) ownerId = parsed;
|
||||
else if (owner is not null) return ApiErrors.BadRequest(context, "invalid_filter", "owner");
|
||||
|
||||
var query = new SequenceQuery
|
||||
{
|
||||
ArchiveId = archive.Id,
|
||||
Year = year,
|
||||
Camera = string.IsNullOrWhiteSpace(camera) ? null : camera,
|
||||
WithStatus = with,
|
||||
WithoutStatus = without,
|
||||
Tag = string.IsNullOrWhiteSpace(tag) ? null : tag,
|
||||
Text = string.IsNullOrWhiteSpace(q) ? null : q,
|
||||
OwnerUserId = ownerId,
|
||||
Sort = order,
|
||||
Limit = limit ?? 100,
|
||||
Cursor = string.IsNullOrEmpty(cursor) ? null : cursor,
|
||||
};
|
||||
var page = await db.Sequences.ListAsync(query, context.RequestAborted);
|
||||
long? total = query.Cursor is null ? await db.Sequences.CountAsync(query, context.RequestAborted) : null;
|
||||
return Results.Json(new Titano.Protocol.Page<SequenceDto>([.. page.Items.Select(Dto.Sequence)], page.NextCursor, total), ProtocolJson.Default.PageSequenceDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> CreateAsync(HttpContext context, Caller caller, CreateSequenceRequest request, TitanoDatabase db, ServerArchive archive,
|
||||
AuditLog audit, EventHub events, TimeProvider time)
|
||||
{
|
||||
var title = request.Title?.Trim();
|
||||
if (title is { Length: > MaxTitle }) return ApiErrors.BadRequest(context, "bad_request", "title");
|
||||
var hint = request.CameraHint?.Trim();
|
||||
var sequence = new Sequence
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
ArchiveId = archive.Id,
|
||||
Title = string.IsNullOrEmpty(title) ? null : title,
|
||||
// Placed in the archive by the ingest when its first frame arrives.
|
||||
Folder = string.Empty,
|
||||
CameraModel = string.IsNullOrEmpty(hint) ? null : hint[..Math.Min(hint.Length, MaxTitle)],
|
||||
Origin = SequenceOrigin.Server,
|
||||
SyncState = SyncState.InSync,
|
||||
OwnerUserId = caller.Id,
|
||||
UpdatedAt = time.GetUtcNow(),
|
||||
};
|
||||
await db.Sequences.UpsertAsync(sequence, context.RequestAborted);
|
||||
await audit.WriteAsync(caller, context, "sequence_created", "sequence", sequence.Id.ToString(), sequence.DisplayTitle);
|
||||
events.Publish(new SequenceChangedEvent(sequence.Id, "created", sequence.FramesVersion, sequence.SettingsVersion));
|
||||
context.Response.Headers.ETag = ETag(sequence);
|
||||
return Results.Json(Dto.Sequence(sequence), ProtocolJson.Default.SequenceDto, statusCode: StatusCodes.Status201Created);
|
||||
}
|
||||
|
||||
private static async Task<IResult> GetAsync(HttpContext context, Guid id, TitanoDatabase db)
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, context.RequestAborted) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
|
||||
var etag = ETag(sequence);
|
||||
if (context.Request.Headers.IfNoneMatch == etag) return Results.StatusCode(StatusCodes.Status304NotModified);
|
||||
context.Response.Headers.ETag = etag;
|
||||
return Results.Json(Dto.Sequence(sequence), ProtocolJson.Default.SequenceDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> UpdateAsync(HttpContext context, Caller caller, Guid id, UpdateSequenceRequest request, TitanoDatabase db,
|
||||
AuditLog audit, EventHub events)
|
||||
{
|
||||
if (request.Title is { Length: > MaxTitle }) return ApiErrors.BadRequest(context, "bad_request", "title");
|
||||
if (request.Notes is { Length: > MaxNotes }) return ApiErrors.BadRequest(context, "bad_request", "notes");
|
||||
if (request.Rating is < 0 or > 5) return ApiErrors.BadRequest(context, "bad_request", "rating");
|
||||
if (request.Tags is { } tags && (tags.Count > MaxTags || tags.Any(t => t is null || t.Trim().Length > TagRepository.MaxLength)))
|
||||
return ApiErrors.BadRequest(context, "bad_request", "tags");
|
||||
if (request.SettingsJson is not null && !IsJson(request.SettingsJson)) return ApiErrors.BadRequest(context, "invalid_settings");
|
||||
|
||||
var expected = request.ExpectedSettingsVersion ?? IfMatchVersion(context);
|
||||
var edit = new SequenceEdit { Title = request.Title, Tags = request.Tags, Rating = request.Rating, Notes = request.Notes, SettingsJson = request.SettingsJson };
|
||||
var result = await db.Sequences.EditAsync(id, edit, expected, context.RequestAborted);
|
||||
switch (result.Outcome)
|
||||
{
|
||||
case SequenceEditOutcome.NotFound:
|
||||
return ApiErrors.NotFound(context, "sequence");
|
||||
case SequenceEditOutcome.Conflict:
|
||||
context.Response.Headers.ETag = ETag(result.Sequence!);
|
||||
return ApiErrors.Conflict(context, "settings_conflict", result.Sequence!.SettingsVersion.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
var sequence = result.Sequence!;
|
||||
await audit.WriteAsync(caller, context, "sequence_updated", "sequence", id.ToString(), Changes(request));
|
||||
events.Publish(new SequenceChangedEvent(id, "settings", sequence.FramesVersion, sequence.SettingsVersion));
|
||||
context.Response.Headers.ETag = ETag(sequence);
|
||||
return Results.Json(Dto.Sequence(sequence), ProtocolJson.Default.SequenceDto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a sequence from the index and moves its folder into the archive's trash (BRIEF §0.2 rule 8: nothing is
|
||||
/// erased). <c>?dryRun=true</c> only reports what would happen. Owners and admins only.
|
||||
/// </summary>
|
||||
private static async Task<IResult> DeleteAsync(HttpContext context, Caller caller, Guid id, bool? dryRun, TitanoDatabase db, ServerArchive archive,
|
||||
ServerOptions options, AuditLog audit, EventHub events)
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, context.RequestAborted) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
|
||||
if (!caller.IsAdmin && sequence.OwnerUserId != caller.Id) return ApiErrors.Forbidden(context);
|
||||
var folder = string.IsNullOrEmpty(sequence.Folder) ? null : ArchivePaths.Resolve(archive.Root, sequence.Folder);
|
||||
var trash = Path.Combine(options.TrashPath, id.ToString("N"));
|
||||
var hasFolder = folder is not null && Directory.Exists(folder);
|
||||
var plan = new SequenceDeletePlan(id, sequence.Folder, hasFolder ? Path.GetRelativePath(archive.Root, trash).Replace('\\', '/') : null,
|
||||
sequence.FrameCount, sequence.TotalBytes, sequence.RenderedCount, dryRun == true);
|
||||
if (dryRun == true) return Results.Json(plan, ProtocolJson.Default.SequenceDeletePlan);
|
||||
|
||||
if (hasFolder)
|
||||
{
|
||||
Directory.CreateDirectory(options.TrashPath);
|
||||
Directory.Move(folder!, trash);
|
||||
}
|
||||
await db.Sequences.DeleteAsync(id, context.RequestAborted);
|
||||
await audit.WriteAsync(caller, context, "sequence_deleted", "sequence", id.ToString(), sequence.DisplayTitle);
|
||||
events.Publish(new SequenceChangedEvent(id, "deleted", sequence.FramesVersion, sequence.SettingsVersion));
|
||||
return Results.Json(plan, ProtocolJson.Default.SequenceDeletePlan);
|
||||
}
|
||||
|
||||
/// <summary>Everything a client needs to mirror a sequence and detect changes (BRIEF §14.5); ETag over the version counters.</summary>
|
||||
private static async Task<IResult> ManifestAsync(HttpContext context, Guid id, TitanoDatabase db)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.Sequences.GetAsync(id, ct) is not { } sequence) return ApiErrors.NotFound(context, "sequence");
|
||||
var etag = string.Create(CultureInfo.InvariantCulture, $"\"m{sequence.SettingsVersion}-{sequence.FramesVersion}-{sequence.RenderedCount}\"");
|
||||
if (context.Request.Headers.IfNoneMatch == etag) return Results.StatusCode(StatusCodes.Status304NotModified);
|
||||
|
||||
var hasProxy = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt);
|
||||
var hasThumb = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt);
|
||||
var frames = new List<ManifestFrame>(sequence.FrameCount);
|
||||
var after = -1;
|
||||
while (true)
|
||||
{
|
||||
var batch = await db.Frames.ListBySequenceAsync(id, after, 1000, ct);
|
||||
frames.AddRange(batch.Select(f => new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, hasProxy, hasThumb)));
|
||||
if (batch.Count < 1000) break;
|
||||
after = batch[^1].Index;
|
||||
}
|
||||
var renders = await db.Renders.ListBySequenceAsync(id, ct);
|
||||
context.Response.Headers.ETag = etag;
|
||||
return Results.Json(new SequenceManifest(id, sequence.FramesVersion, sequence.SettingsVersion, sequence.SettingsJson, sequence.Tags, frames,
|
||||
[.. renders.Select(Dto.Render)]), ProtocolJson.Default.SequenceManifest);
|
||||
}
|
||||
|
||||
private static async Task<IResult> FramesAsync(HttpContext context, Guid id, string? cursor, int? limit, TitanoDatabase db)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.Sequences.GetAsync(id, ct) is null) return ApiErrors.NotFound(context, "sequence");
|
||||
var after = -1;
|
||||
if (!string.IsNullOrEmpty(cursor) && !FrameCursor.TryDecode(cursor, out after)) return ApiErrors.BadRequest(context, "invalid_cursor");
|
||||
var take = Math.Clamp(limit ?? 500, 1, 1000);
|
||||
var frames = await db.Frames.ListBySequenceAsync(id, after, take, ct);
|
||||
var next = frames.Count == take ? FrameCursor.Encode(frames[^1].Index) : null;
|
||||
return Results.Json(new Titano.Protocol.Page<FrameDto>([.. frames.Select(Dto.Frame)], next, null), ProtocolJson.Default.PageFrameDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> AnalysisAsync(HttpContext context, Guid id, TitanoDatabase db) =>
|
||||
await db.Sequences.GetAsync(id, context.RequestAborted) switch
|
||||
{
|
||||
null => ApiErrors.NotFound(context, "sequence"),
|
||||
{ AnalysisJson: { } json } => Results.Text(json, "application/json; charset=utf-8"),
|
||||
_ => ApiErrors.NotFound(context, "analysis"),
|
||||
};
|
||||
|
||||
private static async Task<IResult> StatsAsync(HttpContext context, Guid id, TitanoDatabase db)
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, context.RequestAborted) is not { } s) return ApiErrors.NotFound(context, "sequence");
|
||||
var stats = await db.Frames.GetSequenceStatsAsync(id, context.RequestAborted);
|
||||
return Results.Json(new SequenceStatsDto(
|
||||
id, s.FrameCount, s.TotalBytes, s.FirstCapture, s.LastCapture, s.MedianInterval,
|
||||
[.. stats.ByFormat.Select(f => new KeyValueCount(f.Format.ToString().ToLowerInvariant(), f.Photos, f.Bytes))],
|
||||
stats.Missing, stats.Corrupt, stats.Duplicate, stats.Excluded), ProtocolJson.Default.SequenceStatsDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> RendersAsync(HttpContext context, Guid id, TitanoDatabase db)
|
||||
{
|
||||
if (await db.Sequences.GetAsync(id, context.RequestAborted) is null) return ApiErrors.NotFound(context, "sequence");
|
||||
var renders = await db.Renders.ListBySequenceAsync(id, context.RequestAborted);
|
||||
return Results.Json(renders.Select(Dto.Render).ToList(), ProtocolJson.Default.ListRenderDto);
|
||||
}
|
||||
|
||||
/// <summary>Changes whenever anything the sequence representation shows changes (versions, status, statistics).</summary>
|
||||
private static string ETag(Sequence s) =>
|
||||
string.Create(CultureInfo.InvariantCulture, $"\"{s.SettingsVersion}-{s.FramesVersion}-{s.UpdatedAt.UtcTicks}\"");
|
||||
|
||||
/// <summary>The settings version named by an <c>If-Match</c> ETag of <see cref="ETag"/>, if any.</summary>
|
||||
private static int? IfMatchVersion(HttpContext context)
|
||||
{
|
||||
var header = context.Request.Headers.IfMatch.ToString().Trim('"', ' ');
|
||||
var dash = header.IndexOf('-', StringComparison.Ordinal);
|
||||
return dash > 0 && int.TryParse(header.AsSpan(0, dash), NumberStyles.None, CultureInfo.InvariantCulture, out var version) ? version : null;
|
||||
}
|
||||
|
||||
private static bool TryParseStatus(string? text, out SequenceStatus with, out SequenceStatus without)
|
||||
{
|
||||
with = without = SequenceStatus.None;
|
||||
if (string.IsNullOrWhiteSpace(text)) return true;
|
||||
foreach (var part in text.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
|
||||
{
|
||||
var clear = part.StartsWith('-');
|
||||
var name = clear ? part[1..] : part;
|
||||
var flag = Enum.GetValues<SequenceStatus>().FirstOrDefault(v => v != SequenceStatus.None && v.ToString().Equals(name, StringComparison.OrdinalIgnoreCase));
|
||||
if (flag == SequenceStatus.None) return false;
|
||||
if (clear) without |= flag;
|
||||
else with |= flag;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryParseSort(string? text, out SequenceSort sort)
|
||||
{
|
||||
sort = SequenceSort.Newest;
|
||||
if (string.IsNullOrEmpty(text)) return true;
|
||||
foreach (var candidate in Enum.GetValues<SequenceSort>())
|
||||
{
|
||||
if (candidate.ToString().Equals(text, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
sort = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsJson(string text)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var _ = JsonDocument.Parse(text);
|
||||
return true;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static string Changes(UpdateSequenceRequest r)
|
||||
{
|
||||
var fields = new List<string>();
|
||||
if (r.Title is not null) fields.Add("title");
|
||||
if (r.Tags is not null) fields.Add("tags");
|
||||
if (r.Rating is not null) fields.Add("rating");
|
||||
if (r.Notes is not null) fields.Add("notes");
|
||||
if (r.SettingsJson is not null) fields.Add("settings");
|
||||
return string.Join(", ", fields);
|
||||
}
|
||||
|
||||
/// <summary>Opaque cursor of the frame listing: the index of the last frame returned.</summary>
|
||||
private static class FrameCursor
|
||||
{
|
||||
private const string Prefix = "frames|";
|
||||
|
||||
public static string Encode(int index) =>
|
||||
Base64Url.EncodeToString(Encoding.UTF8.GetBytes(Prefix + index.ToString(CultureInfo.InvariantCulture)));
|
||||
|
||||
public static bool TryDecode(string cursor, out int index)
|
||||
{
|
||||
index = -1;
|
||||
try
|
||||
{
|
||||
var text = Encoding.UTF8.GetString(Base64Url.DecodeFromChars(cursor));
|
||||
return text.StartsWith(Prefix, StringComparison.Ordinal)
|
||||
&& int.TryParse(text.AsSpan(Prefix.Length), NumberStyles.None, CultureInfo.InvariantCulture, out index);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +1,48 @@
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using Titano.Core.Localization;
|
||||
using Titano.Data;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Security;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary><c>/health</c>, <c>/api/v1/server/info</c> and <c>/api/v1/i18n/{culture}</c>.</summary>
|
||||
/// <summary><c>/health</c>, <c>/api/v1/server/info</c> and <c>/api/v1/i18n/{culture}</c> (all anonymous).</summary>
|
||||
public static class SystemEndpoints
|
||||
{
|
||||
private static readonly string[] AdminAreas = ["Common", "Admin"];
|
||||
|
||||
/// <summary>Languages with translated resources (English plus every satellite of the catalog), for the admin language menu.</summary>
|
||||
private static readonly Lazy<string> AdminLanguages = new(() => string.Join(',', Languages.Shipped.Where(code =>
|
||||
{
|
||||
if (code == "en") return true;
|
||||
try
|
||||
{
|
||||
typeof(Loc).Assembly.GetSatelliteAssembly(CultureInfo.GetCultureInfo(code));
|
||||
return true;
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
})));
|
||||
|
||||
public static string Version { get; } =
|
||||
typeof(SystemEndpoints).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "0.0.0";
|
||||
|
||||
public static void Map(WebApplication app, ServerOptions options, ServerState state)
|
||||
public static void Map(WebApplication app, RouteGroupBuilder api)
|
||||
{
|
||||
app.MapGet("/health", () =>
|
||||
{
|
||||
var checks = new Dictionary<string, string>(StringComparer.Ordinal)
|
||||
{
|
||||
["archive"] = Directory.Exists(options.ArchivePath) ? "ok" : "missing",
|
||||
["data"] = Directory.Exists(options.DataPath) ? "ok" : "missing",
|
||||
};
|
||||
return Results.Json(new HealthStatus("ok", Version, checks), ProtocolJson.Default.HealthStatus);
|
||||
});
|
||||
app.MapGet("/health", HealthAsync);
|
||||
|
||||
var api = app.MapGroup("/api/v1");
|
||||
api.MapGet("/server/info", () => Results.Json(new ServerInfo(
|
||||
api.MapGet("/server/info", (ServerOptions options, ServerState state) => Results.Json(new ServerInfo(
|
||||
options.ServerName,
|
||||
Version,
|
||||
ApiVersion.Current,
|
||||
state.Capabilities,
|
||||
state.TlsFingerprint,
|
||||
["jpeg", "png", "tiff", "dng", "cr2", "cr3", "nef", "arw", "raf", "orf", "rw2", "pef", "heic", "webp"],
|
||||
new ServerLimits(options.MaxUploadBytes, options.ChunkSizeBytes)), ProtocolJson.Default.ServerInfo));
|
||||
new ServerLimits(options.MaxUploadBytes, options.ChunkSizeBytes, PasswordHasher.MinimumLength)), ProtocolJson.Default.ServerInfo)).AllowAnonymousCaller();
|
||||
|
||||
api.MapGet("/i18n/{culture}", (string culture) =>
|
||||
{
|
||||
@@ -52,15 +60,46 @@ public static class SystemEndpoints
|
||||
foreach (var (k, v) in Loc.GetArea(area, ci)) strings[k] = v;
|
||||
strings["$culture"] = ci.Name;
|
||||
strings["$rtl"] = ci.TextInfo.IsRightToLeft ? "true" : "false";
|
||||
strings["$languages"] = AdminLanguages.Value;
|
||||
return Results.Json(strings, ProtocolJson.Default.DictionaryStringString);
|
||||
});
|
||||
}).AllowAnonymousCaller();
|
||||
}
|
||||
|
||||
/// <summary>Liveness for the container health check: 503 while the database cannot be reached.</summary>
|
||||
private static async Task<IResult> HealthAsync(HttpContext context, ServerOptions options, TitanoDatabase db)
|
||||
{
|
||||
var checks = new Dictionary<string, string>(StringComparer.Ordinal)
|
||||
{
|
||||
["archive"] = Directory.Exists(options.ArchivePath) ? "ok" : "missing",
|
||||
["data"] = Directory.Exists(options.DataPath) ? "ok" : "missing",
|
||||
};
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted);
|
||||
timeout.CancelAfter(TimeSpan.FromSeconds(3));
|
||||
try
|
||||
{
|
||||
await using var session = await db.Database.OpenAsync(timeout.Token);
|
||||
await session.ExecuteAsync("SELECT 1", [], timeout.Token);
|
||||
checks["database"] = "ok";
|
||||
}
|
||||
catch (Exception ex) when (ex is DatabaseException or OperationCanceledException or IOException)
|
||||
{
|
||||
checks["database"] = "unavailable";
|
||||
}
|
||||
var healthy = checks["database"] == "ok";
|
||||
return Results.Json(new HealthStatus(healthy ? "ok" : "unavailable", Version, checks), ProtocolJson.Default.HealthStatus,
|
||||
statusCode: healthy ? StatusCodes.Status200OK : StatusCodes.Status503ServiceUnavailable);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Process-wide facts computed at startup.</summary>
|
||||
public sealed class ServerState
|
||||
{
|
||||
public string? TlsFingerprint { get; set; }
|
||||
public string? TlsFingerprint { get; init; }
|
||||
|
||||
/// <summary>Engine and version of the database ("PostgreSQL 17.2", "SQLite 3.45.1").</summary>
|
||||
public string Database { get; init; } = string.Empty;
|
||||
|
||||
public List<string> Capabilities { get; } = ["uploads", "sync", "jobs", "render-nodes", "events"];
|
||||
|
||||
public DateTimeOffset StartedAt { get; } = DateTimeOffset.UtcNow;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// <c>/api/v1/uploads</c> (BRIEF §14.4). Chunk, state and cancel calls also serve render nodes uploading their output,
|
||||
/// which start and complete through the node endpoints.
|
||||
/// </summary>
|
||||
public static class UploadEndpoints
|
||||
{
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var uploads = api.MapGroup("/uploads").Requires(Permissions.Upload | Permissions.RenderNode);
|
||||
uploads.MapPost("", StartAsync).Requires(Permissions.Upload);
|
||||
uploads.MapPut("/{id:guid}/chunks/{index:int}", ChunkAsync);
|
||||
uploads.MapGet("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
|
||||
(await service.GetStateAsync(caller, id, context.RequestAborted)).ToResult(context, s => Results.Json(s, ProtocolJson.Default.UploadState)));
|
||||
uploads.MapDelete("/{id:guid}", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
|
||||
(await service.CancelAsync(caller, id, context.RequestAborted)).ToResult(context, _ => Results.NoContent()));
|
||||
uploads.MapPost("/{id:guid}/complete", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
|
||||
(await service.CompleteAsync(caller, id)).ToResult(context, r => Results.Json(r, ProtocolJson.Default.CompleteUploadResponse)))
|
||||
.Requires(Permissions.Upload);
|
||||
}
|
||||
|
||||
private static async Task<IResult> StartAsync(HttpContext context, Caller caller, StartUploadRequest request, UploadService service)
|
||||
{
|
||||
var outcome = await service.StartAsync(caller, request, context.RequestAborted);
|
||||
return outcome.ToResult(context, r => Results.Json(r, ProtocolJson.Default.StartUploadResponse,
|
||||
statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created));
|
||||
}
|
||||
|
||||
private static async Task<IResult> ChunkAsync(HttpContext context, Caller caller, Guid id, int index, UploadService service, ServerOptions options)
|
||||
{
|
||||
// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit.
|
||||
if (context.Features.Get<IHttpMaxRequestBodySizeFeature>() is { IsReadOnly: false } limit) limit.MaxRequestBodySize = options.ChunkSizeBytes;
|
||||
var outcome = await service.ReceiveChunkAsync(caller, id, index, context.Request, context.RequestAborted);
|
||||
return outcome.ToResult(context, r => Results.Json(r, ProtocolJson.Default.ChunkReceived));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
using System.Globalization;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Security;
|
||||
|
||||
namespace Titano.Server.Api;
|
||||
|
||||
/// <summary><c>/api/v1/users</c> (admin): accounts, roles, quotas, render-node right, temporary passwords.</summary>
|
||||
public static class UserEndpoints
|
||||
{
|
||||
private const int MaxDisplayName = 100;
|
||||
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var users = api.MapGroup("/users").Requires(Permissions.ManageUsers);
|
||||
users.MapGet("", ListAsync);
|
||||
users.MapPost("", CreateAsync);
|
||||
users.MapGet("/{id:guid}", GetAsync);
|
||||
users.MapPatch("/{id:guid}", UpdateAsync);
|
||||
users.MapDelete("/{id:guid}", DeleteAsync);
|
||||
users.MapPost("/{id:guid}/reset-password", ResetPasswordAsync);
|
||||
}
|
||||
|
||||
private static async Task<IResult> ListAsync(HttpContext context, TitanoDatabase db, TimeProvider time)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
var users = await db.Users.ListAsync(ct);
|
||||
var latest = await db.Tokens.LatestByUserAsync(ct);
|
||||
var uploads = await db.Uploads.CommittedCountByUserAsync(time.GetUtcNow().AddDays(-30), ct);
|
||||
var rows = users.Select(u =>
|
||||
{
|
||||
var token = latest.GetValueOrDefault(u.Id);
|
||||
return new UserSummaryDto(Dto.User(u), token?.LastUsed ?? u.LastLogin, token?.ClientName, uploads.GetValueOrDefault(u.Id));
|
||||
}).ToList();
|
||||
return Results.Json(rows, ProtocolJson.Default.ListUserSummaryDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> GetAsync(HttpContext context, Guid id, TitanoDatabase db) =>
|
||||
await db.Users.GetAsync(id, context.RequestAborted) is { } user
|
||||
? Results.Json(Dto.User(user), ProtocolJson.Default.UserDto)
|
||||
: ApiErrors.NotFound(context, "user");
|
||||
|
||||
private static async Task<IResult> CreateAsync(HttpContext context, Caller caller, CreateUserRequest request, TitanoDatabase db, AuditLog audit, TimeProvider time)
|
||||
{
|
||||
var username = request.Username?.Trim();
|
||||
if (!Accounts.IsValidUsername(username)) return ApiErrors.BadRequest(context, "invalid_username");
|
||||
if (!Accounts.TryParseRole(request.Role, out var role)) return ApiErrors.BadRequest(context, "invalid_role");
|
||||
if (request.QuotaBytes < 0) return ApiErrors.BadRequest(context, "bad_request", "quotaBytes");
|
||||
var password = string.IsNullOrEmpty(request.Password) ? TemporaryPassword.Generate() : request.Password;
|
||||
if (!PasswordHasher.IsAcceptable(password)) return WeakPassword(context);
|
||||
|
||||
var user = new User
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Username = username!,
|
||||
DisplayName = Clean(request.DisplayName),
|
||||
PasswordHash = PasswordHasher.Hash(password),
|
||||
Role = role,
|
||||
Permissions = Accounts.PermissionsFor(role, request.AllowRenderNode),
|
||||
QuotaBytes = request.QuotaBytes,
|
||||
MustChangePassword = true,
|
||||
Created = time.GetUtcNow(),
|
||||
};
|
||||
try
|
||||
{
|
||||
await db.Users.CreateAsync(user, context.RequestAborted);
|
||||
}
|
||||
catch (DatabaseException ex) when (ex.IsUniqueViolation)
|
||||
{
|
||||
return ApiErrors.Conflict(context, "username_taken");
|
||||
}
|
||||
await audit.WriteAsync(caller, context, "user_created", "user", user.Id.ToString(), $"{user.Username} · {Dto.Role(role)}");
|
||||
return Results.Json(new TemporaryPasswordResponse(Dto.User(user), password), ProtocolJson.Default.TemporaryPasswordResponse, statusCode: StatusCodes.Status201Created);
|
||||
}
|
||||
|
||||
private static async Task<IResult> UpdateAsync(HttpContext context, Caller caller, Guid id, UpdateUserRequest request, TitanoDatabase db, AuditLog audit)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.Users.GetAsync(id, ct) is not { } user) return ApiErrors.NotFound(context, "user");
|
||||
var role = user.Role;
|
||||
if (request.Role is not null && !Accounts.TryParseRole(request.Role, out role)) return ApiErrors.BadRequest(context, "invalid_role");
|
||||
if (request.QuotaBytes < 0) return ApiErrors.BadRequest(context, "bad_request", "quotaBytes");
|
||||
var updated = user with
|
||||
{
|
||||
DisplayName = request.DisplayName is null ? user.DisplayName : Clean(request.DisplayName),
|
||||
Role = role,
|
||||
Permissions = Accounts.PermissionsFor(role, request.AllowRenderNode ?? user.Permissions.HasFlag(Permissions.RenderNode)),
|
||||
QuotaBytes = request.QuotaBytes ?? user.QuotaBytes,
|
||||
Disabled = request.Disabled ?? user.Disabled,
|
||||
};
|
||||
var losesAdmin = IsActiveAdmin(user) && !IsActiveAdmin(updated);
|
||||
if (losesAdmin && user.Id == caller.Id) return ApiErrors.Conflict(context, "self");
|
||||
if (losesAdmin && !await HasOtherActiveAdminAsync(db, user.Id, ct)) return ApiErrors.Conflict(context, "last_admin");
|
||||
|
||||
await db.Users.UpdateAsync(updated, ct);
|
||||
if (updated.Disabled && !user.Disabled) await db.Tokens.RevokeAllAsync(id, ct);
|
||||
await audit.WriteAsync(caller, context, "user_updated", "user", id.ToString(), Changes(user, updated));
|
||||
return Results.Json(Dto.User(updated), ProtocolJson.Default.UserDto);
|
||||
}
|
||||
|
||||
private static async Task<IResult> DeleteAsync(HttpContext context, Caller caller, Guid id, TitanoDatabase db, AuditLog audit)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (id == caller.Id) return ApiErrors.Conflict(context, "self");
|
||||
if (await db.Users.GetAsync(id, ct) is not { } user) return ApiErrors.NotFound(context, "user");
|
||||
if (IsActiveAdmin(user) && !await HasOtherActiveAdminAsync(db, id, ct)) return ApiErrors.Conflict(context, "last_admin");
|
||||
|
||||
// Render jobs of the user's nodes go back to the queue before the nodes disappear with the account.
|
||||
foreach (var node in (await db.RenderNodes.ListAsync(ct)).Where(n => n.UserId == id))
|
||||
await db.Jobs.RequeueRunningAsync(node.Id.ToString(), ct);
|
||||
var reassigned = await db.Sequences.ReassignOwnerAsync(id, caller.Id, ct);
|
||||
await db.Users.DeleteAsync(id, ct);
|
||||
await audit.WriteAsync(caller, context, "user_deleted", "user", id.ToString(),
|
||||
$"{user.Username} · {reassigned.ToString(CultureInfo.InvariantCulture)}");
|
||||
return Results.NoContent();
|
||||
}
|
||||
|
||||
private static async Task<IResult> ResetPasswordAsync(HttpContext context, Caller caller, Guid id, ResetPasswordRequest? request, TitanoDatabase db, AuditLog audit)
|
||||
{
|
||||
var ct = context.RequestAborted;
|
||||
if (await db.Users.GetAsync(id, ct) is not { } user) return ApiErrors.NotFound(context, "user");
|
||||
var password = string.IsNullOrEmpty(request?.Password) ? TemporaryPassword.Generate() : request.Password;
|
||||
if (!PasswordHasher.IsAcceptable(password)) return WeakPassword(context);
|
||||
var updated = user with { PasswordHash = PasswordHasher.Hash(password), MustChangePassword = true };
|
||||
await db.Users.UpdateAsync(updated, ct);
|
||||
await db.Tokens.RevokeAllAsync(id, ct);
|
||||
await audit.WriteAsync(caller, context, "password_reset", "user", id.ToString(), user.Username);
|
||||
return Results.Json(new TemporaryPasswordResponse(Dto.User(updated), password), ProtocolJson.Default.TemporaryPasswordResponse);
|
||||
}
|
||||
|
||||
private static bool IsActiveAdmin(User user) => user.Role == UserRole.Admin && !user.Disabled;
|
||||
|
||||
private static async Task<bool> HasOtherActiveAdminAsync(TitanoDatabase db, Guid except, CancellationToken ct) =>
|
||||
(await db.Users.ListAsync(ct)).Any(u => u.Id != except && IsActiveAdmin(u));
|
||||
|
||||
private static IResult WeakPassword(HttpContext context) =>
|
||||
ApiErrors.BadRequest(context, "weak_password", PasswordHasher.MinimumLength.ToString(CultureInfo.InvariantCulture), PasswordHasher.MinimumLength);
|
||||
|
||||
private static string? Clean(string? displayName)
|
||||
{
|
||||
var trimmed = displayName?.Trim();
|
||||
return string.IsNullOrEmpty(trimmed) ? null : trimmed[..Math.Min(trimmed.Length, MaxDisplayName)];
|
||||
}
|
||||
|
||||
/// <summary>Audit detail listing the fields an edit changed (never secrets).</summary>
|
||||
private static string Changes(User before, User after)
|
||||
{
|
||||
var changes = new List<string>();
|
||||
if (before.DisplayName != after.DisplayName) changes.Add("displayName");
|
||||
if (before.Role != after.Role) changes.Add("role=" + Dto.Role(after.Role));
|
||||
if (before.Permissions != after.Permissions) changes.Add("permissions");
|
||||
if (before.QuotaBytes != after.QuotaBytes) changes.Add("quota=" + after.QuotaBytes.ToString(CultureInfo.InvariantCulture));
|
||||
if (before.Disabled != after.Disabled) changes.Add(after.Disabled ? "disabled" : "enabled");
|
||||
return string.Join(", ", changes);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Titano.Data;
|
||||
|
||||
namespace Titano.Server.Configuration;
|
||||
|
||||
@@ -23,15 +25,44 @@ public enum DatabaseKind
|
||||
|
||||
/// <summary>
|
||||
/// Server configuration (BRIEF §14.9): environment variables override <c>/data/server.json</c>, which overrides defaults.
|
||||
/// Settings applied from the environment are listed in <see cref="Locked"/>; the admin panel cannot change them.
|
||||
/// </summary>
|
||||
public sealed class ServerOptions
|
||||
{
|
||||
/// <summary>Environment variable → setting it fixes, and how its text is applied.</summary>
|
||||
private static readonly (string Variable, string Setting, Action<ServerOptions, string> Apply)[] Variables =
|
||||
[
|
||||
("TITANO_SERVER_NAME", nameof(ServerName), (o, v) => o.ServerName = v),
|
||||
("TITANO_ARCHIVE_PATH", nameof(ArchivePath), (o, v) => o.ArchivePath = v),
|
||||
("TITANO_ADMIN_USER", nameof(AdminUser), (o, v) => o.AdminUser = v),
|
||||
("TITANO_ADMIN_PASSWORD", nameof(AdminPassword), (o, v) => o.AdminPassword = v),
|
||||
("TITANO_DB", nameof(Database), (o, v) => o.ApplyDatabase(v)),
|
||||
("TITANO_DB_HOST", nameof(DbHost), (o, v) => o.DbHost = v),
|
||||
("TITANO_DB_PORT", nameof(DbPort), (o, v) => o.DbPort = Int("TITANO_DB_PORT", v)),
|
||||
("TITANO_DB_NAME", nameof(DbName), (o, v) => o.DbName = v),
|
||||
("TITANO_DB_USER", nameof(DbUser), (o, v) => o.DbUser = v),
|
||||
("TITANO_DB_PASSWORD", nameof(DbPassword), (o, v) => o.DbPassword = v),
|
||||
("TITANO_DB_SSLMODE", nameof(DbSslMode), (o, v) => o.DbSslMode = v),
|
||||
("TITANO_HTTP_PORT", nameof(HttpPort), (o, v) => o.HttpPort = Int("TITANO_HTTP_PORT", v)),
|
||||
("TITANO_HTTPS_PORT", nameof(HttpsPort), (o, v) => o.HttpsPort = Int("TITANO_HTTPS_PORT", v)),
|
||||
("TITANO_TLS_MODE", nameof(TlsMode), (o, v) => o.TlsMode = Enum.Parse<TlsMode>(v, ignoreCase: true)),
|
||||
("TITANO_TLS_CERT", nameof(TlsCert), (o, v) => o.TlsCert = v),
|
||||
("TITANO_TLS_KEY", nameof(TlsKey), (o, v) => o.TlsKey = v),
|
||||
("TITANO_LANG", nameof(Language), (o, v) => o.Language = v),
|
||||
("TITANO_WORKERS", nameof(Workers), (o, v) => o.Workers = Math.Max(1, Int("TITANO_WORKERS", v))),
|
||||
("TITANO_DISCOVERY", nameof(Discovery), (o, v) => o.Discovery = bool.Parse(v)),
|
||||
];
|
||||
|
||||
public string ServerName { get; set; } = "Titano Server";
|
||||
public string ArchivePath { get; set; } = OperatingSystem.IsWindows() ? @"C:\TitanoArchive" : "/archive";
|
||||
public string DataPath { get; set; } = OperatingSystem.IsWindows() ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Titano Server") : "/data";
|
||||
public string? AdminUser { get; set; }
|
||||
public string? AdminPassword { get; set; }
|
||||
public DatabaseKind Database { get; set; } = OperatingSystem.IsWindows() ? DatabaseKind.Sqlite : DatabaseKind.Postgres;
|
||||
|
||||
/// <summary>Complete connection string (from <c>TITANO_DB</c> when it holds one); overrides the <c>Db*</c> parts.</summary>
|
||||
public string? DbConnectionString { get; set; }
|
||||
|
||||
public string DbHost { get; set; } = "postgres";
|
||||
public int DbPort { get; set; } = 5432;
|
||||
public string DbName { get; set; } = "titano";
|
||||
@@ -51,6 +82,27 @@ public sealed class ServerOptions
|
||||
public int JobHistoryDays { get; set; } = 90;
|
||||
public int TokenLifetimeDays { get; set; } = 30;
|
||||
|
||||
/// <summary>Uploads untouched for this long are abandoned: their partial files are deleted.</summary>
|
||||
public int UploadRetentionHours { get; set; } = 48;
|
||||
|
||||
/// <summary>Sign-in attempts accepted per minute and client address (BRIEF §14.2: 5).</summary>
|
||||
public int LoginAttemptsPerMinute { get; set; } = 5;
|
||||
|
||||
/// <summary>Consecutive failed sign-ins that lock an account name for <see cref="LockoutMinutes"/>.</summary>
|
||||
public int LockoutThreshold { get; set; } = 10;
|
||||
|
||||
public int LockoutMinutes { get; set; } = 15;
|
||||
|
||||
/// <summary>A render node silent for this long is offline and its jobs go back to the queue.</summary>
|
||||
public int NodeTimeoutSeconds { get; set; } = 90;
|
||||
|
||||
/// <summary>Chunk uploads one user may run at the same time; more are answered 429.</summary>
|
||||
public int ConcurrentChunksPerUser { get; set; } = 8;
|
||||
|
||||
/// <summary>Settings fixed by environment variables (names of the properties above).</summary>
|
||||
[JsonIgnore]
|
||||
public IReadOnlyCollection<string> Locked { get; private set; } = [];
|
||||
|
||||
[JsonIgnore]
|
||||
public string LogPath => Path.Combine(DataPath, "logs");
|
||||
|
||||
@@ -60,6 +112,17 @@ public sealed class ServerOptions
|
||||
[JsonIgnore]
|
||||
public string UploadsPath => Path.Combine(DataPath, "uploads");
|
||||
|
||||
[JsonIgnore]
|
||||
public string SettingsFile => Path.Combine(DataPath, "server.json");
|
||||
|
||||
/// <summary>Verified uploads wait here, on the archive volume, until the archive ingest moves them into place.</summary>
|
||||
[JsonIgnore]
|
||||
public string StagingPath => Path.Combine(ArchivePath, ".titano", "staging");
|
||||
|
||||
/// <summary>Deleted sequences are moved here (never erased by the server).</summary>
|
||||
[JsonIgnore]
|
||||
public string TrashPath => Path.Combine(ArchivePath, ".titano", "trash");
|
||||
|
||||
/// <summary>Loads <c>{DataPath}/server.json</c> (if present) then applies <c>TITANO_*</c> environment variables.</summary>
|
||||
public static ServerOptions Load(IReadOnlyDictionary<string, string?>? environment = null)
|
||||
{
|
||||
@@ -82,35 +145,38 @@ public sealed class ServerOptions
|
||||
}
|
||||
options.DataPath = dataPath;
|
||||
|
||||
options.ServerName = Env("TITANO_SERVER_NAME") ?? options.ServerName;
|
||||
options.ArchivePath = Env("TITANO_ARCHIVE_PATH") ?? options.ArchivePath;
|
||||
options.AdminUser = Env("TITANO_ADMIN_USER") ?? options.AdminUser;
|
||||
options.AdminPassword = Env("TITANO_ADMIN_PASSWORD") ?? options.AdminPassword;
|
||||
if (Env("TITANO_DB") is { } db) options.Database = Enum.Parse<DatabaseKind>(db, ignoreCase: true);
|
||||
options.DbHost = Env("TITANO_DB_HOST") ?? options.DbHost;
|
||||
if (Env("TITANO_DB_PORT") is { } port) options.DbPort = int.Parse(port, System.Globalization.CultureInfo.InvariantCulture);
|
||||
options.DbName = Env("TITANO_DB_NAME") ?? options.DbName;
|
||||
options.DbUser = Env("TITANO_DB_USER") ?? options.DbUser;
|
||||
options.DbPassword = Env("TITANO_DB_PASSWORD") ?? options.DbPassword;
|
||||
options.DbSslMode = Env("TITANO_DB_SSLMODE") ?? options.DbSslMode;
|
||||
if (Env("TITANO_HTTP_PORT") is { } http) options.HttpPort = int.Parse(http, System.Globalization.CultureInfo.InvariantCulture);
|
||||
if (Env("TITANO_HTTPS_PORT") is { } https) options.HttpsPort = int.Parse(https, System.Globalization.CultureInfo.InvariantCulture);
|
||||
if (Env("TITANO_TLS_MODE") is { } tls) options.TlsMode = Enum.Parse<TlsMode>(tls, ignoreCase: true);
|
||||
options.TlsCert = Env("TITANO_TLS_CERT") ?? options.TlsCert;
|
||||
options.TlsKey = Env("TITANO_TLS_KEY") ?? options.TlsKey;
|
||||
options.Language = Env("TITANO_LANG") ?? options.Language;
|
||||
if (Env("TITANO_WORKERS") is { } workers) options.Workers = Math.Max(1, int.Parse(workers, System.Globalization.CultureInfo.InvariantCulture));
|
||||
if (Env("TITANO_DISCOVERY") is { } discovery) options.Discovery = bool.Parse(discovery);
|
||||
var locked = new List<string>();
|
||||
foreach (var (variable, setting, apply) in Variables)
|
||||
{
|
||||
if (Env(variable) is not { } value) continue;
|
||||
try
|
||||
{
|
||||
apply(options, value);
|
||||
}
|
||||
catch (Exception ex) when (ex is FormatException or ArgumentException)
|
||||
{
|
||||
throw new InvalidOperationException($"{variable} has an invalid value.", ex);
|
||||
}
|
||||
locked.Add(setting);
|
||||
}
|
||||
options.Locked = locked;
|
||||
options.Validate();
|
||||
return options;
|
||||
}
|
||||
|
||||
private static Dictionary<string, string?> ReadEnvironment()
|
||||
/// <summary>The database to open: <see cref="DbConnectionString"/>, the SQLite file in the data folder, or PostgreSQL from the parts.</summary>
|
||||
public DatabaseOptions ToDatabaseOptions()
|
||||
{
|
||||
// Variables may differ only by case on Linux (no_proxy/NO_PROXY); the last one wins.
|
||||
var result = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (System.Collections.DictionaryEntry e in Environment.GetEnvironmentVariables()) result[(string)e.Key] = (string?)e.Value;
|
||||
return result;
|
||||
if (DbConnectionString is { Length: > 0 } cs) return DatabaseOptions.Parse(cs);
|
||||
if (Database == DatabaseKind.Sqlite) return DatabaseOptions.Sqlite(Path.Combine(DataPath, "titano.db"));
|
||||
var builder = new System.Text.StringBuilder()
|
||||
.Append("Host=").Append(Quote(DbHost))
|
||||
.Append(";Port=").Append(DbPort.ToString(CultureInfo.InvariantCulture))
|
||||
.Append(";Database=").Append(Quote(DbName))
|
||||
.Append(";Username=").Append(Quote(DbUser))
|
||||
.Append(";SslMode=").Append(Quote(DbSslMode));
|
||||
if (DbPassword is not null) builder.Append(";Password=").Append(Quote(DbPassword));
|
||||
return DatabaseOptions.Postgres(builder.ToString());
|
||||
}
|
||||
|
||||
public void Validate()
|
||||
@@ -122,6 +188,35 @@ public sealed class ServerOptions
|
||||
// Port 0 asks the OS for a free port (used by the in-process test host).
|
||||
if (HttpPort is < 0 or > 65535 || HttpsPort is < 0 or > 65535)
|
||||
throw new InvalidOperationException("Ports must be between 0 and 65535.");
|
||||
if (ChunkSizeBytes is < 64 * 1024 or > 64 * 1024 * 1024)
|
||||
throw new InvalidOperationException("The chunk size must be between 64 KiB and 64 MiB.");
|
||||
if (MaxUploadBytes < 1 || TokenLifetimeDays < 1 || JobHistoryDays < 1 || UploadRetentionHours < 1)
|
||||
throw new InvalidOperationException("Upload size, token lifetime, job history and upload retention must be positive.");
|
||||
}
|
||||
|
||||
/// <summary>TITANO_DB is <c>postgres</c>, <c>sqlite</c> or a complete connection string (<c>Host=…</c> / <c>Data Source=…</c>).</summary>
|
||||
private void ApplyDatabase(string value)
|
||||
{
|
||||
if (value.Contains('=', StringComparison.Ordinal))
|
||||
{
|
||||
DbConnectionString = value;
|
||||
Database = DatabaseOptions.Parse(value).Dialect == Dialect.Postgres ? DatabaseKind.Postgres : DatabaseKind.Sqlite;
|
||||
return;
|
||||
}
|
||||
Database = Enum.Parse<DatabaseKind>(value, ignoreCase: true);
|
||||
}
|
||||
|
||||
private static int Int(string variable, string value) =>
|
||||
int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var n) ? n : throw new FormatException($"{variable} must be a number.");
|
||||
|
||||
private static string Quote(string value) => "'" + value.Replace("'", "''", StringComparison.Ordinal) + "'";
|
||||
|
||||
private static Dictionary<string, string?> ReadEnvironment()
|
||||
{
|
||||
// Variables may differ only by case on Linux (no_proxy/NO_PROXY); the last one wins.
|
||||
var result = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (System.Collections.DictionaryEntry e in Environment.GetEnvironmentVariables()) result[(string)e.Key] = (string?)e.Value;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Titano.Core.IO;
|
||||
using Titano.Core.Localization;
|
||||
using Titano.Protocol;
|
||||
|
||||
namespace Titano.Server.Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// The admin panel's view of the configuration (BRIEF §14.8 Settings). Saving writes <c>/data/server.json</c> (only the
|
||||
/// edited keys; secrets and environment values are never written) and applies what can change while running; ports,
|
||||
/// TLS, workers, chunk size and job history wait for a restart.
|
||||
/// </summary>
|
||||
public sealed class ServerSettingsStore(ServerOptions options)
|
||||
{
|
||||
private readonly Lock _gate = new();
|
||||
private ServerSettingsDto? _saved;
|
||||
|
||||
public ServerSettingsDto Current()
|
||||
{
|
||||
lock (_gate) return _saved ?? Snapshot(options, restartRequired: false);
|
||||
}
|
||||
|
||||
/// <summary>Validates and saves; returns the error code and the offending setting, or null on success.</summary>
|
||||
public (string Code, string Setting)? Save(ServerSettingsDto request)
|
||||
{
|
||||
if (Validate(request) is { } invalid) return ("invalid_setting", invalid);
|
||||
lock (_gate)
|
||||
{
|
||||
var current = _saved ?? Snapshot(options, restartRequired: false);
|
||||
var changes = Changes(current, request);
|
||||
if (changes.FirstOrDefault(c => options.Locked.Contains(c.Name)) is { Name: { } locked }) return ("setting_locked", locked);
|
||||
if (changes.Count == 0) return null;
|
||||
|
||||
var file = File.Exists(options.SettingsFile) ? JsonNode.Parse(File.ReadAllText(options.SettingsFile)) as JsonObject ?? [] : [];
|
||||
foreach (var (name, value) in changes) file[name] = value;
|
||||
Directory.CreateDirectory(options.DataPath);
|
||||
AtomicFile.WriteAllText(options.SettingsFile, file.ToJsonString(new JsonSerializerOptions { WriteIndented = true }));
|
||||
|
||||
options.ServerName = request.ServerName.Trim();
|
||||
options.Discovery = request.Discovery;
|
||||
options.Language = request.Language;
|
||||
options.MaxUploadBytes = request.MaxUploadBytes;
|
||||
options.TokenLifetimeDays = request.TokenLifetimeDays;
|
||||
options.UploadRetentionHours = request.UploadRetentionHours;
|
||||
var restart = request.HttpPort != options.HttpPort || request.HttpsPort != options.HttpsPort
|
||||
|| !string.Equals(request.TlsMode, options.TlsMode.ToString(), StringComparison.OrdinalIgnoreCase)
|
||||
|| request.TlsCert != options.TlsCert || request.TlsKey != options.TlsKey || request.Workers != options.Workers
|
||||
|| request.ChunkSizeBytes != options.ChunkSizeBytes || request.JobHistoryDays != options.JobHistoryDays;
|
||||
_saved = request with { ServerName = request.ServerName.Trim(), Locked = [.. options.Locked], RestartRequired = restart };
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static ServerSettingsDto Snapshot(ServerOptions o, bool restartRequired) => new(
|
||||
o.ServerName, o.HttpPort, o.HttpsPort, o.TlsMode.ToString().ToLowerInvariant(), o.TlsCert, o.TlsKey, o.Language, o.Workers, o.Discovery,
|
||||
o.MaxUploadBytes, o.ChunkSizeBytes, o.JobHistoryDays, o.TokenLifetimeDays, o.UploadRetentionHours, [.. o.Locked], restartRequired);
|
||||
|
||||
private static string? Validate(ServerSettingsDto r)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(r.ServerName) || r.ServerName.Trim().Length > 60) return nameof(ServerOptions.ServerName);
|
||||
if (r.HttpPort is < 1 or > 65535) return nameof(ServerOptions.HttpPort);
|
||||
if (r.HttpsPort is < 1 or > 65535 || r.HttpsPort == r.HttpPort) return nameof(ServerOptions.HttpsPort);
|
||||
if (!Enum.TryParse<TlsMode>(r.TlsMode, ignoreCase: true, out var tls) || !Enum.IsDefined(tls)) return nameof(ServerOptions.TlsMode);
|
||||
if (tls == TlsMode.Pem && (string.IsNullOrWhiteSpace(r.TlsCert) || !File.Exists(r.TlsCert))) return nameof(ServerOptions.TlsCert);
|
||||
if (tls == TlsMode.Pem && (string.IsNullOrWhiteSpace(r.TlsKey) || !File.Exists(r.TlsKey))) return nameof(ServerOptions.TlsKey);
|
||||
if (!Languages.Shipped.Contains(r.Language, StringComparer.OrdinalIgnoreCase)) return nameof(ServerOptions.Language);
|
||||
if (r.Workers is < 1 or > 64) return nameof(ServerOptions.Workers);
|
||||
if (r.MaxUploadBytes < 1024 * 1024) return nameof(ServerOptions.MaxUploadBytes);
|
||||
if (r.ChunkSizeBytes is < 64 * 1024 or > 64 * 1024 * 1024) return nameof(ServerOptions.ChunkSizeBytes);
|
||||
if (r.JobHistoryDays is < 1 or > 3650) return nameof(ServerOptions.JobHistoryDays);
|
||||
if (r.TokenLifetimeDays is < 1 or > 365) return nameof(ServerOptions.TokenLifetimeDays);
|
||||
if (r.UploadRetentionHours is < 1 or > 8760) return nameof(ServerOptions.UploadRetentionHours);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Settings whose value differs, as server.json keys and JSON values.</summary>
|
||||
private static List<(string Name, JsonNode? Value)> Changes(ServerSettingsDto a, ServerSettingsDto b)
|
||||
{
|
||||
var changes = new List<(string, JsonNode?)>();
|
||||
void Add<T>(string name, T before, T after, Func<T, JsonNode?> json)
|
||||
{
|
||||
if (!EqualityComparer<T>.Default.Equals(before, after)) changes.Add((name, json(after)));
|
||||
}
|
||||
Add(nameof(ServerOptions.ServerName), a.ServerName, b.ServerName.Trim(), v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.HttpPort), a.HttpPort, b.HttpPort, v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.HttpsPort), a.HttpsPort, b.HttpsPort, v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.TlsMode), a.TlsMode.ToLowerInvariant(), b.TlsMode.ToLowerInvariant(), v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.TlsCert), a.TlsCert, b.TlsCert, v => v is null ? null : JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.TlsKey), a.TlsKey, b.TlsKey, v => v is null ? null : JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.Language), a.Language, b.Language, v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.Workers), a.Workers, b.Workers, v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.Discovery), a.Discovery, b.Discovery, v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.MaxUploadBytes), a.MaxUploadBytes, b.MaxUploadBytes, v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.ChunkSizeBytes), a.ChunkSizeBytes, b.ChunkSizeBytes, v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.JobHistoryDays), a.JobHistoryDays, b.JobHistoryDays, v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.TokenLifetimeDays), a.TokenLifetimeDays, b.TokenLifetimeDays, v => JsonValue.Create(v));
|
||||
Add(nameof(ServerOptions.UploadRetentionHours), a.UploadRetentionHours, b.UploadRetentionHours, v => JsonValue.Create(v));
|
||||
return changes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Api;
|
||||
using Titano.Server.Security;
|
||||
|
||||
namespace Titano.Server.Events;
|
||||
|
||||
/// <summary>
|
||||
/// <c>WS /api/v1/events</c>: server events as one JSON text message each (<see cref="ServerEvent"/>, discriminated by
|
||||
/// <c>type</c>). The handshake is authenticated like any API call (bearer header, or the admin cookie from this origin);
|
||||
/// the token is checked again every minute and the socket closes once it is revoked or expires.
|
||||
/// </summary>
|
||||
public static class EventEndpoints
|
||||
{
|
||||
private static readonly TimeSpan Revalidate = TimeSpan.FromSeconds(60);
|
||||
|
||||
public static void Map(RouteGroupBuilder api) => api.MapGet("/events", RunAsync).Requires(Permissions.Read);
|
||||
|
||||
private static async Task<IResult> RunAsync(HttpContext context, Caller caller, EventHub hub, TokenService tokens, IHostApplicationLifetime lifetime)
|
||||
{
|
||||
if (!context.WebSockets.IsWebSocketRequest) return ApiErrors.BadRequest(context, "websocket_required");
|
||||
using var socket = await context.WebSockets.AcceptWebSocketAsync();
|
||||
using var subscription = hub.Subscribe();
|
||||
using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted, lifetime.ApplicationStopping);
|
||||
var receiving = ReceiveUntilClosedAsync(socket, stop);
|
||||
var status = WebSocketCloseStatus.NormalClosure;
|
||||
try
|
||||
{
|
||||
status = await SendAsync(socket, subscription, caller, tokens, stop.Token);
|
||||
}
|
||||
catch (Exception ex) when (ex is OperationCanceledException or WebSocketException)
|
||||
{
|
||||
status = lifetime.ApplicationStopping.IsCancellationRequested ? WebSocketCloseStatus.EndpointUnavailable : WebSocketCloseStatus.NormalClosure;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await stop.CancelAsync();
|
||||
await receiving;
|
||||
}
|
||||
if (socket.State is WebSocketState.Open or WebSocketState.CloseReceived)
|
||||
{
|
||||
using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5));
|
||||
try
|
||||
{
|
||||
await socket.CloseOutputAsync(status, null, timeout.Token);
|
||||
}
|
||||
catch (Exception ex) when (ex is OperationCanceledException or WebSocketException)
|
||||
{
|
||||
// The peer is gone.
|
||||
}
|
||||
}
|
||||
return Results.Empty;
|
||||
}
|
||||
|
||||
/// <summary>Forwards events until cancelled; returns the close status when the caller's token stops being valid.</summary>
|
||||
private static async Task<WebSocketCloseStatus> SendAsync(WebSocket socket, EventHub.Subscription subscription, Caller caller, TokenService tokens,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
using (var wait = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
|
||||
{
|
||||
wait.CancelAfter(Revalidate);
|
||||
try
|
||||
{
|
||||
while (await subscription.Reader.WaitToReadAsync(wait.Token))
|
||||
{
|
||||
while (subscription.Reader.TryRead(out var e))
|
||||
{
|
||||
var payload = JsonSerializer.SerializeToUtf8Bytes(e, ProtocolJson.Default.ServerEvent);
|
||||
await socket.SendAsync(payload, WebSocketMessageType.Text, endOfMessage: true, cancellationToken);
|
||||
}
|
||||
}
|
||||
return WebSocketCloseStatus.NormalClosure;
|
||||
}
|
||||
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
// Quiet minute: fall through to the token check.
|
||||
}
|
||||
}
|
||||
if (!await tokens.IsStillValidAsync(caller, cancellationToken)) return WebSocketCloseStatus.PolicyViolation;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task ReceiveUntilClosedAsync(WebSocket socket, CancellationTokenSource stop)
|
||||
{
|
||||
var buffer = new byte[1024];
|
||||
try
|
||||
{
|
||||
while (!stop.IsCancellationRequested)
|
||||
{
|
||||
var result = await socket.ReceiveAsync(buffer, stop.Token);
|
||||
if (result.MessageType == WebSocketMessageType.Close) break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is OperationCanceledException or WebSocketException)
|
||||
{
|
||||
}
|
||||
await stop.CancelAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System.Threading.Channels;
|
||||
using Titano.Protocol;
|
||||
|
||||
namespace Titano.Server.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Fan-out of server events to WebSocket subscribers (BRIEF §14.3). Each subscriber has a bounded queue that drops
|
||||
/// its oldest events when it falls behind, so a slow client never holds up the server.
|
||||
/// </summary>
|
||||
public sealed class EventHub
|
||||
{
|
||||
private const int QueueLength = 512;
|
||||
private readonly Lock _gate = new();
|
||||
private Channel<ServerEvent>[] _subscribers = [];
|
||||
|
||||
public int SubscriberCount => Volatile.Read(ref _subscribers).Length;
|
||||
|
||||
public Subscription Subscribe()
|
||||
{
|
||||
var channel = Channel.CreateBounded<ServerEvent>(new BoundedChannelOptions(QueueLength)
|
||||
{
|
||||
FullMode = BoundedChannelFullMode.DropOldest,
|
||||
SingleReader = true,
|
||||
});
|
||||
lock (_gate) _subscribers = [.. _subscribers, channel];
|
||||
return new Subscription(this, channel);
|
||||
}
|
||||
|
||||
public void Publish(ServerEvent e)
|
||||
{
|
||||
foreach (var subscriber in Volatile.Read(ref _subscribers)) subscriber.Writer.TryWrite(e);
|
||||
}
|
||||
|
||||
private void Remove(Channel<ServerEvent> channel)
|
||||
{
|
||||
lock (_gate) _subscribers = [.. _subscribers.Where(c => c != channel)];
|
||||
channel.Writer.TryComplete();
|
||||
}
|
||||
|
||||
/// <summary>One subscriber's queue; dispose to unsubscribe.</summary>
|
||||
public sealed class Subscription(EventHub hub, Channel<ServerEvent> channel) : IDisposable
|
||||
{
|
||||
public ChannelReader<ServerEvent> Reader => channel.Reader;
|
||||
|
||||
public void Dispose() => hub.Remove(channel);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// Serves the embedded admin panel at <c>/admin</c>. Requests over plain HTTP from another machine are redirected to
|
||||
/// the HTTPS listener, because the session cookie is <c>Secure</c> (BRIEF §14.2) and would not survive plain HTTP.
|
||||
/// </summary>
|
||||
public static class AdminPanel
|
||||
{
|
||||
public static void Map(WebApplication app, ServerOptions options)
|
||||
{
|
||||
var files = new EmbeddedStaticFiles(typeof(AdminPanel).Assembly, "admin/");
|
||||
var server = app.Services.GetRequiredService<IServer>();
|
||||
app.MapGet("/", () => Results.Redirect("/admin/"));
|
||||
app.MapMethods("/admin/{**path}", ["GET", "HEAD"], async (HttpContext context, string? path) =>
|
||||
{
|
||||
if (HttpsLocation(context, options, server) is { } secure)
|
||||
{
|
||||
context.Response.Redirect(secure);
|
||||
return;
|
||||
}
|
||||
// Relative asset URLs need the trailing slash.
|
||||
if (!context.Request.Path.Value!.EndsWith('/') && string.IsNullOrEmpty(path))
|
||||
{
|
||||
context.Response.Redirect("/admin/");
|
||||
return;
|
||||
}
|
||||
if (!await files.TryServeAsync(context, path ?? string.Empty)) context.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
});
|
||||
}
|
||||
|
||||
private static string? HttpsLocation(HttpContext context, ServerOptions options, IServer server)
|
||||
{
|
||||
if (context.Request.IsHttps || options.TlsMode == TlsMode.Proxy) return null;
|
||||
if (context.Connection.RemoteIpAddress is { } remote && IPAddress.IsLoopback(remote)) return null;
|
||||
foreach (var address in server.Features.Get<IServerAddressesFeature>()?.Addresses ?? [])
|
||||
{
|
||||
if (!address.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) continue;
|
||||
var port = address[(address.LastIndexOf(':') + 1)..].TrimEnd('/');
|
||||
return $"https://{context.Request.Host.Host}:{port}{context.Request.PathBase}{context.Request.Path}{context.Request.QueryString}";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using Microsoft.AspNetCore.Hosting.Server.Features;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Api;
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// LAN discovery (BRIEF §14.9, <c>TITANO_DISCOVERY</c>): every few seconds a <see cref="DiscoveryBeacon"/> is broadcast on
|
||||
/// UDP port <see cref="DiscoveryBeacon.Port"/> of every IPv4 network, so clients can offer this server on first run.
|
||||
/// Switching discovery off in the settings stops it without a restart.
|
||||
/// </summary>
|
||||
public sealed class DiscoveryBeaconService(ServerOptions options, ServerState state, IServer server, ILogger<DiscoveryBeaconService> logger) : BackgroundService
|
||||
{
|
||||
private static readonly TimeSpan Interval = TimeSpan.FromSeconds(5);
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using var udp = new UdpClient(AddressFamily.InterNetwork) { EnableBroadcast = true };
|
||||
var warned = false;
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (options.Discovery)
|
||||
{
|
||||
try
|
||||
{
|
||||
await SendOnceAsync(udp, BroadcastTargets(), stoppingToken);
|
||||
}
|
||||
catch (SocketException ex) when (!warned)
|
||||
{
|
||||
warned = true;
|
||||
logger.LogInformation("LAN discovery beacon cannot be sent: {Reason}", ex.Message);
|
||||
}
|
||||
catch (SocketException)
|
||||
{
|
||||
// Already reported once; networks may come and go.
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
await Task.Delay(Interval, stoppingToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The beacon for the ports Kestrel actually listens on (HTTPS 0 when TLS is terminated elsewhere).</summary>
|
||||
public DiscoveryBeacon CreateBeacon() => new(
|
||||
options.ServerName,
|
||||
SystemEndpoints.Version,
|
||||
BoundPort(Uri.UriSchemeHttp) ?? options.HttpPort,
|
||||
state.TlsFingerprint is null ? 0 : BoundPort(Uri.UriSchemeHttps) ?? options.HttpsPort,
|
||||
state.TlsFingerprint);
|
||||
|
||||
public async Task SendOnceAsync(UdpClient udp, IEnumerable<IPEndPoint> targets, CancellationToken cancellationToken)
|
||||
{
|
||||
var datagram = CreateBeacon().ToBytes();
|
||||
foreach (var target in targets) await udp.SendAsync(datagram, target, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>The limited broadcast address plus the directed broadcast of each IPv4 interface that is up.</summary>
|
||||
private static List<IPEndPoint> BroadcastTargets()
|
||||
{
|
||||
var targets = new List<IPEndPoint> { new(IPAddress.Broadcast, DiscoveryBeacon.Port) };
|
||||
foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
|
||||
{
|
||||
if (nic.OperationalStatus != OperationalStatus.Up || nic.NetworkInterfaceType == NetworkInterfaceType.Loopback) continue;
|
||||
foreach (var unicast in nic.GetIPProperties().UnicastAddresses)
|
||||
{
|
||||
if (unicast.Address.AddressFamily != AddressFamily.InterNetwork || unicast.IPv4Mask is not { } mask) continue;
|
||||
var address = unicast.Address.GetAddressBytes();
|
||||
var bits = mask.GetAddressBytes();
|
||||
for (var i = 0; i < 4; i++) address[i] = (byte)(address[i] | ~bits[i]);
|
||||
var broadcast = new IPEndPoint(new IPAddress(address), DiscoveryBeacon.Port);
|
||||
if (!targets.Contains(broadcast)) targets.Add(broadcast);
|
||||
}
|
||||
}
|
||||
return targets;
|
||||
}
|
||||
|
||||
private int? BoundPort(string scheme)
|
||||
{
|
||||
var addresses = server.Features.Get<IServerAddressesFeature>()?.Addresses ?? [];
|
||||
foreach (var address in addresses)
|
||||
{
|
||||
if (Uri.TryCreate(address.Replace("[::]", "localhost", StringComparison.Ordinal).Replace("0.0.0.0", "localhost", StringComparison.Ordinal),
|
||||
UriKind.Absolute, out var uri) && uri.Scheme == scheme)
|
||||
return uri.Port;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,16 @@ public sealed class LoggerSink(ILoggerFactory factory) : ILogSink
|
||||
{
|
||||
public void Write(CoreLogLevel level, string category, string message, Exception? exception)
|
||||
{
|
||||
var logger = factory.CreateLogger("Titano." + category);
|
||||
ILogger logger;
|
||||
try
|
||||
{
|
||||
logger = factory.CreateLogger("Titano." + category);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// The host that installed this sink has shut down (engine code may outlive it); the line is dropped.
|
||||
return;
|
||||
}
|
||||
var ms = level switch
|
||||
{
|
||||
CoreLogLevel.Trace => MsLogLevel.Trace,
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Api;
|
||||
|
||||
namespace Titano.Server.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// First middleware of the pipeline: API version and security headers on every response, and the
|
||||
/// <c>{ code, message, details }</c> error body for failures that did not produce one (bad requests, unhandled
|
||||
/// exceptions, unknown methods).
|
||||
/// </summary>
|
||||
public sealed class ResponseMiddleware(RequestDelegate next, ILogger<ResponseMiddleware> logger)
|
||||
{
|
||||
/// <summary>The admin panel loads nothing from elsewhere and is never framed.</summary>
|
||||
private const string AdminPolicy =
|
||||
"default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'self'; connect-src 'self'; " +
|
||||
"frame-ancestors 'none'; base-uri 'none'; form-action 'self'";
|
||||
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
var headers = context.Response.Headers;
|
||||
headers[ApiVersion.Header] = ApiVersion.Current.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
headers.XContentTypeOptions = "nosniff";
|
||||
headers["Referrer-Policy"] = "no-referrer";
|
||||
var api = context.Request.Path.StartsWithSegments("/api");
|
||||
if (api) headers.CacheControl = "no-store";
|
||||
if (context.Request.Path.StartsWithSegments("/admin"))
|
||||
{
|
||||
headers.ContentSecurityPolicy = AdminPolicy;
|
||||
headers.XFrameOptions = "DENY";
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await next(context);
|
||||
}
|
||||
catch (BadHttpRequestException ex) when (!context.Response.HasStarted)
|
||||
{
|
||||
await ApiErrors.WriteAsync(context, ex.StatusCode, CodeFor(ex.StatusCode));
|
||||
return;
|
||||
}
|
||||
catch (FormatException) when (!context.Response.HasStarted && context.Request.Query.ContainsKey("cursor"))
|
||||
{
|
||||
// Repositories reject cursors that do not belong to the listing.
|
||||
await ApiErrors.WriteAsync(context, StatusCodes.Status400BadRequest, "invalid_cursor");
|
||||
return;
|
||||
}
|
||||
catch (OperationCanceledException) when (context.RequestAborted.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (Exception ex) when (!context.Response.HasStarted)
|
||||
{
|
||||
logger.LogError(ex, "Unhandled error on {Method} {Path}", context.Request.Method, context.Request.Path);
|
||||
await ApiErrors.WriteAsync(context, StatusCodes.Status500InternalServerError, "internal");
|
||||
return;
|
||||
}
|
||||
|
||||
var response = context.Response;
|
||||
if (api && response.StatusCode >= 400 && !response.HasStarted && response.ContentType is null)
|
||||
await ApiErrors.WriteAsync(context, response.StatusCode, CodeFor(response.StatusCode));
|
||||
}
|
||||
|
||||
private static string CodeFor(int status) => status switch
|
||||
{
|
||||
StatusCodes.Status400BadRequest => "bad_request",
|
||||
StatusCodes.Status401Unauthorized => "unauthorized",
|
||||
StatusCodes.Status403Forbidden => "forbidden",
|
||||
StatusCodes.Status404NotFound => "not_found",
|
||||
StatusCodes.Status405MethodNotAllowed => "method_not_allowed",
|
||||
StatusCodes.Status413PayloadTooLarge => "too_large",
|
||||
StatusCodes.Status415UnsupportedMediaType => "unsupported_media_type",
|
||||
_ => "internal",
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
namespace Titano.Server.Hosting;
|
||||
|
||||
/// <summary>What <c>Titano.Server</c> was asked to do.</summary>
|
||||
public enum ServerCommand
|
||||
{
|
||||
/// <summary>Run in the console (Docker, Linux, development).</summary>
|
||||
Run,
|
||||
|
||||
/// <summary>Run under the Windows Service Control Manager (the command line the installed service uses).</summary>
|
||||
Service,
|
||||
|
||||
InstallService,
|
||||
UninstallService,
|
||||
|
||||
/// <summary>Probe <c>/health</c> of the local server and exit 0/1 (container health check).</summary>
|
||||
HealthCheck,
|
||||
|
||||
Help,
|
||||
}
|
||||
|
||||
/// <summary>Command-line switches of the server; anything else is passed on to the web host.</summary>
|
||||
public sealed record ServerCommandLine(ServerCommand Command, IReadOnlyList<string> HostArguments, string? Error)
|
||||
{
|
||||
public const string Usage = """
|
||||
Titano Server — time-lapse archive, uploads, jobs and rendering.
|
||||
|
||||
Usage: Titano.Server [--service | --install-service | --uninstall-service | --healthcheck | --help]
|
||||
|
||||
(no switch) run in the console until Ctrl+C
|
||||
--service run under the Windows Service Control Manager
|
||||
--install-service register the "TitanoServer" Windows service (automatic start, LocalSystem)
|
||||
--uninstall-service stop and remove the Windows service
|
||||
--healthcheck exit 0 when the local server answers /health, 1 otherwise
|
||||
|
||||
Configuration: /data/server.json (TITANO_DATA_PATH) overridden by environment variables:
|
||||
TITANO_ARCHIVE_PATH TITANO_DATA_PATH TITANO_SERVER_NAME TITANO_ADMIN_USER TITANO_ADMIN_PASSWORD
|
||||
TITANO_DB (postgres | sqlite | connection string) TITANO_DB_HOST TITANO_DB_PORT TITANO_DB_NAME TITANO_DB_USER
|
||||
TITANO_DB_PASSWORD TITANO_DB_SSLMODE TITANO_HTTP_PORT TITANO_HTTPS_PORT TITANO_TLS_MODE (auto | pem | proxy)
|
||||
TITANO_TLS_CERT TITANO_TLS_KEY TITANO_LANG TITANO_WORKERS TITANO_DISCOVERY
|
||||
""";
|
||||
|
||||
public static ServerCommandLine Parse(IReadOnlyList<string> args)
|
||||
{
|
||||
var command = ServerCommand.Run;
|
||||
var rest = new List<string>();
|
||||
string? error = null;
|
||||
foreach (var arg in args)
|
||||
{
|
||||
ServerCommand? parsed = arg switch
|
||||
{
|
||||
"--service" => ServerCommand.Service,
|
||||
"--install-service" => ServerCommand.InstallService,
|
||||
"--uninstall-service" => ServerCommand.UninstallService,
|
||||
"--healthcheck" => ServerCommand.HealthCheck,
|
||||
"--help" or "-h" or "-?" or "/?" => ServerCommand.Help,
|
||||
_ => null,
|
||||
};
|
||||
if (parsed is not { } switchCommand)
|
||||
{
|
||||
rest.Add(arg);
|
||||
continue;
|
||||
}
|
||||
if (command != ServerCommand.Run && command != switchCommand) error = $"{arg} cannot be combined with another command switch.";
|
||||
command = switchCommand;
|
||||
}
|
||||
return new ServerCommandLine(command, rest, error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using Titano.Data;
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Hosting;
|
||||
|
||||
/// <summary>Opens and migrates the server database at startup (BRIEF §6.1), waiting for a PostgreSQL container that is still starting.</summary>
|
||||
public static class ServerDatabase
|
||||
{
|
||||
private const int Attempts = 10;
|
||||
private static readonly TimeSpan RetryDelay = TimeSpan.FromSeconds(3);
|
||||
|
||||
public static async Task<TitanoDatabase> OpenAsync(ServerOptions options, ILogger logger, CancellationToken cancellationToken)
|
||||
{
|
||||
var settings = options.ToDatabaseOptions();
|
||||
for (var attempt = 1; ; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
var db = await TitanoDatabase.OpenAsync(settings, cancellationToken).ConfigureAwait(false);
|
||||
logger.LogInformation("Database {Dialect} ready, schema version {Version}", db.Dialect, db.SchemaVersion);
|
||||
return db;
|
||||
}
|
||||
catch (DatabaseConnectionException ex) when (attempt < Attempts)
|
||||
{
|
||||
logger.LogWarning("Database not reachable yet ({Reason}); retry {Attempt} of {Attempts} in {Delay} s",
|
||||
ex.Message, attempt, Attempts - 1, RetryDelay.TotalSeconds);
|
||||
await Task.Delay(RetryDelay, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Engine and version, e.g. "PostgreSQL 17.2" or "SQLite 3.45.1".</summary>
|
||||
public static async Task<string> DescribeAsync(TitanoDatabase db, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var session = await db.Database.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||
var postgres = db.Dialect == Dialect.Postgres;
|
||||
var version = await session.ScalarAsync<string>(postgres ? "SHOW server_version" : "SELECT sqlite_version()", [], cancellationToken).ConfigureAwait(false);
|
||||
var number = version?.Split(' ', 2)[0] ?? string.Empty;
|
||||
return (postgres ? "PostgreSQL " : "SQLite ") + number;
|
||||
}
|
||||
}
|
||||
@@ -1,48 +1,110 @@
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Api;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Events;
|
||||
using Titano.Server.Jobs;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
namespace Titano.Server.Hosting;
|
||||
|
||||
/// <summary>Builds the Titano Server web application from <see cref="ServerOptions"/> (used by Program and tests).</summary>
|
||||
/// <summary>
|
||||
/// Builds the Titano Server web application from <see cref="ServerOptions"/> (used by Program, the Windows service
|
||||
/// host and tests). The archive module plugs in through the <c>configureServices</c> callback of <see cref="Build"/>,
|
||||
/// which runs before the server's own registrations: it must register <see cref="IArchiveIngest"/> and
|
||||
/// <see cref="IFrameAssetLocator"/> (the server refuses to start without them), may add
|
||||
/// <see cref="Titano.Data.Jobs.IJobHandler"/>s, and may resolve <see cref="TitanoDatabase"/>, <see cref="ServerArchive"/>
|
||||
/// and <see cref="ServerOptions"/> from DI in its factories.
|
||||
/// </summary>
|
||||
public static class ServerHost
|
||||
{
|
||||
public static WebApplication Build(ServerOptions options, string[] args)
|
||||
/// <summary>Request bodies other than upload chunks stay small (BRIEF §14.2 limits).</summary>
|
||||
public const long JsonBodyLimit = 1024 * 1024;
|
||||
|
||||
public static WebApplication Build(ServerOptions options, string[] args, Action<IServiceCollection>? configureServices = null)
|
||||
{
|
||||
Directory.CreateDirectory(options.DataPath);
|
||||
Directory.CreateDirectory(options.LogPath);
|
||||
Directory.CreateDirectory(options.UploadsPath);
|
||||
Directory.CreateDirectory(options.ArchivePath);
|
||||
|
||||
var builder = WebApplication.CreateSlimBuilder(args);
|
||||
var builder = WebApplication.CreateSlimBuilder(new WebApplicationOptions { Args = args, ContentRootPath = AppContext.BaseDirectory });
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddSimpleConsole(c => c.SingleLine = true);
|
||||
builder.Logging.AddProvider(new FileLoggerProvider(new FileLogSink(options.LogPath, "server")));
|
||||
builder.Logging.AddFilter("Microsoft.AspNetCore", Microsoft.Extensions.Logging.LogLevel.Warning);
|
||||
builder.Logging.AddFilter("Microsoft.Hosting.Lifetime", Microsoft.Extensions.Logging.LogLevel.Information);
|
||||
|
||||
using var bootLoggerFactory = LoggerFactory.Create(l => l.AddSimpleConsole(c => c.SingleLine = true));
|
||||
var certificate = CertificateStore.Load(options, bootLoggerFactory.CreateLogger("Titano.Server"));
|
||||
var state = new ServerState { TlsFingerprint = certificate is null ? null : CertificateStore.Fingerprint(certificate) };
|
||||
// The archive module registers first, so a missing piece stops the server before it touches the database.
|
||||
configureServices?.Invoke(builder.Services);
|
||||
RequireArchiveModule(builder.Services);
|
||||
|
||||
builder.WebHost.ConfigureKestrel(k =>
|
||||
using var bootLoggerFactory = LoggerFactory.Create(l => l.AddSimpleConsole(c => c.SingleLine = true));
|
||||
var bootLogger = bootLoggerFactory.CreateLogger("Titano.Server");
|
||||
var certificate = CertificateStore.Load(options, bootLogger);
|
||||
var database = ServerDatabase.OpenAsync(options, bootLogger, CancellationToken.None).GetAwaiter().GetResult();
|
||||
try
|
||||
{
|
||||
k.AddServerHeader = false;
|
||||
k.Limits.MaxRequestBodySize = options.ChunkSizeBytes + 64 * 1024;
|
||||
k.ListenAnyIP(options.HttpPort);
|
||||
if (certificate is not null) k.ListenAnyIP(options.HttpsPort, l =>
|
||||
var archive = ServerArchive.OpenAsync(database, options, TimeProvider.System, CancellationToken.None).GetAwaiter().GetResult();
|
||||
AdminBootstrap.RunAsync(database, options, TimeProvider.System, bootLogger, CancellationToken.None).GetAwaiter().GetResult();
|
||||
var state = new ServerState
|
||||
{
|
||||
l.Protocols = HttpProtocols.Http1AndHttp2;
|
||||
l.UseHttps(certificate);
|
||||
TlsFingerprint = certificate is null ? null : CertificateStore.Fingerprint(certificate),
|
||||
Database = ServerDatabase.DescribeAsync(database, CancellationToken.None).GetAwaiter().GetResult(),
|
||||
};
|
||||
|
||||
builder.WebHost.ConfigureKestrel(k =>
|
||||
{
|
||||
k.AddServerHeader = false;
|
||||
k.Limits.MaxRequestBodySize = JsonBodyLimit;
|
||||
k.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);
|
||||
k.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
|
||||
k.ListenAnyIP(options.HttpPort);
|
||||
if (certificate is not null) k.ListenAnyIP(options.HttpsPort, l =>
|
||||
{
|
||||
l.Protocols = HttpProtocols.Http1AndHttp2;
|
||||
l.UseHttps(certificate);
|
||||
});
|
||||
});
|
||||
});
|
||||
builder.Services.AddSingleton(options);
|
||||
builder.Services.AddSingleton(state);
|
||||
builder.Services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default));
|
||||
ConfigureServices(builder.Services, options, state, database, archive);
|
||||
|
||||
var app = builder.Build();
|
||||
Log.SetSink(new LoggerSink(app.Services.GetRequiredService<ILoggerFactory>()));
|
||||
ConfigurePipeline(app, options);
|
||||
app.Logger.LogInformation("Titano Server {Version} · archive {Archive} · data {Data} · {Database} · TLS {Tls}{Fingerprint}",
|
||||
SystemEndpoints.Version, options.ArchivePath, options.DataPath, state.Database, options.TlsMode,
|
||||
state.TlsFingerprint is null ? string.Empty : " · SHA-256 " + state.TlsFingerprint);
|
||||
app.Lifetime.ApplicationStopping.Register(() => app.Logger.LogInformation("Titano Server stopping"));
|
||||
app.Lifetime.ApplicationStopped.Register(() => database.DisposeAsync().AsTask().GetAwaiter().GetResult());
|
||||
return app;
|
||||
}
|
||||
catch
|
||||
{
|
||||
database.DisposeAsync().AsTask().GetAwaiter().GetResult();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The single composition root of the server's services.</summary>
|
||||
public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, TitanoDatabase database, ServerArchive archive)
|
||||
{
|
||||
services.AddSingleton(options);
|
||||
services.AddSingleton(state);
|
||||
services.AddSingleton(database);
|
||||
services.AddSingleton(archive);
|
||||
services.TryAddSingleton(TimeProvider.System);
|
||||
services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default));
|
||||
services.Configure<RouteHandlerOptions>(o => o.ThrowOnBadRequest = true);
|
||||
services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30));
|
||||
if (options.TlsMode == TlsMode.Proxy)
|
||||
{
|
||||
builder.Services.Configure<ForwardedHeadersOptions>(f =>
|
||||
services.Configure<ForwardedHeadersOptions>(f =>
|
||||
{
|
||||
f.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
|
||||
f.KnownIPNetworks.Clear();
|
||||
@@ -50,49 +112,61 @@ public static class ServerHost
|
||||
});
|
||||
}
|
||||
|
||||
var app = builder.Build();
|
||||
Log.SetSink(new LoggerSink(app.Services.GetRequiredService<ILoggerFactory>()));
|
||||
var logger = app.Logger;
|
||||
logger.LogInformation("Titano Server {Version} · archive {Archive} · data {Data} · TLS {Tls}{Fingerprint}",
|
||||
SystemEndpoints.Version, options.ArchivePath, options.DataPath, options.TlsMode,
|
||||
state.TlsFingerprint is null ? string.Empty : " · SHA-256 " + state.TlsFingerprint);
|
||||
services.AddSingleton<EventHub>();
|
||||
services.AddSingleton<TokenService>();
|
||||
services.AddSingleton<LoginGuard>();
|
||||
services.AddSingleton<AuditLog>();
|
||||
services.AddSingleton<ServerSettingsStore>();
|
||||
services.AddSingleton<UploadStore>();
|
||||
services.AddSingleton<UploadService>();
|
||||
services.AddSingleton<RenderStore>();
|
||||
services.AddSingleton<SequenceJobs>();
|
||||
services.AddSingleton<RenderNodeService>();
|
||||
services.AddSingleton<JobEngineHost>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<JobEngineHost>());
|
||||
services.AddSingleton<RenderNodeMonitor>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<RenderNodeMonitor>());
|
||||
services.AddSingleton<UploadMaintenance>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<UploadMaintenance>());
|
||||
services.AddSingleton<DiscoveryBeaconService>();
|
||||
services.AddHostedService(sp => sp.GetRequiredService<DiscoveryBeaconService>());
|
||||
}
|
||||
|
||||
private static void ConfigurePipeline(WebApplication app, ServerOptions options)
|
||||
{
|
||||
if (options.TlsMode == TlsMode.Proxy) app.UseForwardedHeaders();
|
||||
app.UseMiddleware<ResponseMiddleware>();
|
||||
app.UseWebSockets(new WebSocketOptions { KeepAliveInterval = TimeSpan.FromSeconds(30) });
|
||||
app.UseRouting();
|
||||
app.UseMiddleware<AuthenticationMiddleware>();
|
||||
|
||||
app.Use(async (context, next) =>
|
||||
AdminPanel.Map(app, options);
|
||||
var api = app.MapGroup("/api/v1");
|
||||
SystemEndpoints.Map(app, api);
|
||||
AuthEndpoints.Map(api);
|
||||
UserEndpoints.Map(api);
|
||||
SequenceEndpoints.Map(api);
|
||||
FileEndpoints.Map(api);
|
||||
UploadEndpoints.Map(api);
|
||||
JobEndpoints.Map(api);
|
||||
NodeEndpoints.Map(api);
|
||||
ArchiveEndpoints.Map(api);
|
||||
AdminEndpoints.Map(api);
|
||||
EventEndpoints.Map(api);
|
||||
app.MapFallback("/api/{**path}", (HttpContext context) => ApiErrors.NotFound(context)).AllowAnonymousCaller();
|
||||
}
|
||||
|
||||
/// <summary>Fails fast when the archive module's services are missing (the server would accept uploads it cannot commit).</summary>
|
||||
private static void RequireArchiveModule(IServiceCollection services)
|
||||
{
|
||||
var missing = new List<string>();
|
||||
if (!services.Any(d => d.ServiceType == typeof(IArchiveIngest))) missing.Add($"{typeof(IArchiveIngest).FullName} (commits verified uploads into the archive layout)");
|
||||
if (!services.Any(d => d.ServiceType == typeof(IFrameAssetLocator))) missing.Add($"{typeof(IFrameAssetLocator).FullName} (finds proxies and thumbnails)");
|
||||
if (missing.Count > 0)
|
||||
{
|
||||
context.Response.Headers[ApiVersion.Header] = ApiVersion.Current.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
context.Response.Headers.XContentTypeOptions = "nosniff";
|
||||
context.Response.Headers["Referrer-Policy"] = "no-referrer";
|
||||
try
|
||||
{
|
||||
await next(context);
|
||||
}
|
||||
catch (Exception ex) when (!context.Response.HasStarted && ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogError(ex, "Unhandled error on {Method} {Path}", context.Request.Method, context.Request.Path);
|
||||
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
|
||||
await context.Response.WriteAsJsonAsync(new ApiError("internal", "Unexpected server error."), ProtocolJson.Default.ApiError);
|
||||
}
|
||||
});
|
||||
|
||||
var admin = new EmbeddedStaticFiles(typeof(ServerHost).Assembly, "admin/");
|
||||
app.MapGet("/", () => Results.Redirect("/admin/"));
|
||||
app.MapMethods("/admin/{**path}", ["GET", "HEAD"], async (HttpContext context, string? path) =>
|
||||
{
|
||||
// Relative asset URLs need the trailing slash.
|
||||
if (!context.Request.Path.Value!.EndsWith('/') && string.IsNullOrEmpty(path))
|
||||
{
|
||||
context.Response.Redirect("/admin/");
|
||||
return;
|
||||
}
|
||||
if (!await admin.TryServeAsync(context, path ?? string.Empty)) context.Response.StatusCode = StatusCodes.Status404NotFound;
|
||||
});
|
||||
|
||||
SystemEndpoints.Map(app, options, state);
|
||||
|
||||
app.MapFallback("/api/{**path}", () => Results.Json(new ApiError("not_found", "No such endpoint."), ProtocolJson.Default.ApiError, statusCode: 404));
|
||||
app.Lifetime.ApplicationStopping.Register(() => logger.LogInformation("Titano Server stopping"));
|
||||
return app;
|
||||
throw new InvalidOperationException(
|
||||
"Titano Server cannot start: no implementation is registered for " + string.Join(" and ", missing) +
|
||||
". Register them in the services callback of ServerHost.Build.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using Titano.Core.Localization;
|
||||
using static Titano.Server.Hosting.Windows.ServiceNative;
|
||||
|
||||
namespace Titano.Server.Hosting.Windows;
|
||||
|
||||
/// <summary>
|
||||
/// <c>--install-service</c> / <c>--uninstall-service</c>: registers the server with the Service Control Manager
|
||||
/// (automatic start, LocalSystem, command line <c>"…\Titano.Server.exe" --service</c>) or stops and removes it.
|
||||
/// Both need an elevated prompt; failures surface as <see cref="Win32Exception"/>.
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static unsafe class ServiceInstaller
|
||||
{
|
||||
private const string DisplayName = "Titano Server";
|
||||
|
||||
public static void Install()
|
||||
{
|
||||
var manager = OpenSCManagerW(null, null, ScManagerConnect | ScManagerCreateService);
|
||||
if (manager == 0) throw new Win32Exception(Marshal.GetLastPInvokeError());
|
||||
try
|
||||
{
|
||||
var service = CreateServiceW(manager, WindowsServiceHost.ServiceName, DisplayName, ServiceAllAccess, ServiceWin32OwnProcess, ServiceAutoStart,
|
||||
ServiceErrorNormal, CommandLine(), null, 0, null, null, null);
|
||||
if (service == 0) throw new Win32Exception(Marshal.GetLastPInvokeError());
|
||||
try
|
||||
{
|
||||
fixed (char* text = Loc.L("Server.Service.Description"))
|
||||
{
|
||||
var description = new ServiceDescription { Description = text };
|
||||
ChangeServiceConfig2W(service, ServiceConfigDescription, &description);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseServiceHandle(service);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseServiceHandle(manager);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Uninstall()
|
||||
{
|
||||
var manager = OpenSCManagerW(null, null, ScManagerConnect);
|
||||
if (manager == 0) throw new Win32Exception(Marshal.GetLastPInvokeError());
|
||||
try
|
||||
{
|
||||
var service = OpenServiceW(manager, WindowsServiceHost.ServiceName, Delete | ServiceStop | ServiceQueryStatus);
|
||||
if (service == 0) throw new Win32Exception(Marshal.GetLastPInvokeError());
|
||||
try
|
||||
{
|
||||
ServiceStatus status;
|
||||
if (!ControlService(service, ControlStop, &status) && Marshal.GetLastPInvokeError() is var stopError && stopError != ErrorServiceNotActive)
|
||||
throw new Win32Exception(stopError);
|
||||
if (!DeleteService(service) && Marshal.GetLastPInvokeError() is var deleteError && deleteError != ErrorServiceMarkedForDelete)
|
||||
throw new Win32Exception(deleteError);
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseServiceHandle(service);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
CloseServiceHandle(manager);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The service's command line: this executable (or <c>dotnet</c> plus this assembly) with <c>--service</c>.</summary>
|
||||
private static string CommandLine()
|
||||
{
|
||||
var process = Environment.ProcessPath ?? throw new InvalidOperationException("The executable path is unknown.");
|
||||
var assembly = typeof(ServiceInstaller).Assembly.Location;
|
||||
return Path.GetFileNameWithoutExtension(process).Equals("dotnet", StringComparison.OrdinalIgnoreCase)
|
||||
? $"\"{process}\" \"{assembly}\" --service"
|
||||
: $"\"{process}\" --service";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
|
||||
namespace Titano.Server.Hosting.Windows;
|
||||
|
||||
/// <summary>Service Control Manager functions of advapi32 (no Windows-only package needed).</summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal static unsafe partial class ServiceNative
|
||||
{
|
||||
public const uint ServiceWin32OwnProcess = 0x10;
|
||||
public const uint ServiceAutoStart = 2;
|
||||
public const uint ServiceErrorNormal = 1;
|
||||
public const uint ScManagerConnect = 0x1;
|
||||
public const uint ScManagerCreateService = 0x2;
|
||||
public const uint ServiceAllAccess = 0xF01FF;
|
||||
public const uint ServiceQueryStatus = 0x4;
|
||||
public const uint ServiceStop = 0x20;
|
||||
public const uint Delete = 0x10000;
|
||||
public const uint ServiceConfigDescription = 1;
|
||||
|
||||
public const uint ControlStop = 1;
|
||||
public const uint ControlInterrogate = 4;
|
||||
public const uint ControlShutdown = 5;
|
||||
|
||||
public const int ErrorFailedServiceControllerConnect = 1063;
|
||||
public const int ErrorServiceNotActive = 1062;
|
||||
public const int ErrorServiceMarkedForDelete = 1072;
|
||||
public const uint NoError = 0;
|
||||
public const uint ErrorCallNotImplemented = 120;
|
||||
public const uint ErrorExceptionInService = 1064;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct ServiceStatus
|
||||
{
|
||||
public uint ServiceType;
|
||||
public uint CurrentState;
|
||||
public uint ControlsAccepted;
|
||||
public uint Win32ExitCode;
|
||||
public uint ServiceSpecificExitCode;
|
||||
public uint CheckPoint;
|
||||
public uint WaitHint;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct ServiceTableEntry
|
||||
{
|
||||
public char* ServiceName;
|
||||
public delegate* unmanaged[Stdcall]<uint, char**, void> ServiceProc;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct ServiceDescription
|
||||
{
|
||||
public char* Description;
|
||||
}
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool StartServiceCtrlDispatcherW(ServiceTableEntry* table);
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
|
||||
public static partial nint RegisterServiceCtrlHandlerExW(string serviceName, delegate* unmanaged[Stdcall]<uint, uint, nint, nint, uint> handler, nint context);
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool SetServiceStatus(nint statusHandle, ServiceStatus* status);
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
|
||||
public static partial nint OpenSCManagerW(string? machineName, string? databaseName, uint desiredAccess);
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
|
||||
public static partial nint CreateServiceW(nint manager, string serviceName, string displayName, uint desiredAccess, uint serviceType, uint startType,
|
||||
uint errorControl, string binaryPathName, string? loadOrderGroup, nint tagId, string? dependencies, string? serviceStartName, string? password);
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
|
||||
public static partial nint OpenServiceW(nint manager, string serviceName, uint desiredAccess);
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool ChangeServiceConfig2W(nint service, uint infoLevel, void* info);
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool ControlService(nint service, uint control, ServiceStatus* status);
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool DeleteService(nint service);
|
||||
|
||||
[LibraryImport("advapi32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static partial bool CloseServiceHandle(nint handle);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
namespace Titano.Server.Hosting.Windows;
|
||||
|
||||
/// <summary>SERVICE_STATUS.dwCurrentState values the server reports.</summary>
|
||||
public enum ServiceState : uint
|
||||
{
|
||||
Stopped = 1,
|
||||
StartPending = 2,
|
||||
StopPending = 3,
|
||||
Running = 4,
|
||||
}
|
||||
|
||||
/// <summary>One status report for <c>SetServiceStatus</c>.</summary>
|
||||
public readonly record struct ServiceStatusReport(ServiceState State, uint ControlsAccepted, uint CheckPoint, uint WaitHintMilliseconds, uint ExitCode);
|
||||
|
||||
/// <summary>
|
||||
/// The service's state machine and the rules of SERVICE_STATUS: no controls are accepted while pending, the check point
|
||||
/// grows with every pending report and is zero otherwise, a stopped service carries its exit code.
|
||||
/// </summary>
|
||||
public sealed class ServiceStatusTracker
|
||||
{
|
||||
public const uint AcceptStop = 0x1;
|
||||
public const uint AcceptShutdown = 0x4;
|
||||
|
||||
private readonly Lock _gate = new();
|
||||
private uint _checkPoint;
|
||||
|
||||
public ServiceStatusReport Current { get; private set; } = new(ServiceState.Stopped, 0, 0, 0, 0);
|
||||
|
||||
public ServiceStatusReport StartPending(uint waitHintMilliseconds) => Pending(ServiceState.StartPending, waitHintMilliseconds, ServiceState.Stopped, ServiceState.StartPending);
|
||||
|
||||
public ServiceStatusReport Running()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
Require(ServiceState.StartPending);
|
||||
_checkPoint = 0;
|
||||
return Current = new ServiceStatusReport(ServiceState.Running, AcceptStop | AcceptShutdown, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Stopping from start-up or from running; repeating it reports progress (a higher check point).</summary>
|
||||
public ServiceStatusReport StopPending(uint waitHintMilliseconds) =>
|
||||
Pending(ServiceState.StopPending, waitHintMilliseconds, ServiceState.StartPending, ServiceState.Running, ServiceState.StopPending);
|
||||
|
||||
public ServiceStatusReport Stopped(uint exitCode)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_checkPoint = 0;
|
||||
return Current = new ServiceStatusReport(ServiceState.Stopped, 0, 0, 0, exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceStatusReport Pending(ServiceState state, uint waitHintMilliseconds, params ServiceState[] from)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
Require(from);
|
||||
_checkPoint = Current.State == state ? _checkPoint + 1 : 1;
|
||||
return Current = new ServiceStatusReport(state, 0, _checkPoint, waitHintMilliseconds, 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void Require(params ServiceState[] allowed)
|
||||
{
|
||||
if (!allowed.Contains(Current.State)) throw new InvalidOperationException($"A service cannot go from {Current.State} to that state.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using static Titano.Server.Hosting.Windows.ServiceNative;
|
||||
|
||||
namespace Titano.Server.Hosting.Windows;
|
||||
|
||||
/// <summary>
|
||||
/// Runs the server as a Windows service with the SCM API directly (StartServiceCtrlDispatcherW,
|
||||
/// RegisterServiceCtrlHandlerExW, SetServiceStatus). Stop and shutdown requests stop the web host gracefully: running
|
||||
/// jobs keep their checkpoints and resume at the next start.
|
||||
/// </summary>
|
||||
[SupportedOSPlatform("windows")]
|
||||
public static class WindowsServiceHost
|
||||
{
|
||||
public const string ServiceName = "TitanoServer";
|
||||
private const uint StartWaitHint = 60_000;
|
||||
private const uint StopWaitHint = 35_000;
|
||||
|
||||
private static readonly ServiceStatusTracker Status = new();
|
||||
private static Func<WebApplication>? _build;
|
||||
private static WebApplication? _app;
|
||||
private static nint _statusHandle;
|
||||
|
||||
/// <summary>
|
||||
/// Hands the process to the Service Control Manager and returns when the service has stopped. False when the
|
||||
/// process was not started by the SCM (for example from a console).
|
||||
/// </summary>
|
||||
public static unsafe bool Run(Func<WebApplication> build)
|
||||
{
|
||||
_build = build;
|
||||
fixed (char* name = ServiceName)
|
||||
{
|
||||
var table = stackalloc ServiceTableEntry[2];
|
||||
table[0] = new ServiceTableEntry { ServiceName = name, ServiceProc = &ServiceMain };
|
||||
table[1] = default;
|
||||
if (StartServiceCtrlDispatcherW(table)) return true;
|
||||
}
|
||||
var error = Marshal.GetLastPInvokeError();
|
||||
if (error == ErrorFailedServiceControllerConnect) return false;
|
||||
throw new Win32Exception(error);
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
|
||||
private static unsafe void ServiceMain(uint argc, char** argv)
|
||||
{
|
||||
_statusHandle = RegisterServiceCtrlHandlerExW(ServiceName, &Handler, 0);
|
||||
if (_statusHandle == 0) return;
|
||||
Report(Status.StartPending(StartWaitHint));
|
||||
try
|
||||
{
|
||||
_app = _build!();
|
||||
_app.StartAsync().GetAwaiter().GetResult();
|
||||
Report(Status.Running());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex);
|
||||
Report(Status.Stopped(ErrorExceptionInService));
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
|
||||
private static unsafe uint Handler(uint control, uint eventType, nint eventData, nint context)
|
||||
{
|
||||
switch (control)
|
||||
{
|
||||
case ControlStop or ControlShutdown:
|
||||
if (Status.Current.State is ServiceState.Running or ServiceState.StartPending)
|
||||
{
|
||||
Report(Status.StopPending(StopWaitHint));
|
||||
// The handler must return at once; the host stops on the thread pool.
|
||||
_ = Task.Run(StopAsync);
|
||||
}
|
||||
return NoError;
|
||||
case ControlInterrogate:
|
||||
Report(Status.Current);
|
||||
return NoError;
|
||||
default:
|
||||
return ErrorCallNotImplemented;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task StopAsync()
|
||||
{
|
||||
uint exitCode = 0;
|
||||
try
|
||||
{
|
||||
if (_app is { } app)
|
||||
{
|
||||
await app.StopAsync();
|
||||
await app.DisposeAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex);
|
||||
exitCode = ErrorExceptionInService;
|
||||
}
|
||||
Report(Status.Stopped(exitCode));
|
||||
}
|
||||
|
||||
private static unsafe void Report(ServiceStatusReport report)
|
||||
{
|
||||
var status = new ServiceStatus
|
||||
{
|
||||
ServiceType = ServiceWin32OwnProcess,
|
||||
CurrentState = (uint)report.State,
|
||||
ControlsAccepted = report.ControlsAccepted,
|
||||
Win32ExitCode = report.ExitCode,
|
||||
CheckPoint = report.CheckPoint,
|
||||
WaitHint = report.WaitHintMilliseconds,
|
||||
};
|
||||
SetServiceStatus(_statusHandle, &status);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using System.Diagnostics;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Jobs;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Api;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Events;
|
||||
|
||||
namespace Titano.Server.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Hosts the server's <see cref="JobEngine"/> (BRIEF §14.6) with the <see cref="IJobHandler"/>s registered in DI and
|
||||
/// relays its state changes and progress as WebSocket events. Render jobs are never run here: render nodes claim them
|
||||
/// (BRIEF §14.7). On shutdown running jobs stay marked running and are requeued from their checkpoints at the next start.
|
||||
/// </summary>
|
||||
public sealed class JobEngineHost : IHostedService
|
||||
{
|
||||
private static readonly long ProgressInterval = Stopwatch.Frequency / 4;
|
||||
|
||||
private readonly TitanoDatabase _db;
|
||||
private readonly EventHub _events;
|
||||
private readonly HashSet<JobKind> _kinds;
|
||||
private readonly Dictionary<Guid, long> _lastProgress = [];
|
||||
private readonly Lock _gate = new();
|
||||
|
||||
public JobEngineHost(TitanoDatabase db, IEnumerable<IJobHandler> handlers, ServerOptions options, EventHub events)
|
||||
{
|
||||
_db = db;
|
||||
_events = events;
|
||||
var local = handlers.Where(h => h.Kind != JobKind.Render).ToList();
|
||||
_kinds = [.. local.Select(h => h.Kind)];
|
||||
// Without handlers no worker starts: an engine with an empty kind list would claim every kind.
|
||||
Workers = _kinds.Count == 0 ? 0 : options.Workers;
|
||||
Engine = new JobEngine(db, local, new JobEngineOptions
|
||||
{
|
||||
Workers = Workers,
|
||||
WorkerId = WorkerId,
|
||||
Kinds = _kinds,
|
||||
HistoryRetention = TimeSpan.FromDays(options.JobHistoryDays),
|
||||
});
|
||||
Engine.JobChanged += OnJobChanged;
|
||||
Engine.Progress += OnProgress;
|
||||
}
|
||||
|
||||
/// <summary>Node id stamped on jobs this server runs (its workers use <c>WorkerId#n</c>).</summary>
|
||||
public static string WorkerId { get; } = "server:" + Environment.MachineName;
|
||||
|
||||
public JobEngine Engine { get; }
|
||||
|
||||
public int Workers { get; }
|
||||
|
||||
/// <summary>True when a job of <paramref name="kind"/> has somewhere to run: a local handler, or render nodes for renders.</summary>
|
||||
public bool CanRun(JobKind kind) => kind == JobKind.Render || _kinds.Contains(kind);
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken) => Engine.StartAsync(cancellationToken);
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken) => await Engine.DisposeAsync();
|
||||
|
||||
/// <summary>Publishes a state change made outside the engine (render nodes, cancellation of node jobs).</summary>
|
||||
public void Publish(Job job) => OnJobChanged(job);
|
||||
|
||||
private void OnJobChanged(Job job)
|
||||
{
|
||||
if (job.State != JobState.Running)
|
||||
{
|
||||
lock (_gate) _lastProgress.Remove(job.Id);
|
||||
}
|
||||
_events.Publish(new JobStateEvent(job.Id, JobWire.State(job.State), job.Error));
|
||||
}
|
||||
|
||||
private void OnProgress(JobProgressInfo progress)
|
||||
{
|
||||
var now = Stopwatch.GetTimestamp();
|
||||
lock (_gate)
|
||||
{
|
||||
if (_lastProgress.TryGetValue(progress.JobId, out var last) && now - last < ProgressInterval && progress.Done < progress.Total) return;
|
||||
_lastProgress[progress.JobId] = now;
|
||||
}
|
||||
_events.Publish(new JobProgressEvent(progress.JobId, progress.Done, progress.Total, progress.ItemsPerSecond, progress.Eta?.TotalSeconds));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Events;
|
||||
|
||||
namespace Titano.Server.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Marks render nodes offline when they miss heartbeats for <see cref="ServerOptions.NodeTimeoutSeconds"/> and gives
|
||||
/// their running jobs back to the queue for another node; a job that lost its node <see cref="MaxAttempts"/> times fails.
|
||||
/// </summary>
|
||||
public sealed class RenderNodeMonitor(TitanoDatabase db, ServerOptions options, JobEngineHost jobs, EventHub events, TimeProvider time,
|
||||
ILogger<RenderNodeMonitor> logger) : BackgroundService
|
||||
{
|
||||
public const int MaxAttempts = 5;
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(Math.Clamp(options.NodeTimeoutSeconds / 3.0, 1, 30));
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await SweepAsync(stoppingToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogWarning(ex, "Render node check failed");
|
||||
}
|
||||
try
|
||||
{
|
||||
await Task.Delay(interval, time, stoppingToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One pass: silent nodes go offline and their jobs are requeued (or failed after too many attempts).</summary>
|
||||
public async Task SweepAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var silentSince = time.GetUtcNow() - TimeSpan.FromSeconds(options.NodeTimeoutSeconds);
|
||||
foreach (var node in await db.RenderNodes.ListSilentAsync(silentSince, RenderNodeService.Offline, cancellationToken))
|
||||
{
|
||||
await db.RenderNodes.SetStateAsync(node.Id, RenderNodeService.Offline, cancellationToken);
|
||||
events.Publish(new NodeStateEvent(node.Id, node.ClientName, RenderNodeService.Offline));
|
||||
var key = RenderNodeService.NodeKey(node);
|
||||
var lost = await db.Jobs.ListRunningByNodeAsync(key, cancellationToken);
|
||||
foreach (var job in lost)
|
||||
{
|
||||
var line = RenderNodeService.AppendLog(job.LogTail, $"Render node {node.ClientName} stopped responding.");
|
||||
if (job.Attempts >= MaxAttempts)
|
||||
{
|
||||
await db.Jobs.FinishNodeJobAsync(job.Id, key, JobState.Failed, $"Lost its render node {job.Attempts} times.", line, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
await db.Jobs.UpdateNodeProgressAsync(job.Id, key, job.ProgressDone, job.ProgressTotal, null, null, line, cancellationToken);
|
||||
}
|
||||
}
|
||||
var requeued = await db.Jobs.RequeueRunningAsync(key, cancellationToken);
|
||||
logger.LogWarning("Render node {Node} is offline; {Count} job(s) back in the queue", node.ClientName, requeued);
|
||||
foreach (var job in lost)
|
||||
{
|
||||
if (await db.Jobs.GetAsync(job.Id, cancellationToken) is { } current) jobs.Publish(current);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Repositories;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Api;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Events;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
using UploadState = Titano.Data.Repositories.UploadState;
|
||||
|
||||
namespace Titano.Server.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Render nodes (BRIEF §14.7): clients with the render-node right register their encoders, heartbeat, claim queued
|
||||
/// render jobs they can encode, report progress, and upload the output in resumable chunks that become a render of
|
||||
/// the sequence. A node that stops heart-beating loses its jobs to the queue (<see cref="RenderNodeMonitor"/>).
|
||||
/// </summary>
|
||||
public sealed class RenderNodeService(
|
||||
TitanoDatabase db,
|
||||
ServerOptions options,
|
||||
UploadService uploads,
|
||||
UploadStore store,
|
||||
RenderStore renders,
|
||||
JobEngineHost jobs,
|
||||
EventHub events,
|
||||
TimeProvider time)
|
||||
{
|
||||
public const string Idle = "idle";
|
||||
public const string Busy = "busy";
|
||||
public const string Offline = "offline";
|
||||
private const int LogTailLines = 40;
|
||||
|
||||
public async Task<Outcome<RenderNodeDto>> RegisterAsync(Caller caller, RegisterNodeRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var name = request.ClientName?.Trim();
|
||||
if (string.IsNullOrEmpty(name) || name.Length > 100) return Outcome<RenderNodeDto>.Fail(400, "bad_request", "clientName");
|
||||
if (request.Encoders is null || request.Encoders.Count > 64 || request.Encoders.Any(e => string.IsNullOrWhiteSpace(e?.Codec)))
|
||||
return Outcome<RenderNodeDto>.Fail(400, "bad_request", "encoders");
|
||||
|
||||
var existing = (await db.RenderNodes.ListAsync(cancellationToken)).FirstOrDefault(n => n.UserId == caller.Id && n.ClientName == name);
|
||||
var id = existing?.Id ?? Guid.CreateVersion7();
|
||||
var running = await db.Jobs.ListRunningByNodeAsync(id.ToString(), cancellationToken);
|
||||
var node = new RenderNode
|
||||
{
|
||||
Id = id,
|
||||
UserId = caller.Id,
|
||||
ClientName = name,
|
||||
CapabilitiesJson = JsonSerializer.Serialize(request with { ClientName = name }, ProtocolJson.Default.RegisterNodeRequest),
|
||||
LastHeartbeat = time.GetUtcNow(),
|
||||
State = running.Count > 0 ? Busy : Idle,
|
||||
};
|
||||
await db.RenderNodes.RegisterAsync(node, cancellationToken);
|
||||
events.Publish(new NodeStateEvent(node.Id, node.ClientName, node.State));
|
||||
return Dto.Node(node, caller.User.Username);
|
||||
}
|
||||
|
||||
public async Task<Outcome<bool>> HeartbeatAsync(Caller caller, Guid nodeId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome<bool>.Fail(404, "not_found", "node");
|
||||
await TouchAsync(node, cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>The next queued render job this node can encode (highest priority, then oldest), or null when none fits.</summary>
|
||||
public async Task<Outcome<ClaimedJob?>> ClaimAsync(Caller caller, Guid nodeId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome<ClaimedJob?>.Fail(404, "not_found", "node");
|
||||
var capabilities = Dto.Capabilities(node);
|
||||
foreach (var queued in await db.Jobs.ListQueuedAsync(JobKind.Render, 50, cancellationToken))
|
||||
{
|
||||
if (JobEndpoints.ParseRender(queued.ParametersJson) is not { } parameters || capabilities is null || !CanEncode(capabilities, parameters)) continue;
|
||||
if (await db.Jobs.TryClaimAsync(queued.Id, NodeKey(node), cancellationToken) is not { } job) continue;
|
||||
jobs.Publish(job);
|
||||
if (job.TargetIds.Count != 1 || await db.Sequences.GetAsync(job.TargetIds[0], cancellationToken) is not { } sequence)
|
||||
{
|
||||
await FinishAsync(node, job, JobState.Failed, "The sequence no longer exists.", cancellationToken);
|
||||
continue;
|
||||
}
|
||||
await TouchAsync(node, cancellationToken);
|
||||
return new ClaimedJob(Dto.Job(job), sequence.Id, parameters.SettingsJson ?? sequence.SettingsJson ?? "{}", parameters.PresetJson,
|
||||
parameters.UseProxies, await ManifestFramesAsync(sequence, cancellationToken));
|
||||
}
|
||||
await TouchAsync(node, cancellationToken);
|
||||
return (ClaimedJob?)null;
|
||||
}
|
||||
|
||||
public async Task<Outcome<bool>> ProgressAsync(Caller caller, Guid nodeId, Guid jobId, NodeProgress progress, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome<bool>.Fail(404, "not_found", "node");
|
||||
if (await AssignedJobAsync(node, jobId, cancellationToken) is not { } job) return Outcome<bool>.Fail(409, "job_not_assigned");
|
||||
double? eta = progress.ItemsPerSecond is > 0 && progress.Total > progress.Done ? (progress.Total - progress.Done) / progress.ItemsPerSecond.Value : null;
|
||||
var tail = AppendLog(job.LogTail, progress.LogLine);
|
||||
if (!await db.Jobs.UpdateNodeProgressAsync(jobId, NodeKey(node), progress.Done, progress.Total, progress.ItemsPerSecond,
|
||||
eta is { } seconds ? TimeSpan.FromSeconds(seconds) : null, tail, cancellationToken))
|
||||
return Outcome<bool>.Fail(409, "job_not_assigned");
|
||||
await TouchAsync(node, cancellationToken);
|
||||
events.Publish(new JobProgressEvent(jobId, progress.Done, progress.Total, progress.ItemsPerSecond, eta));
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<Outcome<bool>> FailAsync(Caller caller, Guid nodeId, Guid jobId, NodeJobFailure failure, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome<bool>.Fail(404, "not_found", "node");
|
||||
if (await AssignedJobAsync(node, jobId, cancellationToken) is not { } job) return Outcome<bool>.Fail(409, "job_not_assigned");
|
||||
var error = string.IsNullOrWhiteSpace(failure.Error) ? "Render failed on the node." : failure.Error.Trim()[..Math.Min(failure.Error.Trim().Length, 500)];
|
||||
return await FinishAsync(node, job, JobState.Failed, error, cancellationToken) ? true : Outcome<bool>.Fail(409, "job_not_assigned");
|
||||
}
|
||||
|
||||
/// <summary>Starts (or resumes) the chunked upload of a job's output.</summary>
|
||||
public async Task<Outcome<StartUploadResponse>> StartResultAsync(Caller caller, Guid nodeId, Guid jobId, RenderResultRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome<StartUploadResponse>.Fail(404, "not_found", "node");
|
||||
if (await AssignedJobAsync(node, jobId, cancellationToken) is not { } job) return Outcome<StartUploadResponse>.Fail(409, "job_not_assigned");
|
||||
if (UploadRules.SafeFileName(request.FileName) is not { } fileName) return Outcome<StartUploadResponse>.Fail(400, "invalid_file_name");
|
||||
if (!UploadRules.IsSha256(request.Sha256)) return Outcome<StartUploadResponse>.Fail(400, "invalid_hash");
|
||||
if (request.Size <= 0) return Outcome<StartUploadResponse>.Fail(400, "bad_request", "size");
|
||||
if (request.Size > options.MaxUploadBytes)
|
||||
return Outcome<StartUploadResponse>.Fail(413, "too_large", options.MaxUploadBytes.ToString(CultureInfo.InvariantCulture));
|
||||
if (string.IsNullOrWhiteSpace(request.PresetName)) return Outcome<StartUploadResponse>.Fail(400, "bad_request", "presetName");
|
||||
|
||||
var sha = request.Sha256.ToLowerInvariant();
|
||||
if (await db.Uploads.FindByJobAsync(jobId, cancellationToken) is { State: UploadState.Receiving } open && open.Sha256 == sha && open.Size == request.Size
|
||||
&& store.Exists(open.Id))
|
||||
return Started(open);
|
||||
if (UploadRules.FreeBytes(options.UploadsPath) is { } free && free < request.Size) return Outcome<StartUploadResponse>.Fail(507, "insufficient_storage");
|
||||
|
||||
var now = time.GetUtcNow();
|
||||
var upload = await db.Uploads.CreateAsync(new Upload
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
UserId = caller.Id,
|
||||
SequenceId = job.TargetIds.Count == 1 ? job.TargetIds[0] : null,
|
||||
FileName = fileName,
|
||||
Size = request.Size,
|
||||
Sha256 = sha,
|
||||
ChunkSize = options.ChunkSizeBytes,
|
||||
State = UploadState.Receiving,
|
||||
Created = now,
|
||||
UpdatedAt = now,
|
||||
JobId = jobId,
|
||||
RenderJson = JsonSerializer.Serialize(request with { FileName = fileName, Sha256 = sha }, ProtocolJson.Default.RenderResultRequest),
|
||||
}, cancellationToken);
|
||||
store.Create(upload.Id, upload.Size);
|
||||
return Started(upload);
|
||||
}
|
||||
|
||||
/// <summary>Verifies the uploaded output, files it as a render of the sequence and marks the job done.</summary>
|
||||
public async Task<Outcome<RenderDto>> CompleteResultAsync(Caller caller, Guid nodeId, Guid jobId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (await OwnedNodeAsync(caller, nodeId, cancellationToken) is not { } node) return Outcome<RenderDto>.Fail(404, "not_found", "node");
|
||||
if (await AssignedJobAsync(node, jobId, cancellationToken) is not { } job) return Outcome<RenderDto>.Fail(409, "job_not_assigned");
|
||||
var upload = await db.Uploads.FindByJobAsync(jobId, cancellationToken);
|
||||
if (upload is not { State: UploadState.Receiving } || upload.RenderJson is null) return Outcome<RenderDto>.Fail(409, "upload_state");
|
||||
if (!upload.IsComplete)
|
||||
return Outcome<RenderDto>.Fail(409, "incomplete", (upload.ChunkCount - upload.ReceivedCount).ToString(CultureInfo.InvariantCulture));
|
||||
if (!await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Receiving, UploadState.Committing, cancellationToken: cancellationToken))
|
||||
return Outcome<RenderDto>.Fail(409, "upload_state");
|
||||
|
||||
if (await uploads.StageVerifiedAsync(upload, cancellationToken) is not { } staged) return Outcome<RenderDto>.Fail(422, "hash_mismatch");
|
||||
var output = JsonSerializer.Deserialize(upload.RenderJson, ProtocolJson.Default.RenderResultRequest)!;
|
||||
var sequence = await db.Sequences.GetAsync(job.TargetIds[0], cancellationToken);
|
||||
var render = sequence is null ? null : await renders.CommitAsync(sequence, staged, output, node.ClientName, cancellationToken);
|
||||
if (render is null)
|
||||
{
|
||||
UploadStore.TryDelete(staged);
|
||||
await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Failed, cancellationToken: CancellationToken.None);
|
||||
store.Delete(upload.Id);
|
||||
await FinishAsync(node, job, JobState.Failed, "The sequence has no folder in the archive.", CancellationToken.None);
|
||||
return Outcome<RenderDto>.Fail(409, "upload_state", "sequence");
|
||||
}
|
||||
await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Committed, cancellationToken: CancellationToken.None);
|
||||
store.Delete(upload.Id);
|
||||
await FinishAsync(node, job, JobState.Done, null, CancellationToken.None);
|
||||
if (await db.Sequences.GetAsync(sequence!.Id, CancellationToken.None) is { } updated)
|
||||
events.Publish(new SequenceChangedEvent(updated.Id, "renders", updated.FramesVersion, updated.SettingsVersion));
|
||||
return Dto.Render(render);
|
||||
}
|
||||
|
||||
/// <summary>Running-job owner key stored in <c>jobs.node_id</c>.</summary>
|
||||
public static string NodeKey(RenderNode node) => node.Id.ToString();
|
||||
|
||||
private async Task<RenderNode?> OwnedNodeAsync(Caller caller, Guid nodeId, CancellationToken cancellationToken) =>
|
||||
await db.RenderNodes.GetAsync(nodeId, cancellationToken) is { } node && node.UserId == caller.Id ? node : null;
|
||||
|
||||
private async Task<Job?> AssignedJobAsync(RenderNode node, Guid jobId, CancellationToken cancellationToken) =>
|
||||
await db.Jobs.GetAsync(jobId, cancellationToken) is { State: JobState.Running } job && job.NodeId == NodeKey(node) ? job : null;
|
||||
|
||||
private async Task<bool> FinishAsync(RenderNode node, Job job, JobState state, string? error, CancellationToken cancellationToken)
|
||||
{
|
||||
var tail = error is null ? null : AppendLog(job.LogTail, error);
|
||||
if (!await db.Jobs.FinishNodeJobAsync(job.Id, NodeKey(node), state, error, tail, cancellationToken)) return false;
|
||||
if (await db.Jobs.GetAsync(job.Id, cancellationToken) is { } finished) jobs.Publish(finished);
|
||||
await TouchAsync(node, cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Records a sign of life and publishes the node's state when it changed.</summary>
|
||||
private async Task TouchAsync(RenderNode node, CancellationToken cancellationToken)
|
||||
{
|
||||
var state = (await db.Jobs.ListRunningByNodeAsync(NodeKey(node), cancellationToken)).Count > 0 ? Busy : Idle;
|
||||
await db.RenderNodes.HeartbeatAsync(node.Id, state, time.GetUtcNow(), cancellationToken);
|
||||
if (state != node.State) events.Publish(new NodeStateEvent(node.Id, node.ClientName, state));
|
||||
}
|
||||
|
||||
private async Task<List<ManifestFrame>> ManifestFramesAsync(Sequence sequence, CancellationToken cancellationToken)
|
||||
{
|
||||
var hasProxy = sequence.Status.HasFlag(SequenceStatus.ProxiesBuilt);
|
||||
var hasThumb = sequence.Status.HasFlag(SequenceStatus.ThumbsBuilt);
|
||||
var frames = new List<ManifestFrame>(sequence.FrameCount);
|
||||
var after = -1;
|
||||
while (true)
|
||||
{
|
||||
var batch = await db.Frames.ListBySequenceAsync(sequence.Id, after, 1000, cancellationToken);
|
||||
frames.AddRange(batch.Where(f => !f.Flags.HasFlag(FrameFlags.Excluded))
|
||||
.Select(f => new ManifestFrame(f.Id, f.Index, f.FileName, f.Size, f.Sha256 ?? string.Empty, hasProxy, hasThumb)));
|
||||
if (batch.Count < 1000) return frames;
|
||||
after = batch[^1].Index;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The node has an encoder for the job's codec and handles its resolution (0 = no announced limit).</summary>
|
||||
private static bool CanEncode(RegisterNodeRequest node, RenderJobParameters job) =>
|
||||
node.Encoders.Any(e => string.Equals(e.Codec, job.Codec, StringComparison.OrdinalIgnoreCase))
|
||||
&& (node.MaxWidth <= 0 || job.Width <= node.MaxWidth)
|
||||
&& (node.MaxHeight <= 0 || job.Height <= node.MaxHeight);
|
||||
|
||||
internal static string? AppendLog(string? tail, string? line)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line)) return tail;
|
||||
var lines = (tail ?? string.Empty).Split('\n', StringSplitOptions.RemoveEmptyEntries).ToList();
|
||||
lines.Add(string.Create(CultureInfo.InvariantCulture, $"{DateTime.UtcNow:HH:mm:ss} {line.Trim()[..Math.Min(line.Trim().Length, 300)]}"));
|
||||
return string.Join('\n', lines.TakeLast(LogTailLines));
|
||||
}
|
||||
|
||||
private static StartUploadResponse Started(Upload upload) =>
|
||||
new(upload.Id, upload.ChunkSize, UploadRules.ReceivedChunks(upload), AlreadyExists: false, FrameId: null);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
|
||||
namespace Titano.Server.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Follow-up work after frames arrive (BRIEF §14.4): thumbnails, proxies and light analysis of the sequence, queued at
|
||||
/// background priority once per sequence while one is still waiting, and only for kinds this server can run.
|
||||
/// </summary>
|
||||
public sealed class SequenceJobs(TitanoDatabase db, JobEngineHost jobs)
|
||||
{
|
||||
private static readonly JobKind[] AfterNewFrames = [JobKind.Thumbs, JobKind.Proxies, JobKind.LightAnalyse];
|
||||
|
||||
private readonly SemaphoreSlim _gate = new(1, 1);
|
||||
|
||||
public async Task FramesAddedAsync(Sequence sequence, CancellationToken cancellationToken)
|
||||
{
|
||||
await _gate.WaitAsync(cancellationToken);
|
||||
try
|
||||
{
|
||||
foreach (var kind in AfterNewFrames)
|
||||
{
|
||||
if (!jobs.CanRun(kind) || await db.Jobs.IsQueuedAsync(kind, sequence.Id, cancellationToken)) continue;
|
||||
await jobs.Engine.EnqueueAsync(kind, [sequence.Id], sequence.DisplayTitle, priority: JobPriority.Background, cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_gate.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,53 @@
|
||||
using System.ComponentModel;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Hosting;
|
||||
using Titano.Server.Hosting.Windows;
|
||||
|
||||
if (args.Contains("--healthcheck")) return await HealthProbe.RunAsync();
|
||||
var command = ServerCommandLine.Parse(args);
|
||||
if (command.Error is not null)
|
||||
{
|
||||
await Console.Error.WriteLineAsync(command.Error);
|
||||
return 2;
|
||||
}
|
||||
|
||||
var app = ServerHost.Build(ServerOptions.Load(), args);
|
||||
await app.RunAsync();
|
||||
return 0;
|
||||
switch (command.Command)
|
||||
{
|
||||
case ServerCommand.Help:
|
||||
Console.WriteLine(ServerCommandLine.Usage);
|
||||
return 0;
|
||||
case ServerCommand.HealthCheck:
|
||||
return await HealthProbe.RunAsync();
|
||||
case ServerCommand.Service or ServerCommand.InstallService or ServerCommand.UninstallService when !OperatingSystem.IsWindows():
|
||||
await Console.Error.WriteLineAsync("Windows services are available on Windows only.");
|
||||
return 2;
|
||||
case ServerCommand.InstallService when OperatingSystem.IsWindows():
|
||||
return ServiceAction(ServiceInstaller.Install, $"Installed the {WindowsServiceHost.ServiceName} service.");
|
||||
case ServerCommand.UninstallService when OperatingSystem.IsWindows():
|
||||
return ServiceAction(ServiceInstaller.Uninstall, $"Removed the {WindowsServiceHost.ServiceName} service.");
|
||||
case ServerCommand.Service when OperatingSystem.IsWindows():
|
||||
if (WindowsServiceHost.Run(() => ServerHost.Build(ServerOptions.Load(), [.. command.HostArguments]))) return 0;
|
||||
await Console.Error.WriteLineAsync("--service is used by the Service Control Manager; start the service with 'sc start TitanoServer'.");
|
||||
return 1;
|
||||
default:
|
||||
var app = ServerHost.Build(ServerOptions.Load(), [.. command.HostArguments]);
|
||||
await app.RunAsync();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ServiceAction(Action action, string done)
|
||||
{
|
||||
try
|
||||
{
|
||||
action();
|
||||
Console.WriteLine(done);
|
||||
return 0;
|
||||
}
|
||||
catch (Win32Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(ex.Message);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Entry point type, visible to the in-process test host.</summary>
|
||||
public partial class Program;
|
||||
@@ -0,0 +1,38 @@
|
||||
using Titano.Core.Model;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Endpoint metadata read by <see cref="AuthenticationMiddleware"/>. Every <c>/api</c> endpoint requires a signed-in
|
||||
/// caller unless it carries <see cref="Anonymous"/>; the most specific rule (endpoint over group) wins whole.
|
||||
/// </summary>
|
||||
public sealed class AccessRule
|
||||
{
|
||||
public static readonly AccessRule Anonymous = new() { AllowAnonymous = true };
|
||||
|
||||
public static readonly AccessRule SignedIn = new();
|
||||
|
||||
public bool AllowAnonymous { get; private init; }
|
||||
|
||||
/// <summary>The caller needs at least one of these permissions; <see cref="Permissions.None"/> = any signed-in caller.</summary>
|
||||
public Permissions AnyOf { get; private init; }
|
||||
|
||||
/// <summary>Reachable while the account must still replace its temporary password (me, password, logout).</summary>
|
||||
public bool DuringPasswordChange { get; private init; }
|
||||
|
||||
public static AccessRule Requiring(Permissions anyOf) => new() { AnyOf = anyOf };
|
||||
|
||||
public static AccessRule PasswordChange { get; } = new() { DuringPasswordChange = true };
|
||||
}
|
||||
|
||||
public static class AccessRuleExtensions
|
||||
{
|
||||
public static TBuilder AllowAnonymousCaller<TBuilder>(this TBuilder builder) where TBuilder : IEndpointConventionBuilder =>
|
||||
builder.WithMetadata(AccessRule.Anonymous);
|
||||
|
||||
public static TBuilder Requires<TBuilder>(this TBuilder builder, Permissions anyOf) where TBuilder : IEndpointConventionBuilder =>
|
||||
builder.WithMetadata(AccessRule.Requiring(anyOf));
|
||||
|
||||
public static TBuilder AllowDuringPasswordChange<TBuilder>(this TBuilder builder) where TBuilder : IEndpointConventionBuilder =>
|
||||
builder.WithMetadata(AccessRule.PasswordChange);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using Titano.Core.Model;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>Account rules shared by bootstrap, sign-in and user management.</summary>
|
||||
public static class Accounts
|
||||
{
|
||||
public const int MaxUsernameLength = 64;
|
||||
|
||||
/// <summary>Letters, digits, dot, dash and underscore; 1–64 characters.</summary>
|
||||
public static bool IsValidUsername(string? name) =>
|
||||
name is { Length: > 0 and <= MaxUsernameLength } && name.All(c => char.IsAsciiLetterOrDigit(c) || c is '.' or '-' or '_');
|
||||
|
||||
/// <summary>The case-insensitive key of a user name (lock-out bookkeeping).</summary>
|
||||
public static string Key(string? name) => (name ?? string.Empty).Trim().ToLowerInvariant();
|
||||
|
||||
/// <summary>The role's default permissions with the render-node right granted or withheld explicitly.</summary>
|
||||
public static Permissions PermissionsFor(UserRole role, bool renderNode) =>
|
||||
(RolePermissions.Default(role) & ~Permissions.RenderNode) | (renderNode ? Permissions.RenderNode : Permissions.None);
|
||||
|
||||
/// <summary>Parses a role name (admin, editor, uploader, viewer); numbers are not accepted.</summary>
|
||||
public static bool TryParseRole(string? text, out UserRole role)
|
||||
{
|
||||
foreach (var candidate in Enum.GetValues<UserRole>())
|
||||
{
|
||||
if (string.Equals(candidate.ToString(), text, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
role = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
role = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>
|
||||
/// First-run administrator (BRIEF §14.9): when no admin account exists, one is created from
|
||||
/// <c>TITANO_ADMIN_USER</c>/<c>TITANO_ADMIN_PASSWORD</c> and must change its password at first sign-in. Without a
|
||||
/// configured password a temporary one is generated and written to <c>/data/initial-admin-password.txt</c> (owner-only),
|
||||
/// never to the log; the file is removed once an administrator changes their password.
|
||||
/// </summary>
|
||||
public static class AdminBootstrap
|
||||
{
|
||||
public const string DefaultUsername = "admin";
|
||||
private const string PasswordFileName = "initial-admin-password.txt";
|
||||
|
||||
public static async Task RunAsync(TitanoDatabase db, ServerOptions options, TimeProvider time, ILogger logger, CancellationToken cancellationToken)
|
||||
{
|
||||
var users = await db.Users.ListAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (users.Any(u => u.Role == UserRole.Admin))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(options.AdminPassword))
|
||||
logger.LogInformation("An administrator exists; TITANO_ADMIN_USER and TITANO_ADMIN_PASSWORD are ignored");
|
||||
return;
|
||||
}
|
||||
|
||||
var username = string.IsNullOrWhiteSpace(options.AdminUser) ? DefaultUsername : options.AdminUser.Trim();
|
||||
if (!Accounts.IsValidUsername(username))
|
||||
throw new InvalidOperationException("TITANO_ADMIN_USER may only contain letters, digits, dot, dash and underscore (up to 64 characters).");
|
||||
if (users.Any(u => Accounts.Key(u.Username) == Accounts.Key(username)))
|
||||
throw new InvalidOperationException($"No administrator exists and TITANO_ADMIN_USER '{username}' names an existing account; choose another name.");
|
||||
|
||||
var password = options.AdminPassword;
|
||||
string? passwordFile = null;
|
||||
if (string.IsNullOrEmpty(password))
|
||||
{
|
||||
password = TemporaryPassword.Generate();
|
||||
passwordFile = Path.Combine(options.DataPath, PasswordFileName);
|
||||
await File.WriteAllTextAsync(passwordFile, password + Environment.NewLine, cancellationToken).ConfigureAwait(false);
|
||||
if (!OperatingSystem.IsWindows()) File.SetUnixFileMode(passwordFile, UnixFileMode.UserRead | UnixFileMode.UserWrite);
|
||||
}
|
||||
|
||||
var admin = new User
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Username = username,
|
||||
DisplayName = null,
|
||||
PasswordHash = PasswordHasher.Hash(password),
|
||||
Role = UserRole.Admin,
|
||||
Permissions = Permissions.All,
|
||||
MustChangePassword = true,
|
||||
Created = time.GetUtcNow(),
|
||||
};
|
||||
await db.Users.CreateAsync(admin, cancellationToken).ConfigureAwait(false);
|
||||
await db.Audit.AppendAsync(new AuditEntry
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
At = admin.Created,
|
||||
Action = "user_created",
|
||||
TargetKind = "user",
|
||||
TargetId = admin.Id.ToString(),
|
||||
Details = "admin",
|
||||
Username = username,
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (passwordFile is null)
|
||||
logger.LogWarning("Created administrator '{User}' from TITANO_ADMIN_USER/TITANO_ADMIN_PASSWORD; the password must be changed at first sign-in", username);
|
||||
else
|
||||
logger.LogWarning("Created administrator '{User}'; its temporary password is in {File} and must be changed at first sign-in", username, passwordFile);
|
||||
}
|
||||
|
||||
/// <summary>Removes the generated password file (called when an administrator sets their own password).</summary>
|
||||
public static void ForgetInitialPassword(ServerOptions options)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(Path.Combine(options.DataPath, PasswordFileName));
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// Best effort: the file holds a password that no longer works.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>Appends who did what to the audit log (BRIEF §14.2): sign-ins, user, sequence, archive and setting changes.</summary>
|
||||
public sealed class AuditLog(TitanoDatabase db, TimeProvider time)
|
||||
{
|
||||
public Task WriteAsync(Caller caller, HttpContext context, string action, string? targetKind = null, string? targetId = null, string? details = null) =>
|
||||
WriteAsync(caller.Id, caller.User.Username, context, action, targetKind, targetId, details);
|
||||
|
||||
public Task WriteAsync(Guid? userId, string? username, HttpContext? context, string action, string? targetKind = null, string? targetId = null,
|
||||
string? details = null) =>
|
||||
db.Audit.AppendAsync(new AuditEntry
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
At = time.GetUtcNow(),
|
||||
UserId = userId,
|
||||
Username = username,
|
||||
Action = action,
|
||||
TargetKind = targetKind,
|
||||
TargetId = targetId,
|
||||
Details = details,
|
||||
RemoteAddress = context?.Connection.RemoteIpAddress?.ToString(),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Server.Api;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the caller of every <c>/api</c> request (bearer token or admin cookie) and enforces the endpoint's
|
||||
/// <see cref="AccessRule"/>: 401 without a valid token, 403 for a missing permission, a missing CSRF header on a
|
||||
/// cookie request, or a pending password change. Runs after routing, so the endpoint's metadata is known.
|
||||
/// </summary>
|
||||
public sealed class AuthenticationMiddleware(RequestDelegate next, TokenService tokens)
|
||||
{
|
||||
public async Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
var rule = context.GetEndpoint()?.Metadata.GetMetadata<AccessRule>();
|
||||
if (rule is null && !context.Request.Path.StartsWithSegments("/api"))
|
||||
{
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
rule ??= AccessRule.SignedIn;
|
||||
if (rule.AllowAnonymous)
|
||||
{
|
||||
await next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
var caller = await AuthenticateAsync(context);
|
||||
if (caller is null)
|
||||
{
|
||||
context.Response.Headers.WWWAuthenticate = "Bearer";
|
||||
await ApiErrors.WriteAsync(context, StatusCodes.Status401Unauthorized, "unauthorized");
|
||||
return;
|
||||
}
|
||||
if (caller.Source == CredentialSource.Cookie && !IsSameSiteRequest(context, caller))
|
||||
{
|
||||
await ApiErrors.WriteAsync(context, StatusCodes.Status403Forbidden, "csrf_failed");
|
||||
return;
|
||||
}
|
||||
if (caller.User.MustChangePassword && !rule.DuringPasswordChange)
|
||||
{
|
||||
await ApiErrors.WriteAsync(context, StatusCodes.Status403Forbidden, "password_change_required");
|
||||
return;
|
||||
}
|
||||
if (rule.AnyOf != Permissions.None && !caller.Has(rule.AnyOf))
|
||||
{
|
||||
await ApiErrors.WriteAsync(context, StatusCodes.Status403Forbidden, "forbidden");
|
||||
return;
|
||||
}
|
||||
context.Features.Set(caller);
|
||||
await next(context);
|
||||
}
|
||||
|
||||
private Task<Caller?> AuthenticateAsync(HttpContext context)
|
||||
{
|
||||
var authorization = context.Request.Headers.Authorization.ToString();
|
||||
if (authorization.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
|
||||
return tokens.ValidateAsync(authorization[7..].Trim(), CredentialSource.Bearer, context.RequestAborted);
|
||||
if (context.Request.Cookies.TryGetValue(SessionCookies.Session, out var cookie))
|
||||
return tokens.ValidateAsync(cookie, CredentialSource.Cookie, context.RequestAborted);
|
||||
return Task.FromResult<Caller?>(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cookie requests must prove they come from the admin panel: unsafe methods carry the CSRF header, WebSocket
|
||||
/// upgrades (which cannot) must come from this origin.
|
||||
/// </summary>
|
||||
private static bool IsSameSiteRequest(HttpContext context, Caller caller)
|
||||
{
|
||||
if (context.WebSockets.IsWebSocketRequest)
|
||||
{
|
||||
var origin = context.Request.Headers.Origin.ToString();
|
||||
return origin.Equals($"{context.Request.Scheme}://{context.Request.Host}", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
var method = context.Request.Method;
|
||||
if (HttpMethods.IsGet(method) || HttpMethods.IsHead(method) || HttpMethods.IsOptions(method)) return true;
|
||||
return SessionCookies.HasValidCsrf(context, caller);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Sign-in throttling (BRIEF §14.2): at most <see cref="ServerOptions.LoginAttemptsPerMinute"/> attempts per client
|
||||
/// address and minute, and an account name locked for <see cref="ServerOptions.LockoutMinutes"/> after
|
||||
/// <see cref="ServerOptions.LockoutThreshold"/> consecutive failures. Names without an account are tracked the same
|
||||
/// way, so a lockout reveals nothing about which names exist. State is per process and resets at restart.
|
||||
/// </summary>
|
||||
public sealed class LoginGuard(ServerOptions options, TimeProvider time)
|
||||
{
|
||||
private static readonly TimeSpan Window = TimeSpan.FromMinutes(1);
|
||||
private const int PruneAbove = 10_000;
|
||||
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Dictionary<string, Queue<DateTimeOffset>> _attempts = new(StringComparer.Ordinal);
|
||||
private readonly Dictionary<string, AccountState> _accounts = new(StringComparer.Ordinal);
|
||||
|
||||
/// <summary>Counts an attempt from <paramref name="address"/>; false, with the time to wait, once its budget is spent.</summary>
|
||||
public bool TryAttempt(string address, out TimeSpan retryAfter)
|
||||
{
|
||||
var now = time.GetUtcNow();
|
||||
lock (_gate)
|
||||
{
|
||||
if (_attempts.Count > PruneAbove) Prune(now);
|
||||
if (!_attempts.TryGetValue(address, out var recent)) _attempts[address] = recent = new Queue<DateTimeOffset>();
|
||||
while (recent.Count > 0 && now - recent.Peek() >= Window) recent.Dequeue();
|
||||
if (recent.Count >= options.LoginAttemptsPerMinute)
|
||||
{
|
||||
retryAfter = Window - (now - recent.Peek());
|
||||
return false;
|
||||
}
|
||||
recent.Enqueue(now);
|
||||
retryAfter = TimeSpan.Zero;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>True while <paramref name="account"/> (a normalised user name) is locked.</summary>
|
||||
public bool IsLocked(string account, out TimeSpan retryAfter)
|
||||
{
|
||||
var now = time.GetUtcNow();
|
||||
lock (_gate)
|
||||
{
|
||||
if (_accounts.TryGetValue(account, out var state) && state.LockedUntil is { } until && until > now)
|
||||
{
|
||||
retryAfter = until - now;
|
||||
return true;
|
||||
}
|
||||
retryAfter = TimeSpan.Zero;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void RecordFailure(string account)
|
||||
{
|
||||
var now = time.GetUtcNow();
|
||||
var span = TimeSpan.FromMinutes(options.LockoutMinutes);
|
||||
lock (_gate)
|
||||
{
|
||||
if (_accounts.Count > PruneAbove) Prune(now);
|
||||
var state = _accounts.GetValueOrDefault(account);
|
||||
if (state?.LockedUntil is { } until && until > now) return;
|
||||
if (state is null || state.LockedUntil is not null || now - state.FirstFailure > span) state = new AccountState(0, now, null);
|
||||
var failures = state.Failures + 1;
|
||||
_accounts[account] = failures >= options.LockoutThreshold
|
||||
? new AccountState(0, now, now + span)
|
||||
: state with { Failures = failures };
|
||||
}
|
||||
}
|
||||
|
||||
public void RecordSuccess(string account)
|
||||
{
|
||||
lock (_gate) _accounts.Remove(account);
|
||||
}
|
||||
|
||||
private void Prune(DateTimeOffset now)
|
||||
{
|
||||
foreach (var (address, recent) in _attempts.ToList())
|
||||
{
|
||||
if (recent.Count == 0 || now - recent.Last() >= Window) _attempts.Remove(address);
|
||||
}
|
||||
var span = TimeSpan.FromMinutes(options.LockoutMinutes);
|
||||
foreach (var (account, state) in _accounts.ToList())
|
||||
{
|
||||
if ((state.LockedUntil ?? state.FirstFailure + span) < now) _accounts.Remove(account);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record AccountState(int Failures, DateTimeOffset FirstFailure, DateTimeOffset? LockedUntil);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Password hashes (BRIEF §14.2): PBKDF2-SHA512, 210,000 iterations, 16-byte random salt per hash, stored as
|
||||
/// <c>pbkdf2-sha512$<iterations>$<salt>$<hash></c> (base64). The format names its algorithm and cost, so
|
||||
/// older hashes keep verifying and are upgraded at the next sign-in.
|
||||
/// </summary>
|
||||
public static class PasswordHasher
|
||||
{
|
||||
public const int MinimumLength = 10;
|
||||
public const int Iterations = 210_000;
|
||||
private const string Algorithm = "pbkdf2-sha512";
|
||||
private const int SaltBytes = 16;
|
||||
private const int HashBytes = 64;
|
||||
|
||||
/// <summary>Verified against unknown user names so a miss costs as much time as a wrong password.</summary>
|
||||
private static readonly Lazy<string> DummyHash = new(() => Hash(Convert.ToBase64String(RandomNumberGenerator.GetBytes(18))));
|
||||
|
||||
public static string Hash(string password)
|
||||
{
|
||||
var salt = RandomNumberGenerator.GetBytes(SaltBytes);
|
||||
var hash = Derive(password, salt, Iterations);
|
||||
return string.Create(CultureInfo.InvariantCulture, $"{Algorithm}${Iterations}${Convert.ToBase64String(salt)}${Convert.ToBase64String(hash)}");
|
||||
}
|
||||
|
||||
/// <summary>Constant-time check of <paramref name="password"/> against a stored hash; malformed hashes never match.</summary>
|
||||
public static bool Verify(string password, string storedHash)
|
||||
{
|
||||
var parts = storedHash.Split('$');
|
||||
if (parts.Length != 4 || parts[0] != Algorithm
|
||||
|| !int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var iterations) || iterations < 1)
|
||||
return false;
|
||||
byte[] salt, expected;
|
||||
try
|
||||
{
|
||||
salt = Convert.FromBase64String(parts[2]);
|
||||
expected = Convert.FromBase64String(parts[3]);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (salt.Length == 0 || expected.Length == 0) return false;
|
||||
var actual = Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, HashAlgorithmName.SHA512, expected.Length);
|
||||
return CryptographicOperations.FixedTimeEquals(actual, expected);
|
||||
}
|
||||
|
||||
/// <summary>Spends the same time as <see cref="Verify"/> for a name that has no account.</summary>
|
||||
public static void VerifyDummy(string password) => Verify(password, DummyHash.Value);
|
||||
|
||||
/// <summary>True when the hash was made with fewer iterations than the current policy.</summary>
|
||||
public static bool NeedsRehash(string storedHash)
|
||||
{
|
||||
var parts = storedHash.Split('$');
|
||||
return parts.Length != 4 || parts[0] != Algorithm
|
||||
|| !int.TryParse(parts[1], NumberStyles.None, CultureInfo.InvariantCulture, out var iterations) || iterations < Iterations;
|
||||
}
|
||||
|
||||
/// <summary>Policy check for passwords users choose: at least <see cref="MinimumLength"/> characters, not only spaces.</summary>
|
||||
public static bool IsAcceptable(string? password) =>
|
||||
password is not null && password.Length >= MinimumLength && !string.IsNullOrWhiteSpace(password);
|
||||
|
||||
private static byte[] Derive(string password, byte[] salt, int iterations) =>
|
||||
Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterations, HashAlgorithmName.SHA512, HashBytes);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using System.Buffers.Text;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>
|
||||
/// The web admin's cookie session (BRIEF §14.2): the token in an <c>HttpOnly; Secure; SameSite=Strict</c> cookie, plus a
|
||||
/// script-readable CSRF cookie whose value must come back in <see cref="CsrfHeader"/> on every unsafe request. The CSRF
|
||||
/// value is derived from the session token, so a planted cookie pair cannot forge it.
|
||||
/// </summary>
|
||||
public static class SessionCookies
|
||||
{
|
||||
public const string Session = "titano_session";
|
||||
public const string Csrf = "titano_csrf";
|
||||
public const string CsrfHeader = "X-CSRF-Token";
|
||||
|
||||
public static void Issue(HttpContext context, IssuedToken token)
|
||||
{
|
||||
context.Response.Cookies.Append(Session, token.Secret, Options(httpOnly: true));
|
||||
context.Response.Cookies.Append(Csrf, CsrfFor(token.Stored.TokenHash), Options(httpOnly: false));
|
||||
}
|
||||
|
||||
public static void Clear(HttpContext context)
|
||||
{
|
||||
context.Response.Cookies.Delete(Session, Options(httpOnly: true));
|
||||
context.Response.Cookies.Delete(Csrf, Options(httpOnly: false));
|
||||
}
|
||||
|
||||
/// <summary>Constant-time check of the request's CSRF header against the caller's session.</summary>
|
||||
public static bool HasValidCsrf(HttpContext context, Caller caller)
|
||||
{
|
||||
var header = context.Request.Headers[CsrfHeader].ToString();
|
||||
var expected = CsrfFor(caller.TokenHash);
|
||||
return header.Length == expected.Length
|
||||
&& CryptographicOperations.FixedTimeEquals(Encoding.ASCII.GetBytes(header), Encoding.ASCII.GetBytes(expected));
|
||||
}
|
||||
|
||||
/// <summary>Secure cookies only travel over HTTPS (or to the machine itself, which browsers treat as secure).</summary>
|
||||
public static bool IsSecureTransport(HttpContext context) =>
|
||||
context.Request.IsHttps || (context.Connection.RemoteIpAddress is { } remote && IPAddress.IsLoopback(remote));
|
||||
|
||||
private static string CsrfFor(string tokenHash) =>
|
||||
Base64Url.EncodeToString(SHA256.HashData(Encoding.UTF8.GetBytes("titano-csrf|" + tokenHash)));
|
||||
|
||||
private static CookieOptions Options(bool httpOnly) => new()
|
||||
{
|
||||
HttpOnly = httpOnly,
|
||||
Secure = true,
|
||||
SameSite = SameSiteMode.Strict,
|
||||
Path = "/",
|
||||
IsEssential = true,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>One-time passwords for new accounts and resets: three groups of four unambiguous characters (≈ 70 bits).</summary>
|
||||
public static class TemporaryPassword
|
||||
{
|
||||
private const string Alphabet = "abcdefghjkmnpqrstuvwxyz23456789";
|
||||
|
||||
public static string Generate()
|
||||
{
|
||||
var chars = RandomNumberGenerator.GetItems<char>(Alphabet, 12);
|
||||
return $"{new string(chars, 0, 4)}-{new string(chars, 4, 4)}-{new string(chars, 8, 4)}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
using System.Buffers.Text;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Security;
|
||||
|
||||
/// <summary>How a request presented its token.</summary>
|
||||
public enum CredentialSource
|
||||
{
|
||||
/// <summary><c>Authorization: Bearer</c> (desktop client, uploader, render node).</summary>
|
||||
Bearer,
|
||||
|
||||
/// <summary>HttpOnly session cookie of the web admin; unsafe methods also need the CSRF header.</summary>
|
||||
Cookie,
|
||||
}
|
||||
|
||||
/// <summary>The signed-in account behind a request.</summary>
|
||||
public sealed record Caller(User User, string TokenHash, CredentialSource Source, string? ClientName)
|
||||
{
|
||||
public Guid Id => User.Id;
|
||||
|
||||
public bool IsAdmin => User.Role == UserRole.Admin;
|
||||
|
||||
/// <summary>True when the account holds at least one of <paramref name="any"/>.</summary>
|
||||
public bool Has(Permissions any) => (User.Permissions & any) != 0;
|
||||
|
||||
/// <summary>Minimal-API binding: endpoint handlers take the caller resolved by <see cref="AuthenticationMiddleware"/>.</summary>
|
||||
public static ValueTask<Caller?> BindAsync(HttpContext context) => ValueTask.FromResult(context.Features.Get<Caller>());
|
||||
}
|
||||
|
||||
/// <summary>A freshly issued token: the secret (returned once) and what is stored (its hash).</summary>
|
||||
public sealed record IssuedToken(string Secret, SessionToken Stored);
|
||||
|
||||
/// <summary>
|
||||
/// Opaque bearer tokens (BRIEF §14.2): 32 random bytes, stored only as their SHA-256, sliding expiry, revocation.
|
||||
/// The same tokens back the admin panel's cookie session.
|
||||
/// </summary>
|
||||
public sealed class TokenService(TitanoDatabase db, ServerOptions options, TimeProvider time)
|
||||
{
|
||||
/// <summary>Sliding expiry is written at most this often per token, not on every request.</summary>
|
||||
private static readonly TimeSpan TouchInterval = TimeSpan.FromMinutes(1);
|
||||
|
||||
private TimeSpan Lifetime => TimeSpan.FromDays(options.TokenLifetimeDays);
|
||||
|
||||
public async Task<IssuedToken> IssueAsync(User user, string? clientName, CancellationToken cancellationToken)
|
||||
{
|
||||
var secret = Base64Url.EncodeToString(RandomNumberGenerator.GetBytes(32));
|
||||
var now = time.GetUtcNow();
|
||||
var name = clientName?.Trim();
|
||||
var token = new SessionToken
|
||||
{
|
||||
TokenHash = HashOf(secret),
|
||||
UserId = user.Id,
|
||||
ClientName = string.IsNullOrEmpty(name) ? null : name[..Math.Min(name.Length, 100)],
|
||||
Created = now,
|
||||
Expires = now + Lifetime,
|
||||
LastUsed = now,
|
||||
};
|
||||
await db.Tokens.InsertAsync(token, cancellationToken).ConfigureAwait(false);
|
||||
return new IssuedToken(secret, token);
|
||||
}
|
||||
|
||||
/// <summary>The caller a token belongs to, or null when it is unknown, revoked, expired or its account is disabled.</summary>
|
||||
public async Task<Caller?> ValidateAsync(string secret, CredentialSource source, CancellationToken cancellationToken)
|
||||
{
|
||||
if (secret.Length is 0 or > 128) return null;
|
||||
var hash = HashOf(secret);
|
||||
var token = await db.Tokens.GetAsync(hash, cancellationToken).ConfigureAwait(false);
|
||||
var now = time.GetUtcNow();
|
||||
if (token is null || token.Revoked || token.Expires <= now) return null;
|
||||
var user = await db.Users.GetAsync(token.UserId, cancellationToken).ConfigureAwait(false);
|
||||
if (user is null || user.Disabled) return null;
|
||||
if (now - token.LastUsed >= TouchInterval && !await db.Tokens.TouchAsync(hash, now, now + Lifetime, cancellationToken).ConfigureAwait(false))
|
||||
return null;
|
||||
return new Caller(user, hash, source, token.ClientName);
|
||||
}
|
||||
|
||||
/// <summary>True while the caller's token is still live and its account enabled (long-lived WebSocket sessions).</summary>
|
||||
public async Task<bool> IsStillValidAsync(Caller caller, CancellationToken cancellationToken)
|
||||
{
|
||||
var token = await db.Tokens.GetAsync(caller.TokenHash, cancellationToken).ConfigureAwait(false);
|
||||
if (token is null || token.Revoked || token.Expires <= time.GetUtcNow()) return false;
|
||||
return await db.Users.GetAsync(caller.Id, cancellationToken).ConfigureAwait(false) is { Disabled: false };
|
||||
}
|
||||
|
||||
/// <summary>Stored form of a token: lower-case hex SHA-256 of its text.</summary>
|
||||
public static string HashOf(string secret) => Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(secret)));
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Archive-relative paths from the database → absolute paths, refusing anything that would leave the archive root
|
||||
/// (BRIEF §14.2: path traversal impossible). Clients never send paths; only ids that the database maps to these.
|
||||
/// </summary>
|
||||
public static class ArchivePaths
|
||||
{
|
||||
private static readonly StringComparison PathComparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
|
||||
|
||||
/// <summary>The absolute path of <paramref name="relative"/> under <paramref name="root"/>, or null when it escapes the root.</summary>
|
||||
public static string? Resolve(string root, string? relative)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(relative) || relative.Contains('\0', StringComparison.Ordinal)) return null;
|
||||
var portable = relative.Replace('\\', '/');
|
||||
if (Path.IsPathRooted(portable) || portable.StartsWith('/')) return null;
|
||||
string full;
|
||||
try
|
||||
{
|
||||
full = Path.GetFullPath(Path.Combine(Root(root), portable));
|
||||
}
|
||||
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return IsInside(root, full) ? full : null;
|
||||
}
|
||||
|
||||
/// <summary>True when <paramref name="path"/> lies strictly inside <paramref name="root"/>.</summary>
|
||||
public static bool IsInside(string root, string path)
|
||||
{
|
||||
var full = Path.GetFullPath(path);
|
||||
return full.StartsWith(Root(root) + Path.DirectorySeparatorChar, PathComparison);
|
||||
}
|
||||
|
||||
/// <summary>A file of the archive that may be served: inside the root, existing, and not a symbolic link.</summary>
|
||||
public static FileInfo? ExistingFile(string root, string? relative)
|
||||
{
|
||||
if (Resolve(root, relative) is not { } full) return null;
|
||||
var file = new FileInfo(full);
|
||||
return file.Exists && file.LinkTarget is null ? file : null;
|
||||
}
|
||||
|
||||
private static string Root(string root) => Path.TrimEndingDirectorySeparator(Path.GetFullPath(root));
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
using System.Collections.Frozen;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>Streams archive files (originals, proxies, thumbnails, exports) with Range, If-Range and conditional GET support.</summary>
|
||||
public static class FileResponses
|
||||
{
|
||||
private static readonly FrozenDictionary<string, string> Types = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
[".jpg"] = "image/jpeg",
|
||||
[".jpeg"] = "image/jpeg",
|
||||
[".png"] = "image/png",
|
||||
[".gif"] = "image/gif",
|
||||
[".tif"] = "image/tiff",
|
||||
[".tiff"] = "image/tiff",
|
||||
[".dng"] = "image/x-adobe-dng",
|
||||
[".heic"] = "image/heic",
|
||||
[".heif"] = "image/heif",
|
||||
[".webp"] = "image/webp",
|
||||
[".avif"] = "image/avif",
|
||||
[".mp4"] = "video/mp4",
|
||||
[".mov"] = "video/quicktime",
|
||||
[".avi"] = "video/x-msvideo",
|
||||
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public static string ContentTypeOf(string fileName) =>
|
||||
Types.TryGetValue(Path.GetExtension(fileName), out var type) ? type : "application/octet-stream";
|
||||
|
||||
/// <summary>
|
||||
/// The byte range [<paramref name="offset"/>, offset + <paramref name="length"/>) of <paramref name="file"/> as a
|
||||
/// range-capable response; null when the range does not fit the file (the asset changed under the index).
|
||||
/// </summary>
|
||||
public static IResult? Serve(HttpContext context, FileInfo file, long offset, long length, string contentType, string cacheControl,
|
||||
string? etag = null, string? downloadName = null)
|
||||
{
|
||||
if (offset < 0 || length < 0 || offset > file.Length - length) return null;
|
||||
context.Response.Headers.CacheControl = cacheControl;
|
||||
return Results.Stream(new FileSlice(file.FullName, offset, length), contentType, downloadName, file.LastWriteTimeUtc,
|
||||
etag is null ? null : new EntityTagHeaderValue("\"" + etag + "\""), enableRangeProcessing: true);
|
||||
}
|
||||
|
||||
/// <summary>Read-only, seekable view of part of a file (one thumbnail inside a pack, or the whole file).</summary>
|
||||
private sealed class FileSlice : Stream
|
||||
{
|
||||
private readonly FileStream _file;
|
||||
private readonly long _offset;
|
||||
private readonly long _length;
|
||||
private long _position;
|
||||
|
||||
public FileSlice(string path, long offset, long length)
|
||||
{
|
||||
_file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 1 << 16, FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
_offset = offset;
|
||||
_length = length;
|
||||
}
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => true;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => _length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => _position;
|
||||
set => _position = Math.Clamp(value, 0, _length);
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count) => Read(buffer.AsSpan(offset, count));
|
||||
|
||||
public override int Read(Span<byte> buffer)
|
||||
{
|
||||
var count = Available(buffer.Length);
|
||||
if (count == 0) return 0;
|
||||
_file.Position = _offset + _position;
|
||||
var read = _file.Read(buffer[..count]);
|
||||
_position += read;
|
||||
return read;
|
||||
}
|
||||
|
||||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
|
||||
ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
|
||||
|
||||
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var count = Available(buffer.Length);
|
||||
if (count == 0) return 0;
|
||||
_file.Position = _offset + _position;
|
||||
var read = await _file.ReadAsync(buffer[..count], cancellationToken).ConfigureAwait(false);
|
||||
_position += read;
|
||||
return read;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin) => Position = origin switch
|
||||
{
|
||||
SeekOrigin.Begin => offset,
|
||||
SeekOrigin.Current => _position + offset,
|
||||
_ => _length + offset,
|
||||
};
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
}
|
||||
|
||||
public override void SetLength(long value) => throw new NotSupportedException();
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing) _file.Dispose();
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
public override async ValueTask DisposeAsync()
|
||||
{
|
||||
await _file.DisposeAsync().ConfigureAwait(false);
|
||||
await base.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private int Available(int requested) => (int)Math.Min(requested, _length - _position);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Titano.Core.Model;
|
||||
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>Regenerable images derived from an original (BRIEF §8.5).</summary>
|
||||
public enum FrameAsset
|
||||
{
|
||||
/// <summary>JPEG proxy under the sequence's <c>proxies/</c> folder.</summary>
|
||||
Proxy,
|
||||
|
||||
/// <summary>Thumbnail inside the sequence's packed thumbnail file.</summary>
|
||||
Thumbnail,
|
||||
}
|
||||
|
||||
/// <summary>Where a derived image lives: a byte range of a file inside the archive (a whole proxy file or one thumbnail of a pack).</summary>
|
||||
public sealed record AssetLocation(string Path, long Offset, long Length, string ContentType);
|
||||
|
||||
/// <summary>
|
||||
/// Finds proxies and thumbnails in the archive layout, which the archive module owns (BRIEF §7). The server streams
|
||||
/// what it returns with Range support after checking the path stays inside the archive root; null means not built yet.
|
||||
/// Registered by the host composition alongside <see cref="Titano.Data.Archiving.IArchiveIngest"/>.
|
||||
/// </summary>
|
||||
public interface IFrameAssetLocator
|
||||
{
|
||||
ValueTask<AssetLocation?> LocateAsync(Archive archive, Sequence sequence, Frame frame, FrameAsset asset, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Titano.Core.IO;
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Protocol;
|
||||
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>Files a verified render output under the sequence's <c>exports/</c> folder (BRIEF §7) and records it.</summary>
|
||||
public sealed class RenderStore(TitanoDatabase db, ServerArchive archive, TimeProvider time)
|
||||
{
|
||||
/// <summary>
|
||||
/// Moves <paramref name="stagedFile"/> (on the archive volume) to <c><sequence folder>/exports/</c>, never
|
||||
/// replacing an existing export, and inserts the render row. Null when the sequence has no folder in the archive yet.
|
||||
/// </summary>
|
||||
public async Task<Render?> CommitAsync(Sequence sequence, string stagedFile, RenderResultRequest output, string producedBy, CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sequence.Folder) || ArchivePaths.Resolve(archive.Root, sequence.Folder.TrimEnd('/') + "/exports") is not { } exports)
|
||||
return null;
|
||||
Directory.CreateDirectory(exports);
|
||||
var target = Unique(exports, output.FileName);
|
||||
File.Move(stagedFile, target);
|
||||
var render = new Render
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
SequenceId = sequence.Id,
|
||||
PresetName = output.PresetName,
|
||||
OutputPath = LongPath.ToPortableRelative(archive.Root, target),
|
||||
Codec = output.Codec,
|
||||
Width = output.Width,
|
||||
Height = output.Height,
|
||||
Fps = output.Fps,
|
||||
DurationSeconds = output.DurationSeconds,
|
||||
Bytes = output.Size,
|
||||
Created = time.GetUtcNow(),
|
||||
ProducedBy = producedBy,
|
||||
};
|
||||
await db.Renders.InsertAsync(render, cancellationToken);
|
||||
return render;
|
||||
}
|
||||
|
||||
private static string Unique(string folder, string fileName)
|
||||
{
|
||||
var candidate = Path.Combine(folder, fileName);
|
||||
var stem = Path.GetFileNameWithoutExtension(fileName);
|
||||
var extension = Path.GetExtension(fileName);
|
||||
for (var n = 2; File.Exists(candidate); n++) candidate = Path.Combine(folder, $"{stem} ({n}){extension}");
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Data;
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// The archive this server serves (<see cref="ServerOptions.ArchivePath"/>) and its database row, created at first
|
||||
/// start. The admin panel can change its name, templates and proxy policy at runtime.
|
||||
/// </summary>
|
||||
public sealed class ServerArchive(Archive archive)
|
||||
{
|
||||
private volatile Archive _current = archive;
|
||||
|
||||
public Archive Current
|
||||
{
|
||||
get => _current;
|
||||
set => _current = value;
|
||||
}
|
||||
|
||||
public Guid Id => _current.Id;
|
||||
|
||||
public string Root => _current.RootPath;
|
||||
|
||||
/// <summary>Finds the archive row for the configured root, or creates it with the default templates.</summary>
|
||||
public static async Task<ServerArchive> OpenAsync(TitanoDatabase db, ServerOptions options, TimeProvider time, CancellationToken cancellationToken)
|
||||
{
|
||||
var root = Path.TrimEndingDirectorySeparator(Path.GetFullPath(options.ArchivePath));
|
||||
var comparison = OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
|
||||
var existing = (await db.Archives.ListAsync(cancellationToken).ConfigureAwait(false))
|
||||
.FirstOrDefault(a => string.Equals(Path.TrimEndingDirectorySeparator(a.RootPath), root, comparison));
|
||||
if (existing is not null) return new ServerArchive(existing);
|
||||
var created = new Archive { Id = Guid.CreateVersion7(), Name = options.ServerName, RootPath = root, Created = time.GetUtcNow() };
|
||||
await db.Archives.UpsertAsync(created, cancellationToken).ConfigureAwait(false);
|
||||
return new ServerArchive(created);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using Titano.Data;
|
||||
using Titano.Data.Repositories;
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Upload housekeeping (BRIEF §14.4): at start, uploads a crash left half-committed are settled; then every half hour
|
||||
/// abandoned uploads expire, orphaned partial and staging files are removed, old upload records and dead tokens purged.
|
||||
/// </summary>
|
||||
public sealed class UploadMaintenance(TitanoDatabase db, ServerOptions options, UploadStore store, TimeProvider time, ILogger<UploadMaintenance> logger)
|
||||
: BackgroundService
|
||||
{
|
||||
private static readonly TimeSpan Interval = TimeSpan.FromMinutes(30);
|
||||
private static readonly TimeSpan FinishedRetention = TimeSpan.FromDays(90);
|
||||
private static readonly TimeSpan StagingRetention = TimeSpan.FromDays(1);
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
await RecoverAsync(stoppingToken);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogError(ex, "Upload recovery failed");
|
||||
}
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await SweepAsync(stoppingToken);
|
||||
await Task.Delay(Interval, time, stoppingToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Upload cleanup failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uploads left committing by a crash: committed when the frame with their hash exists, otherwise back to receiving
|
||||
/// (the partial file is intact, so the client simply completes again).
|
||||
/// </summary>
|
||||
public async Task RecoverAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
foreach (var upload in await db.Uploads.ListByStateAsync(UploadState.Committing, cancellationToken))
|
||||
{
|
||||
var frame = upload.JobId is null ? (await db.Frames.FindBySha256Async(upload.Sha256, cancellationToken)).FirstOrDefault() : null;
|
||||
if (frame is not null)
|
||||
{
|
||||
await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Committed, frame.Id, cancellationToken);
|
||||
store.Delete(upload.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Receiving, cancellationToken: cancellationToken);
|
||||
}
|
||||
logger.LogInformation("Recovered interrupted upload {Upload} ({File})", upload.Id, upload.FileName);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SweepAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var now = time.GetUtcNow();
|
||||
var abandonedBefore = now - TimeSpan.FromHours(options.UploadRetentionHours);
|
||||
var expired = 0;
|
||||
foreach (var upload in await db.Uploads.ListStaleAsync(abandonedBefore, cancellationToken))
|
||||
{
|
||||
if (!await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Receiving, UploadState.Cancelled, cancellationToken: cancellationToken)) continue;
|
||||
store.Delete(upload.Id);
|
||||
expired++;
|
||||
}
|
||||
|
||||
var active = new HashSet<Guid>();
|
||||
foreach (var state in new[] { UploadState.Receiving, UploadState.Committing })
|
||||
active.UnionWith((await db.Uploads.ListByStateAsync(state, cancellationToken)).Select(u => u.Id));
|
||||
foreach (var (id, _) in store.List().ToList())
|
||||
{
|
||||
if (!active.Contains(id)) store.Delete(id);
|
||||
}
|
||||
if (Directory.Exists(options.StagingPath))
|
||||
{
|
||||
// Only this server's staging files (named by upload id) are touched; clients may share the archive.
|
||||
foreach (var file in new DirectoryInfo(options.StagingPath).EnumerateFiles())
|
||||
{
|
||||
if (Guid.TryParseExact(Path.GetFileNameWithoutExtension(file.Name), "N", out var id) && !active.Contains(id)
|
||||
&& file.LastWriteTimeUtc < now.UtcDateTime - StagingRetention)
|
||||
UploadStore.TryDelete(file.FullName);
|
||||
}
|
||||
}
|
||||
|
||||
await db.Uploads.PurgeFinishedAsync(now - FinishedRetention, cancellationToken);
|
||||
await db.Tokens.PurgeAsync(now, cancellationToken);
|
||||
if (expired > 0) logger.LogInformation("Expired {Count} abandoned upload(s)", expired);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>Validation shared by frame and render uploads.</summary>
|
||||
public static class UploadRules
|
||||
{
|
||||
/// <summary>A plain file name (no folders, no characters Windows or Linux refuse), or null.</summary>
|
||||
public static string? SafeFileName(string? name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name)) return null;
|
||||
var trimmed = name.Trim();
|
||||
if (trimmed.Length > 255 || trimmed is "." or ".." || trimmed.EndsWith('.')) return null;
|
||||
foreach (var c in trimmed)
|
||||
{
|
||||
if (char.IsControl(c) || c is '/' or '\\' or '<' or '>' or ':' or '"' or '|' or '?' or '*') return null;
|
||||
}
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
/// <summary>64 hexadecimal characters.</summary>
|
||||
public static bool IsSha256(string? value) => value is { Length: 64 } && value.All(char.IsAsciiHexDigit);
|
||||
|
||||
/// <summary>Indices of the chunks set in a received-chunk bitmap.</summary>
|
||||
public static List<int> ReceivedChunks(Titano.Data.Repositories.Upload upload)
|
||||
{
|
||||
var received = new List<int>(upload.ReceivedCount);
|
||||
for (var i = 0; i < upload.ChunkCount; i++)
|
||||
{
|
||||
if (upload.HasChunk(i)) received.Add(i);
|
||||
}
|
||||
return received;
|
||||
}
|
||||
|
||||
/// <summary>Free bytes on the volume holding <paramref name="path"/>, or null when the platform cannot tell.</summary>
|
||||
public static long? FreeBytes(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new DriveInfo(Path.GetFullPath(path)).AvailableFreeSpace;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or ArgumentException or UnauthorizedAccessException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Globalization;
|
||||
using Titano.Data;
|
||||
using Titano.Data.Archiving;
|
||||
using Titano.Data.Repositories;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Api;
|
||||
using Titano.Server.Configuration;
|
||||
using Titano.Server.Events;
|
||||
using Titano.Server.Jobs;
|
||||
using Titano.Server.Security;
|
||||
using UploadState = Titano.Data.Repositories.UploadState;
|
||||
using UploadStateDto = Titano.Protocol.UploadState;
|
||||
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Chunked, resumable, hash-verified frame uploads (BRIEF §14.4): start (short-circuit on a known SHA-256, resume an
|
||||
/// unfinished upload, quota and disk checks), idempotent chunks, and completion that stages the file on the archive
|
||||
/// volume, verifies its hash and hands it to <see cref="IArchiveIngest"/>.
|
||||
/// </summary>
|
||||
public sealed class UploadService(
|
||||
TitanoDatabase db,
|
||||
ServerOptions options,
|
||||
ServerArchive archive,
|
||||
UploadStore store,
|
||||
IArchiveIngest ingest,
|
||||
SequenceJobs sequenceJobs,
|
||||
EventHub events,
|
||||
IHostApplicationLifetime lifetime,
|
||||
TimeProvider time,
|
||||
ILogger<UploadService> logger)
|
||||
{
|
||||
/// <summary>Space kept free on the upload volume beyond the announced size.</summary>
|
||||
private const long DiskReserve = 64L * 1024 * 1024;
|
||||
|
||||
private readonly ConcurrentDictionary<Guid, int> _chunksInFlight = new();
|
||||
|
||||
public async Task<Outcome<StartUploadResponse>> StartAsync(Caller caller, StartUploadRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
if (UploadRules.SafeFileName(request.FileName) is not { } fileName) return Fail<StartUploadResponse>(400, "invalid_file_name");
|
||||
if (!UploadRules.IsSha256(request.Sha256)) return Fail<StartUploadResponse>(400, "invalid_hash");
|
||||
if (request.Size <= 0) return Fail<StartUploadResponse>(400, "bad_request", "size");
|
||||
if (request.Size > options.MaxUploadBytes) return Fail<StartUploadResponse>(413, "too_large", options.MaxUploadBytes.ToString(CultureInfo.InvariantCulture));
|
||||
if (request.SessionKey is { Length: > 200 }) return Fail<StartUploadResponse>(400, "bad_request", "sessionKey");
|
||||
if (request.SequenceId is { } target && await db.Sequences.GetAsync(target, cancellationToken) is null)
|
||||
return Fail<StartUploadResponse>(404, "not_found", "sequence");
|
||||
|
||||
var sha = request.Sha256.ToLowerInvariant();
|
||||
var known = await db.Frames.FindBySha256Async(sha, cancellationToken);
|
||||
if (known.Count > 0)
|
||||
{
|
||||
var frame = known.FirstOrDefault(f => f.SequenceId == request.SequenceId) ?? known[0];
|
||||
return new StartUploadResponse(null, options.ChunkSizeBytes, [], AlreadyExists: true, frame.Id);
|
||||
}
|
||||
|
||||
var resumable = await db.Uploads.FindReceivingAsync(caller.Id, sha, cancellationToken);
|
||||
if (resumable is not null && resumable.Size == request.Size && resumable.SequenceId == request.SequenceId && store.Exists(resumable.Id))
|
||||
return Started(resumable);
|
||||
|
||||
if (caller.User.QuotaBytes > 0)
|
||||
{
|
||||
var pending = await db.Uploads.PendingBytesAsync(caller.Id, cancellationToken);
|
||||
if (caller.User.UsedBytes + pending + request.Size > caller.User.QuotaBytes)
|
||||
return Fail<StartUploadResponse>(403, "quota_exceeded", caller.User.QuotaBytes.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
if (UploadRules.FreeBytes(options.UploadsPath) is { } free && free < request.Size + DiskReserve)
|
||||
return Fail<StartUploadResponse>(507, "insufficient_storage");
|
||||
|
||||
var now = time.GetUtcNow();
|
||||
var upload = await db.Uploads.CreateAsync(new Upload
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
UserId = caller.Id,
|
||||
SequenceId = request.SequenceId,
|
||||
FileName = fileName,
|
||||
Size = request.Size,
|
||||
Sha256 = sha,
|
||||
CapturedAt = request.CapturedAt,
|
||||
ChunkSize = options.ChunkSizeBytes,
|
||||
State = UploadState.Receiving,
|
||||
Created = now,
|
||||
UpdatedAt = now,
|
||||
SessionKey = string.IsNullOrWhiteSpace(request.SessionKey) ? null : request.SessionKey.Trim(),
|
||||
}, cancellationToken);
|
||||
store.Create(upload.Id, upload.Size);
|
||||
return Started(upload);
|
||||
}
|
||||
|
||||
/// <summary>Stores chunk <paramref name="index"/> of an upload the caller owns; repeating a received chunk changes nothing.</summary>
|
||||
public async Task<Outcome<ChunkReceived>> ReceiveChunkAsync(Caller caller, Guid id, int index, HttpRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var upload = await db.Uploads.GetAsync(id, cancellationToken);
|
||||
if (upload is null || upload.UserId != caller.Id) return Fail<ChunkReceived>(404, "not_found", "upload");
|
||||
if (upload.State != UploadState.Receiving) return Fail<ChunkReceived>(409, "upload_state");
|
||||
if (index < 0 || index >= upload.ChunkCount) return Fail<ChunkReceived>(400, "chunk_out_of_range");
|
||||
var expected = Math.Min(upload.ChunkSize, upload.Size - (long)index * upload.ChunkSize);
|
||||
if (request.ContentLength is { } announced && announced != expected) return Fail<ChunkReceived>(400, "chunk_size", expected.ToString(CultureInfo.InvariantCulture));
|
||||
if (upload.HasChunk(index)) return new ChunkReceived(index, UploadRules.ReceivedChunks(upload));
|
||||
|
||||
if (_chunksInFlight.AddOrUpdate(caller.Id, 1, (_, n) => n + 1) > options.ConcurrentChunksPerUser)
|
||||
{
|
||||
Release(caller.Id);
|
||||
return Fail<ChunkReceived>(429, "too_many_uploads");
|
||||
}
|
||||
try
|
||||
{
|
||||
if (!await store.WriteChunkAsync(id, (long)index * upload.ChunkSize, request.Body, expected, cancellationToken))
|
||||
return Fail<ChunkReceived>(400, "chunk_size", expected.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Release(caller.Id);
|
||||
}
|
||||
var updated = await db.Uploads.MarkChunkReceivedAsync(id, index, cancellationToken);
|
||||
return updated is null ? Fail<ChunkReceived>(404, "not_found", "upload") : new ChunkReceived(index, UploadRules.ReceivedChunks(updated));
|
||||
}
|
||||
|
||||
public async Task<Outcome<UploadStateDto>> GetStateAsync(Caller caller, Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var upload = await db.Uploads.GetAsync(id, cancellationToken);
|
||||
if (upload is null || upload.UserId != caller.Id) return Fail<UploadStateDto>(404, "not_found", "upload");
|
||||
return new UploadStateDto(upload.Id, upload.FileName, upload.Size, upload.Sha256, upload.ChunkSize, UploadRules.ReceivedChunks(upload),
|
||||
upload.State.ToString().ToLowerInvariant());
|
||||
}
|
||||
|
||||
/// <summary>Abandons an upload that is still receiving and deletes its partial file.</summary>
|
||||
public async Task<Outcome<bool>> CancelAsync(Caller caller, Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var upload = await db.Uploads.GetAsync(id, cancellationToken);
|
||||
if (upload is null || upload.UserId != caller.Id) return Fail<bool>(404, "not_found", "upload");
|
||||
if (!await db.Uploads.TryTransitionAsync(id, UploadState.Receiving, UploadState.Cancelled, cancellationToken: cancellationToken))
|
||||
return Fail<bool>(409, "upload_state");
|
||||
store.Delete(id);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies and commits a frame upload. Completing an already committed upload answers its frame again, so a client
|
||||
/// may retry a completion whose response it lost. Runs to the end even if the client disconnects.
|
||||
/// </summary>
|
||||
public async Task<Outcome<CompleteUploadResponse>> CompleteAsync(Caller caller, Guid id)
|
||||
{
|
||||
var stopping = lifetime.ApplicationStopping;
|
||||
var upload = await db.Uploads.GetAsync(id, stopping);
|
||||
if (upload is null || upload.UserId != caller.Id) return Fail<CompleteUploadResponse>(404, "not_found", "upload");
|
||||
if (upload.JobId is not null) return Fail<CompleteUploadResponse>(409, "wrong_upload_kind");
|
||||
if (upload is { State: UploadState.Committed, FrameId: { } committed } && await db.Frames.GetAsync(committed, stopping) is { } done)
|
||||
return new CompleteUploadResponse(done.Id, done.SequenceId, done.Index);
|
||||
if (upload.State != UploadState.Receiving) return Fail<CompleteUploadResponse>(409, "upload_state");
|
||||
if (!upload.IsComplete)
|
||||
return Fail<CompleteUploadResponse>(409, "incomplete", (upload.ChunkCount - upload.ReceivedCount).ToString(CultureInfo.InvariantCulture));
|
||||
if (!await db.Uploads.TryTransitionAsync(id, UploadState.Receiving, UploadState.Committing, cancellationToken: stopping))
|
||||
return Fail<CompleteUploadResponse>(409, "upload_state");
|
||||
|
||||
var user = await db.Users.GetAsync(caller.Id, stopping);
|
||||
if (user is { QuotaBytes: > 0 } && user.UsedBytes + upload.Size > user.QuotaBytes)
|
||||
{
|
||||
await FailAsync(upload);
|
||||
return Fail<CompleteUploadResponse>(403, "quota_exceeded", user.QuotaBytes.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
var staged = await StageVerifiedAsync(upload, stopping);
|
||||
if (staged is null) return Fail<CompleteUploadResponse>(422, "hash_mismatch");
|
||||
|
||||
IngestResult result;
|
||||
try
|
||||
{
|
||||
result = await ingest.CommitAsync(new IngestRequest(
|
||||
archive.Id,
|
||||
upload.SequenceId,
|
||||
NewSequenceTitle: null,
|
||||
upload.SequenceId is null ? upload.SessionKey ?? "user-" + caller.Id.ToString("N") : null,
|
||||
staged,
|
||||
upload.FileName,
|
||||
upload.Size,
|
||||
upload.Sha256,
|
||||
upload.CapturedAt,
|
||||
caller.Id), stopping);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
// The part file is intact: the client may complete again.
|
||||
logger.LogError(ex, "Archive ingest of upload {Upload} ({File}) failed", id, upload.FileName);
|
||||
UploadStore.TryDelete(staged);
|
||||
await db.Uploads.TryTransitionAsync(id, UploadState.Committing, UploadState.Receiving, cancellationToken: CancellationToken.None);
|
||||
return Fail<CompleteUploadResponse>(503, "commit_failed");
|
||||
}
|
||||
|
||||
await db.Uploads.TryTransitionAsync(id, UploadState.Committing, UploadState.Committed, result.FrameId, CancellationToken.None);
|
||||
store.Delete(id);
|
||||
if (File.Exists(staged)) UploadStore.TryDelete(staged);
|
||||
if (!result.Duplicate) await db.Users.AddUsedBytesAsync(caller.Id, upload.Size, CancellationToken.None);
|
||||
if (await db.Sequences.GetAsync(result.SequenceId, CancellationToken.None) is { } sequence)
|
||||
{
|
||||
await sequenceJobs.FramesAddedAsync(sequence, CancellationToken.None);
|
||||
events.Publish(new SequenceChangedEvent(sequence.Id, "frames", sequence.FramesVersion, sequence.SettingsVersion));
|
||||
}
|
||||
return new CompleteUploadResponse(result.FrameId, result.SequenceId, result.Index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a fully received upload to the archive staging folder and checks its SHA-256. On a mismatch the upload
|
||||
/// fails, its files are deleted and null is returned; the client starts over.
|
||||
/// </summary>
|
||||
public async Task<string?> StageVerifiedAsync(Upload upload, CancellationToken cancellationToken)
|
||||
{
|
||||
var staged = Path.Combine(options.StagingPath, upload.Id.ToString("N") + Path.GetExtension(upload.FileName).ToLowerInvariant());
|
||||
string hash;
|
||||
try
|
||||
{
|
||||
hash = await store.StageAsync(upload.Id, staged, cancellationToken);
|
||||
}
|
||||
catch
|
||||
{
|
||||
UploadStore.TryDelete(staged);
|
||||
await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Receiving, cancellationToken: CancellationToken.None);
|
||||
throw;
|
||||
}
|
||||
if (hash == upload.Sha256) return staged;
|
||||
logger.LogWarning("Upload {Upload} ({File}) arrived damaged: SHA-256 {Actual} instead of {Expected}", upload.Id, upload.FileName, hash, upload.Sha256);
|
||||
UploadStore.TryDelete(staged);
|
||||
await FailAsync(upload);
|
||||
return null;
|
||||
}
|
||||
|
||||
private async Task FailAsync(Upload upload)
|
||||
{
|
||||
await db.Uploads.TryTransitionAsync(upload.Id, UploadState.Committing, UploadState.Failed, cancellationToken: CancellationToken.None);
|
||||
store.Delete(upload.Id);
|
||||
}
|
||||
|
||||
private StartUploadResponse Started(Upload upload) =>
|
||||
new(upload.Id, upload.ChunkSize, UploadRules.ReceivedChunks(upload), AlreadyExists: false, FrameId: null);
|
||||
|
||||
private void Release(Guid userId)
|
||||
{
|
||||
if (_chunksInFlight.AddOrUpdate(userId, 0, (_, n) => n - 1) <= 0) _chunksInFlight.TryRemove(new KeyValuePair<Guid, int>(userId, 0));
|
||||
}
|
||||
|
||||
private static Outcome<T> Fail<T>(int status, string code, string? details = null) => Outcome<T>.Fail(status, code, details);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using System.Buffers;
|
||||
using System.Security.Cryptography;
|
||||
using Titano.Server.Configuration;
|
||||
|
||||
namespace Titano.Server.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Files behind resumable uploads (BRIEF §14.4): <c>/data/uploads/{id}.part</c> receives chunks at their offsets in any
|
||||
/// order; at completion it is copied, hashing on the way, to a staging file on the archive volume.
|
||||
/// </summary>
|
||||
public sealed class UploadStore(ServerOptions options)
|
||||
{
|
||||
private const string Extension = ".part";
|
||||
|
||||
public string PartPath(Guid id) => Path.Combine(options.UploadsPath, id.ToString("N") + Extension);
|
||||
|
||||
public bool Exists(Guid id) => File.Exists(PartPath(id));
|
||||
|
||||
/// <summary>Creates the part file at its final size, so chunks can land at their offsets.</summary>
|
||||
public void Create(Guid id, long size)
|
||||
{
|
||||
Directory.CreateDirectory(options.UploadsPath);
|
||||
using var file = new FileStream(PartPath(id), FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite);
|
||||
file.SetLength(size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes exactly <paramref name="length"/> bytes of <paramref name="body"/> at <paramref name="offset"/> and flushes
|
||||
/// them to disk; false when the body is shorter or longer (nothing is marked received then).
|
||||
/// </summary>
|
||||
public async Task<bool> WriteChunkAsync(Guid id, long offset, Stream body, long length, CancellationToken cancellationToken)
|
||||
{
|
||||
var buffer = ArrayPool<byte>.Shared.Rent(1 << 16);
|
||||
try
|
||||
{
|
||||
await using var file = new FileStream(PartPath(id), FileMode.Open, FileAccess.Write, FileShare.ReadWrite, 1 << 16, FileOptions.Asynchronous);
|
||||
file.Position = offset;
|
||||
long total = 0;
|
||||
int read;
|
||||
while ((read = await body.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
|
||||
{
|
||||
total += read;
|
||||
if (total > length) return false;
|
||||
await file.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
if (total != length) return false;
|
||||
await file.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
file.Flush(flushToDisk: true);
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Copies the assembled upload to <paramref name="stagingPath"/> (synced to disk) and returns its lower-case hex SHA-256.</summary>
|
||||
public async Task<string> StageAsync(Guid id, string stagingPath, CancellationToken cancellationToken)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(stagingPath)!);
|
||||
using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256);
|
||||
var buffer = ArrayPool<byte>.Shared.Rent(1 << 20);
|
||||
try
|
||||
{
|
||||
await using var source = new FileStream(PartPath(id), FileMode.Open, FileAccess.Read, FileShare.Read, 1 << 16, FileOptions.Asynchronous | FileOptions.SequentialScan);
|
||||
await using var target = new FileStream(stagingPath, FileMode.Create, FileAccess.Write, FileShare.None, 1 << 16, FileOptions.Asynchronous);
|
||||
int read;
|
||||
while ((read = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) > 0)
|
||||
{
|
||||
hash.AppendData(buffer, 0, read);
|
||||
await target.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
await target.FlushAsync(cancellationToken).ConfigureAwait(false);
|
||||
target.Flush(flushToDisk: true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(buffer);
|
||||
}
|
||||
return Convert.ToHexStringLower(hash.GetHashAndReset());
|
||||
}
|
||||
|
||||
public void Delete(Guid id) => TryDelete(PartPath(id));
|
||||
|
||||
/// <summary>Every part file with its last write time (cleanup of uploads the database no longer knows).</summary>
|
||||
public IEnumerable<(Guid Id, DateTime LastWriteUtc)> List()
|
||||
{
|
||||
if (!Directory.Exists(options.UploadsPath)) yield break;
|
||||
foreach (var path in Directory.EnumerateFiles(options.UploadsPath, "*" + Extension))
|
||||
{
|
||||
if (Guid.TryParseExact(Path.GetFileNameWithoutExtension(path), "N", out var id)) yield return (id, File.GetLastWriteTimeUtc(path));
|
||||
}
|
||||
}
|
||||
|
||||
public static void TryDelete(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(path);
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// Retried by the next cleanup pass.
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user