Servers and the agent, backend (2.1.0): join, jobs, schedules, port list, stats, alerts
- Agent API /api/agent/v1: join with a one-time token, then poll, stats, report and export upload, all signed with the server's Ed25519 key. - Jobs signed with a separate job key (HUB_JOB_SIGNING_KEY_FILE) when delivered, config sealed to the server; one at a time per server; missed when not picked up in their window. - New install (licence, ports, preflight against the release's env template), Proxy done with public URL checks, scheduled / emergency (2FA, reason) / batch updates with email and banner, uninstall with an encrypted export first (the key is never stored), adopt and linking existing installs, purge after 30 days. - Port list, install defaults, server configuration (encrypted), stats per minute / hour with roll-up, server alerts. contracts 1.3.0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* The agent API (2.1.0, crosscode-platform docs/AGENT-1.0-PLAN.md): servers call these; the Hub
|
||||
* never calls a server. Everything after `join` is signed with the server's Ed25519 key.
|
||||
*/
|
||||
|
||||
const express = require('express');
|
||||
const rateLimit = require('express-rate-limit');
|
||||
const servers = require('../utils/servers');
|
||||
const jobs = require('../utils/jobs');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const wrap = fn => (req, res) => fn(req, res).catch(e => {
|
||||
const status = e.status || 500;
|
||||
if (status >= 500) console.error('[agent api]', e);
|
||||
res.status(status).json({ code: e.code || 'SERVER_ERROR', message: status >= 500 && !e.status ? 'Server error' : e.message });
|
||||
});
|
||||
|
||||
const joinLimiter = rateLimit({ windowMs: 15 * 60 * 1000, max: 20, standardHeaders: true, legacyHeaders: false, message: { code: 'RATE_LIMITED', message: 'Too many join attempts.' } });
|
||||
const agentLimiter = rateLimit({ windowMs: 60 * 1000, max: 120, standardHeaders: true, legacyHeaders: false, message: { code: 'RATE_LIMITED', message: 'Too many requests.' } });
|
||||
|
||||
router.post('/join', joinLimiter, wrap(async (req, res) => {
|
||||
res.status(201).json(await servers.join(req.body));
|
||||
}));
|
||||
|
||||
router.post('/poll', agentLimiter, servers.requireAgent, wrap(async (req, res) => {
|
||||
res.json(await jobs.poll(req.server, req.body));
|
||||
}));
|
||||
|
||||
router.post('/stats', agentLimiter, servers.requireAgent, wrap(async (req, res) => {
|
||||
await servers.recordStats(req.server, req.body);
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
router.post('/report', agentLimiter, servers.requireAgent, wrap(async (req, res) => {
|
||||
await jobs.receiveReport(req.server, req.body);
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
// The encrypted export: raw bytes, signed like the rest (the signature covers the bytes).
|
||||
router.put('/exports/:id', agentLimiter, express.raw({ type: 'application/octet-stream', limit: '2gb' }), servers.requireAgent, wrap(async (req, res) => {
|
||||
if (!/^exp_[A-Za-z0-9_-]{8,64}$/.test(req.params.id)) return res.status(404).json({ code: 'NOT_FOUND', message: 'Unknown export.' });
|
||||
await jobs.receiveExport(req.server, req.params.id, req.body);
|
||||
res.status(204).end();
|
||||
}));
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user