release: Multi-Hoster-Upload v2.0.3
This commit is contained in:
1 parent
6cdccf71f2
commit
1c4d096cb9
31 files changed
+2304
-131
No files matched your search
@@ -522,6 +522,21 @@ class ConfigStore {
|
||||
});
|
||||
}
|
||||
|
||||
replaceSettings(config) {
|
||||
return this._enqueueWrite(() => {
|
||||
const current = this.load();
|
||||
const globalSettings = this._clone(config.globalSettings);
|
||||
globalSettings.pendingQueue = current.globalSettings.pendingQueue ?? null;
|
||||
return this._commit({
|
||||
hosters: this._clone(config.hosters),
|
||||
hosterSettings: this._clone(config.hosterSettings),
|
||||
globalSettings,
|
||||
history: this._clone(current.history || []),
|
||||
rotationCursors: {}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
loadHistory() {
|
||||
if (this._historyMigrated) {
|
||||
return this._readHistoryFile() || [];
|
||||
|
||||
@@ -0,0 +1,251 @@
|
||||
const crypto = require('node:crypto');
|
||||
const zlib = require('node:zlib');
|
||||
|
||||
const ONLINE_BACKUP_API_URL = 'https://uploader.24-music.de/backup-api';
|
||||
const KEY_PREFIX = 'MHU2-';
|
||||
const KEY_BODY_LENGTH = 70;
|
||||
const RECORD_ID_LENGTH = 16;
|
||||
const MASTER_KEY_LENGTH = 32;
|
||||
const CHECKSUM_LENGTH = 4;
|
||||
const NONCE_LENGTH = 12;
|
||||
const AUTH_TAG_LENGTH = 16;
|
||||
const BLOB_VERSION = 1;
|
||||
const MAX_BLOB_BYTES = 256 * 1024;
|
||||
const MAX_RESPONSE_BYTES = 512 * 1024;
|
||||
const MAX_PLAINTEXT_BYTES = 512 * 1024;
|
||||
const REQUEST_TIMEOUT_MS = 12_000;
|
||||
const KEY_CONTEXT = Buffer.from('MHU2-ONLINE-KEY-V1', 'utf8');
|
||||
const AAD_CONTEXT = Buffer.from('MHU-ONLINE-BACKUP-V1', 'utf8');
|
||||
|
||||
function checksum(idBytes, masterKey) {
|
||||
return crypto.createHash('sha256').update(KEY_CONTEXT).update(idBytes).update(masterKey).digest().subarray(0, CHECKSUM_LENGTH);
|
||||
}
|
||||
|
||||
function deriveSecret(masterKey, idBytes, purpose) {
|
||||
return Buffer.from(crypto.hkdfSync('sha256', masterKey, idBytes, Buffer.from(`MHU-ONLINE-${purpose}-V1`, 'utf8'), 32));
|
||||
}
|
||||
|
||||
function deriveDeleteSecret(parsed) {
|
||||
return deriveSecret(parsed.masterKey, parsed.idBytes, 'DELETE');
|
||||
}
|
||||
|
||||
function aad(idBytes) {
|
||||
return Buffer.concat([AAD_CONTEXT, idBytes]);
|
||||
}
|
||||
|
||||
function encodeKey(idBytes, masterKey) {
|
||||
const body = Buffer.concat([idBytes, masterKey, checksum(idBytes, masterKey)]).toString('base64url');
|
||||
return `${KEY_PREFIX}${body}`;
|
||||
}
|
||||
|
||||
function validatePayload(value) {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
throw new Error('Online-Sicherung enthält keine gültigen Einstellungen');
|
||||
}
|
||||
if (
|
||||
value.version !== 1
|
||||
|| value.kind !== 'settings-only'
|
||||
|| typeof value.appVersion !== 'string'
|
||||
|| typeof value.exportedAt !== 'string'
|
||||
|| !value.settings
|
||||
|| typeof value.settings !== 'object'
|
||||
|| Array.isArray(value.settings)
|
||||
|| Object.prototype.hasOwnProperty.call(value, 'session')
|
||||
|| Object.prototype.hasOwnProperty.call(value, 'history')
|
||||
) {
|
||||
throw new Error('Online-Sicherung enthält keine gültigen Einstellungen');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function endpoint(baseUrl, relativePath) {
|
||||
const normalized = String(baseUrl || '').trim().replace(/\/+$/, '');
|
||||
const url = new URL(`${normalized}${relativePath}`);
|
||||
if (url.protocol !== 'https:' && !['127.0.0.1', 'localhost', '::1'].includes(url.hostname)) {
|
||||
throw new Error('Online-Sicherungen benötigen eine sichere HTTPS-Verbindung');
|
||||
}
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
async function requestText(url, init, options = {}) {
|
||||
const controller = new AbortController();
|
||||
const timeoutMs = Number.isFinite(options.timeoutMs) ? options.timeoutMs : REQUEST_TIMEOUT_MS;
|
||||
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
||||
try {
|
||||
const response = await (options.fetchImpl || fetch)(url, { ...init, signal: controller.signal });
|
||||
const body = await readLimitedText(response);
|
||||
return { response, body };
|
||||
} catch {
|
||||
if (controller.signal.aborted) throw new Error('Online-Sicherungsdienst antwortet nicht');
|
||||
throw new Error('Online-Sicherungsdienst ist nicht erreichbar');
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
async function readLimitedText(response) {
|
||||
const contentLength = Number(response.headers.get('content-length') || '0');
|
||||
if (Number.isFinite(contentLength) && contentLength > MAX_RESPONSE_BYTES) {
|
||||
throw new Error('Antwort des Online-Sicherungsdienstes ist zu groß');
|
||||
}
|
||||
if (!response.body) return '';
|
||||
const reader = response.body.getReader();
|
||||
const chunks = [];
|
||||
let total = 0;
|
||||
while (true) {
|
||||
const result = await reader.read();
|
||||
if (result.done) break;
|
||||
total += result.value.byteLength;
|
||||
if (total > MAX_RESPONSE_BYTES) {
|
||||
await reader.cancel();
|
||||
throw new Error('Antwort des Online-Sicherungsdienstes ist zu groß');
|
||||
}
|
||||
chunks.push(Buffer.from(result.value));
|
||||
}
|
||||
return Buffer.concat(chunks).toString('utf8');
|
||||
}
|
||||
|
||||
function parseOnlineBackupKey(key) {
|
||||
const normalized = String(key || '').trim();
|
||||
if (!new RegExp(`^${KEY_PREFIX}[A-Za-z0-9_-]{${KEY_BODY_LENGTH}}$`).test(normalized)) {
|
||||
throw new Error('Online-Sicherungsschlüssel ist ungültig');
|
||||
}
|
||||
const decoded = Buffer.from(normalized.slice(KEY_PREFIX.length), 'base64url');
|
||||
if (decoded.length !== RECORD_ID_LENGTH + MASTER_KEY_LENGTH + CHECKSUM_LENGTH) {
|
||||
throw new Error('Online-Sicherungsschlüssel ist ungültig');
|
||||
}
|
||||
if (decoded.toString('base64url') !== normalized.slice(KEY_PREFIX.length)) {
|
||||
throw new Error('Online-Sicherungsschlüssel ist ungültig');
|
||||
}
|
||||
const idBytes = decoded.subarray(0, RECORD_ID_LENGTH);
|
||||
const masterKey = decoded.subarray(RECORD_ID_LENGTH, RECORD_ID_LENGTH + MASTER_KEY_LENGTH);
|
||||
const actualChecksum = decoded.subarray(RECORD_ID_LENGTH + MASTER_KEY_LENGTH);
|
||||
const expectedChecksum = checksum(idBytes, masterKey);
|
||||
if (!crypto.timingSafeEqual(actualChecksum, expectedChecksum)) {
|
||||
throw new Error('Online-Sicherungsschlüssel ist beschädigt');
|
||||
}
|
||||
return {
|
||||
id: idBytes.toString('base64url'),
|
||||
idBytes: Buffer.from(idBytes),
|
||||
masterKey: Buffer.from(masterKey)
|
||||
};
|
||||
}
|
||||
|
||||
function createOnlineBackup(settings, appVersion, exportedAt = new Date().toISOString()) {
|
||||
const idBytes = crypto.randomBytes(RECORD_ID_LENGTH);
|
||||
const masterKey = crypto.randomBytes(MASTER_KEY_LENGTH);
|
||||
const key = encodeKey(idBytes, masterKey);
|
||||
const encryptionKey = deriveSecret(masterKey, idBytes, 'ENCRYPTION');
|
||||
const nonce = crypto.randomBytes(NONCE_LENGTH);
|
||||
const payload = {
|
||||
version: 1,
|
||||
kind: 'settings-only',
|
||||
appVersion: String(appVersion || ''),
|
||||
exportedAt,
|
||||
settings: JSON.parse(JSON.stringify(settings))
|
||||
};
|
||||
const plaintext = Buffer.from(JSON.stringify(payload), 'utf8');
|
||||
if (plaintext.length > MAX_PLAINTEXT_BYTES) {
|
||||
throw new Error('Einstellungen sind für eine Online-Sicherung zu groß');
|
||||
}
|
||||
const compressed = zlib.gzipSync(plaintext, { level: 9 });
|
||||
const cipher = crypto.createCipheriv('aes-256-gcm', encryptionKey, nonce, { authTagLength: AUTH_TAG_LENGTH });
|
||||
cipher.setAAD(aad(idBytes));
|
||||
const ciphertext = Buffer.concat([cipher.update(compressed), cipher.final()]);
|
||||
const blobBytes = Buffer.concat([Buffer.from([BLOB_VERSION]), nonce, cipher.getAuthTag(), ciphertext]);
|
||||
if (blobBytes.length > MAX_BLOB_BYTES) {
|
||||
throw new Error('Einstellungen sind für eine Online-Sicherung zu groß');
|
||||
}
|
||||
const parsed = parseOnlineBackupKey(key);
|
||||
const deleteVerifier = crypto.createHash('sha256').update(deriveDeleteSecret(parsed)).digest('base64url');
|
||||
return {
|
||||
key,
|
||||
record: {
|
||||
id: parsed.id,
|
||||
blob: blobBytes.toString('base64url'),
|
||||
deleteVerifier
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function restoreOnlineBackup(key, blob) {
|
||||
const parsed = parseOnlineBackupKey(key);
|
||||
if (typeof blob !== 'string' || !/^[A-Za-z0-9_-]+$/.test(blob) || blob.length > Math.ceil(MAX_BLOB_BYTES * 4 / 3) + 4) {
|
||||
throw new Error('Online-Sicherung ist beschädigt');
|
||||
}
|
||||
const bytes = Buffer.from(blob, 'base64url');
|
||||
if (bytes.toString('base64url') !== blob || bytes.length < 1 + NONCE_LENGTH + AUTH_TAG_LENGTH || bytes[0] !== BLOB_VERSION) {
|
||||
throw new Error('Online-Sicherung ist beschädigt');
|
||||
}
|
||||
const nonce = bytes.subarray(1, 1 + NONCE_LENGTH);
|
||||
const tag = bytes.subarray(1 + NONCE_LENGTH, 1 + NONCE_LENGTH + AUTH_TAG_LENGTH);
|
||||
const ciphertext = bytes.subarray(1 + NONCE_LENGTH + AUTH_TAG_LENGTH);
|
||||
try {
|
||||
const decipher = crypto.createDecipheriv(
|
||||
'aes-256-gcm',
|
||||
deriveSecret(parsed.masterKey, parsed.idBytes, 'ENCRYPTION'),
|
||||
nonce,
|
||||
{ authTagLength: AUTH_TAG_LENGTH }
|
||||
);
|
||||
decipher.setAAD(aad(parsed.idBytes));
|
||||
decipher.setAuthTag(tag);
|
||||
const compressed = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
||||
const plaintext = zlib.gunzipSync(compressed, { maxOutputLength: MAX_PLAINTEXT_BYTES }).toString('utf8');
|
||||
return validatePayload(JSON.parse(plaintext));
|
||||
} catch (error) {
|
||||
if (error instanceof Error && /keine gültigen Einstellungen/.test(error.message)) throw error;
|
||||
throw new Error('Online-Sicherung konnte nicht entschlüsselt werden oder ist beschädigt');
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadOnlineBackup(record, baseUrl = ONLINE_BACKUP_API_URL, options) {
|
||||
const { response } = await requestText(endpoint(baseUrl, '/v1/backups'), {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
||||
body: JSON.stringify(record)
|
||||
}, options);
|
||||
if (response.status !== 201) throw new Error('Online-Sicherung konnte nicht gespeichert werden');
|
||||
}
|
||||
|
||||
async function downloadOnlineBackup(key, baseUrl = ONLINE_BACKUP_API_URL, options) {
|
||||
const parsed = parseOnlineBackupKey(key);
|
||||
const { response, body } = await requestText(endpoint(baseUrl, '/v1/backups/restore'), {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
||||
body: JSON.stringify({ id: parsed.id })
|
||||
}, options);
|
||||
if (response.status !== 200) {
|
||||
throw new Error(response.status === 404 ? 'Online-Sicherung wurde nicht gefunden' : 'Online-Sicherung konnte nicht geladen werden');
|
||||
}
|
||||
let value;
|
||||
try {
|
||||
value = JSON.parse(body);
|
||||
} catch {
|
||||
throw new Error('Online-Sicherungsdienst hat ungültige Daten geliefert');
|
||||
}
|
||||
if (typeof value?.blob !== 'string') throw new Error('Online-Sicherungsdienst hat ungültige Daten geliefert');
|
||||
return restoreOnlineBackup(key, value.blob);
|
||||
}
|
||||
|
||||
async function deleteOnlineBackup(key, baseUrl = ONLINE_BACKUP_API_URL, options) {
|
||||
const parsed = parseOnlineBackupKey(key);
|
||||
const deleteSecret = deriveDeleteSecret(parsed).toString('base64url');
|
||||
const { response } = await requestText(endpoint(baseUrl, '/v1/backups/delete'), {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
||||
body: JSON.stringify({ id: parsed.id, deleteSecret })
|
||||
}, options);
|
||||
if (response.status !== 204) {
|
||||
throw new Error(response.status === 404 ? 'Online-Sicherung wurde nicht gefunden' : 'Online-Sicherung konnte nicht gelöscht werden');
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ONLINE_BACKUP_API_URL,
|
||||
createOnlineBackup,
|
||||
deleteOnlineBackup,
|
||||
downloadOnlineBackup,
|
||||
parseOnlineBackupKey,
|
||||
restoreOnlineBackup,
|
||||
uploadOnlineBackup
|
||||
};
|
||||
@@ -0,0 +1,22 @@
|
||||
(function (root) {
|
||||
'use strict';
|
||||
|
||||
function createSerializedRunner(task) {
|
||||
if (typeof task !== 'function') throw new TypeError('task must be a function');
|
||||
let pending = Promise.resolve();
|
||||
return {
|
||||
run(...args) {
|
||||
const result = pending.catch(() => {}).then(() => task(...args));
|
||||
pending = result;
|
||||
return result;
|
||||
},
|
||||
flush() {
|
||||
return pending;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const api = { createSerializedRunner };
|
||||
if (typeof module !== 'undefined' && module.exports) module.exports = api;
|
||||
else if (root) root.SerializedRunner = api;
|
||||
})(typeof window !== 'undefined' ? window : this);
|
||||
@@ -0,0 +1,57 @@
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
function clone(value) {
|
||||
return JSON.parse(JSON.stringify(value));
|
||||
}
|
||||
|
||||
function validateSettings(value) {
|
||||
if (
|
||||
!value
|
||||
|| typeof value !== 'object'
|
||||
|| Array.isArray(value)
|
||||
|| !value.hosters
|
||||
|| typeof value.hosters !== 'object'
|
||||
|| Array.isArray(value.hosters)
|
||||
|| !value.hosterSettings
|
||||
|| typeof value.hosterSettings !== 'object'
|
||||
|| Array.isArray(value.hosterSettings)
|
||||
|| !value.globalSettings
|
||||
|| typeof value.globalSettings !== 'object'
|
||||
|| Array.isArray(value.globalSettings)
|
||||
) {
|
||||
throw new Error('Backup hat eine ungültige Struktur');
|
||||
}
|
||||
}
|
||||
|
||||
function createPortableSettingsSnapshot(config) {
|
||||
validateSettings(config);
|
||||
const snapshot = {
|
||||
hosters: clone(config.hosters),
|
||||
hosterSettings: clone(config.hosterSettings),
|
||||
globalSettings: clone(config.globalSettings),
|
||||
history: []
|
||||
};
|
||||
snapshot.globalSettings.pendingQueue = null;
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
function prepareImportedSettings(value, options = {}) {
|
||||
validateSettings(value);
|
||||
const imported = createPortableSettingsSnapshot(value);
|
||||
const pathExists = options.pathExists || fs.existsSync;
|
||||
const pathDirname = options.pathDirname || path.dirname;
|
||||
const globalSettings = imported.globalSettings;
|
||||
if (globalSettings.logFilePath && !pathExists(pathDirname(globalSettings.logFilePath))) {
|
||||
globalSettings.logFilePath = '';
|
||||
}
|
||||
if (globalSettings.folderMonitor && typeof globalSettings.folderMonitor === 'object') {
|
||||
if (globalSettings.folderMonitor.folderPath && !pathExists(globalSettings.folderMonitor.folderPath)) {
|
||||
globalSettings.folderMonitor.folderPath = '';
|
||||
globalSettings.folderMonitor.enabled = false;
|
||||
}
|
||||
}
|
||||
return imported;
|
||||
}
|
||||
|
||||
module.exports = { createPortableSettingsSnapshot, prepareImportedSettings };
|
||||
@@ -0,0 +1,19 @@
|
||||
function createSettingsImportGate(isUploadRunning) {
|
||||
if (typeof isUploadRunning !== 'function') throw new TypeError('isUploadRunning must be a function');
|
||||
let importing = false;
|
||||
return {
|
||||
begin() {
|
||||
if (importing) throw new Error('Einstellungen werden bereits importiert');
|
||||
if (isUploadRunning()) throw new Error('Während laufender Uploads können keine Einstellungen importiert werden');
|
||||
importing = true;
|
||||
},
|
||||
end() {
|
||||
importing = false;
|
||||
},
|
||||
canStartUpload() {
|
||||
return !importing;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { createSettingsImportGate };
|
||||
@@ -56,6 +56,16 @@ class UploadManager extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
replaceAccountPools(accountPools) {
|
||||
this.accountPools = accountPools && typeof accountPools === 'object' ? accountPools : {};
|
||||
this._failedAccounts.clear();
|
||||
this._accountOverrides.clear();
|
||||
this._suspectSizeMemo.clear();
|
||||
this._suspectGoodAccounts.clear();
|
||||
this._doodApiKeyCache.clear();
|
||||
this._baselineCache.clear();
|
||||
}
|
||||
|
||||
switchAccount(hoster, fallbackAccount) {
|
||||
const prev = this._accountOverrides.get(hoster);
|
||||
this._accountOverrides.set(hoster, fallbackAccount);
|
||||
|
||||
Reference in new issue
Block a user