/*
* 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) {
sendJson(res, 502, { error: 'Could not reach the Anthropic API: ' + err.message });
}
});
}
function serveStatic(req, res) {
const urlPath = decodeURIComponent(req.url.split('?')[0]);
if (urlPath === '/') {
const index = `
Project SDE | Meeting Toolkit
Project SDE| Meeting Toolkit
Meeting Toolkit
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.