tuxevil-rotator 3.11.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/README.md +13 -12
- package/package.json +8 -4
- package/src/admin-auth.ts +233 -3
- package/src/dashboard-app.ts +257 -0
- package/src/dashboard-live.ts +654 -0
- package/src/dashboard-types.ts +192 -0
- package/src/dashboard.ts +165 -732
- package/src/db-store.ts +43 -0
- package/src/exposure.ts +21 -1
- package/src/proxy.ts +71 -25
- package/src/rotator.ts +30 -30
- package/src/settings-repository.ts +7 -0
- package/src/types.ts +2 -0
- package/src/web/app.tsx +765 -0
- package/src/web/components/charts.tsx +338 -0
- package/src/web/components/command.tsx +279 -0
- package/src/web/components/icons.tsx +357 -0
- package/src/web/components/overlays.tsx +432 -0
- package/src/web/components/ui.tsx +421 -0
- package/src/web/lib/accounts.ts +109 -0
- package/src/web/lib/attention.ts +219 -0
- package/src/web/lib/colors.ts +103 -0
- package/src/web/lib/format.ts +113 -0
- package/src/web/lib/mask.ts +92 -0
- package/src/web/lib/pools.ts +199 -0
- package/src/web/lib/status.ts +242 -0
- package/src/web/lib/usage.ts +296 -0
- package/src/web/main.tsx +16 -0
- package/src/web/nav.ts +28 -0
- package/src/web/pages/AccountDetail.tsx +346 -0
- package/src/web/pages/Accounts.tsx +581 -0
- package/src/web/pages/Keys.tsx +431 -0
- package/src/web/pages/Overview.tsx +600 -0
- package/src/web/pages/Requests.tsx +644 -0
- package/src/web/pages/Settings.tsx +546 -0
- package/src/web/pages/Usage.tsx +346 -0
- package/src/web/pages/shared.ts +36 -0
- package/src/web/state/actions.ts +211 -0
- package/src/web/state/api.ts +273 -0
- package/src/web/state/prefs.ts +19 -0
- package/src/web/state/router.ts +92 -0
- package/src/web/state/store.ts +266 -0
- package/src/web/styles/app.css +5043 -0
- package/src/web/tsconfig.json +14 -0
- package/src/static/dashboard-keys.js +0 -535
- package/src/static/dashboard-logs.js +0 -800
- package/src/static/dashboard.css +0 -2741
- package/src/static/dashboard.js +0 -3334
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// Serves the dashboard single-page app from src/web.
|
|
2
|
+
//
|
|
3
|
+
// The client is TypeScript + Preact, bundled in memory with esbuild the first
|
|
4
|
+
// time it is needed (tsx already ships esbuild, so there is no separate build
|
|
5
|
+
// step and `npm install && npm start` keeps working). Set
|
|
6
|
+
// TUXEVIL_ROTATOR_DASHBOARD_DEV=1 to rebuild on every page load while editing.
|
|
7
|
+
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { gzipSync } from "node:zlib";
|
|
13
|
+
import { rotatorEnv } from "./env.js";
|
|
14
|
+
import { logger } from "./logger.js";
|
|
15
|
+
|
|
16
|
+
const appLogger = logger.child("dashboard");
|
|
17
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
18
|
+
const WEB_ENTRY = join(__dirname, "web", "main.tsx");
|
|
19
|
+
const ASSET_PREFIX = "/dashboard/assets/";
|
|
20
|
+
|
|
21
|
+
interface DashboardAsset {
|
|
22
|
+
path: string;
|
|
23
|
+
type: string;
|
|
24
|
+
body: Buffer;
|
|
25
|
+
gzip: Buffer;
|
|
26
|
+
etag: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface DashboardBundle {
|
|
30
|
+
js: DashboardAsset;
|
|
31
|
+
css: DashboardAsset;
|
|
32
|
+
html: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let bundlePromise: Promise<DashboardBundle> | null = null;
|
|
36
|
+
|
|
37
|
+
function isDevMode(): boolean {
|
|
38
|
+
return rotatorEnv("DASHBOARD_DEV") === "1";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Locked down to same-origin resources. Preact sets inline styles through
|
|
43
|
+
* the CSSOM, which `style-src 'self'` allows; style attributes in markup and
|
|
44
|
+
* inline scripts are refused.
|
|
45
|
+
*/
|
|
46
|
+
export const DASHBOARD_CSP = [
|
|
47
|
+
"default-src 'none'",
|
|
48
|
+
"script-src 'self'",
|
|
49
|
+
"style-src 'self'",
|
|
50
|
+
"img-src 'self' data:",
|
|
51
|
+
"font-src 'self'",
|
|
52
|
+
"connect-src 'self'",
|
|
53
|
+
"manifest-src 'self'",
|
|
54
|
+
"base-uri 'none'",
|
|
55
|
+
"form-action 'self'",
|
|
56
|
+
"frame-ancestors 'none'",
|
|
57
|
+
].join("; ");
|
|
58
|
+
|
|
59
|
+
const FAVICON =
|
|
60
|
+
"data:image/svg+xml," +
|
|
61
|
+
encodeURIComponent(
|
|
62
|
+
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><rect width="32" height="32" rx="8" fill="#5b4bdb"/><path d="M9 11h14M9 16h10M9 21h6" stroke="#fff" stroke-width="3" stroke-linecap="round"/></svg>',
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
function makeAsset(name: string, ext: string, body: Buffer, type: string): DashboardAsset {
|
|
66
|
+
const hash = createHash("sha256").update(body).digest("hex").slice(0, 12);
|
|
67
|
+
return {
|
|
68
|
+
path: `${ASSET_PREFIX}${name}.${hash}.${ext}`,
|
|
69
|
+
type,
|
|
70
|
+
body,
|
|
71
|
+
gzip: gzipSync(body),
|
|
72
|
+
etag: `"${hash}"`,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function renderShell(js: DashboardAsset, css: DashboardAsset): string {
|
|
77
|
+
return `<!doctype html>
|
|
78
|
+
<html lang="en">
|
|
79
|
+
<head>
|
|
80
|
+
<meta charset="utf-8">
|
|
81
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
82
|
+
<meta name="color-scheme" content="light dark">
|
|
83
|
+
<meta name="referrer" content="no-referrer">
|
|
84
|
+
<title>Tuxevil Rotator</title>
|
|
85
|
+
<link rel="icon" href="${FAVICON}">
|
|
86
|
+
<link rel="stylesheet" href="${css.path}">
|
|
87
|
+
<script type="module" src="${js.path}"></script>
|
|
88
|
+
</head>
|
|
89
|
+
<body>
|
|
90
|
+
<div id="app"></div>
|
|
91
|
+
<noscript>The Tuxevil Rotator dashboard needs JavaScript.</noscript>
|
|
92
|
+
</body>
|
|
93
|
+
</html>
|
|
94
|
+
`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function buildBundle(): Promise<DashboardBundle> {
|
|
98
|
+
const { build } = await import("esbuild");
|
|
99
|
+
const dev = isDevMode();
|
|
100
|
+
const result = await build({
|
|
101
|
+
entryPoints: [WEB_ENTRY],
|
|
102
|
+
bundle: true,
|
|
103
|
+
write: false,
|
|
104
|
+
format: "esm",
|
|
105
|
+
platform: "browser",
|
|
106
|
+
target: ["es2020"],
|
|
107
|
+
outdir: "/dashboard-out",
|
|
108
|
+
entryNames: "app",
|
|
109
|
+
jsx: "automatic",
|
|
110
|
+
jsxImportSource: "preact",
|
|
111
|
+
minify: !dev,
|
|
112
|
+
sourcemap: dev ? "inline" : false,
|
|
113
|
+
legalComments: "none",
|
|
114
|
+
logLevel: "silent",
|
|
115
|
+
define: { "process.env.NODE_ENV": dev ? '"development"' : '"production"' },
|
|
116
|
+
});
|
|
117
|
+
const jsFile = result.outputFiles.find((f) => f.path.endsWith(".js"));
|
|
118
|
+
const cssFile = result.outputFiles.find((f) => f.path.endsWith(".css"));
|
|
119
|
+
if (!jsFile || !cssFile) throw new Error("dashboard bundle is missing app.js or app.css");
|
|
120
|
+
const js = makeAsset("app", "js", Buffer.from(jsFile.contents), "text/javascript; charset=utf-8");
|
|
121
|
+
const css = makeAsset("app", "css", Buffer.from(cssFile.contents), "text/css; charset=utf-8");
|
|
122
|
+
return { js, css, html: renderShell(js, css) };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** `fresh` rebuilds in dev mode; asset requests reuse the bundle the page was rendered with. */
|
|
126
|
+
export function loadDashboardBundle(fresh = false): Promise<DashboardBundle> {
|
|
127
|
+
if (!bundlePromise || (fresh && isDevMode())) {
|
|
128
|
+
const started = Date.now();
|
|
129
|
+
const pending = buildBundle();
|
|
130
|
+
bundlePromise = pending;
|
|
131
|
+
pending.then(
|
|
132
|
+
(bundle) =>
|
|
133
|
+
appLogger.info(
|
|
134
|
+
`Dashboard bundle ready in ${Date.now() - started}ms (${Math.round(bundle.js.body.length / 1024)} KB js, ${Math.round(bundle.css.body.length / 1024)} KB css)`,
|
|
135
|
+
),
|
|
136
|
+
(err) => {
|
|
137
|
+
appLogger.error(`Dashboard bundle failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
138
|
+
if (bundlePromise === pending) bundlePromise = null;
|
|
139
|
+
},
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
return bundlePromise!;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Start building at server start so the first page load is fast and errors surface early. */
|
|
146
|
+
export function prewarmDashboard(): void {
|
|
147
|
+
loadDashboardBundle().catch(() => {
|
|
148
|
+
// logged in loadDashboardBundle; retried on the next request
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function acceptsGzip(req: IncomingMessage): boolean {
|
|
153
|
+
const header = req.headers["accept-encoding"];
|
|
154
|
+
const value = Array.isArray(header) ? header.join(",") : header || "";
|
|
155
|
+
return /\bgzip\b/.test(value);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function securityHeaders(): Record<string, string> {
|
|
159
|
+
return {
|
|
160
|
+
"Content-Security-Policy": DASHBOARD_CSP,
|
|
161
|
+
"X-Content-Type-Options": "nosniff",
|
|
162
|
+
"Referrer-Policy": "no-referrer",
|
|
163
|
+
"X-Frame-Options": "DENY",
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function sendBuildError(res: ServerResponse, err: unknown): void {
|
|
168
|
+
res.writeHead(500, {
|
|
169
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
170
|
+
"Cache-Control": "no-store",
|
|
171
|
+
});
|
|
172
|
+
res.end(
|
|
173
|
+
isDevMode()
|
|
174
|
+
? `Dashboard failed to build:\n\n${err instanceof Error ? err.message : String(err)}`
|
|
175
|
+
: "Dashboard failed to build. Check the server logs.",
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export async function serveDashboardShell(res: ServerResponse): Promise<void> {
|
|
180
|
+
let bundle: DashboardBundle;
|
|
181
|
+
try {
|
|
182
|
+
bundle = await loadDashboardBundle(true);
|
|
183
|
+
} catch (err) {
|
|
184
|
+
sendBuildError(res, err);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
res.writeHead(200, {
|
|
188
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
189
|
+
"Cache-Control": "no-store",
|
|
190
|
+
...securityHeaders(),
|
|
191
|
+
});
|
|
192
|
+
res.end(bundle.html);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Returns false when the path is not a dashboard asset. */
|
|
196
|
+
export async function serveDashboardAsset(
|
|
197
|
+
req: IncomingMessage,
|
|
198
|
+
res: ServerResponse,
|
|
199
|
+
pathname: string,
|
|
200
|
+
): Promise<boolean> {
|
|
201
|
+
if (!pathname.startsWith(ASSET_PREFIX)) return false;
|
|
202
|
+
let bundle: DashboardBundle;
|
|
203
|
+
try {
|
|
204
|
+
bundle = await loadDashboardBundle();
|
|
205
|
+
} catch (err) {
|
|
206
|
+
sendBuildError(res, err);
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
const asset = [bundle.js, bundle.css].find((a) => a.path === pathname);
|
|
210
|
+
if (!asset) {
|
|
211
|
+
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-store" });
|
|
212
|
+
res.end("Not found");
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
const headers: Record<string, string> = {
|
|
216
|
+
"Content-Type": asset.type,
|
|
217
|
+
"Cache-Control": "public, max-age=31536000, immutable",
|
|
218
|
+
ETag: asset.etag,
|
|
219
|
+
Vary: "Accept-Encoding",
|
|
220
|
+
"X-Content-Type-Options": "nosniff",
|
|
221
|
+
};
|
|
222
|
+
if (req.headers["if-none-match"] === asset.etag) {
|
|
223
|
+
res.writeHead(304, headers);
|
|
224
|
+
res.end();
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
if (acceptsGzip(req)) {
|
|
228
|
+
res.writeHead(200, { ...headers, "Content-Encoding": "gzip" });
|
|
229
|
+
res.end(asset.gzip);
|
|
230
|
+
} else {
|
|
231
|
+
res.writeHead(200, headers);
|
|
232
|
+
res.end(asset.body);
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** JSON response, gzipped when large and the client accepts it. */
|
|
238
|
+
export function sendDashboardJson(
|
|
239
|
+
req: IncomingMessage,
|
|
240
|
+
res: ServerResponse,
|
|
241
|
+
status: number,
|
|
242
|
+
body: unknown,
|
|
243
|
+
): void {
|
|
244
|
+
const json = Buffer.from(JSON.stringify(body));
|
|
245
|
+
const headers: Record<string, string> = {
|
|
246
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
247
|
+
"Cache-Control": "no-store",
|
|
248
|
+
Vary: "Accept-Encoding",
|
|
249
|
+
};
|
|
250
|
+
if (json.length > 2048 && acceptsGzip(req)) {
|
|
251
|
+
res.writeHead(status, { ...headers, "Content-Encoding": "gzip" });
|
|
252
|
+
res.end(gzipSync(json));
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
res.writeHead(status, headers);
|
|
256
|
+
res.end(json);
|
|
257
|
+
}
|