Spaces:
Paused
Paused
Protect Hermes app route with gateway token
Browse filesFiles changed (2)
- README.md+17
- health-server.js+47-8
45 unmodified lines46474849505145 unmodified lines| `CLOUDFLARE_WORKERS_TOKEN` | Optional | Auto-creates a Worker proxy for Telegram Bot API traffic || `UPTIMEROBOT_API_KEY` | Optional | Auto-creates a monitor for `/health` |## LLM ProvidersHuggingMess supports two configuration styles:45 unmodified lines464748495051525354555657585960616263646566676845 unmodified lines| `CLOUDFLARE_WORKERS_TOKEN` | Optional | Auto-creates a Worker proxy for Telegram Bot API traffic || `UPTIMEROBOT_API_KEY` | Optional | Auto-creates a monitor for `/health` |## Access ControlHermes' built-in dashboard is local-first and does not provide its own public auth layer. HuggingMess adds wrapper-level auth for the exposed Space routes.Set this Space secret:```textGATEWAY_TOKEN=your-strong-password-or-token```Then:- Opening `/app/` asks for browser Basic Auth.- Use any username.- Use `GATEWAY_TOKEN` as the password.- API routes under `/v1/*` accept `Authorization: Bearer <GATEWAY_TOKEN>`.## LLM ProvidersHuggingMess supports two configuration styles:
11 unmodified lines12131415161720 unmodified lines38394041424376 unmodified lines12012112212312412512689 unmodified lines2162172182192202215 unmodified lines22722822923023123219 unmodified lines25225325425525625725825926026126226326411 unmodified linesconst startTime = Date.now();const API_SERVER_KEY = process.env.API_SERVER_KEY || "";const APP_BASE = "/app";const SYNC_STATUS_FILE = "/tmp/huggingmess-sync-status.json";const UPTIMEROBOT_STATUS_FILE = "/tmp/huggingmess-uptimerobot-status.json";20 unmodified linesreturn fallback;}function proxyRequest(req, res, targetPort, rewritePath = (path) => path) {const parsed = new URL(req.url, "http://localhost");const targetPath = rewritePath(parsed.pathname) + parsed.search;76 unmodified linesfunction renderDashboard(data) {const syncStatus = String(data.backup?.status || "unknown").toUpperCase();const dashboardLink = data.dashboard ? `<a class="button" href="${APP_BASE}/">Open Hermes App</a>` : "";const apiLink = data.gateway ? `<a class="button secondary" href="/v1/models">API Models</a>` : "";const keepAlive = data.uptimerobot?.configured? `UptimeRobot is monitoring <code>${data.uptimerobot.url}</code>.`89 unmodified lines}if (path === APP_BASE || path.startsWith(`${APP_BASE}/`)) {proxyRequest(req, res, DASHBOARD_PORT, (p) => p.replace(/^\/app/, "") || "/");return;}5 unmodified linespath.startsWith("/dashboard-plugins/") ||path.startsWith("/ds-assets/")) {proxyRequest(req, res, DASHBOARD_PORT);return;}19 unmodified lines}if (path === "/v1" || path.startsWith("/v1/")) {if (API_SERVER_KEY) {const expected = `Bearer ${API_SERVER_KEY}`;if (req.headers.authorization !== expected) {res.writeHead(401, { "content-type": "application/json" });res.end(JSON.stringify({ error: "unauthorized", message: "Use Authorization: Bearer <GATEWAY_TOKEN>." }));return;}}proxyRequest(req, res, GATEWAY_PORT);return;11 unmodified lines1213141516171820 unmodified lines394041424344454647484950515253545556575859606162636465666768697071727374757677787976 unmodified lines15615715815916016116289 unmodified lines2522532542552562572585 unmodified lines26426526626726826927019 unmodified lines29029129229329429529629729829930030130230311 unmodified linesconst startTime = Date.now();const API_SERVER_KEY = process.env.API_SERVER_KEY || "";const APP_BASE = "/app";const AUTH_REALM = "HuggingMess";const SYNC_STATUS_FILE = "/tmp/huggingmess-sync-status.json";const UPTIMEROBOT_STATUS_FILE = "/tmp/huggingmess-uptimerobot-status.json";20 unmodified linesreturn fallback;}function getBearerToken(req) {const value = req.headers.authorization || "";const match = /^Bearer\s+(.+)$/i.exec(value);return match ? match[1] : "";}function getBasicPassword(req) {const value = req.headers.authorization || "";const match = /^Basic\s+(.+)$/i.exec(value);if (!match) return "";try {const decoded = Buffer.from(match[1], "base64").toString("utf8");const separator = decoded.indexOf(":");return separator >= 0 ? decoded.slice(separator + 1) : "";} catch {return "";}}function isAuthorized(req) {if (!API_SERVER_KEY) return true;return getBearerToken(req) === API_SERVER_KEY || getBasicPassword(req) === API_SERVER_KEY;}function requireAuth(req, res) {if (isAuthorized(req)) return true;res.writeHead(401, {"content-type": "text/plain; charset=utf-8","www-authenticate": `Basic realm="${AUTH_REALM}", charset="UTF-8"`,"cache-control": "no-store",});res.end("Authentication required. Use any username and your GATEWAY_TOKEN as the password.");return false;}function proxyRequest(req, res, targetPort, rewritePath = (path) => path) {const parsed = new URL(req.url, "http://localhost");const targetPath = rewritePath(parsed.pathname) + parsed.search;76 unmodified linesfunction renderDashboard(data) {const syncStatus = String(data.backup?.status || "unknown").toUpperCase();const dashboardLink = data.dashboard ? `<a class="button" href="${APP_BASE}/" target="_blank" rel="noopener noreferrer">Open Hermes App</a>` : "";const apiLink = data.gateway ? `<a class="button secondary" href="/v1/models">API Models</a>` : "";const keepAlive = data.uptimerobot?.configured? `UptimeRobot is monitoring <code>${data.uptimerobot.url}</code>.`89 unmodified lines}if (path === APP_BASE || path.startsWith(`${APP_BASE}/`)) {if (!requireAuth(req, res)) return;proxyRequest(req, res, DASHBOARD_PORT, (p) => p.replace(/^\/app/, "") || "/");return;}5 unmodified linespath.startsWith("/dashboard-plugins/") ||path.startsWith("/ds-assets/")) {if (!requireAuth(req, res)) return;proxyRequest(req, res, DASHBOARD_PORT);return;}19 unmodified lines}if (path === "/v1" || path.startsWith("/v1/")) {if (!isAuthorized(req)) {res.writeHead(401, {"content-type": "application/json","www-authenticate": `Bearer realm="${AUTH_REALM}"`,"cache-control": "no-store",});res.end(JSON.stringify({ error: "unauthorized", message: "Use Authorization: Bearer <GATEWAY_TOKEN>." }));return;}proxyRequest(req, res, GATEWAY_PORT);return;
README.md+17
health-server.js+47-8