- docker-compose.yml: mark 'egress' network internal: true. Every Docker bridge network gets outbound NAT to the internet by default, not just 'default' - missing this meant sde-meeting-toolkit still had a direct route out despite being off 'default'. Confirmed via a real smoke test run (Check 2 failed before this fix). - egress-proxy/tinyproxy.conf: 'Group nogroup' doesn't exist on Alpine, only 'nobody'. Fixed to 'Group nobody' for both User and Group. - server.js: surface err.cause alongside err.message when the Anthropic call fails. undici wraps the real reason in a generic 'fetch failed' TypeError; without this the actual cause was invisible in both the API response and the logs.
509 lines
21 KiB
JavaScript
509 lines
21 KiB
JavaScript
/*
|
|
* Local server for the SDE meeting toolkit.
|
|
* Serves the static tool files.
|
|
* Proxies AI draft requests to the Anthropic API, so the API key
|
|
* stays on the server and never appears in the browser.
|
|
* Identifies each caller by login and enforces a daily token quota per person.
|
|
*/
|
|
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const ROOT = __dirname;
|
|
const PORT = process.env.PORT || 5173;
|
|
const ANTHROPIC_VERSION = '2023-06-01';
|
|
|
|
/* ===== Outbound egress restriction =====
|
|
* When EGRESS_PROXY_URL is set (only in the hardened Docker deployment,
|
|
* via docker-compose.yml), the container has no direct route to the
|
|
* internet at all. The only outbound call this app makes - to
|
|
* api.anthropic.com - is routed through a small egress-proxy container
|
|
* that only allows that one destination. Local, non-Docker runs leave
|
|
* this unset and call the API directly with Node's built-in fetch, same
|
|
* as always.
|
|
*
|
|
* Important: this deliberately uses undici's OWN fetch() here, not
|
|
* Node's global fetch(). Node's global fetch is powered by an internal,
|
|
* pinned copy of undici, which is not guaranteed to match the version of
|
|
* the standalone 'undici' package installed from npm. Passing that
|
|
* package's ProxyAgent as a `dispatcher` to the GLOBAL fetch throws
|
|
* ("invalid onRequestStart method") because the two don't speak the same
|
|
* internal protocol - confirmed while building this. Using undici's own
|
|
* fetch alongside its own ProxyAgent avoids the mismatch entirely. */
|
|
let doFetch = fetch; // Node's built-in global fetch, used when there's no proxy to route through.
|
|
if (process.env.EGRESS_PROXY_URL) {
|
|
const { fetch: undiciFetch, ProxyAgent } = require('undici');
|
|
const dispatcher = new ProxyAgent(process.env.EGRESS_PROXY_URL);
|
|
doFetch = (url, opts) => undiciFetch(url, { ...opts, dispatcher });
|
|
console.log('Outbound traffic restricted: routing through egress proxy at ' + process.env.EGRESS_PROXY_URL);
|
|
}
|
|
|
|
const MIME = {
|
|
'.html': 'text/html; charset=utf-8',
|
|
'.js': 'text/javascript; charset=utf-8',
|
|
'.css': 'text/css; charset=utf-8',
|
|
'.json': 'application/json; charset=utf-8',
|
|
'.svg': 'image/svg+xml',
|
|
'.png': 'image/png',
|
|
'.skill': 'application/zip'
|
|
};
|
|
|
|
function loadEnvFile() {
|
|
const envPath = path.join(ROOT, '.env');
|
|
let content;
|
|
try {
|
|
content = fs.readFileSync(envPath, 'utf8');
|
|
} catch (err) {
|
|
return; // no .env file; that is allowed, the AI draft route will report it
|
|
}
|
|
content.split('\n').forEach((line) => {
|
|
const trimmed = line.trim();
|
|
if (!trimmed || trimmed.startsWith('#')) return;
|
|
const idx = trimmed.indexOf('=');
|
|
if (idx === -1) return;
|
|
const key = trimmed.slice(0, idx).trim();
|
|
const value = trimmed.slice(idx + 1).trim().replace(/^["']|["']$/g, '');
|
|
if (key && !(key in process.env)) process.env[key] = value;
|
|
});
|
|
}
|
|
loadEnvFile();
|
|
|
|
function sendJson(res, status, obj) {
|
|
const body = JSON.stringify(obj);
|
|
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
res.end(body);
|
|
}
|
|
|
|
/* ===== Access control =====
|
|
* Named users. Set APP_USERS as a comma-separated list of user:password
|
|
* pairs, for example: APP_USERS=alice:pass1,bob:pass2
|
|
* Each name is also the identity used for the daily token quota below.
|
|
*
|
|
* Legacy single-user mode: set APP_USERNAME and APP_PASSWORD instead. That
|
|
* name becomes the one identity everyone shares (no per-person quota).
|
|
*
|
|
* Leave all of the above unset for solo local use: no login prompt, and
|
|
* usage is tracked under the identity "local".
|
|
*
|
|
* This is a simple shared-credential gate, meant to sit behind the company
|
|
* VPN and internal network, not to replace them. */
|
|
function loadUserMap() {
|
|
const map = {};
|
|
if (process.env.APP_USERS) {
|
|
process.env.APP_USERS.split(',').forEach((pair) => {
|
|
const idx = pair.indexOf(':');
|
|
if (idx === -1) return;
|
|
const name = pair.slice(0, idx).trim();
|
|
const pass = pair.slice(idx + 1).trim();
|
|
if (name && pass) map[name] = pass;
|
|
});
|
|
} else if (process.env.APP_USERNAME && process.env.APP_PASSWORD) {
|
|
map[process.env.APP_USERNAME] = process.env.APP_PASSWORD;
|
|
}
|
|
return map;
|
|
}
|
|
const USER_MAP = loadUserMap();
|
|
const AUTH_REQUIRED = Object.keys(USER_MAP).length > 0;
|
|
|
|
// Returns { ok, user, attempted }. "user" is the identified name on success
|
|
// (null on failure); "attempted" is the name that was tried, even on
|
|
// failure, so a failed attempt can still be logged without ever logging
|
|
// the password itself.
|
|
function checkBasicAuth(req) {
|
|
if (!AUTH_REQUIRED) return { ok: true, user: 'local', attempted: null };
|
|
const header = req.headers['authorization'] || '';
|
|
if (!header.startsWith('Basic ')) return { ok: false, user: null, attempted: null };
|
|
let decoded;
|
|
try {
|
|
decoded = Buffer.from(header.slice(6), 'base64').toString('utf8');
|
|
} catch (err) {
|
|
return { ok: false, user: null, attempted: null };
|
|
}
|
|
const idx = decoded.indexOf(':');
|
|
if (idx === -1) return { ok: false, user: null, attempted: null };
|
|
const name = decoded.slice(0, idx);
|
|
const pass = decoded.slice(idx + 1);
|
|
const ok = USER_MAP[name] === pass;
|
|
return { ok, user: ok ? name : null, attempted: name };
|
|
}
|
|
function requireAuth(res) {
|
|
res.writeHead(401, {
|
|
'WWW-Authenticate': 'Basic realm="SDE Meeting Toolkit"',
|
|
'Content-Type': 'text/plain'
|
|
});
|
|
res.end('Authentication required.');
|
|
}
|
|
|
|
/* ===== Login lockout (brute-force protection) =====
|
|
* Separate from the AI-proxy rate limiter below: this protects the login
|
|
* gate itself. Basic Auth has no built-in throttling, so without this a
|
|
* script could try passwords as fast as the network allows.
|
|
*
|
|
* After LOGIN_ATTEMPT_MAX failed logins from one IP within
|
|
* LOGIN_ATTEMPT_WINDOW_MS, that IP is locked out for LOGIN_LOCKOUT_MS -
|
|
* every request refused with 429, even with the correct password, until
|
|
* the lockout expires. In-memory only: resets if the process restarts. */
|
|
const LOGIN_ATTEMPT_MAX = parseInt(process.env.LOGIN_ATTEMPT_MAX || '10', 10);
|
|
const LOGIN_ATTEMPT_WINDOW_MS = parseInt(process.env.LOGIN_ATTEMPT_WINDOW_MS || String(10 * 60 * 1000), 10);
|
|
const LOGIN_LOCKOUT_MS = parseInt(process.env.LOGIN_LOCKOUT_MS || String(15 * 60 * 1000), 10);
|
|
const loginFailures = new Map(); // ip -> [timestamps]
|
|
const loginLockouts = new Map(); // ip -> lockout-expires-at (ms epoch)
|
|
|
|
function isLockedOut(ip) {
|
|
const until = loginLockouts.get(ip);
|
|
if (!until) return false;
|
|
if (Date.now() < until) return true;
|
|
loginLockouts.delete(ip);
|
|
loginFailures.delete(ip);
|
|
return false;
|
|
}
|
|
function recordLoginFailure(ip) {
|
|
const now = Date.now();
|
|
const hits = (loginFailures.get(ip) || []).filter((t) => now - t < LOGIN_ATTEMPT_WINDOW_MS);
|
|
hits.push(now);
|
|
loginFailures.set(ip, hits);
|
|
if (hits.length > LOGIN_ATTEMPT_MAX) {
|
|
loginLockouts.set(ip, now + LOGIN_LOCKOUT_MS);
|
|
}
|
|
}
|
|
function clearLoginFailures(ip) {
|
|
loginFailures.delete(ip);
|
|
}
|
|
|
|
/* ===== Sign-in audit log =====
|
|
* Appends one JSON line per login event to AUTH_LOG_FILE: timestamp,
|
|
* source IP, the username, and the result. Never logs the password.
|
|
* Durable across restarts, same as the token usage file below, as long as
|
|
* AUTH_LOG_FILE's folder is a mounted volume.
|
|
*
|
|
* Basic Auth resends credentials on every request (every page, every
|
|
* asset, every AI call), not just once at "login" - logging every one of
|
|
* those would flood this file with noise. Failures are logged individually
|
|
* (that is what makes brute-force attempts visible). Successes are
|
|
* debounced per (ip, user) to roughly one line per session, not one per
|
|
* request. */
|
|
const AUTH_LOG_FILE = process.env.AUTH_LOG_FILE || path.join(ROOT, 'data', 'auth-log.jsonl');
|
|
const SIGNIN_LOG_DEBOUNCE_MS = parseInt(process.env.SIGNIN_LOG_DEBOUNCE_MS || String(30 * 60 * 1000), 10);
|
|
const lastLoggedSignin = new Map(); // "ip|user" -> timestamp
|
|
|
|
function logAuthEvent(ip, user, result) {
|
|
const line = JSON.stringify({ ts: new Date().toISOString(), ip, user: user || null, result }) + '\n';
|
|
try {
|
|
fs.mkdirSync(path.dirname(AUTH_LOG_FILE), { recursive: true });
|
|
fs.appendFileSync(AUTH_LOG_FILE, line);
|
|
} catch (err) {
|
|
console.error('Could not write auth log:', err.message);
|
|
}
|
|
}
|
|
function shouldLogSuccess(ip, user) {
|
|
const key = ip + '|' + user;
|
|
const now = Date.now();
|
|
const last = lastLoggedSignin.get(key) || 0;
|
|
if (now - last < SIGNIN_LOG_DEBOUNCE_MS) return false;
|
|
lastLoggedSignin.set(key, now);
|
|
return true;
|
|
}
|
|
|
|
/* ===== Rate limiting for the AI proxy =====
|
|
* Caps AI draft calls per source IP, independent of the token quota below.
|
|
* This catches a runaway script quickly, before it burns through a whole
|
|
* day's token quota in a few seconds. Resets if the process restarts. */
|
|
const RATE_LIMIT_MAX = parseInt(process.env.RATE_LIMIT_MAX || '20', 10);
|
|
const RATE_LIMIT_WINDOW_MS = parseInt(process.env.RATE_LIMIT_WINDOW_MS || String(5 * 60 * 1000), 10);
|
|
const rateLimitHits = new Map();
|
|
function isRateLimited(ip) {
|
|
const now = Date.now();
|
|
const hits = (rateLimitHits.get(ip) || []).filter((t) => now - t < RATE_LIMIT_WINDOW_MS);
|
|
hits.push(now);
|
|
rateLimitHits.set(ip, hits);
|
|
return hits.length > RATE_LIMIT_MAX;
|
|
}
|
|
|
|
/* ===== Per-user daily token quota (off by default) =====
|
|
* On the back burner: real usage does not look large enough to justify
|
|
* running this. Left in place, disabled, in case that changes. Set
|
|
* TOKEN_LIMIT_PER_USER to a positive number to turn it back on; leave it
|
|
* unset or 0 for unlimited, which skips the check and the usage file.
|
|
*
|
|
* TOKEN_LIMIT_PER_USER caps combined input+output tokens per identified user,
|
|
* per UTC calendar day. Usage is written to TOKEN_USAGE_FILE after every AI
|
|
* call, so it survives a container restart. Mount that file's folder as a
|
|
* volume in Docker or it resets on every redeploy.
|
|
*
|
|
* The check runs before the API call using the day's tally so far: if the
|
|
* user is already at or over the limit, the call is refused with no cost.
|
|
* A single call in progress when the limit is reached can still push the
|
|
* tally past the cap by that one call's tokens; the next call is blocked.
|
|
* That is an accepted tradeoff, since real token cost of a call is only
|
|
* known once its response returns. */
|
|
const TOKEN_LIMIT_PER_USER = parseInt(process.env.TOKEN_LIMIT_PER_USER || '0', 10);
|
|
const TOKEN_LIMIT_ENABLED = TOKEN_LIMIT_PER_USER > 0;
|
|
const TOKEN_USAGE_FILE = process.env.TOKEN_USAGE_FILE || path.join(ROOT, 'data', 'token-usage.json');
|
|
|
|
function todayUTC() {
|
|
return new Date().toISOString().slice(0, 10); // "YYYY-MM-DD"
|
|
}
|
|
function loadUsage() {
|
|
try {
|
|
return JSON.parse(fs.readFileSync(TOKEN_USAGE_FILE, 'utf8'));
|
|
} catch (err) {
|
|
return {};
|
|
}
|
|
}
|
|
function saveUsage(usage) {
|
|
try {
|
|
fs.mkdirSync(path.dirname(TOKEN_USAGE_FILE), { recursive: true });
|
|
fs.writeFileSync(TOKEN_USAGE_FILE, JSON.stringify(usage));
|
|
} catch (err) {
|
|
console.error('Could not write token usage file:', err.message);
|
|
}
|
|
}
|
|
function usedTokensToday(usage, user) {
|
|
const rec = usage[user];
|
|
if (!rec || rec.date !== todayUTC()) return 0;
|
|
return rec.tokens;
|
|
}
|
|
function addTokens(user, tokens) {
|
|
const usage = loadUsage();
|
|
const today = todayUTC();
|
|
const rec = usage[user] && usage[user].date === today ? usage[user] : { date: today, tokens: 0 };
|
|
rec.tokens += tokens;
|
|
usage[user] = rec;
|
|
saveUsage(usage);
|
|
return rec.tokens;
|
|
}
|
|
function secondsUntilUTCMidnight() {
|
|
const now = new Date();
|
|
const midnight = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1));
|
|
return Math.round((midnight - now) / 1000);
|
|
}
|
|
|
|
/* ===== Mock mode =====
|
|
* Set MOCK_AI=true to test the whole app, including the AI draft button and
|
|
* the token quota above, with no API key and no real API call. Returns a
|
|
* canned breadcrumb draft with a synthetic usage count, shaped like a real
|
|
* Anthropic response, so the frontend parsing, gate checks, and quota
|
|
* accounting all run exactly as they would against the live API. */
|
|
function mockClaudeResponse(res, user) {
|
|
const draft = {
|
|
domain: 'MOCK: Progress Visibility',
|
|
driver: 'MOCK driver: schedule forecasts drift from field reality.',
|
|
cap: 'MOCK capability: a rules-of-credit progress check against the field.',
|
|
metric: 'MOCK metric: claiming accuracy, reported % vs field-verified %, by phase.',
|
|
method: 'MOCK method: monthly spot audit of 30+ sampled assets.',
|
|
baseline: 'MOCK baseline: first audit result.'
|
|
};
|
|
const usage = { input_tokens: 950, output_tokens: 280 };
|
|
const body = {
|
|
content: [{ type: 'text', text: JSON.stringify(draft) }],
|
|
usage
|
|
};
|
|
if (TOKEN_LIMIT_ENABLED) addTokens(user, usage.input_tokens + usage.output_tokens);
|
|
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
res.end(JSON.stringify(body));
|
|
}
|
|
|
|
function proxyToClaude(req, res, user) {
|
|
if (String(process.env.MOCK_AI).toLowerCase() === 'true') {
|
|
mockClaudeResponse(res, user);
|
|
return;
|
|
}
|
|
if (!process.env.ANTHROPIC_API_KEY) {
|
|
sendJson(res, 500, {
|
|
error: 'ANTHROPIC_API_KEY is not set. Add it to the .env file in this folder, then restart the server.'
|
|
});
|
|
return;
|
|
}
|
|
let body = '';
|
|
req.on('data', (chunk) => { body += chunk; });
|
|
req.on('end', async () => {
|
|
try {
|
|
const upstream = await doFetch('https://api.anthropic.com/v1/messages', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'x-api-key': process.env.ANTHROPIC_API_KEY,
|
|
'anthropic-version': ANTHROPIC_VERSION
|
|
},
|
|
body
|
|
});
|
|
const text = await upstream.text();
|
|
res.writeHead(upstream.status, { 'Content-Type': 'application/json; charset=utf-8' });
|
|
res.end(text);
|
|
if (TOKEN_LIMIT_ENABLED) {
|
|
try {
|
|
const parsed = JSON.parse(text);
|
|
if (parsed.usage) {
|
|
addTokens(user, (parsed.usage.input_tokens || 0) + (parsed.usage.output_tokens || 0));
|
|
}
|
|
} catch (err) {
|
|
// Response was not JSON, or had no usage field. Nothing to record.
|
|
}
|
|
}
|
|
} catch (err) {
|
|
// undici's fetch wraps the real reason in a generic "fetch failed"
|
|
// TypeError, with the actual cause one level down in err.cause.
|
|
// Surface both, or "fetch failed" alone tells you nothing.
|
|
const detail = err.cause ? (err.message + ' - ' + err.cause.message) : err.message;
|
|
console.error('Anthropic API call failed:', detail);
|
|
sendJson(res, 502, { error: 'Could not reach the Anthropic API: ' + detail });
|
|
}
|
|
});
|
|
}
|
|
|
|
function serveStatic(req, res) {
|
|
const urlPath = decodeURIComponent(req.url.split('?')[0]);
|
|
if (urlPath === '/') {
|
|
const index = `<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Project SDE | Meeting Toolkit</title>
|
|
<style>
|
|
:root{
|
|
--bg:#f4f4f4; --layer:#ffffff; --layer-hover:#e8e8e8;
|
|
--border:#e0e0e0; --border-strong:#8d8d8d;
|
|
--text:#161616; --text-secondary:#525252; --text-helper:#6f6f6f;
|
|
--interactive:#0f62fe; --interactive-hover:#0353e9;
|
|
--focus:#0f62fe;
|
|
}
|
|
*{box-sizing:border-box; margin:0; padding:0;}
|
|
body{
|
|
font-family:'IBM Plex Sans','Segoe UI',system-ui,-apple-system,sans-serif;
|
|
background:var(--bg); color:var(--text); font-size:16px; line-height:1.4;
|
|
}
|
|
a{color:inherit; text-decoration:none;}
|
|
a:focus-visible, button:focus-visible{outline:2px solid var(--focus); outline-offset:1px;}
|
|
header{
|
|
background:var(--text); color:#fff; padding:0 24px;
|
|
display:flex; align-items:center; gap:24px; height:56px;
|
|
}
|
|
header .brand{font-size:15px; letter-spacing:.16px;}
|
|
header .brand strong{font-weight:600;}
|
|
header .brand span{color:#c6c6c6; font-weight:400;}
|
|
main{max-width:900px; margin:0 auto; padding:56px 24px;}
|
|
main h1{font-size:28px; font-weight:600; margin-bottom:8px;}
|
|
main p.lead{color:var(--text-secondary); font-size:16px; margin-bottom:40px; max-width:640px;}
|
|
.cards{display:grid; grid-template-columns:repeat(auto-fit, minmax(280px, 1fr)); gap:16px;}
|
|
.card{
|
|
display:block; background:var(--layer); border:1px solid var(--border);
|
|
border-top:3px solid var(--interactive); padding:24px; transition:background .1s;
|
|
}
|
|
.card:hover{background:var(--layer-hover);}
|
|
.card h2{font-size:18px; font-weight:600; margin-bottom:8px;}
|
|
.card p{color:var(--text-secondary); font-size:14px; margin-bottom:16px;}
|
|
.card .go{color:var(--interactive); font-size:14px; font-weight:600;}
|
|
.card:hover .go{color:var(--interactive-hover);}
|
|
footer{max-width:900px; margin:0 auto; padding:0 24px 40px; color:var(--text-helper); font-size:13px;}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<div class="brand"><strong>Project SDE</strong> <span>| Meeting Toolkit</span></div>
|
|
</header>
|
|
<main>
|
|
<h1>Meeting Toolkit</h1>
|
|
<p class="lead">Two tools for the Micron Pilot. Cluster and name problems in the Field Problem Workshop, then lock scope decisions in the Scope Lock Meeting Suite. Clusters tagged with a candidate solution can be sent straight to the Scope Boundary Board.</p>
|
|
<div class="cards">
|
|
<a class="card" href="/tools/field-problem-workshop.html">
|
|
<h2>Field Problem Workshop</h2>
|
|
<p>Cluster raw problems, write breadcrumbs, and promote root causes into tracked outcomes.</p>
|
|
<span class="go">Open the workshop →</span>
|
|
</a>
|
|
<a class="card" href="/tools/scope-lock-meeting-suite.html">
|
|
<h2>Scope Lock Meeting Suite</h2>
|
|
<p>Sort scope into MVP, Later, Out, or Parking Lot, rank priorities, and log decisions.</p>
|
|
<span class="go">Open the suite →</span>
|
|
</a>
|
|
</div>
|
|
</main>
|
|
<footer>Project SDE · Innovation Team</footer>
|
|
</body>
|
|
</html>`;
|
|
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
res.end(index);
|
|
return;
|
|
}
|
|
const filePath = path.normalize(path.join(ROOT, urlPath));
|
|
if (!filePath.startsWith(ROOT)) {
|
|
res.writeHead(403);
|
|
res.end('Forbidden');
|
|
return;
|
|
}
|
|
fs.readFile(filePath, (err, data) => {
|
|
if (err) {
|
|
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
res.end('Not found: ' + urlPath);
|
|
return;
|
|
}
|
|
const ext = path.extname(filePath);
|
|
res.writeHead(200, { 'Content-Type': MIME[ext] || 'application/octet-stream' });
|
|
res.end(data);
|
|
});
|
|
}
|
|
|
|
const server = http.createServer((req, res) => {
|
|
const ip = req.socket.remoteAddress || 'unknown';
|
|
|
|
if (AUTH_REQUIRED && isLockedOut(ip)) {
|
|
res.writeHead(429, { 'Content-Type': 'text/plain' });
|
|
res.end('Too many failed logins from this address. Try again later.');
|
|
return;
|
|
}
|
|
|
|
const auth = checkBasicAuth(req);
|
|
if (!auth.ok) {
|
|
if (AUTH_REQUIRED) {
|
|
recordLoginFailure(ip);
|
|
logAuthEvent(ip, auth.attempted, 'failure');
|
|
}
|
|
requireAuth(res);
|
|
return;
|
|
}
|
|
const user = auth.user;
|
|
if (AUTH_REQUIRED) {
|
|
clearLoginFailures(ip);
|
|
if (shouldLogSuccess(ip, user)) logAuthEvent(ip, user, 'success');
|
|
}
|
|
|
|
if (req.method === 'POST' && req.url === '/api/claude') {
|
|
if (isRateLimited(ip)) {
|
|
sendJson(res, 429, { error: `Rate limit reached (${RATE_LIMIT_MAX} AI drafts per ${Math.round(RATE_LIMIT_WINDOW_MS / 60000)} min). Wait a bit and try again.` });
|
|
return;
|
|
}
|
|
if (TOKEN_LIMIT_ENABLED) {
|
|
const usedToday = usedTokensToday(loadUsage(), user);
|
|
if (usedToday >= TOKEN_LIMIT_PER_USER) {
|
|
sendJson(res, 429, {
|
|
error: `Daily token quota reached (${usedToday}/${TOKEN_LIMIT_PER_USER} tokens for "${user}"). Resets in ${secondsUntilUTCMidnight()}s, at UTC midnight.`
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
proxyToClaude(req, res, user);
|
|
return;
|
|
}
|
|
if (req.method === 'GET') {
|
|
serveStatic(req, res);
|
|
return;
|
|
}
|
|
res.writeHead(405, { 'Content-Type': 'text/plain' });
|
|
res.end('Method not allowed');
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
console.log(`SDE meeting toolkit running at http://localhost:${PORT}`);
|
|
if (String(process.env.MOCK_AI).toLowerCase() === 'true') {
|
|
console.log('MOCK_AI=true: AI draft button returns a canned response. No API key used or needed.');
|
|
} else {
|
|
console.log(process.env.ANTHROPIC_API_KEY ? 'ANTHROPIC_API_KEY loaded: AI draft button is live.' : 'No ANTHROPIC_API_KEY found: AI draft button will return an error until you add one to .env.');
|
|
}
|
|
console.log(AUTH_REQUIRED ? `Login required: ${Object.keys(USER_MAP).length} named user(s) configured.` : 'No login required: no APP_USERS or APP_USERNAME/APP_PASSWORD are set.');
|
|
if (AUTH_REQUIRED) {
|
|
console.log(`Login lockout: ${LOGIN_ATTEMPT_MAX} failed attempts per ${Math.round(LOGIN_ATTEMPT_WINDOW_MS / 60000)} min locks an address out for ${Math.round(LOGIN_LOCKOUT_MS / 60000)} min.`);
|
|
console.log(`Sign-in audit log: ${AUTH_LOG_FILE}`);
|
|
}
|
|
console.log(TOKEN_LIMIT_ENABLED ? `Per-user daily token quota: ${TOKEN_LIMIT_PER_USER} tokens. Usage file: ${TOKEN_USAGE_FILE}` : 'No per-user token quota: TOKEN_LIMIT_PER_USER is unset or 0 (unlimited, feature on the back burner).');
|
|
});
|