Sharing a toolbox meant exporting a JSON file and passing it around by hand. A toolbox can now be sent from the site while staying local first: the browser compresses and encrypts the v2 export with AES-GCM, and the key lives only in the link fragment, so the server keeps for at most 24 hours a copy it cannot read. - Add the /api/shares service: upload, metadata, download and revoke, with files in SHARES_DIR, a purge every 10 minutes, and single-use links (the default) deleted only once fully downloaded. - Cap the server load: 50 MiB per share, total storage, free disk space, daily volume, concurrent and stalled uploads, reads per link, plus a wide per-connection cap kept in memory without storing IPs. - Limit each browser to 2 shares and 100 MiB per day and 2 active links, and show the existing link of a toolbox already shared. - Add the send dialog (card and game drawer) and the /receive page, which previews or directly imports through the existing import path. - Move shared HTTP helpers to services/http.js for both APIs. - Document the format, delivery, server layout and privacy wording. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
1.7 KiB
JavaScript
46 lines
1.7 KiB
JavaScript
// Rôle : regroupe les petits outils HTTP partagés par les API du serveur Node.
|
|
|
|
export function bool(value, fallback = false) {
|
|
if (value == null || value === "") return fallback;
|
|
return /^(1|true|yes|on)$/i.test(String(value));
|
|
}
|
|
|
|
export function json(res, status, body, headers = {}) {
|
|
const payload = JSON.stringify(body);
|
|
res.writeHead(status, {
|
|
"Content-Type": "application/json; charset=utf-8",
|
|
"Content-Length": Buffer.byteLength(payload),
|
|
"Cache-Control": "no-store",
|
|
"X-Content-Type-Options": "nosniff",
|
|
...headers
|
|
});
|
|
res.end(payload);
|
|
}
|
|
|
|
export function httpError(status, code, message, extra = {}) {
|
|
return Object.assign(new Error(message), { status, code, ...extra });
|
|
}
|
|
|
|
// Caddy remplace X-Forwarded-For par l'adresse réelle du client (voir docs/SERVER.md).
|
|
export function clientAddress(req) {
|
|
return String(req.headers["x-forwarded-for"] || req.socket?.remoteAddress || "unknown").split(",")[0].trim();
|
|
}
|
|
|
|
// Les compteurs ne valent que pour la minute en cours : ils sont tous oubliés à la suivante pour que la mémoire reste bornée.
|
|
export function createRateLimiter(now) {
|
|
const buckets = new Map();
|
|
let currentMinute = null;
|
|
const limiter = (req, scope, limit = 60) => {
|
|
const key = `${clientAddress(req)}:${scope}`;
|
|
const minute = Math.floor(now() / 60_000);
|
|
if (minute !== currentMinute) {
|
|
buckets.clear();
|
|
currentMinute = minute;
|
|
}
|
|
const count = (buckets.get(key) || 0) + 1;
|
|
buckets.set(key, count);
|
|
if (count > limit) throw httpError(429, "RATE_LIMITED", "Trop de tentatives. Réessayez dans quelques instants.");
|
|
};
|
|
limiter.bucketCount = () => buckets.size;
|
|
return limiter;
|
|
}
|