|
|
|
@@ -20,12 +20,18 @@ import {
|
|
|
|
|
computeTorrentV2PieceRoot,
|
|
|
|
|
} from './torrent-v2-service.js';
|
|
|
|
|
|
|
|
|
|
export const DM_FILE_CHUNK_BYTES = TORRENT_V2_PIECE_BYTES;
|
|
|
|
|
export const 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_PUBLIC_BASE = 'https://dm-files.shineup.me';
|
|
|
|
|
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();
|
|
|
|
@@ -52,7 +58,7 @@ function wsUrlToHttpBase(wsUrl = '') {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uploadPreimage({ sessionId, fileId, encryptedSize, timeMs }) {
|
|
|
|
|
return `DM_FILE_UPLOAD_V1:${sessionId}:${fileId}:${Number(encryptedSize)}:${Number(timeMs)}`;
|
|
|
|
|
return `DM_R2_PREPARE_V1:${sessionId}:${fileId}:${Number(encryptedSize)}:${Number(timeMs)}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readErrorMessage(response) {
|
|
|
|
@@ -71,7 +77,10 @@ async function readErrorMessage(response) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function objectUrl(serverBase, id) {
|
|
|
|
|
return `${String(serverBase || '').replace(/\/$/, '')}/dm-files/${encodeURIComponent(String(id || ''))}`;
|
|
|
|
|
const base = String(serverBase || '').replace(/\/$/, '');
|
|
|
|
|
// Старые вложения читаем с access-сервера, новые — с отдельного домена R2.
|
|
|
|
|
const path = new URL(base).hostname === 'dm-files.shineup.me' ? '' : '/dm-files';
|
|
|
|
|
return `${base}${path}/${encodeURIComponent(String(id || ''))}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function objectBaseFromUrl(url = '') {
|
|
|
|
@@ -143,66 +152,58 @@ async function createUploadContext({ login, sessionId, wsUrl }) {
|
|
|
|
|
|
|
|
|
|
async function uploadEncryptedObject(bytes, context) {
|
|
|
|
|
const payload = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes || 0);
|
|
|
|
|
const fileId = bytesToBase58(await sha256Bytes(payload));
|
|
|
|
|
const url = objectUrl(context.serverBase, fileId);
|
|
|
|
|
|
|
|
|
|
// Быстрая дедупликация: если сервер уже подтверждает content-addressed объект,
|
|
|
|
|
// не отправляем его байты повторно. HEAD на сервере сам перепроверяет SHA-256 файла.
|
|
|
|
|
try {
|
|
|
|
|
const existing = await fetch(url, { method: 'HEAD', cache: 'no-store' });
|
|
|
|
|
if (existing.ok) {
|
|
|
|
|
const storedSize = Number(existing.headers.get('Content-Length') || -1);
|
|
|
|
|
const etag = String(existing.headers.get('ETag') || '').replace(/^"|"$/g, '');
|
|
|
|
|
if (storedSize === payload.byteLength && (!etag || etag === fileId)) {
|
|
|
|
|
return { id: fileId, url, encryptedSize: payload.byteLength, alreadyExists: true };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// Старый сервер или временная ошибка HEAD не должны ломать загрузку: PUT остаётся источником истины.
|
|
|
|
|
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,
|
|
|
|
|
sessionId: context.sessionId, fileId, encryptedSize: payload.byteLength, timeMs,
|
|
|
|
|
}));
|
|
|
|
|
let response;
|
|
|
|
|
try {
|
|
|
|
|
response = await fetch(url, {
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/octet-stream',
|
|
|
|
|
'X-Shine-Session-Id': context.sessionId,
|
|
|
|
|
'X-Shine-Time-Ms': String(timeMs),
|
|
|
|
|
'X-Shine-Content-Length': String(payload.byteLength),
|
|
|
|
|
'X-Shine-Signature': signatureB64,
|
|
|
|
|
},
|
|
|
|
|
body: payload,
|
|
|
|
|
cache: 'no-store',
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new Error(`Не удалось загрузить часть файла на сервер отправителя: ${error?.message || 'network error'}`);
|
|
|
|
|
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}`);
|
|
|
|
|
}
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
const detail = await readErrorMessage(response);
|
|
|
|
|
throw new Error(detail || `Сервер отклонил часть файла (HTTP ${response.status})`);
|
|
|
|
|
const answer = await prepare.json();
|
|
|
|
|
const url = `${DM_R2_PUBLIC_BASE}/${fileId}`;
|
|
|
|
|
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})`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let alreadyExists = false;
|
|
|
|
|
try {
|
|
|
|
|
const payloadJson = await response.clone().json();
|
|
|
|
|
alreadyExists = Boolean(payloadJson?.alreadyExists);
|
|
|
|
|
} catch {
|
|
|
|
|
// Ответ старого сервера может не содержать JSON-флаг; успешного HTTP достаточно.
|
|
|
|
|
}
|
|
|
|
|
return { id: fileId, url, encryptedSize: payload.byteLength, alreadyExists };
|
|
|
|
|
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', cache: 'no-store' });
|
|
|
|
|
response = await fetch(url, { method: 'GET' });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new Error(`Не удалось скачать зашифрованные данные: ${error?.message || 'network error'}`);
|
|
|
|
|
}
|
|
|
|
@@ -226,26 +227,6 @@ function decodeJson(bytes) {
|
|
|
|
|
return JSON.parse(decoder.decode(bytes));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function encryptAndUploadManifestPage({ descriptors, pageIndex, fileKey, ivPrefix, uploadContext }) {
|
|
|
|
|
const plain = encodeJson({ v: MANIFEST_VERSION, page: pageIndex, chunks: descriptors });
|
|
|
|
|
const iv = pageIv(ivPrefix, pageIndex);
|
|
|
|
|
const aad = pageAad(ivPrefix, pageIndex);
|
|
|
|
|
let encrypted;
|
|
|
|
|
try {
|
|
|
|
|
encrypted = await encryptBytesAesGcm(plain, fileKey, iv, aad);
|
|
|
|
|
const stored = await uploadEncryptedObject(encrypted, uploadContext);
|
|
|
|
|
return {
|
|
|
|
|
id: stored.id,
|
|
|
|
|
count: descriptors.length,
|
|
|
|
|
encryptedSize: stored.encryptedSize,
|
|
|
|
|
};
|
|
|
|
|
} finally {
|
|
|
|
|
plain.fill(0);
|
|
|
|
|
iv.fill(0);
|
|
|
|
|
aad.fill(0);
|
|
|
|
|
encrypted?.fill(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatDmFileSize(bytes = 0) {
|
|
|
|
|
const value = Math.max(0, Number(bytes || 0));
|
|
|
|
@@ -256,148 +237,164 @@ export function formatDmFileSize(bytes = 0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* V2 uploader: no whole-file size limit. Only one 1 MiB plaintext piece is held in
|
|
|
|
|
* memory at a time. Every piece is independently AES-256-GCM encrypted and stored
|
|
|
|
|
* as Base58(SHA-256(ciphertext)).
|
|
|
|
|
* Новые DM v3: AES-GCM по 8 МиБ, SHA-256(ciphertext) для каждого объекта.
|
|
|
|
|
* BitTorrent/Merkle не вычисляются. Старые v1/v2 остаются доступны на чтение.
|
|
|
|
|
*/
|
|
|
|
|
export async function encryptAndUploadDmFile({
|
|
|
|
|
file,
|
|
|
|
|
login,
|
|
|
|
|
sessionId,
|
|
|
|
|
wsUrl,
|
|
|
|
|
kind = 'file',
|
|
|
|
|
durationMs = 0,
|
|
|
|
|
onProgress = null,
|
|
|
|
|
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 fileSize = Math.max(0, Number(file.size || 0));
|
|
|
|
|
const chunkCount = Math.ceil(fileSize / DM_FILE_CHUNK_BYTES);
|
|
|
|
|
const pageRefs = [];
|
|
|
|
|
let pageDescriptors = [];
|
|
|
|
|
let pageIndex = 0;
|
|
|
|
|
const pieceAccumulator = new TorrentV2PieceAccumulator();
|
|
|
|
|
let singlePieceRoot = null;
|
|
|
|
|
let completedBytes = 0;
|
|
|
|
|
|
|
|
|
|
const descriptors = [];
|
|
|
|
|
let encryptedTotal = 0;
|
|
|
|
|
try {
|
|
|
|
|
for (let index = 0; index < chunkCount; index += 1) {
|
|
|
|
|
const start = index * DM_FILE_CHUNK_BYTES;
|
|
|
|
|
const end = Math.min(fileSize, start + DM_FILE_CHUNK_BYTES);
|
|
|
|
|
const plain = new Uint8Array(await file.slice(start, end).arrayBuffer());
|
|
|
|
|
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 {
|
|
|
|
|
const pieceRoot = await computeTorrentV2PieceRoot(plain, {
|
|
|
|
|
padToPieceLength: fileSize > TORRENT_V2_PIECE_BYTES,
|
|
|
|
|
});
|
|
|
|
|
if (!pieceRoot) throw new Error('Не удалось вычислить torrent-v2 hash части');
|
|
|
|
|
if (chunkCount <= 1) singlePieceRoot = pieceRoot.slice();
|
|
|
|
|
else await pieceAccumulator.addPieceRoot(pieceRoot);
|
|
|
|
|
|
|
|
|
|
encrypted = await encryptBytesAesGcm(plain, fileKey, iv, aad);
|
|
|
|
|
const stored = await uploadEncryptedObject(encrypted, uploadContext);
|
|
|
|
|
pageDescriptors.push({
|
|
|
|
|
i: index,
|
|
|
|
|
id: stored.id,
|
|
|
|
|
ps: plain.byteLength,
|
|
|
|
|
es: encrypted.byteLength,
|
|
|
|
|
ph: bytesToBase64Url(pieceRoot),
|
|
|
|
|
});
|
|
|
|
|
completedBytes += plain.byteLength;
|
|
|
|
|
onProgress?.({
|
|
|
|
|
phase: 'chunks',
|
|
|
|
|
chunkIndex: index,
|
|
|
|
|
chunkCount,
|
|
|
|
|
processedBytes: completedBytes,
|
|
|
|
|
totalBytes: fileSize,
|
|
|
|
|
});
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pageDescriptors.length >= DM_FILE_MANIFEST_PAGE_CHUNKS || index === chunkCount - 1) {
|
|
|
|
|
const pageRef = await encryptAndUploadManifestPage({
|
|
|
|
|
descriptors: pageDescriptors,
|
|
|
|
|
pageIndex,
|
|
|
|
|
fileKey,
|
|
|
|
|
ivPrefix,
|
|
|
|
|
uploadContext,
|
|
|
|
|
});
|
|
|
|
|
pageRefs.push(pageRef);
|
|
|
|
|
pageDescriptors = [];
|
|
|
|
|
pageIndex += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const piecesRoot = fileSize <= 0
|
|
|
|
|
? null
|
|
|
|
|
: (chunkCount <= 1 ? singlePieceRoot : await pieceAccumulator.finalize());
|
|
|
|
|
const torrentInfo = await computeTorrentV2InfoHash({
|
|
|
|
|
name: normalizeFileName(file.name),
|
|
|
|
|
size: fileSize,
|
|
|
|
|
piecesRoot,
|
|
|
|
|
const manifestBytes = encodeJson({
|
|
|
|
|
v: R2_MANIFEST_VERSION, scheme: R2_MANIFEST_SCHEME,
|
|
|
|
|
chunkSize: DM_FILE_CHUNK_BYTES, fileSize, chunkCount, chunks: descriptors,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const rootManifest = {
|
|
|
|
|
v: MANIFEST_VERSION,
|
|
|
|
|
scheme: MANIFEST_SCHEME,
|
|
|
|
|
chunkSize: DM_FILE_CHUNK_BYTES,
|
|
|
|
|
fileSize,
|
|
|
|
|
chunkCount,
|
|
|
|
|
pages: pageRefs,
|
|
|
|
|
torrent: {
|
|
|
|
|
metaVersion: 2,
|
|
|
|
|
blockLength: TORRENT_V2_BLOCK_BYTES,
|
|
|
|
|
pieceLength: TORRENT_V2_PIECE_BYTES,
|
|
|
|
|
piecesRoot: piecesRoot ? bytesToBase64Url(piecesRoot) : '',
|
|
|
|
|
infoHash: torrentInfo.infoHashB64Url,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const rootPlain = encodeJson(rootManifest);
|
|
|
|
|
const iv = rootIv(ivPrefix);
|
|
|
|
|
const aad = rootAad(ivPrefix);
|
|
|
|
|
let rootEncrypted = null;
|
|
|
|
|
let encryptedManifest = null;
|
|
|
|
|
try {
|
|
|
|
|
rootEncrypted = await encryptBytesAesGcm(rootPlain, fileKey, iv, aad);
|
|
|
|
|
const storedRoot = await uploadEncryptedObject(rootEncrypted, uploadContext);
|
|
|
|
|
encryptedManifest = await encryptBytesAesGcm(manifestBytes, fileKey, iv, aad);
|
|
|
|
|
const stored = await uploadEncryptedObject(encryptedManifest, uploadContext);
|
|
|
|
|
onProgress?.({ phase: 'manifest', processedBytes: fileSize, totalBytes: fileSize, chunkCount });
|
|
|
|
|
return {
|
|
|
|
|
version: 2,
|
|
|
|
|
id: storedRoot.id,
|
|
|
|
|
url: storedRoot.url,
|
|
|
|
|
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: pageRefs.reduce((sum, item) => sum + Number(item.encryptedSize || 0), 0) + storedRoot.encryptedSize,
|
|
|
|
|
chunkSize: DM_FILE_CHUNK_BYTES,
|
|
|
|
|
chunkCount,
|
|
|
|
|
torrentV2InfoHashB64Url: torrentInfo.infoHashB64Url,
|
|
|
|
|
torrentV2PiecesRootB64Url: piecesRoot ? bytesToBase64Url(piecesRoot) : '',
|
|
|
|
|
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))),
|
|
|
|
|
};
|
|
|
|
|
} finally {
|
|
|
|
|
rootPlain.fill(0);
|
|
|
|
|
manifestBytes.fill(0);
|
|
|
|
|
iv.fill(0);
|
|
|
|
|
aad.fill(0);
|
|
|
|
|
rootEncrypted?.fill(0);
|
|
|
|
|
encryptedManifest?.fill(0);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
fileKey.fill(0);
|
|
|
|
|
ivPrefix.fill(0);
|
|
|
|
|
singlePieceRoot?.fill(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Получение зашифрованного манифеста v3 без BitTorrent-метаданных. */
|
|
|
|
|
async function loadV3RootManifest(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('Недостаточно данных для расшифровки личного файла');
|
|
|
|
|
}
|
|
|
|
|
let encrypted = null;
|
|
|
|
|
let plain = null;
|
|
|
|
|
const iv = rootIv(ivPrefix);
|
|
|
|
|
const aad = rootAad(ivPrefix);
|
|
|
|
|
try {
|
|
|
|
|
encrypted = await fetchVerifiedObject(url, id);
|
|
|
|
|
plain = await decryptBytesAesGcm(encrypted, key, iv, aad);
|
|
|
|
|
const manifest = decodeJson(plain);
|
|
|
|
|
const size = Number(manifest?.fileSize);
|
|
|
|
|
const count = Number(manifest?.chunkCount);
|
|
|
|
|
if (manifest?.v !== R2_MANIFEST_VERSION || manifest.scheme !== R2_MANIFEST_SCHEME
|
|
|
|
|
|| manifest.chunkSize !== DM_FILE_CHUNK_BYTES
|
|
|
|
|
|| !Number.isSafeInteger(size) || size < 0 || size > DM_R2_FILE_MAX_BYTES
|
|
|
|
|
|| size !== Number(attachment.size)
|
|
|
|
|
|| count !== Math.ceil(size / DM_FILE_CHUNK_BYTES)
|
|
|
|
|
|| !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(DM_FILE_CHUNK_BYTES, size - i * DM_FILE_CHUNK_BYTES);
|
|
|
|
|
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(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 encrypted = await fetchVerifiedObject(objectUrl(context.serverBase, item.id), item.id);
|
|
|
|
|
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();
|
|
|
|
@@ -418,9 +415,9 @@ async function loadV2RootManifest(attachment) {
|
|
|
|
|
if (Number(manifest?.v) !== MANIFEST_VERSION || manifest?.scheme !== MANIFEST_SCHEME) {
|
|
|
|
|
throw new Error('Неизвестная версия манифеста файла');
|
|
|
|
|
}
|
|
|
|
|
if (Number(manifest.chunkSize) !== DM_FILE_CHUNK_BYTES) throw new Error('Неожиданный размер chunk в манифесте');
|
|
|
|
|
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) / DM_FILE_CHUNK_BYTES)) {
|
|
|
|
|
if (Number(manifest.chunkCount) !== Math.ceil(Number(manifest.fileSize || 0) / LEGACY_DM_FILE_CHUNK_BYTES)) {
|
|
|
|
|
throw new Error('Некорректное число частей в манифесте');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -635,7 +632,7 @@ export async function decryptDmFileToBlob(attachment = {}, options = {}) {
|
|
|
|
|
return blob;
|
|
|
|
|
}
|
|
|
|
|
const parts = [];
|
|
|
|
|
await decryptV2ToSink(attachment, async (plain) => {
|
|
|
|
|
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' });
|
|
|
|
@@ -672,7 +669,7 @@ export async function downloadAndDecryptDmFile(attachment = {}, { onProgress = n
|
|
|
|
|
|
|
|
|
|
if (writable) {
|
|
|
|
|
try {
|
|
|
|
|
await decryptV2ToSink(attachment, async (plain) => {
|
|
|
|
|
await decryptChunkedToSink(attachment, async (plain) => {
|
|
|
|
|
await writable.write(plain);
|
|
|
|
|
}, { onProgress });
|
|
|
|
|
await writable.close();
|
|
|
|
@@ -692,7 +689,7 @@ export async function downloadAndDecryptDmFile(attachment = {}, { onProgress = n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function buildDmFileTorrentV2(attachment = {}) {
|
|
|
|
|
if (Number(attachment?.version || 1) < 2) throw new Error('Torrent-v2 metadata есть только у файлов Сияния v2');
|
|
|
|
|
if (Number(attachment?.version || 1) !== 2) throw new Error('Torrent-v2 metadata есть только у старых файлов Сияния v2');
|
|
|
|
|
const context = await loadV2RootManifest(attachment);
|
|
|
|
|
const pieceLayer = [];
|
|
|
|
|
try {
|
|
|
|
|