feat: add automatic account cooldown recovery

Replace session-long account failure pauses with classified 15, 30, 60, and 120 minute cooldowns for temporary account problems. Keep credential, OTP, banned, and disabled states manual while ignoring unknown, file, network, hoster, and bare WAF errors. Reset escalation after confirmed uploads, deduplicate parallel failures, publish revisioned pause snapshots, and show a stable localized countdown with automatic reactivation.
This commit is contained in:
Sucukdeluxe
2026-08-22 18:18:02 +02:00
parent e986c552dc
commit b769467d08
13 changed files with 653 additions and 35 deletions
+152 -1
View File
@@ -24,4 +24,155 @@ function createAccountPicker({ hosters, hosterSettings, hasCreds, indices }) {
return pick;
}
module.exports = { createAccountPicker, enabledAccountsFor };
function classifyAccountFailure(error) {
if (!error || error.transientNetwork === true || error.hosterTransient === true || error.fileRejected === true) return 'none';
if (error.otpRequired === true) return 'manual';
const message = String(error.message || error);
const manualPatterns = [
/otp|two[- ]?factor|verification code/i,
/Falscher (User|Username|Passwort)/i,
/Incorrect (Login|Password)/i,
/invalid (credentials|api[- ]?key|token)/i,
/unauthori[sz]ed|not authorized|\b401\b/i,
/(account|user) (banned|suspended|disabled|gesperrt)/i,
/API[- ]?Key (fehlt|prüfen)|missing API[- ]?key/i,
/Login fehlgeschlagen/i
];
if (manualPatterns.some(pattern => pattern.test(message))) return 'manual';
const cooldownPatterns = [
/\b429\b|rate[- ]?limit|too many requests/i,
/quota|not enough (disk )?(space|storage)|insufficient (disk )?space/i,
/disk (space )?full|storage (exhausted|full|voll|limit)|account (full|voll)/i,
/session (expired|abgelaufen)|CSRF[- ]?Token nicht gefunden|not logged in/i,
/Keine Session erhalten|Session konnte nicht verifiziert werden/i,
/sess_id nicht gefunden|session id not found/i
];
if (error.accountError === true || cooldownPatterns.some(pattern => pattern.test(message))) return 'cooldown';
return 'none';
}
function createAccountCooldownController(options = {}) {
const now = typeof options.now === 'function' ? options.now : Date.now;
const setTimer = typeof options.setTimer === 'function' ? options.setTimer : setTimeout;
const clearTimer = typeof options.clearTimer === 'function' ? options.clearTimer : clearTimeout;
const onClearAccount = typeof options.onClearAccount === 'function' ? options.onClearAccount : () => {};
const onChange = typeof options.onChange === 'function' ? options.onChange : () => {};
const active = new Map();
const failures = new Map();
const cooldowns = [15, 30, 60, 120];
let timer = null;
function keyOf(hoster, accountId) {
return `${hoster}:${accountId}`;
}
function records() {
return [...active.values()]
.sort((left, right) => left.key.localeCompare(right.key))
.map(record => ({ ...record }));
}
function publish(cause) {
onChange(records(), cause);
}
function schedule() {
if (timer !== null) {
clearTimer(timer);
timer = null;
}
const deadlines = [...active.values()]
.filter(record => record.mode === 'cooldown' && Number.isFinite(record.pausedUntil))
.map(record => record.pausedUntil);
if (deadlines.length === 0) return;
const delay = Math.max(0, Math.min(...deadlines) - now());
timer = setTimer(() => {
timer = null;
releaseExpired();
}, delay);
}
function markFailure({ hoster, accountId, mode }) {
if (!hoster || !accountId || mode === 'none') return null;
const key = keyOf(hoster, accountId);
const current = active.get(key);
if (current?.mode === 'manual' && mode !== 'manual') return { ...current };
if (current?.mode === mode && (mode === 'manual' || current.pausedUntil > now())) return { ...current };
const count = (failures.get(key) || 0) + 1;
failures.set(key, count);
const minutes = cooldowns[Math.min(count - 1, cooldowns.length - 1)];
const record = {
key,
hoster,
accountId,
mode: mode === 'manual' ? 'manual' : 'cooldown',
failures: count,
pausedUntil: mode === 'manual' ? null : now() + minutes * 60_000
};
active.set(key, record);
publish('failed');
schedule();
return { ...record };
}
function releaseExpired() {
const currentTime = now();
const released = [];
for (const [key, record] of active) {
if (record.mode !== 'cooldown' || record.pausedUntil > currentTime) continue;
active.delete(key);
released.push(key);
onClearAccount(record.hoster, record.accountId);
}
if (released.length > 0) publish('expired');
schedule();
return released;
}
function reset(hoster, accountId, cause = 'reset') {
const key = keyOf(hoster, accountId);
const removed = active.delete(key);
const resetFailures = failures.delete(key);
if (removed || resetFailures) onClearAccount(hoster, accountId);
if (removed) publish(cause);
schedule();
return removed || resetFailures;
}
function markSuccess(hoster, accountId) {
return reset(hoster, accountId, 'success');
}
function clear() {
const current = records();
active.clear();
failures.clear();
for (const record of current) onClearAccount(record.hoster, record.accountId);
if (current.length > 0) publish('clear');
schedule();
return current.length;
}
function dispose() {
if (timer !== null) clearTimer(timer);
timer = null;
}
return Object.freeze({
activeKeys: () => [...active.keys()].sort(),
clear,
dispose,
list: records,
markFailure,
markSuccess,
releaseExpired,
reset
});
}
module.exports = {
classifyAccountFailure,
createAccountCooldownController,
createAccountPicker,
enabledAccountsFor
};
+30 -3
View File
@@ -12,6 +12,7 @@ const Semaphore = require('./semaphore');
const Throttle = require('./throttle');
const { probeFileHead } = require('./file-probe');
const { normalizeFailureDetails } = require('./upload-diagnostics');
const { classifyAccountFailure } = require('./account-rotation');
const DEFAULT_SETTINGS = {
retries: 3,
@@ -117,6 +118,10 @@ class UploadManager extends EventEmitter {
return n;
}
_emitAccountSucceeded(task) {
if (task?.hoster && task?.accountId) this.emit('account-succeeded', { hoster: task.hoster, accountId: task.accountId });
}
// True if the hoster has a usable override stored that differs from the
// account currently in the task and isn't itself already marked failed.
// Used by the retry loop to decide "retry on same account vs break to
@@ -742,6 +747,7 @@ class UploadManager extends EventEmitter {
attempt
});
recordFinalResult('done', { result });
this._emitAccountSucceeded(task);
return;
} catch (err) {
this.activeJobs.delete(uploadId);
@@ -872,6 +878,7 @@ class UploadManager extends EventEmitter {
}
emitFinalStatus('done', { result: alt.result, speedKbs: alt.speedKbs, elapsed: alt.elapsed, attempt: 1 });
recordFinalResult('done', { result: alt.result });
this._emitAccountSucceeded(task);
return;
}
const stoppedInAlternates = this.stopAfterActive && !signal.aborted;
@@ -932,13 +939,19 @@ class UploadManager extends EventEmitter {
});
break;
}
const pauseMode = classifyAccountFailure(lastError);
const alreadyMarked = this._failedAccounts.has(task.hoster + ':' + task.accountId);
if (!alreadyMarked) {
if (pauseMode !== 'none' && !alreadyMarked) {
this._failedAccounts.set(task.hoster + ':' + task.accountId, true);
this._rotLog('mark-failed', {
jobId, hoster: task.hoster, fileName, accountId: task.accountId,
lastError: lastError ? lastError.message : null
});
this.emit('account-paused', {
hoster: task.hoster,
accountId: task.accountId,
mode: pauseMode
});
this.emit('account-failed', { hoster: task.hoster, accountId: task.accountId });
await this._sleep(800, signal);
// Re-check after the await: the user could have cancelled while
@@ -946,10 +959,15 @@ class UploadManager extends EventEmitter {
// this, rotation proceeds another full attempt-loop's worth of
// work before the next signal-check inside _executeUpload notices.
if (signal.aborted || this.stopAfterActive) break;
} else {
} else if (alreadyMarked) {
this._rotLog('already-marked', {
jobId, hoster: task.hoster, fileName, accountId: task.accountId
});
} else {
this._rotLog('skip-account-pause', {
jobId, hoster: task.hoster, fileName, accountId: task.accountId,
lastError: lastError ? lastError.message : null
});
}
const override = this._accountOverrides.get(task.hoster);
if (!override) {
@@ -1045,6 +1063,7 @@ class UploadManager extends EventEmitter {
this.sessionBytes += fileSize;
emitFinalStatus('done', { result, speedKbs: currentSpeedKbs, elapsed: Math.round((Date.now() - jobStart) / 1000), attempt });
recordFinalResult('done', { result });
this._emitAccountSucceeded(task);
return;
} catch (err) {
this.activeJobs.delete(uploadId);
@@ -1212,12 +1231,20 @@ class UploadManager extends EventEmitter {
// override and reroute normal-sized files away from a primary that
// still works for them.
if (err && err.accountError === true) {
this._failedAccounts.set(task.hoster + ':' + account.id, true);
const key = task.hoster + ':' + account.id;
const pauseMode = classifyAccountFailure(err);
if (pauseMode === 'none' || this._failedAccounts.has(key)) continue;
this._failedAccounts.set(key, true);
this._rotLog('mark-failed', {
jobId, hoster: task.hoster, fileName, accountId: account.id,
lastError: err && err.message ? err.message : String(err),
suspectAlternate: true
});
this.emit('account-paused', {
hoster: task.hoster,
accountId: account.id,
mode: pauseMode
});
}
}
}