Send a toolbox through an encrypted link
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>
This commit is contained in:
parent
957bfdc86f
commit
bec9ea5b2f
43 changed files with 2349 additions and 59 deletions
74
website/src/features/toolboxes/sharing/sharesApi.js
Normal file
74
website/src/features/toolboxes/sharing/sharesApi.js
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
// Rôle : centralise les appels à l'API d'envoi de toolbox et rend leurs erreurs lisibles.
|
||||
const FALLBACK_ERROR = "Le service d'envoi est indisponible. Réessayez plus tard.";
|
||||
|
||||
function apiError(status, payload, fallback = FALLBACK_ERROR) {
|
||||
return Object.assign(new Error(payload?.error?.message || fallback), { status, code: payload?.error?.code || "" });
|
||||
}
|
||||
|
||||
async function failedResponse(response) {
|
||||
return apiError(response.status, await response.json().catch(() => null));
|
||||
}
|
||||
|
||||
export async function loadSharesConfig() {
|
||||
const response = await fetch("/api/shares/config", { headers: { Accept: "application/json" } });
|
||||
if (!response.ok) throw await failedResponse(response);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// XMLHttpRequest reste nécessaire pour suivre la progression d'un envoi : fetch ne l'expose pas.
|
||||
export function uploadShare(bytes, { singleUse = true, onProgress } = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = new XMLHttpRequest();
|
||||
request.open("POST", "/api/shares");
|
||||
request.setRequestHeader("Content-Type", "application/octet-stream");
|
||||
request.setRequestHeader("X-Sokkog-Share", "1");
|
||||
if (singleUse) request.setRequestHeader("X-Share-Single-Use", "1");
|
||||
request.responseType = "json";
|
||||
request.upload.onprogress = (event) => {
|
||||
if (event.lengthComputable) onProgress?.(event.loaded / event.total);
|
||||
};
|
||||
request.onload = () => {
|
||||
if (request.status === 201 && request.response?.id) resolve(request.response);
|
||||
else reject(apiError(request.status, request.response));
|
||||
};
|
||||
request.onerror = () => reject(apiError(0, null));
|
||||
request.send(bytes);
|
||||
});
|
||||
}
|
||||
|
||||
export async function loadShareMeta(id) {
|
||||
const response = await fetch(`/api/shares/${encodeURIComponent(id)}/meta`, { headers: { Accept: "application/json" } });
|
||||
if (!response.ok) throw await failedResponse(response);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function downloadShare(id, { onProgress } = {}) {
|
||||
const response = await fetch(`/api/shares/${encodeURIComponent(id)}`);
|
||||
if (!response.ok) throw await failedResponse(response);
|
||||
const total = Number(response.headers.get("Content-Length")) || 0;
|
||||
if (!response.body) return new Uint8Array(await response.arrayBuffer());
|
||||
const reader = response.body.getReader();
|
||||
const chunks = [];
|
||||
let received = 0;
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
chunks.push(value);
|
||||
received += value.length;
|
||||
if (total) onProgress?.(received / total);
|
||||
}
|
||||
const bytes = new Uint8Array(received);
|
||||
let offset = 0;
|
||||
chunks.forEach((chunk) => {
|
||||
bytes.set(chunk, offset);
|
||||
offset += chunk.length;
|
||||
});
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// Un lien déjà expiré ou utilisé n'existe plus : la révocation est alors considérée comme faite.
|
||||
export async function revokeShare(id, deleteToken) {
|
||||
const response = await fetch(`/api/shares/${encodeURIComponent(id)}`, { method: "DELETE", headers: { "X-Share-Delete-Token": deleteToken } });
|
||||
if (response.ok || response.status === 404) return true;
|
||||
throw await failedResponse(response);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue