Files
SHiNE-server/shine-UI/js/services/dm-file-service.js
T

782 lines
32 KiB
JavaScript

import {
base64UrlToBytes,
bytesToBase58,
bytesToBase64Url,
decryptBytesAesGcm,
encryptBytesAesGcm,
importPkcs8Ed25519,
randomBytes,
sha256Bytes,
signBase64,
utf8Bytes,
} from './crypto-utils.js';
import { loadSessionMaterial } from './key-vault.js';
import {
TORRENT_V2_BLOCK_BYTES,
TORRENT_V2_PIECE_BYTES,
TorrentV2PieceAccumulator,
buildTorrentV2MetainfoBytes,
computeTorrentV2InfoHash,
computeTorrentV2PieceRoot,
} from './torrent-v2-service.js';
import { resolveUserFileUrl, updatedUserFileUrlAfterError } from './user-file-server-service.js';
export const DM_FILE_CHUNK_BYTES = 64 * 1024 * 1024;
// Старые DM v3 используют блоки по 8 МиБ: размер фиксируется в манифесте.
const PREVIOUS_R2_DM_FILE_CHUNK_BYTES = 8 * 1024 * 1024;
const LEGACY_DM_FILE_CHUNK_BYTES = TORRENT_V2_PIECE_BYTES;
export const DM_FILE_MANIFEST_PAGE_CHUNKS = 256;
export const DM_MAX_ATTACHMENTS_PER_MESSAGE = 10;
const MANIFEST_VERSION = 2;
const MANIFEST_SCHEME = 'SHINE-DM-CHUNKED-AES-256-GCM';
const R2_MANIFEST_VERSION = 3;
const R2_MANIFEST_SCHEME = 'SHINE-DM-R2-AES-256-GCM';
const DM_R2_OBJECT_MAX_BYTES = DM_FILE_CHUNK_BYTES + 16;
const DM_R2_FILE_MAX_BYTES = 500 * 1024 * 1024;
const ROOT_IV_TOKEN = 0xffffffffffffffffn;
const PAGE_IV_BASE = 0x8000000000000000n;
const encoder = new TextEncoder();
const decoder = new TextDecoder();
function normalizeFileName(value = '') {
const cleaned = String(value || 'file')
.replace(/[\\/\u0000-\u001f\u007f]/g, '_')
.trim();
return (cleaned || 'file').slice(0, 180);
}
function wsUrlToHttpBase(wsUrl = '') {
const parsed = new URL(String(wsUrl || ''), window.location.href);
if (parsed.protocol === 'wss:') parsed.protocol = 'https:';
else if (parsed.protocol === 'ws:') parsed.protocol = 'http:';
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
throw new Error('Не удалось определить HTTP-адрес сервера Сияния');
}
parsed.pathname = '/';
parsed.search = '';
parsed.hash = '';
return parsed.origin;
}
function uploadPreimage({ sessionId, fileId, encryptedSize, timeMs }) {
return `DM_R2_PREPARE_V1:${sessionId}:${fileId}:${Number(encryptedSize)}:${Number(timeMs)}`;
}
async function readErrorMessage(response) {
try {
const raw = String(await response.text()).trim();
if (!raw) return '';
try {
const payload = JSON.parse(raw);
return String(payload?.message || payload?.error || raw).trim();
} catch {
return raw;
}
} catch {
return '';
}
}
function objectUrl(serverBase, id) {
const base = String(serverBase || '').replace(/\/$/, '');
// Старые вложения читаем с access-сервера, новые — с отдельного домена R2.
const path = new URL(base).hostname.startsWith('dm-files.') ? '' : '/dm-files';
return `${base}${path}/${encodeURIComponent(String(id || ''))}`;
}
function objectBaseFromUrl(url = '') {
const parsed = new URL(String(url || ''), window.location.href);
return parsed.origin;
}
function makeIv(prefix4, token) {
if (!(prefix4 instanceof Uint8Array) || prefix4.byteLength !== 4) {
throw new Error('Некорректный IV prefix файла');
}
const iv = new Uint8Array(12);
iv.set(prefix4, 0);
new DataView(iv.buffer).setBigUint64(4, BigInt(token), false);
return iv;
}
function chunkIv(prefix4, index) {
return makeIv(prefix4, BigInt(index));
}
function pageIv(prefix4, index) {
return makeIv(prefix4, PAGE_IV_BASE + BigInt(index));
}
function rootIv(prefix4) {
return makeIv(prefix4, ROOT_IV_TOKEN);
}
function makeAad(prefix4, domain, index = 0) {
return utf8Bytes(`SHINE-DM-FILE-V2:${bytesToBase64Url(prefix4)}:${domain}:${index}`);
}
function chunkAad(prefix4, index) {
return makeAad(prefix4, 'chunk', index);
}
function pageAad(prefix4, index) {
return makeAad(prefix4, 'page', index);
}
function rootAad(prefix4) {
return makeAad(prefix4, 'root', 0);
}
function assertSafeIndex(index, label = 'index') {
const n = Number(index);
if (!Number.isSafeInteger(n) || n < 0) throw new Error(`Некорректный ${label}`);
return n;
}
async function createUploadContext({ login, sessionId, wsUrl }) {
const cleanLogin = String(login || '').trim();
const cleanSessionId = String(sessionId || '').trim();
if (!cleanLogin || !cleanSessionId) throw new Error('Нет активной пользовательской сессии');
const sessionMaterial = await loadSessionMaterial(cleanLogin);
if (!sessionMaterial?.sessionPrivPkcs8) {
throw new Error('На устройстве нет сохранённого session key для загрузки файла');
}
if (sessionMaterial.sessionId && String(sessionMaterial.sessionId) !== cleanSessionId) {
throw new Error('Сохранённый session key относится к другой сессии');
}
return {
sessionId: cleanSessionId,
serverBase: wsUrlToHttpBase(wsUrl),
privateKey: await importPkcs8Ed25519(sessionMaterial.sessionPrivPkcs8),
};
}
async function uploadEncryptedObject(bytes, context) {
const payload = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes || 0);
if (payload.byteLength < 16 || payload.byteLength > DM_R2_OBJECT_MAX_BYTES) {
throw new Error('Зашифрованный объект DM превышает лимит R2');
}
const fileId = bytesToBase58(await sha256Bytes(payload));
const timeMs = Date.now();
const signatureB64 = await signBase64(context.privateKey, uploadPreimage({
sessionId: context.sessionId, fileId, encryptedSize: payload.byteLength, timeMs,
}));
const prepare = await fetch(`${context.serverBase}/api/dm-files/prepare`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Shine-Session-Id': context.sessionId,
'X-Shine-Time-Ms': String(timeMs),
'X-Shine-Signature': signatureB64,
},
body: JSON.stringify({ fileId, size: payload.byteLength }),
});
if (!prepare.ok) {
const detail = await readErrorMessage(prepare);
throw new Error(detail || `R2 DM prepare: HTTP ${prepare.status}`);
}
const answer = await prepare.json();
// Сервер prepare — владелец R2, ссылка нужна лишь для HEAD проверки 412.
const url = String(answer.fileUrl || '').trim();
const fileUrl = new URL(url);
if (fileUrl.protocol !== 'https:' || fileUrl.username || fileUrl.password || fileUrl.search
|| fileUrl.hash || !fileUrl.pathname.endsWith(`/${fileId}`)) {
throw new Error('Сервер вернул некорректный адрес R2');
}
if (!answer.exists) {
if (!answer.uploadUrl || !answer.requiredHeaders) throw new Error('Сервер не вернул параметры загрузки R2');
let result;
try {
result = await fetch(answer.uploadUrl, {
method: 'PUT', headers: answer.requiredHeaders, body: payload,
});
} catch (error) {
throw new Error(`Не удалось загрузить зашифрованные данные в R2: ${error?.message || 'network error'}`);
}
if (result.status === 412) {
// Другой клиент успел создать объект. Его реальные байты проверит получатель по SHA-256.
const check = await fetch(url, { method: 'HEAD' });
if (!check.ok || Number(check.headers.get('Content-Length')) !== payload.byteLength) {
throw new Error('Конфликт загрузки зашифрованного объекта R2');
}
} else if (!result.ok) {
const detail = await readErrorMessage(result);
throw new Error(detail || `Ошибка загрузки зашифрованных данных в R2 (HTTP ${result.status})`);
}
}
return { id: fileId, url, encryptedSize: payload.byteLength, alreadyExists: Boolean(answer.exists) };
}
async function fetchVerifiedObject(url, expectedId) {
let response;
try {
response = await fetch(url, { method: 'GET', credentials: 'omit', referrerPolicy: 'no-referrer' });
} catch (error) {
throw new Error(`Не удалось скачать зашифрованные данные: ${error?.message || 'network error'}`);
}
if (!response.ok) {
throw new Error(response.status === 404 ? 'Часть файла больше не найдена на сервере отправителя' : `Ошибка скачивания файла (HTTP ${response.status})`);
}
const bytes = new Uint8Array(await response.arrayBuffer());
const actualId = bytesToBase58(await sha256Bytes(bytes));
if (actualId !== String(expectedId || '')) {
bytes.fill(0);
throw new Error('SHA-256 зашифрованной части не совпал: данные повреждены или подменены');
}
return bytes;
}
function encodeJson(value) {
return encoder.encode(JSON.stringify(value));
}
function decodeJson(bytes) {
return JSON.parse(decoder.decode(bytes));
}
export function formatDmFileSize(bytes = 0) {
const value = Math.max(0, Number(bytes || 0));
if (value < 1024) return `${value} Б`;
if (value < 1024 * 1024) return `${(value / 1024).toFixed(1)} КБ`;
if (value < 1024 * 1024 * 1024) return `${(value / (1024 * 1024)).toFixed(value >= 10 * 1024 * 1024 ? 0 : 1)} МБ`;
return `${(value / (1024 * 1024 * 1024)).toFixed(value >= 10 * 1024 * 1024 * 1024 ? 1 : 2)} ГБ`;
}
/**
* Новые DM v3: AES-GCM по 64 МиБ, SHA-256(ciphertext) для каждого объекта.
* BitTorrent/Merkle не вычисляются. Старые v1/v2 остаются доступны на чтение.
*/
export async function encryptAndUploadDmFile({
file, login, sessionId, wsUrl, kind = 'file', durationMs = 0, onProgress = null,
} = {}) {
if (!file || typeof file.slice !== 'function') throw new Error('Файл не выбран');
const fileSize = Number(file.size);
if (!Number.isSafeInteger(fileSize) || fileSize < 0 || fileSize > DM_R2_FILE_MAX_BYTES) {
throw new Error('Максимальный размер личного файла — 500 МиБ');
}
const uploadContext = await createUploadContext({ login, sessionId, wsUrl });
const fileKey = randomBytes(32);
const ivPrefix = randomBytes(4);
const chunkCount = Math.ceil(fileSize / DM_FILE_CHUNK_BYTES);
const descriptors = [];
let encryptedTotal = 0;
try {
for (let index = 0; index < chunkCount; index += 1) {
const start = index * DM_FILE_CHUNK_BYTES;
const plain = new Uint8Array(await file.slice(start, Math.min(fileSize, start + DM_FILE_CHUNK_BYTES)).arrayBuffer());
const iv = chunkIv(ivPrefix, index);
const aad = chunkAad(ivPrefix, index);
let encrypted = null;
try {
encrypted = await encryptBytesAesGcm(plain, fileKey, iv, aad);
const stored = await uploadEncryptedObject(encrypted, uploadContext);
descriptors.push({ i: index, id: stored.id, ps: plain.byteLength, es: stored.encryptedSize });
encryptedTotal += stored.encryptedSize;
onProgress?.({ phase: 'chunks', chunkIndex: index, chunkCount,
processedBytes: Math.min(fileSize, start + plain.byteLength), totalBytes: fileSize });
} finally {
plain.fill(0);
iv.fill(0);
aad.fill(0);
encrypted?.fill(0);
}
}
const manifestBytes = encodeJson({
v: R2_MANIFEST_VERSION, scheme: R2_MANIFEST_SCHEME,
chunkSize: DM_FILE_CHUNK_BYTES, fileSize, chunkCount, chunks: descriptors,
});
const iv = rootIv(ivPrefix);
const aad = rootAad(ivPrefix);
let encryptedManifest = null;
try {
encryptedManifest = await encryptBytesAesGcm(manifestBytes, fileKey, iv, aad);
const stored = await uploadEncryptedObject(encryptedManifest, uploadContext);
onProgress?.({ phase: 'manifest', processedBytes: fileSize, totalBytes: fileSize, chunkCount });
return {
version: R2_MANIFEST_VERSION,
id: stored.id, url: stored.url,
keyB64Url: bytesToBase64Url(fileKey),
ivPrefixB64Url: bytesToBase64Url(ivPrefix),
name: normalizeFileName(file.name),
mime: String(file.type || 'application/octet-stream').trim().slice(0, 160) || 'application/octet-stream',
size: fileSize, encryptedSize: encryptedTotal + stored.encryptedSize,
chunkSize: DM_FILE_CHUNK_BYTES, chunkCount,
kind: String(kind || 'file') === 'voice' ? 'voice' : 'file',
durationMs: Math.max(0, Math.floor(Number(durationMs || 0))),
uploadedAtMs: Date.now(),
};
} finally {
manifestBytes.fill(0);
iv.fill(0);
aad.fill(0);
encryptedManifest?.fill(0);
}
} finally {
fileKey.fill(0);
ivPrefix.fill(0);
}
}
/** Получение зашифрованного манифеста v3 без BitTorrent-метаданных. */
export const DM_FILE_RETENTION_MS = 14 * 24 * 60 * 60 * 1000;
export function isDmFileExpired(attachment, messageTimestampMs = 0) {
// v1/v2: legacy-хранилище не имеет 14-дневного R2 lifecycle.
if (Number(attachment?.version || 1) < 3) return false;
const timestamp = Number(attachment?.uploadedAtMs || attachment?.messageTimestampMs || messageTimestampMs || 0);
return Number.isFinite(timestamp) && timestamp > 0 && Date.now() - timestamp >= DM_FILE_RETENTION_MS;
}
function assertDmFileNotExpired(attachment) {
if (isDmFileExpired(attachment)) throw new Error('DM_FILE_EXPIRED');
}
function v3ObjectUrl(base, id) {
return `${String(base || '').replace(/\/$/, '')}/${encodeURIComponent(String(id || ''))}`;
}
async function v3RootUrl(attachment) {
// Для исторических v3 URL оставляем как источник; новый v3 содержит только hash/id.
if (attachment?.url) return String(attachment.url);
if (!attachment?.senderLogin) throw new Error('Неизвестен отправитель DM-файла');
return resolveUserFileUrl({ login: attachment.senderLogin, id: attachment.id, kind: 'dm' });
}
async function fetchV3ObjectWithRefresh(url, id, attachment) {
try {
return { bytes: await fetchVerifiedObject(url, id), url };
} catch (error) {
if (!attachment?.senderLogin || String(error?.message || '').includes('SHA-256')) throw error;
let updated;
try {
updated = await updatedUserFileUrlAfterError({
login: attachment.senderLogin, id, kind: 'dm', previousUrl: url,
});
} catch { throw error; }
if (!updated) throw error;
return { bytes: await fetchVerifiedObject(updated, id), url: updated };
}
}
/** Получение зашифрованного манифеста v3 без BitTorrent-метаданных. */
async function loadV3RootManifest(attachment) {
const id = String(attachment?.id || '').trim();
assertDmFileNotExpired(attachment);
const url = await v3RootUrl(attachment);
const key = base64UrlToBytes(String(attachment?.keyB64Url || ''));
const ivPrefix = base64UrlToBytes(String(attachment?.ivPrefixB64Url || ''));
if (!id || !url || key.byteLength !== 32 || ivPrefix.byteLength !== 4) {
key.fill(0); ivPrefix.fill(0);
throw new Error('Недостаточно данных для расшифровки личного файла');
}
let encrypted = null;
let plain = null;
const iv = rootIv(ivPrefix);
const aad = rootAad(ivPrefix);
try {
const found = await fetchV3ObjectWithRefresh(url, id, attachment);
encrypted = found.bytes;
plain = await decryptBytesAesGcm(encrypted, key, iv, aad);
const manifest = decodeJson(plain);
const size = Number(manifest?.fileSize);
const count = Number(manifest?.chunkCount);
const chunkSize = manifest?.chunkSize;
// Размер блока определяется зашифрованным манифестом, а не текущим размером для отправки.
// Это сохраняет чтение DM v3, созданных до перехода с 8 МиБ на 64 МиБ.
const supportedChunkSize = chunkSize === PREVIOUS_R2_DM_FILE_CHUNK_BYTES || chunkSize === DM_FILE_CHUNK_BYTES;
if (manifest?.v !== R2_MANIFEST_VERSION || manifest.scheme !== R2_MANIFEST_SCHEME
|| !supportedChunkSize
|| !Number.isSafeInteger(size) || size < 0 || size > DM_R2_FILE_MAX_BYTES
|| size !== Number(attachment.size)
|| (Number(attachment.chunkSize || 0) > 0 && Number(attachment.chunkSize) !== chunkSize)
|| count !== Math.ceil(size / chunkSize)
|| (Number(attachment.chunkCount || 0) > 0 && Number(attachment.chunkCount) !== count)
|| !Array.isArray(manifest.chunks) || manifest.chunks.length !== count) {
throw new Error('Некорректный манифест личного файла R2');
}
for (let i = 0; i < count; i += 1) {
const item = manifest.chunks[i];
const expectedSize = Math.min(chunkSize, size - i * chunkSize);
if (item?.i !== i || !/^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(String(item.id || ''))
|| item.ps !== expectedSize || item.es !== expectedSize + 16) {
throw new Error('Повреждены ссылки на части файла');
}
}
return { manifest, key, ivPrefix, serverBase: objectBaseFromUrl(found.url) };
} catch (error) {
key.fill(0); ivPrefix.fill(0);
throw error;
} finally {
encrypted?.fill(0);
plain?.fill(0);
iv.fill(0);
aad.fill(0);
}
}
async function decryptV3ToSink(attachment, sink, { onProgress = null } = {}) {
const context = await loadV3RootManifest(attachment);
let processedBytes = 0;
try {
for (const item of context.manifest.chunks) {
const found = await fetchV3ObjectWithRefresh(v3ObjectUrl(context.serverBase, item.id), item.id, attachment);
const encrypted = found.bytes;
context.serverBase = objectBaseFromUrl(found.url);
const iv = chunkIv(context.ivPrefix, item.i);
const aad = chunkAad(context.ivPrefix, item.i);
let plain = null;
try {
if (encrypted.byteLength !== item.es) throw new Error('Неверный размер зашифрованной части');
plain = await decryptBytesAesGcm(encrypted, context.key, iv, aad);
if (plain.byteLength !== item.ps) throw new Error('Неверный размер расшифрованной части');
await sink(plain, item);
processedBytes += plain.byteLength;
onProgress?.({ processedBytes, totalBytes: context.manifest.fileSize,
chunkIndex: item.i, chunkCount: context.manifest.chunkCount });
} finally {
encrypted.fill(0);
iv.fill(0);
aad.fill(0);
plain?.fill(0);
}
}
if (processedBytes !== context.manifest.fileSize) throw new Error('Неверный итоговый размер файла');
return context.manifest;
} finally {
context.key.fill(0);
context.ivPrefix.fill(0);
}
}
async function decryptChunkedToSink(attachment, sink, options) {
if (Number(attachment?.version) >= 3) return decryptV3ToSink(attachment, sink, options);
return decryptV2ToSink(attachment, sink, options);
}
async function loadV2RootManifest(attachment) {
const id = String(attachment?.id || '').trim();
const url = String(attachment?.url || '').trim();
const key = base64UrlToBytes(String(attachment?.keyB64Url || ''));
const ivPrefix = base64UrlToBytes(String(attachment?.ivPrefixB64Url || ''));
if (!id || !url || key.byteLength !== 32 || ivPrefix.byteLength !== 4) {
key.fill(0);
ivPrefix.fill(0);
throw new Error('В сообщении не хватает данных для расшифровки chunked-файла');
}
const encrypted = await fetchVerifiedObject(url, id);
const iv = rootIv(ivPrefix);
const aad = rootAad(ivPrefix);
let plain;
try {
plain = await decryptBytesAesGcm(encrypted, key, iv, aad);
const manifest = decodeJson(plain);
if (Number(manifest?.v) !== MANIFEST_VERSION || manifest?.scheme !== MANIFEST_SCHEME) {
throw new Error('Неизвестная версия манифеста файла');
}
if (Number(manifest.chunkSize) !== LEGACY_DM_FILE_CHUNK_BYTES) throw new Error('Неожиданный размер chunk в манифесте');
if (Number(manifest.fileSize) !== Number(attachment?.size || 0)) throw new Error('Размер файла не совпал с манифестом');
if (Number(manifest.chunkCount) !== Math.ceil(Number(manifest.fileSize || 0) / LEGACY_DM_FILE_CHUNK_BYTES)) {
throw new Error('Некорректное число частей в манифесте');
}
const piecesRoot = manifest?.torrent?.piecesRoot ? base64UrlToBytes(manifest.torrent.piecesRoot) : null;
const recomputedInfo = await computeTorrentV2InfoHash({
name: normalizeFileName(attachment?.name || 'file'),
size: Number(manifest.fileSize || 0),
piecesRoot,
});
if (recomputedInfo.infoHashB64Url !== String(manifest?.torrent?.infoHash || '')) {
piecesRoot?.fill(0);
throw new Error('BitTorrent v2 infohash манифеста не совпал');
}
piecesRoot?.fill(0);
return { manifest, key, ivPrefix, serverBase: objectBaseFromUrl(url) };
} catch (error) {
key.fill(0);
ivPrefix.fill(0);
throw error;
} finally {
encrypted.fill(0);
iv.fill(0);
aad.fill(0);
plain?.fill(0);
}
}
async function loadV2ManifestPage({ pageRef, pageIndex, key, ivPrefix, serverBase }) {
const id = String(pageRef?.id || '').trim();
if (!id) throw new Error('В манифесте отсутствует идентификатор страницы');
const encrypted = await fetchVerifiedObject(objectUrl(serverBase, id), id);
const iv = pageIv(ivPrefix, pageIndex);
const aad = pageAad(ivPrefix, pageIndex);
let plain;
try {
plain = await decryptBytesAesGcm(encrypted, key, iv, aad);
const page = decodeJson(plain);
if (Number(page?.v) !== MANIFEST_VERSION || Number(page?.page) !== pageIndex || !Array.isArray(page?.chunks)) {
throw new Error('Повреждена страница манифеста');
}
if (Number(pageRef?.count || 0) !== page.chunks.length) throw new Error('Размер страницы манифеста не совпал');
return page.chunks;
} finally {
encrypted.fill(0);
iv.fill(0);
aad.fill(0);
plain?.fill(0);
}
}
async function *iterateV2Chunks(context) {
let expectedIndex = 0;
for (let pageIndex = 0; pageIndex < context.manifest.pages.length; pageIndex += 1) {
const descriptors = await loadV2ManifestPage({
pageRef: context.manifest.pages[pageIndex],
pageIndex,
key: context.key,
ivPrefix: context.ivPrefix,
serverBase: context.serverBase,
});
for (const descriptor of descriptors) {
if (assertSafeIndex(descriptor?.i, 'индекс chunk') !== expectedIndex) {
throw new Error('Нарушен порядок частей файла');
}
expectedIndex += 1;
yield descriptor;
}
}
if (expectedIndex !== Number(context.manifest.chunkCount || 0)) {
throw new Error('Манифест содержит неполный список частей');
}
}
async function decryptV2Chunk(context, descriptor) {
const index = assertSafeIndex(descriptor?.i, 'индекс chunk');
const id = String(descriptor?.id || '').trim();
if (!id) throw new Error('У части файла отсутствует hash');
const encrypted = await fetchVerifiedObject(objectUrl(context.serverBase, id), id);
const iv = chunkIv(context.ivPrefix, index);
const aad = chunkAad(context.ivPrefix, index);
let plain;
try {
plain = await decryptBytesAesGcm(encrypted, context.key, iv, aad);
} catch {
throw new Error(`Не удалось расшифровать часть ${index + 1}`);
} finally {
encrypted.fill(0);
iv.fill(0);
aad.fill(0);
}
if (plain.byteLength !== Number(descriptor?.ps || 0)) {
plain.fill(0);
throw new Error(`Размер расшифрованной части ${index + 1} не совпал`);
}
const expectedPieceHash = String(descriptor?.ph || '').trim();
const pieceRoot = await computeTorrentV2PieceRoot(plain, {
padToPieceLength: Number(context.manifest.fileSize || 0) > TORRENT_V2_PIECE_BYTES,
});
if (!pieceRoot || bytesToBase64Url(pieceRoot) !== expectedPieceHash) {
plain.fill(0);
pieceRoot?.fill(0);
throw new Error(`BitTorrent v2 SHA-256 части ${index + 1} не совпал`);
}
return { plain, pieceRoot };
}
async function verifyCompletedTorrentRoot(context, accumulator, singlePieceRoot) {
const expectedRoot = String(context.manifest?.torrent?.piecesRoot || '');
if (!expectedRoot && Number(context.manifest.fileSize || 0) === 0) return;
const actualRoot = Number(context.manifest.chunkCount || 0) <= 1
? singlePieceRoot
: await accumulator.finalize();
if (!actualRoot || bytesToBase64Url(actualRoot) !== expectedRoot) {
actualRoot?.fill(0);
throw new Error('Итоговый BitTorrent v2 pieces root не совпал');
}
actualRoot.fill(0);
}
async function decryptV2ToSink(attachment, sink, { onProgress = null } = {}) {
const context = await loadV2RootManifest(attachment);
const accumulator = new TorrentV2PieceAccumulator();
let singlePieceRoot = null;
let processedBytes = 0;
try {
for await (const descriptor of iterateV2Chunks(context)) {
const { plain, pieceRoot } = await decryptV2Chunk(context, descriptor);
try {
if (Number(context.manifest.chunkCount || 0) <= 1) singlePieceRoot = pieceRoot.slice();
else await accumulator.addPieceRoot(pieceRoot);
await sink(plain, descriptor);
processedBytes += plain.byteLength;
onProgress?.({
processedBytes,
totalBytes: Number(context.manifest.fileSize || 0),
chunkIndex: Number(descriptor.i),
chunkCount: Number(context.manifest.chunkCount || 0),
});
} finally {
plain.fill(0);
pieceRoot.fill(0);
}
}
if (processedBytes !== Number(context.manifest.fileSize || 0)) {
throw new Error('Итоговый размер расшифрованного файла не совпал');
}
await verifyCompletedTorrentRoot(context, accumulator, singlePieceRoot);
return context.manifest;
} finally {
context.key.fill(0);
context.ivPrefix.fill(0);
singlePieceRoot?.fill(0);
}
}
async function downloadV1(attachment = {}) {
const fileId = String(attachment?.id || '').trim();
const fileUrl = String(attachment?.url || '').trim();
const keyB64Url = String(attachment?.keyB64Url || '').trim();
const ivB64Url = String(attachment?.ivB64Url || '').trim();
if (!fileId || !fileUrl || !keyB64Url || !ivB64Url) throw new Error('В сообщении не хватает данных для расшифровки файла');
const encryptedBytes = await fetchVerifiedObject(fileUrl, fileId);
const keyBytes = base64UrlToBytes(keyB64Url);
const ivBytes = base64UrlToBytes(ivB64Url);
let plainBytes;
try {
plainBytes = await decryptBytesAesGcm(encryptedBytes, keyBytes, ivBytes);
} catch {
throw new Error('Не удалось расшифровать файл: ключ или содержимое повреждены');
} finally {
encryptedBytes.fill(0);
keyBytes.fill(0);
ivBytes.fill(0);
}
if (plainBytes.byteLength !== Number(attachment?.size || 0)) {
plainBytes.fill(0);
throw new Error('Размер расшифрованного файла не совпал с сообщением');
}
return plainBytes;
}
function suggestedPickerTypes(attachment) {
const mime = String(attachment?.mime || '').trim();
if (!mime || mime === 'application/octet-stream') return undefined;
const name = normalizeFileName(attachment?.name || 'file');
const dot = name.lastIndexOf('.');
const extension = dot >= 0 ? name.slice(dot) : '';
return [{
description: 'Файл Сияния',
accept: { [mime]: extension ? [extension] : [] },
}];
}
function saveBlob(blob, name) {
const objectUrlValue = URL.createObjectURL(blob);
const anchor = document.createElement('a');
anchor.href = objectUrlValue;
anchor.download = normalizeFileName(name || 'file');
anchor.rel = 'noopener';
anchor.style.display = 'none';
document.body.append(anchor);
anchor.click();
anchor.remove();
window.setTimeout(() => URL.revokeObjectURL(objectUrlValue), 30_000);
}
export async function decryptDmFileToBlob(attachment = {}, options = {}) {
assertDmFileNotExpired(attachment);
if (Number(attachment?.version || 1) < 2) {
const plain = await downloadV1(attachment);
const blob = new Blob([plain], { type: String(attachment?.mime || 'application/octet-stream') });
plain.fill(0);
return blob;
}
const parts = [];
await decryptChunkedToSink(attachment, async (plain) => {
parts.push(plain.slice().buffer);
}, options);
return new Blob(parts, { type: String(attachment?.mime || 'application/octet-stream') || 'application/octet-stream' });
}
export async function downloadAndDecryptDmFile(attachment = {}, { onProgress = null } = {}) {
assertDmFileNotExpired(attachment);
if (Number(attachment?.version || 1) < 2) {
const plain = await downloadV1(attachment);
const blob = new Blob([plain], { type: String(attachment?.mime || 'application/octet-stream') });
plain.fill(0);
saveBlob(blob, attachment?.name);
return { streamed: false, size: blob.size };
}
let writable = null;
if (typeof window.showSaveFilePicker === 'function') {
try {
const handle = await window.showSaveFilePicker({
suggestedName: normalizeFileName(attachment?.name || 'file'),
types: suggestedPickerTypes(attachment),
});
writable = await handle.createWritable();
} catch (error) {
if (error?.name === 'AbortError') return { cancelled: true };
// Some browsers expose the API but reject certain MIME/type descriptors.
try {
const handle = await window.showSaveFilePicker({ suggestedName: normalizeFileName(attachment?.name || 'file') });
writable = await handle.createWritable();
} catch (secondError) {
if (secondError?.name === 'AbortError') return { cancelled: true };
}
}
}
if (writable) {
try {
await decryptChunkedToSink(attachment, async (plain) => {
await writable.write(plain);
}, { onProgress });
await writable.close();
writable = null;
return { streamed: true, size: Number(attachment?.size || 0) };
} catch (error) {
try { await writable?.abort?.(); } catch { /* ignore */ }
throw error;
}
}
// Cross-browser fallback: still chunk-download/decrypt, but the final Blob is held
// in memory because Firefox/Safari do not yet expose a writable download stream.
const blob = await decryptDmFileToBlob(attachment, { onProgress });
saveBlob(blob, attachment?.name);
return { streamed: false, size: blob.size };
}
export async function buildDmFileTorrentV2(attachment = {}) {
if (Number(attachment?.version || 1) !== 2) throw new Error('Torrent-v2 metadata есть только у старых файлов Сияния v2');
const context = await loadV2RootManifest(attachment);
const pieceLayer = [];
try {
for await (const descriptor of iterateV2Chunks(context)) {
if (Number(context.manifest.fileSize || 0) > TORRENT_V2_PIECE_BYTES) {
const hash = base64UrlToBytes(String(descriptor?.ph || ''));
if (hash.byteLength !== 32) throw new Error('Повреждён torrent piece layer');
pieceLayer.push(hash);
}
}
const piecesRoot = context.manifest?.torrent?.piecesRoot
? base64UrlToBytes(context.manifest.torrent.piecesRoot)
: null;
const bytes = buildTorrentV2MetainfoBytes({
name: normalizeFileName(attachment?.name || 'file'),
size: Number(attachment?.size || 0),
piecesRoot,
pieceLayer,
});
piecesRoot?.fill(0);
pieceLayer.forEach((hash) => hash.fill(0));
return new Blob([bytes], { type: 'application/x-bittorrent' });
} finally {
context.key.fill(0);
context.ivPrefix.fill(0);
}
}