v0.2.7: 修复Redis连接 + 启动管理后台

- 修复Redis认证 (配置密码)
- 启动Python管理后台 (端口9531, 15个功能开关)
- 统一版本号 0.2.7
- 更新docker-compose.yml (镜像版本/Redis URL/Admin服务)
This commit is contained in:
2026-05-17 02:22:18 +08:00
commit 83cbfaf03f
164 changed files with 25195 additions and 0 deletions

View File

@@ -0,0 +1,140 @@
import { getDb } from '../database/database';
import { localTimestamp, formatLocalDate } from '../utils/time';
import { CloudConfig, getCloudConfigById } from './credential.service';
export async function dailyCheckIn(id: number): Promise<{
success: boolean;
message: string;
signedDays?: number;
}> {
const db = getDb();
const config = db.prepare(
'SELECT * FROM cloud_configs WHERE id = ?'
).get(id) as CloudConfig | undefined;
if (!config || !config.cookie) {
return { success: false, message: '未找到该网盘的有效配置' };
}
try {
const { QuarkDriver } = require('./drivers/quark.driver');
const driver = new QuarkDriver({ cookie: config.cookie });
const result = await driver.checkIn();
if (result.success) {
db.prepare(
`UPDATE cloud_configs SET
checkin_status = 'success',
last_checkin_at = datetime('now','localtime'),
checkin_message = ?,
consecutive_failures = 0
WHERE id = ?`
).run(result.message || '签到成功', id);
} else {
db.prepare(
`UPDATE cloud_configs SET
checkin_status = 'failed',
last_checkin_at = datetime('now','localtime'),
checkin_message = ?,
consecutive_failures = consecutive_failures + 1
WHERE id = ?`
).run(result.message || '签到失败', id);
}
try {
const storage = await driver.getStorageInfo();
db.prepare(
`UPDATE cloud_configs SET storage_used = ?, storage_total = ? WHERE id = ?`
).run(storage.used, storage.total, id);
} catch {
// Storage info fetch is best-effort
}
return result;
} catch (err: any) {
db.prepare(
`UPDATE cloud_configs SET
checkin_status = 'failed',
last_checkin_at = datetime('now','localtime'),
checkin_message = ?,
consecutive_failures = consecutive_failures + 1
WHERE id = ?`
).run(err.message || '签到失败:未知错误', id);
return { success: false, message: `签到失败:${err.message || '未知错误'}` };
}
}
/** Get all quark drives that need checkin today. */
export function getDrivesForCheckin(): CloudConfig[] {
const db = getDb();
return db.prepare(
`SELECT * FROM cloud_configs
WHERE cloud_type = 'quark' AND is_active = 1
AND (checkin_status IS NULL OR checkin_status != 'success'
OR last_checkin_at IS NULL
OR last_checkin_at < date('now','localtime'))`
).all() as CloudConfig[];
}
/** Skip checkin for a drive. */
export function skipCheckin(id: number): boolean {
const db = getDb();
const result = db.prepare(
`UPDATE cloud_configs SET
checkin_status = 'skipped',
last_checkin_at = datetime('now','localtime'),
checkin_message = '已跳过签到'
WHERE id = ?`
).run(id);
return result.changes > 0;
}
/** Get today's checkin summary for quark drives. */
export function getCheckinSummary(): {
total: number;
success: number;
failed: number;
pending: number;
skipped: number;
} {
const db = getDb();
const todayStr = formatLocalDate();
const all = db.prepare(
`SELECT checkin_status, COUNT(*) as count FROM cloud_configs
WHERE cloud_type = 'quark' AND is_active = 1
GROUP BY checkin_status`
).all() as { checkin_status: string; count: number }[];
const neverCheckedInToday = db.prepare(
`SELECT COUNT(*) as count FROM cloud_configs
WHERE cloud_type = 'quark' AND is_active = 1
AND (last_checkin_at IS NULL OR last_checkin_at < ?)`
).get(todayStr) as { count: number };
const summary: Record<string, number> = {
success: 0,
failed: 0,
pending: 0,
skipped: 0,
};
for (const row of all) {
if (row.checkin_status === 'none' || row.checkin_status === null) {
summary.pending += row.count;
} else if (row.checkin_status in summary) {
summary[row.checkin_status] += row.count;
}
}
summary.pending += neverCheckedInToday.count;
const total = Object.values(summary).reduce((a, b) => a + b, 0);
return {
total,
success: summary.success,
failed: summary.failed,
pending: summary.pending,
skipped: summary.skipped,
};
}

View File

@@ -0,0 +1,254 @@
import { getDb } from '../database/database';
import { getSystemConfig, updateSystemConfig } from '../admin/system-config.service';
import { formatLocalDate, formatLocalDateTime } from '../utils/time';
import { QuarkDriver } from './drivers/quark.driver';
import { BaiduDriver } from './drivers/baidu.driver';
// ═══════════════════════════════════════════════════════════════════════════
// CloudCleanupDriver — contract that each cloud driver must fulfill
// to participate in the cleanup cycle.
//
// To add a new cloud type (e.g. Baidu, Aliyun), implement these three
// methods in the driver and register it in getDriverForCleanup() below.
// The controller (this file) handles WHEN and WITH WHAT parameters;
// the driver handles HOW.
// ═══════════════════════════════════════════════════════════════════════════
/** Each cleanup operation returns { trashed: number; errors: string[] } */
interface CleanupOpResult { trashed: number; errors: string[] }
interface CloudCleanupDriver {
/** Trash date folders (YYYY-MM-DD) older than `days`. */
cleanupOldDateFolders(days: number): Promise<CleanupOpResult>;
/**
* If used space exceeds thresholdPercent% of TOTAL capacity,
* delete oldest date folders until totalBytes * deletePercent/100
* of TOTAL capacity is freed.
* @param thresholdPercent — trigger when usage >= this % of total
* @param deletePercent — free this % of total capacity
*/
cleanupBySpaceThreshold(thresholdPercent: number, deletePercent: number): Promise<CleanupOpResult>;
/** Permanently empty the recycle bin. */
emptyTrash(): Promise<boolean>;
}
// ═══════════════════════════════════════════════════════════════════════════
// Driver factory — create the right driver for a given cloud config.
// When adding a new cloud type, add a case here.
// ═══════════════════════════════════════════════════════════════════════════
function getDriverForCleanup(config: { cloud_type: string; cookie: string }): CloudCleanupDriver | null {
switch (config.cloud_type) {
case 'quark':
return new QuarkDriver({ cookie: config.cookie });
case 'baidu':
return new BaiduDriver({ cookie: config.cookie });
// case 'aliyun': return new AliyunDriver({ cookie: config.cookie });
default:
return null;
}
}
// ═══════════════════════════════════════════════════════════════════════════
// Cleanup controller — reads system configs and dispatches to each
// active cloud driver. Every driver receives the same parameters;
// the driver decides whether/how to act.
// ═══════════════════════════════════════════════════════════════════════════
interface CleanupStats {
filesTrashed: number;
logsDeleted: number;
trashEmptied: boolean;
errors: string[];
}
/** Get all active cloud configs (any type). Used by the orchestrator. */
function getActiveCleanupConfigs(): Array<{ id: number; cloud_type: string; cookie: string; nickname?: string }> {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, cookie, nickname FROM cloud_configs
WHERE is_active = 1 AND cookie IS NOT NULL AND cookie != ''`
).all() as Array<{ id: number; cloud_type: string; cookie: string; nickname?: string }>;
}
/**
* Dispatch cleanupOldDateFolders to every active driver.
* Each driver receives the same `days` parameter.
*/
async function cleanupCloudFiles(days: number): Promise<CleanupOpResult> {
const configs = getActiveCleanupConfigs();
const errors: string[] = [];
let totalTrashed = 0;
for (const cfg of configs) {
const driver = getDriverForCleanup(cfg);
if (!driver) {
console.log(`[Cleanup] No driver for cloud_type="${cfg.cloud_type}", skipping config #${cfg.id}`);
continue;
}
try {
const result = await driver.cleanupOldDateFolders(days);
totalTrashed += result.trashed;
errors.push(...result.errors.map(e => `[${cfg.cloud_type}#${cfg.id}] ${e}`));
} catch (err: any) {
errors.push(`[${cfg.cloud_type}#${cfg.id}] cleanupOldDateFolders: ${err.message}`);
}
await new Promise(r => setTimeout(r, 1000));
}
return { trashed: totalTrashed, errors };
}
/**
* Dispatch cleanupBySpaceThreshold to every active driver.
* Each driver receives the same threshold/delete percentages.
*/
async function cleanupAllBySpaceThreshold(
thresholdPercent: number,
deletePercent: number,
): Promise<CleanupOpResult> {
const configs = getActiveCleanupConfigs();
const errors: string[] = [];
let totalTrashed = 0;
for (const cfg of configs) {
const driver = getDriverForCleanup(cfg);
if (!driver) {
console.log(`[Cleanup] No driver for cloud_type="${cfg.cloud_type}", skipping config #${cfg.id}`);
continue;
}
try {
const result = await driver.cleanupBySpaceThreshold(thresholdPercent, deletePercent);
totalTrashed += result.trashed;
errors.push(...result.errors.map(e => `[${cfg.cloud_type}#${cfg.id}] ${e}`));
} catch (err: any) {
errors.push(`[${cfg.cloud_type}#${cfg.id}] cleanupBySpaceThreshold: ${err.message}`);
}
await new Promise(r => setTimeout(r, 1000));
}
return { trashed: totalTrashed, errors };
}
/**
* Dispatch emptyTrash to every active driver.
*/
export async function emptyAllTrash(): Promise<{ emptied: boolean; errors: string[] }> {
const configs = getActiveCleanupConfigs();
const errors: string[] = [];
let emptied = false;
for (const cfg of configs) {
const driver = getDriverForCleanup(cfg);
if (!driver) continue;
try {
const ok = await driver.emptyTrash();
if (ok) {
emptied = true;
console.log(`[Cleanup] ✅ Emptied trash for [${cfg.cloud_type}#${cfg.id}]`);
} else {
errors.push(`[${cfg.cloud_type}#${cfg.id}] empty trash failed`);
}
} catch (err: any) {
errors.push(`[${cfg.cloud_type}#${cfg.id}]: ${err.message}`);
}
}
return { emptied, errors };
}
/**
* Delete save_records older than the specified number of days.
*/
function cleanupLogs(days: number): number {
const db = getDb();
const cutoffStr = formatLocalDateTime(new Date(Date.now() - days * 24 * 60 * 60 * 1000));
const result = db.prepare('DELETE FROM save_records WHERE created_at < ?').run(cutoffStr);
console.log(`[Cleanup] Deleted ${result.changes} save records older than ${days} days (before ${cutoffStr})`);
return result.changes;
}
/**
* Run full cleanup cycle:
* 0. Force-clean by space threshold (if enabled & exceeded) — priority highest
* 1. Delete old save_records
* 2. Trash old date folders by retention days
* 3. Empty recycle bin (permanently free space)
*/
export async function runFullCleanup(): Promise<CleanupStats> {
const fileDays = parseInt(getSystemConfig('cleanup_file_retention_days') || '7', 10);
const logDays = parseInt(getSystemConfig('cleanup_log_retention_days') || '30', 10);
const emptyTrashEnabled = getSystemConfig('cleanup_empty_trash') !== 'false';
const stats: CleanupStats = { filesTrashed: 0, logsDeleted: 0, trashEmptied: false, errors: [] };
// 0. Space threshold (highest priority)
const thresholdEnabled = getSystemConfig('cleanup_space_threshold_enabled');
if (thresholdEnabled === 'true') {
const thresholdPercent = parseInt(getSystemConfig('cleanup_space_threshold_percent') || '90', 10);
const deletePercent = parseInt(getSystemConfig('cleanup_space_threshold_delete_percent') || '10', 10);
if (thresholdPercent > 0 && thresholdPercent < 100) {
try {
const result = await cleanupAllBySpaceThreshold(thresholdPercent, deletePercent);
stats.filesTrashed += result.trashed;
stats.errors.push(...result.errors);
} catch (err: any) {
stats.errors.push(`空间阈值清理失败: ${err.message}`);
}
}
}
// 1. Delete old save_records
try {
stats.logsDeleted = cleanupLogs(logDays);
} catch (err: any) {
stats.errors.push(`日志清理失败: ${err.message}`);
}
// 2. Trash old files from cloud drives
try {
const result = await cleanupCloudFiles(fileDays);
stats.filesTrashed += result.trashed;
stats.errors.push(...result.errors);
} catch (err: any) {
stats.errors.push(`文件清理失败: ${err.message}`);
}
// 3. Empty recycle bin (only if enabled, and only if we trashed something)
if (emptyTrashEnabled && stats.filesTrashed > 0) {
try {
const result = await emptyAllTrash();
stats.trashEmptied = result.emptied;
stats.errors.push(...result.errors);
} catch (err: any) {
stats.errors.push(`清空回收站失败: ${err.message}`);
}
}
// Save last run timestamp and stats
updateSystemConfig('cleanup_last_run', formatLocalDateTime());
updateSystemConfig('cleanup_last_stats',
JSON.stringify({ filesTrashed: stats.filesTrashed, logsDeleted: stats.logsDeleted, trashEmptied: stats.trashEmptied, errors: stats.errors.length })
);
return stats;
}
/**
* Check if a daily cleanup is due and run it.
* Called periodically by the scheduler (setInterval).
*/
export async function checkAndRunScheduledCleanup(): Promise<void> {
const enabled = getSystemConfig('cleanup_enabled');
if (enabled !== 'true') return;
const lastRun = getSystemConfig('cleanup_last_run');
const todayStr = formatLocalDate();
if (lastRun && lastRun.startsWith(todayStr)) return;
console.log(`[Cleanup] Scheduled cleanup starting at ${new Date().toISOString()}...`);
const stats = await runFullCleanup();
console.log(`[Cleanup] Done: trashed ${stats.filesTrashed} folders, deleted ${stats.logsDeleted} logs, emptied trash: ${stats.trashEmptied}, errors: ${stats.errors.length}`);
}

View File

@@ -0,0 +1,58 @@
import { getSystemConfig } from '../admin/system-config.service';
export interface CloudTypeInfo {
type: string;
label: string;
icon: string;
enabled: boolean;
}
/** Official brand icons — Baidu via SimpleIcons, Aliyun via SimpleIcons Alibaba Cloud. */
const ICONS: Record<string, string> = {
baidu: 'data:image/svg+xml,%3Csvg%20fill%3D%22%232932E1%22%20role%3D%22img%22%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Ctitle%3EBaidu%3C%2Ftitle%3E%3Cpath%20d%3D%22M9.154%200C7.71%200%206.54%201.658%206.54%203.707c0%202.051%201.171%203.71%202.615%203.71%201.446%200%202.614-1.659%202.614-3.71C11.768%201.658%2010.6%200%209.154%200zm7.025.594C14.86.58%2013.347%202.589%2013.2%203.927c-.187%201.745.25%203.487%202.179%203.735%201.933.25%203.175-1.806%203.422-3.364.252-1.555-.995-3.364-2.362-3.674a1.218%201.218%200%200%200-.261-.03zM3.582%205.535a2.811%202.811%200%200%200-.156.008c-2.118.19-2.428%203.24-2.428%203.24-.287%201.41.686%204.425%203.297%203.864%202.617-.561%202.262-3.68%202.183-4.362-.125-1.018-1.292-2.773-2.896-2.75zm16.534%201.753c-2.308%200-2.617%202.119-2.617%203.616%200%201.43.121%203.425%202.988%203.362%202.867-.063%202.553-3.238%202.553-3.988%200-.745-.62-2.99-2.924-2.99zm-8.264%202.478c-1.424.014-2.708.925-3.323%201.947-1.118%201.868-2.863%203.05-3.112%203.363-.25.309-3.61%202.116-2.864%205.42.746%203.301%203.365%203.237%203.365%203.237s1.93.19%204.171-.31c2.24-.495%204.17.123%204.17.123s5.233%201.748%206.665-1.616c1.43-3.364-.808-5.109-.808-5.109s-2.99-2.306-4.736-4.798c-1.072-1.665-2.348-2.268-3.528-2.257zm-2.234%203.84l1.542.024v8.197H7.758c-1.47-.291-2.055-1.292-2.13-1.462-.072-.173-.488-.976-.268-2.343.635-2.049%202.447-2.196%202.447-2.196h1.81zm3.964%202.39v3.881c.096.413.612.488.612.488h1.614v-4.343h1.689v5.782h-3.915c-1.517-.39-1.59-1.465-1.59-1.465v-4.317zm-5.458%201.147c-.66.197-.978.708-1.05.928-.076.22-.247.78-.1%201.269.294%201.095%201.248%201.144%201.248%201.144h1.37v-3.34z%22%2F%3E%3C%2Fsvg%3E',
aliyun: 'data:image/svg+xml,%3Csvg%20fill%3D%22%23FF6A00%22%20role%3D%22img%22%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Ctitle%3EAlibaba%20Cloud%3C%2Ftitle%3E%3Cpath%20d%3D%22M3.996%204.517h5.291L8.01%206.324%204.153%207.506a1.668%201.668%200%200%200-1.165%201.601v5.786a1.668%201.668%200%200%200%201.165%201.6l3.857%201.183%201.277%201.807H3.996A3.996%203.996%200%200%201%200%2015.487V8.513a3.996%203.996%200%200%201%203.996-3.996m16.008%200h-5.291l1.277%201.807%203.857%201.182c.715.227%201.17.889%201.165%201.601v5.786a1.668%201.668%200%200%201-1.165%201.6l-3.857%201.183-1.277%201.807h5.291A3.996%203.996%200%200%200%2024%2015.487V8.513a3.996%203.996%200%200%200-3.996-3.996m-4.007%208.345H8.002v-1.804h7.995Z%22%2F%3E%3C%2Fsvg%3E',
quark: 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%231A7BFF%22%2F%3E%3Cpath%20d%3D%22M12%204C8%204%206%207%205%2010l3%202-2%204c3%200%205-1%206-3%201%202%203%203%206%203l-2-4%203-2c-1-3-3-6-7-6z%22%20fill%3D%22%23fff%22%20opacity%3D%22.9%22%2F%3E%3Ccircle%20cx%3D%2212%22%20cy%3D%229%22%20r%3D%222%22%20fill%3D%22%23fff%22%2F%3E%3C%2Fsvg%3E',
'115': 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%232377E0%22%2F%3E%3Ctext%20x%3D%2212%22%20y%3D%2217%22%20font-size%3D%2211%22%20font-weight%3D%22bold%22%20fill%3D%22%23fff%22%20text-anchor%3D%22middle%22%20font-family%3D%22Arial%2Csans-serif%22%3E115%3C%2Ftext%3E%3C%2Fsvg%3E',
tianyi: 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%230066CC%22%2F%3E%3Cpath%20d%3D%22M7%2017c-2.5%200-4.5-1.8-4.5-4.2S4.5%208.6%207%208.6c.3-2.3%202.3-4%204.7-4%202.2%200%204%201.4%204.5%203.3.3-.1.6-.1.9-.1%202.2%200%204%201.6%204%203.6S19.3%2015%2017.1%2015H7v2z%22%20fill%3D%22%23fff%22%20opacity%3D%22.9%22%2F%3E%3Cpath%20d%3D%22M14%2012l-3-3v6l3-3z%22%20fill%3D%22%230066CC%22%2F%3E%3C%2Fsvg%3E',
'123pan': 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%232875FF%22%2F%3E%3Ctext%20x%3D%2212%22%20y%3D%2217%22%20font-size%3D%2210%22%20font-weight%3D%22bold%22%20fill%3D%22%23fff%22%20text-anchor%3D%22middle%22%20font-family%3D%22Arial%2Csans-serif%22%3E123%3C%2Ftext%3E%3C%2Fsvg%3E',
uc: 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%23FF7A00%22%2F%3E%3Ctext%20x%3D%2212%22%20y%3D%2217%22%20font-size%3D%2212%22%20font-weight%3D%22bold%22%20fill%3D%22%23fff%22%20text-anchor%3D%22middle%22%20font-family%3D%22Arial%2Csans-serif%22%3EUC%3C%2Ftext%3E%3C%2Fsvg%3E',
xunlei: 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%230084FF%22%2F%3E%3Cpath%20d%3D%22M12%202L6%2013h3.5L9%2022l7-12h-3.5L12%202z%22%20fill%3D%22%23fff%22%2F%3E%3C%2Fsvg%3E',
pikpak: 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%237B2FF7%22%2F%3E%3Cpath%20d%3D%22M7%207h3v10H7V7zm7%200h3v10h-3V7z%22%20fill%3D%22%23fff%22%20opacity%3D%22.3%22%2F%3E%3Crect%20x%3D%2210%22%20y%3D%225%22%20width%3D%224%22%20height%3D%2214%22%20rx%3D%221%22%20fill%3D%22%23fff%22%2F%3E%3C%2Fsvg%3E',
magnet: 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%236366F1%22%2F%3E%3Cpath%20d%3D%22M7%2016l5-5m-5%200l5%205m5-5l-5-5m5%200l-5%205%22%20stroke%3D%22%23fff%22%20stroke-width%3D%222%22%20stroke-linecap%3D%22round%22%20fill%3D%22none%22%2F%3E%3Ccircle%20cx%3D%2212%22%20cy%3D%2211%22%20r%3D%221%22%20fill%3D%22%23fff%22%2F%3E%3C%2Fsvg%3E',
ed2k: 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%238B4513%22%2F%3E%3Ctext%20x%3D%2212%22%20y%3D%2217%22%20font-size%3D%2211%22%20font-weight%3D%22bold%22%20fill%3D%22%23fff%22%20text-anchor%3D%22middle%22%20font-family%3D%22Arial%2Csans-serif%22%3EeD%3C%2Ftext%3E%3C%2Fsvg%3E',
others: 'data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Crect%20width%3D%2224%22%20height%3D%2224%22%20rx%3D%224%22%20fill%3D%22%239CA3AF%22%2F%3E%3Cpath%20d%3D%22M6%2013c0-2.8%202.2-5%205-5a5%205%200%200%201%204.5%202.7A4%204%200%200%201%2020%2014a4%204%200%200%201-3%203.9h-8A4%204%200%200%201%206%2013z%22%20fill%3D%22none%22%20stroke%3D%22%23fff%22%20stroke-width%3D%221.5%22%20stroke-linejoin%3D%22round%22%2F%3E%3C%2Fsvg%3E',
};
export const ALL_CLOUD_TYPES: { type: string; label: string; icon: string }[] = [
{ type: 'quark', label: '夸克网盘', icon: ICONS.quark },
{ type: 'baidu', label: '百度网盘', icon: ICONS.baidu },
{ type: 'aliyun', label: '阿里云盘', icon: ICONS.aliyun },
{ type: '115', label: '115 网盘', icon: ICONS['115'] },
{ type: 'tianyi', label: '天翼云盘', icon: ICONS.tianyi },
{ type: '123pan', label: '123 云盘', icon: ICONS['123pan'] },
{ type: 'uc', label: 'UC 网盘', icon: ICONS.uc },
{ type: 'xunlei', label: '迅雷网盘', icon: ICONS.xunlei },
{ type: 'pikpak', label: 'PikPak', icon: ICONS.pikpak },
{ type: 'magnet', label: '磁力链接', icon: ICONS.magnet },
{ type: 'ed2k', label: '电驴链接', icon: ICONS.ed2k },
{ type: 'others', label: '其他', icon: ICONS.others },
];
export function isCloudTypeEnabled(type: string): boolean {
const val = getSystemConfig(`cloud_type_${type}_enabled`);
if (val === null) return type !== 'others';
return val === "true" || val === "1";
}
export function getAllCloudTypes(): CloudTypeInfo[] {
return ALL_CLOUD_TYPES.map(ct => ({ ...ct, enabled: isCloudTypeEnabled(ct.type) }));
}
export function getEnabledCloudTypeSet(): Set<string> {
const enabled = new Set<string>();
for (const ct of ALL_CLOUD_TYPES) {
if (isCloudTypeEnabled(ct.type)) enabled.add(ct.type);
}
return enabled;
}

View File

@@ -0,0 +1,317 @@
import { getDb } from '../database/database';
import { localTimestamp, formatLocalDateTime } from '../utils/time';
import { getSystemConfig } from '../admin/system-config.service';
import { QuarkDriver } from './drivers/quark.driver';
import { BaiduDriver } from './drivers/baidu.driver';
import { CloudConfig, getAndValidateCredential, getActiveCloudConfigs } from './credential.service';
import { lookupIpLocation } from './ip-lookup';
/** In-flight save dedup: prevents concurrent saves of the same URL (race condition fix) */
const inFlightSaves = new Map<string, Promise<SaveResult>>();
export interface SaveResult {
success: boolean;
shareUrl?: string;
share_url?: string;
sharePwd?: string;
folderName?: string;
message: string;
file_count?: number;
folder_count?: number;
duration_ms?: number;
}
export interface SaveRecord {
id: number;
source_type: string;
source_title: string | null;
source_url: string;
target_cloud: string;
share_url: string | null;
share_pwd: string | null;
file_size: string | null;
file_count: number;
folder_count: number;
duration_ms: number;
status: string;
error_message: string | null;
folder_name: string | null;
original_folder_name: string | null;
ip_address: string | null;
ip_location: string | null;
created_at: string;
}
/** Core save logic extracted so inFlight dedup can wrap it */
async function doSaveFromShare(shareUrl: string, cloudType: string, sourceTitle?: string, ipAddress?: string): Promise<SaveResult> {
const db = getDb();
const ipLocation = await lookupIpLocation(ipAddress || '');
// ── Short-term dedup: prevent duplicate saves of the same URL within 60 seconds ──
const DEDUP_WINDOW_SEC = 60;
let dedupCutoff = '';
try {
const recentCutoff = db.prepare(
`SELECT datetime('now','localtime', '-${DEDUP_WINDOW_SEC} seconds') as cutoff`
).get() as { cutoff: string };
dedupCutoff = recentCutoff.cutoff;
const recentRecord = db.prepare(
`SELECT share_url, share_pwd, status, error_message, folder_name, original_folder_name FROM save_records
WHERE source_url = ? AND created_at >= ?
ORDER BY created_at DESC LIMIT 1`
).get(shareUrl, dedupCutoff) as {
share_url: string | null; share_pwd: string | null; status: string;
error_message: string | null; folder_name: string | null; original_folder_name: string | null;
} | undefined;
if (recentRecord) {
const alreadySaved = recentRecord.status === 'success' || recentRecord.status === 'reused';
if (alreadySaved && recentRecord.share_url) {
console.log(`[Share] 🛡️ Dedup: ${shareUrl} was saved ${DEDUP_WINDOW_SEC}s ago (status=${recentRecord.status}), returning existing share link`);
db.prepare(
`INSERT INTO save_records (source_type, source_title, source_url, target_cloud, share_url, share_pwd, file_size, file_count, folder_count, duration_ms, status, error_message, folder_name, original_folder_name, ip_address, ip_location, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
).run(
cloudType, sourceTitle || null, shareUrl, cloudType,
recentRecord.share_url, recentRecord.share_pwd || null,
null, 0, 0, 0, 'reused', null,
recentRecord.folder_name || null, recentRecord.original_folder_name || null,
ipAddress || null, ipLocation, localTimestamp(),
);
return {
success: true,
message: `🛡️ 此资源刚在 ${DEDUP_WINDOW_SEC} 秒内转存过,直接返回已有分享链接`,
share_url: recentRecord.share_url, shareUrl: recentRecord.share_url,
sharePwd: recentRecord.share_pwd || '', folderName: '',
file_count: 0, folder_count: 0, duration_ms: 0,
};
}
}
} catch (err: any) {
console.log(`[Share] Dedup check failed: ${err.message}, proceeding with normal save`);
}
// ── Share link reuse: if same source URL was already saved successfully, validate and reuse ──
const reuseEnabled = getSystemConfig('save_reuse_enabled');
if (reuseEnabled !== 'false') {
try {
const existing = db.prepare(
`SELECT share_url, share_pwd, folder_name, original_folder_name FROM save_records
WHERE source_url = ? AND status IN ('success', 'reused') AND share_url IS NOT NULL AND share_url != ''
ORDER BY created_at DESC LIMIT 1`
).get(shareUrl) as { share_url: string; share_pwd: string; folder_name: string | null; original_folder_name: string | null } | undefined;
if (existing?.share_url) {
const { LinkValidator } = await import('../validation/link-validator.service');
const validator = new LinkValidator();
const validation = await validator.validate(existing.share_url, 'quark');
if (validation.status === 'valid') {
const isFirstReuse = dedupCutoff ? !db.prepare(
`SELECT 1 FROM save_records WHERE source_url = ? AND created_at >= ? AND status = 'reused' LIMIT 1`
).get(shareUrl, dedupCutoff) : true;
const reuseStatus = isFirstReuse ? 'success' : 'reused';
const reuseMsg = isFirstReuse
? `♻️ 检测到此资源之前已转存过,直接复用已存在的分享链接`
: `♻️ 短时间内重复请求,复用已有分享链接`;
console.log(`[Share] ♻️ Reusing existing share link for ${shareUrl}: ${existing.share_url} (firstReuse=${isFirstReuse})`);
db.prepare(
`INSERT INTO save_records (source_type, source_title, source_url, target_cloud, share_url, share_pwd, file_size, file_count, folder_count, duration_ms, status, error_message, folder_name, original_folder_name, ip_address, ip_location, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
).run(
cloudType, sourceTitle || null, shareUrl, cloudType,
existing.share_url, existing.share_pwd || null,
null, 0, 0, 0, reuseStatus, null,
existing.folder_name || null, existing.original_folder_name || null,
ipAddress || null, ipLocation, localTimestamp(),
);
return {
success: true, message: reuseMsg,
share_url: existing.share_url, shareUrl: existing.share_url,
sharePwd: existing.share_pwd || '', folderName: '',
file_count: 0, folder_count: 0, duration_ms: 0,
};
}
console.log(`[Share] Existing share link for ${shareUrl} is invalid/expired, will re-save`);
}
} catch (err: any) {
console.log(`[Share] Link reuse check failed: ${err.message}, proceeding with normal save`);
}
}
// ── Unified credential validation ──
const credential = await getAndValidateCredential(cloudType);
if (!credential.valid || !credential.config) {
return { success: false, message: credential.message };
}
const config = credential.config;
const startTime = Date.now();
try {
let driverResult: { success: boolean; message: string; shareUrl?: string; sharePwd?: string; folderName?: string; fileCount?: number; folderCount?: number; originalFolderName?: string };
switch (cloudType) {
case 'quark': {
const driver = new QuarkDriver({ cookie: config.cookie!, nickname: config.nickname });
driverResult = await driver.saveFromShare(shareUrl, sourceTitle);
break;
}
case 'baidu': {
const driver = new BaiduDriver({ cookie: config.cookie!, nickname: config.nickname });
driverResult = await driver.saveFromShare(shareUrl, sourceTitle);
break;
}
case 'aliyun':
return { success: false, message: '阿里云盘保存功能暂未实现' };
default:
return { success: false, message: `暂不支持 ${cloudType} 的保存功能` };
}
const durationMs = Date.now() - startTime;
if (driverResult.success) {
db.prepare(
`UPDATE cloud_configs SET last_used_at = datetime('now','localtime'), total_saves = total_saves + 1, consecutive_failures = 0 WHERE id = ?`
).run(config.id);
} else if ((driverResult as any).cookieExpired) {
// Cookie expired — don't count as failure, user needs to re-login
} else {
db.prepare(
`UPDATE cloud_configs SET consecutive_failures = consecutive_failures + 1 WHERE id = ?`
).run(config.id);
}
db.prepare(
`INSERT INTO save_records (source_type, source_title, source_url, target_cloud, share_url, share_pwd, file_size, file_count, folder_count, duration_ms, status, error_message, folder_name, original_folder_name, ip_address, ip_location, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
).run(
cloudType, sourceTitle || driverResult.folderName || null, shareUrl, cloudType,
driverResult.shareUrl || null, driverResult.sharePwd || null,
null, driverResult.fileCount || 0, driverResult.folderCount || 0,
durationMs, driverResult.success ? 'success' : 'failed',
driverResult.success ? null : driverResult.message,
driverResult.folderName || null, driverResult.originalFolderName || null,
ipAddress || null, ipLocation, localTimestamp(),
);
return {
success: driverResult.success,
message: driverResult.message,
share_url: driverResult.shareUrl || '',
shareUrl: driverResult.shareUrl,
sharePwd: (driverResult as any).sharePwd || '',
folderName: driverResult.folderName || '',
file_count: driverResult.fileCount || 0,
folder_count: driverResult.folderCount || 0,
duration_ms: durationMs,
};
} catch (err: any) {
const durationMs = Date.now() - startTime;
const errorMessage = err.message || 'Failed to save to cloud';
db.prepare(
`UPDATE cloud_configs SET consecutive_failures = consecutive_failures + 1 WHERE id = ?`
).run(config.id);
db.prepare(
`INSERT INTO save_records (source_type, source_url, target_cloud, duration_ms, status, error_message, ip_address, ip_location, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
).run(cloudType, shareUrl, cloudType, durationMs, 'failed', errorMessage, ipAddress || null, ipLocation, localTimestamp());
return { success: false, message: errorMessage };
}
}
export async function saveFromShare(shareUrl: string, cloudType: string, sourceTitle?: string, ipAddress?: string): Promise<SaveResult> {
const key = `${cloudType}:${shareUrl}`;
const inflight = inFlightSaves.get(key);
if (inflight) {
console.log(`[Share] ⏳ In-flight: ${shareUrl} — another save is already running, awaiting result`);
return inflight;
}
const promise = doSaveFromShare(shareUrl, cloudType, sourceTitle, ipAddress);
inFlightSaves.set(key, promise);
try {
return await promise;
} finally {
inFlightSaves.delete(key);
}
}
// ── Save Records ──────────────────────────────────────────────────
export function getSaveRecords(page: number = 1, pageSize: number = 20, startDate?: string, endDate?: string, status?: string, sourceType?: string, keyword?: string): { total: number; records: SaveRecord[]; summary?: { total: number; success: number; failed: number; reused: number } } {
const db = getDb();
const offset = (page - 1) * pageSize;
const conditions: string[] = [];
const params: any[] = [];
const summaryConditions: string[] = [];
const summaryParams: any[] = [];
if (startDate) {
conditions.push('created_at >= ?'); params.push(startDate);
summaryConditions.push('created_at >= ?'); summaryParams.push(startDate);
}
if (endDate) {
conditions.push('created_at < ?'); params.push(endDate);
summaryConditions.push('created_at < ?'); summaryParams.push(endDate);
}
if (status) { conditions.push('status = ?'); params.push(status); }
if (sourceType) {
conditions.push('source_type = ?'); params.push(sourceType);
summaryConditions.push('source_type = ?'); summaryParams.push(sourceType);
}
if (keyword) { conditions.push('source_title LIKE ?'); params.push(`%${keyword}%`); }
const where = conditions.length > 0 ? 'WHERE ' + conditions.join(' AND ') : '';
const total = (db.prepare(`SELECT COUNT(*) as count FROM save_records ${where}`).get(...params) as any).count;
const records = db.prepare(
`SELECT * FROM save_records ${where} ORDER BY created_at DESC LIMIT ? OFFSET ?`
).all(...params, pageSize, offset) as SaveRecord[];
const summaryWhere = summaryConditions.length > 0 ? 'WHERE ' + summaryConditions.join(' AND ') : '';
const summaryRows = db.prepare(
`SELECT status, COUNT(*) as cnt FROM save_records ${summaryWhere} GROUP BY status`
).all(...summaryParams) as { status: string; cnt: number }[];
let sumTotal = 0, sumSuccess = 0, sumFailed = 0, sumReused = 0;
for (const r of summaryRows) {
sumTotal += r.cnt;
if (r.status === 'success') sumSuccess = r.cnt;
else if (r.status === 'failed') sumFailed = r.cnt;
else if (r.status === 'reused') sumReused = r.cnt;
}
const summary = { total: sumTotal, success: sumSuccess, failed: sumFailed, reused: sumReused };
return { total, records, summary };
}
export function cleanupOldSaveRecords(): void {
const db = getDb();
const cutoff = formatLocalDateTime(new Date(Date.now() - 60 * 24 * 60 * 60 * 1000));
const deleted = db.prepare('DELETE FROM save_records WHERE created_at < ?').run(cutoff);
console.log(`[Cleanup] Deleted ${deleted.changes} save records older than 60 days (before ${cutoff})`);
}
// ── Storage Refresh ───────────────────────────────────────────────
export async function refreshAllStorageInfo(): Promise<void> {
const configs = getActiveCloudConfigs().filter(c => c.cloud_type === 'quark' && c.cookie);
if (configs.length === 0) return;
for (const cfg of configs) {
try {
const { QuarkDriver } = require('./drivers/quark.driver');
const driver = new QuarkDriver({ cookie: cfg.cookie, nickname: cfg.nickname });
const storage = await driver.getStorageInfo();
if (storage.totalBytes > 0 || storage.usedBytes > 0) {
const db = getDb();
db.prepare(
`UPDATE cloud_configs SET storage_used = ?, storage_total = ? WHERE id = ?`
).run(storage.used, storage.total, cfg.id);
}
} catch (err: any) {
console.error(`[Storage] Failed to refresh quark#${cfg.id}:`, err.message);
}
}
}

View File

@@ -0,0 +1,373 @@
import { getDb } from '../database/database';
import { localTimestamp, formatLocalDate, formatLocalDateTime } from '../utils/time';
export interface CloudConfig {
id: number;
cloud_type: string;
cookie?: string;
nickname?: string;
is_active: number;
storage_used?: string;
storage_total?: string;
checkin_status: string; // 'none'|'success'|'failed'|'pending'|'skipped'
last_checkin_at?: string;
checkin_message?: string;
consecutive_failures: number;
last_used_at?: string;
total_saves: number;
created_at: string;
updated_at: string;
verification_status?: string;
}
// ── Cookie UID Extraction ────────────────────────────────────────
function extractCookieUid(cookie: string): string {
if (!cookie) return '';
let m = cookie.match(/__uid=([a-zA-Z0-9+/=_-]+)/);
if (m) return m[1];
m = cookie.match(/b-user-id=([a-zA-Z0-9-]+)/);
if (m) return m[1];
return '';
}
// ── Config CRUD ──────────────────────────────────────────────────
export function getCloudConfigs(): CloudConfig[] {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at, verification_status
FROM cloud_configs ORDER BY id ASC`
).all() as CloudConfig[];
}
export function getAvailableClouds(): CloudConfig[] {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at
FROM cloud_configs WHERE is_active = 1 ORDER BY id ASC`
).all() as CloudConfig[];
}
/** Returns the first active config matching the given cloud type. */
export function getCloudConfigByType(cloudType: string): CloudConfig | undefined {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at, verification_status
FROM cloud_configs WHERE cloud_type = ? AND is_active = 1
ORDER BY id ASC LIMIT 1`
).get(cloudType) as CloudConfig | undefined;
}
export function getCloudConfigById(id: number): CloudConfig | undefined {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at, verification_status
FROM cloud_configs WHERE id = ?`
).get(id) as CloudConfig | undefined;
}
/** Returns all active cloud configs (used by save flow for cloud type switching). */
export function getActiveCloudConfigs(): CloudConfig[] {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at
FROM cloud_configs WHERE is_active = 1
ORDER BY cloud_type ASC, id ASC`
).all() as CloudConfig[];
}
export function saveCloudConfig(data: {
id?: number;
cloud_type: string;
cookie?: string;
nickname?: string;
cookie_uid?: string;
promotion_account?: string;
is_active?: number;
storage_used?: string;
storage_total?: string;
}): CloudConfig {
const db = getDb();
const cookieUidForUpdate = data.cookie ? extractCookieUid(data.cookie) : null;
if (data.id) {
db.prepare(
`UPDATE cloud_configs SET
cloud_type = COALESCE(?, cloud_type),
cookie = COALESCE(?, cookie),
nickname = COALESCE(?, nickname),
cookie_uid = COALESCE(?, cookie_uid),
promotion_account = COALESCE(?, promotion_account),
is_active = COALESCE(?, is_active),
storage_used = COALESCE(?, storage_used),
storage_total = COALESCE(?, storage_total),
consecutive_failures = 0,
updated_at = ?
WHERE id = ?`
).run(data.cloud_type, data.cookie || null, data.nickname || null, cookieUidForUpdate || null, data.promotion_account || null, data.is_active ?? 1, data.storage_used || null, data.storage_total || null, localTimestamp(), data.id);
} else {
const existing = db.prepare(
'SELECT id, nickname FROM cloud_configs WHERE cloud_type = ? AND is_active = 1 LIMIT 1'
).get(data.cloud_type) as any;
if (existing) {
db.prepare(
`UPDATE cloud_configs SET
cookie = COALESCE(?, cookie),
nickname = COALESCE(?, nickname),
cookie_uid = COALESCE(?, cookie_uid),
promotion_account = COALESCE(?, promotion_account),
is_active = COALESCE(?, is_active),
storage_used = COALESCE(?, storage_used),
storage_total = COALESCE(?, storage_total),
consecutive_failures = 0,
updated_at = ?
WHERE id = ?`
).run(data.cookie || null, data.nickname || null, cookieUidForUpdate || null, data.promotion_account || null, data.is_active ?? 1, data.storage_used || null, data.storage_total || null, localTimestamp(), existing.id);
} else {
db.prepare(
'INSERT INTO cloud_configs (cloud_type, cookie, nickname, cookie_uid, promotion_account, is_active, storage_used, storage_total, consecutive_failures) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0)'
).run(data.cloud_type, data.cookie || null, data.nickname || null, cookieUidForUpdate || null, data.promotion_account || null, data.is_active ?? 1, data.storage_used || null, data.storage_total || null);
}
}
const savedId = data.id || (db.prepare('SELECT last_insert_rowid() as id').get() as any).id;
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at
FROM cloud_configs WHERE id = ?`
).get(savedId) as CloudConfig;
}
export function deleteCloudConfig(id: number): boolean {
const db = getDb();
const result = db.prepare('DELETE FROM cloud_configs WHERE id = ?').run(id);
return result.changes > 0;
}
// ── Cookie Validation ────────────────────────────────────────────
async function fetchQuarkNickname(cookie: string): Promise<string | null> {
const MAX_RETRIES = 2;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
try {
const response = await fetch('https://pan.quark.cn/account/info', {
headers: {
'Cookie': cookie,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Referer': 'https://pan.quark.cn/',
},
signal: AbortSignal.timeout(15000),
});
if (!response.ok) return null;
const data = await response.json() as any;
if (data?.data?.nickname) return data.data.nickname;
} catch {
if (attempt < MAX_RETRIES) {
await new Promise(r => setTimeout(r, 1500));
continue;
}
}
}
return null;
}
export async function testCloudConnection(id: number): Promise<{
success: boolean;
message: string;
nickname?: string;
storage_used?: string;
storage_total?: string;
}> {
const config = getCloudConfigById(id);
if (!config) {
return { success: false, message: 'Cloud config not found' };
}
if (!config.cookie) {
return { success: false, message: 'Cookie not configured' };
}
try {
let valid = false;
let nickname = '';
let storageUsed = config.storage_used || '';
let storageTotal = config.storage_total || '';
if (config.cloud_type === 'baidu') {
const { BaiduDriver } = require('./drivers/baidu.driver');
const driver = new BaiduDriver({ cookie: config.cookie, nickname: config.nickname });
valid = await driver.validate();
if (valid) {
const info = await driver.getUserInfo();
if (info) {
nickname = config.nickname || info.nickname || '百度网盘';
const fmt = (b: number) => b >= 1024**3 ? (b/1024**3).toFixed(2)+' GB' : (b/1024**2).toFixed(2)+' MB';
storageUsed = fmt(info.usedBytes);
storageTotal = fmt(info.totalBytes);
}
}
} else {
const { QuarkDriver } = require('./drivers/quark.driver');
const driver = new QuarkDriver({ cookie: config.cookie, nickname: config.nickname });
valid = await driver.validate();
if (valid) {
nickname = config.nickname || (await fetchQuarkNickname(config.cookie)) || '夸克网盘';
const storage = await driver.getStorageInfoQuick();
storageTotal = (storage.total !== '-' && storage.total !== '0 B') ? storage.total : (config.storage_total || '');
}
}
const db = getDb();
if (!valid) {
db.prepare(
`UPDATE cloud_configs SET verification_status = 'invalid', updated_at = ? WHERE id = ?`
).run(localTimestamp(), id);
return { success: false, message: '连接失败Cookie 无效或已过期,或网络暂时异常' };
}
db.prepare(
`UPDATE cloud_configs SET nickname = ?, storage_total = ?, storage_used = ?, is_active = 1, verification_status = 'valid', updated_at = ? WHERE id = ?`
).run(nickname, storageTotal, storageUsed, localTimestamp(), id);
return {
success: true,
message: '连接成功',
nickname,
storage_used: storageUsed,
storage_total: storageTotal,
};
} catch (err: any) {
try {
const db = getDb();
db.prepare(
`UPDATE cloud_configs SET verification_status = 'invalid', updated_at = ? WHERE id = ?`
).run(localTimestamp(), id);
} catch {}
return { success: false, message: `连接失败:${err.message || '未知错误'}` };
}
}
export async function testCloudConnectionWithCookie(cloudType: string, cookie: string): Promise<{
success: boolean;
message: string;
nickname?: string;
storage_used?: string;
storage_total?: string;
}> {
try {
const { QuarkDriver } = require('./drivers/quark.driver');
const driver = new QuarkDriver({ cookie, nickname: '' });
const valid = await driver.validate();
if (!valid) {
return { success: false, message: '连接失败Cookie 无效或已过期' };
}
const nickname = (await fetchQuarkNickname(cookie)) || cloudType;
const storage = await driver.getStorageInfo();
return {
success: true,
message: '连接成功',
nickname,
storage_used: storage.used,
storage_total: storage.total,
};
} catch (err: any) {
return { success: false, message: `连接失败:${err.message || '未知错误'}` };
}
}
// ── Unified Credential Validation ─────────────────────────────────
export interface CredentialValidationResult {
valid: boolean;
config?: CloudConfig;
errorCode?: string;
message: string;
}
/**
* Get and validate a credential for the given cloud type.
*
* This is the unified entry point for all save/transfer operations.
* It handles:
* 1. Finding an active config with < 5 consecutive failures (round-robin)
* 2. Validating cookie freshness via driver.validate()
* 3. Returning structured result with error codes
*
* Reference: search-ucmao get_and_validate_credential() pattern.
*/
export async function getAndValidateCredential(cloudType: string): Promise<CredentialValidationResult> {
const db = getDb();
const config = db.prepare(
`SELECT * FROM cloud_configs
WHERE cloud_type = ? AND is_active = 1
AND consecutive_failures < 5
ORDER BY last_used_at ASC NULLS FIRST
LIMIT 1`
).get(cloudType) as CloudConfig | undefined;
if (!config) {
return {
valid: false,
errorCode: 'NO_AVAILABLE_DRIVE',
message: `Cloud type "${cloudType}" is not configured or no available drives`,
};
}
if (!config.cookie) {
return {
valid: false,
errorCode: 'COOKIE_MISSING',
message: `Cookie not configured for ${cloudType} drive #${config.id}`,
};
}
try {
let cookieValid = false;
if (cloudType === 'baidu') {
const { BaiduDriver } = require('./drivers/baidu.driver');
const driver = new BaiduDriver({ cookie: config.cookie, nickname: config.nickname });
cookieValid = await driver.validate();
} else {
const { QuarkDriver } = require('./drivers/quark.driver');
const driver = new QuarkDriver({ cookie: config.cookie, nickname: config.nickname });
cookieValid = await driver.validate();
}
if (!cookieValid) {
db.prepare(
`UPDATE cloud_configs SET verification_status = 'invalid', updated_at = ? WHERE id = ?`
).run(localTimestamp(), config.id);
return {
valid: false,
errorCode: 'COOKIE_EXPIRED',
message: `Cookie expired or invalid for ${cloudType} drive #${config.id}`,
};
}
return {
valid: true,
config,
message: 'ok',
};
} catch (err: any) {
return {
valid: false,
errorCode: 'VALIDATION_ERROR',
message: `Credential validation failed: ${err.message}`,
};
}
}

View File

@@ -0,0 +1,354 @@
import { getDb } from '../database/database';
import { localTimestamp, formatLocalDate, formatLocalDateTime } from '../utils/time';
export interface CloudConfig {
id: number;
cloud_type: string;
cookie?: string;
nickname?: string;
is_active: number;
storage_used?: string;
storage_total?: string;
checkin_status: string; // 'none'|'success'|'failed'|'pending'|'skipped'
last_checkin_at?: string;
checkin_message?: string;
consecutive_failures: number;
last_used_at?: string;
total_saves: number;
created_at: string;
updated_at: string;
verification_status?: string;
}
// ── Config CRUD ──────────────────────────────────────────────────
export function getCloudConfigs(): CloudConfig[] {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at, verification_status
FROM cloud_configs ORDER BY id ASC`
).all() as CloudConfig[];
}
export function getAvailableClouds(): CloudConfig[] {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at
FROM cloud_configs WHERE is_active = 1 ORDER BY id ASC`
).all() as CloudConfig[];
}
/** Returns the first active config matching the given cloud type. */
export function getCloudConfigByType(cloudType: string): CloudConfig | undefined {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at, verification_status
FROM cloud_configs WHERE cloud_type = ? AND is_active = 1
ORDER BY id ASC LIMIT 1`
).get(cloudType) as CloudConfig | undefined;
}
export function getCloudConfigById(id: number): CloudConfig | undefined {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at, verification_status
FROM cloud_configs WHERE id = ?`
).get(id) as CloudConfig | undefined;
}
/** Returns all active cloud configs (used by save flow for cloud type switching). */
export function getActiveCloudConfigs(): CloudConfig[] {
const db = getDb();
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at
FROM cloud_configs WHERE is_active = 1
ORDER BY cloud_type ASC, id ASC`
).all() as CloudConfig[];
}
export function saveCloudConfig(data: {
id?: number;
cloud_type: string;
cookie?: string;
nickname?: string;
is_active?: number;
storage_used?: string;
storage_total?: string;
}): CloudConfig {
const db = getDb();
if (data.id) {
db.prepare(
`UPDATE cloud_configs SET
cloud_type = COALESCE(?, cloud_type),
cookie = COALESCE(?, cookie),
nickname = COALESCE(?, nickname),
is_active = COALESCE(?, is_active),
storage_used = COALESCE(?, storage_used),
storage_total = COALESCE(?, storage_total),
consecutive_failures = 0,
updated_at = ?
WHERE id = ?`
).run(data.cloud_type, data.cookie || null, data.nickname || null, data.is_active ?? 1, data.storage_used || null, data.storage_total || null, localTimestamp(), data.id);
} else {
const existing = db.prepare(
'SELECT id, nickname FROM cloud_configs WHERE cloud_type = ? AND is_active = 1 LIMIT 1'
).get(data.cloud_type) as any;
if (existing) {
db.prepare(
`UPDATE cloud_configs SET
cookie = COALESCE(?, cookie),
nickname = COALESCE(?, nickname),
is_active = COALESCE(?, is_active),
storage_used = COALESCE(?, storage_used),
storage_total = COALESCE(?, storage_total),
consecutive_failures = 0,
updated_at = ?
WHERE id = ?`
).run(data.cookie || null, data.nickname || null, data.is_active ?? 1, data.storage_used || null, data.storage_total || null, localTimestamp(), existing.id);
} else {
db.prepare(
'INSERT INTO cloud_configs (cloud_type, cookie, nickname, is_active, storage_used, storage_total, consecutive_failures) VALUES (?, ?, ?, ?, ?, ?, 0)'
).run(data.cloud_type, data.cookie || null, data.nickname || null, data.is_active ?? 1, data.storage_used || null, data.storage_total || null);
}
}
const savedId = data.id || (db.prepare('SELECT last_insert_rowid() as id').get() as any).id;
return db.prepare(
`SELECT id, cloud_type, cookie, nickname, is_active, storage_used, storage_total,
checkin_status, last_checkin_at, checkin_message, consecutive_failures,
last_used_at, total_saves, created_at, updated_at
FROM cloud_configs WHERE id = ?`
).get(savedId) as CloudConfig;
}
export function deleteCloudConfig(id: number): boolean {
const db = getDb();
const result = db.prepare('DELETE FROM cloud_configs WHERE id = ?').run(id);
return result.changes > 0;
}
// ── Cookie Validation ────────────────────────────────────────────
async function fetchQuarkNickname(cookie: string): Promise<string | null> {
const MAX_RETRIES = 2;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
try {
const response = await fetch('https://pan.quark.cn/account/info', {
headers: {
'Cookie': cookie,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Referer': 'https://pan.quark.cn/',
},
signal: AbortSignal.timeout(15000),
});
if (!response.ok) return null;
const data = await response.json() as any;
if (data?.data?.nickname) return data.data.nickname;
} catch {
if (attempt < MAX_RETRIES) {
await new Promise(r => setTimeout(r, 1500));
continue;
}
}
}
return null;
}
export async function testCloudConnection(id: number): Promise<{
success: boolean;
message: string;
nickname?: string;
storage_used?: string;
storage_total?: string;
}> {
const config = getCloudConfigById(id);
if (!config) {
return { success: false, message: 'Cloud config not found' };
}
if (!config.cookie) {
return { success: false, message: 'Cookie not configured' };
}
try {
let valid = false;
let nickname = '';
let storageUsed = config.storage_used || '';
let storageTotal = config.storage_total || '';
if (config.cloud_type === 'baidu') {
const { BaiduDriver } = require('./drivers/baidu.driver');
const driver = new BaiduDriver({ cookie: config.cookie, nickname: config.nickname });
valid = await driver.validate();
if (valid) {
const info = await driver.getUserInfo();
if (info) {
nickname = config.nickname || info.nickname || '百度网盘';
const fmt = (b: number) => b >= 1024**3 ? (b/1024**3).toFixed(2)+' GB' : (b/1024**2).toFixed(2)+' MB';
storageUsed = fmt(info.usedBytes);
storageTotal = fmt(info.totalBytes);
}
}
} else {
const { QuarkDriver } = require('./drivers/quark.driver');
const driver = new QuarkDriver({ cookie: config.cookie, nickname: config.nickname });
valid = await driver.validate();
if (valid) {
nickname = config.nickname || (await fetchQuarkNickname(config.cookie)) || '夸克网盘';
const storage = await driver.getStorageInfoQuick();
storageTotal = (storage.total !== '-' && storage.total !== '0 B') ? storage.total : (config.storage_total || '');
}
}
const db = getDb();
if (!valid) {
db.prepare(
`UPDATE cloud_configs SET verification_status = 'invalid', updated_at = ? WHERE id = ?`
).run(localTimestamp(), id);
return { success: false, message: '连接失败Cookie 无效或已过期,或网络暂时异常' };
}
db.prepare(
`UPDATE cloud_configs SET nickname = ?, storage_total = ?, storage_used = ?, is_active = 1, verification_status = 'valid', updated_at = ? WHERE id = ?`
).run(nickname, storageTotal, storageUsed, localTimestamp(), id);
return {
success: true,
message: '连接成功',
nickname,
storage_used: storageUsed,
storage_total: storageTotal,
};
} catch (err: any) {
try {
const db = getDb();
db.prepare(
`UPDATE cloud_configs SET verification_status = 'invalid', updated_at = ? WHERE id = ?`
).run(localTimestamp(), id);
} catch {}
return { success: false, message: `连接失败:${err.message || '未知错误'}` };
}
}
export async function testCloudConnectionWithCookie(cloudType: string, cookie: string): Promise<{
success: boolean;
message: string;
nickname?: string;
storage_used?: string;
storage_total?: string;
}> {
try {
const { QuarkDriver } = require('./drivers/quark.driver');
const driver = new QuarkDriver({ cookie, nickname: '' });
const valid = await driver.validate();
if (!valid) {
return { success: false, message: '连接失败Cookie 无效或已过期' };
}
const nickname = (await fetchQuarkNickname(cookie)) || cloudType;
const storage = await driver.getStorageInfo();
return {
success: true,
message: '连接成功',
nickname,
storage_used: storage.used,
storage_total: storage.total,
};
} catch (err: any) {
return { success: false, message: `连接失败:${err.message || '未知错误'}` };
}
}
// ── Unified Credential Validation ─────────────────────────────────
export interface CredentialValidationResult {
valid: boolean;
config?: CloudConfig;
errorCode?: string;
message: string;
}
/**
* Get and validate a credential for the given cloud type.
*
* This is the unified entry point for all save/transfer operations.
* It handles:
* 1. Finding an active config with < 5 consecutive failures (round-robin)
* 2. Validating cookie freshness via driver.validate()
* 3. Returning structured result with error codes
*
* Reference: search-ucmao get_and_validate_credential() pattern.
*/
export async function getAndValidateCredential(cloudType: string): Promise<CredentialValidationResult> {
const db = getDb();
const config = db.prepare(
`SELECT * FROM cloud_configs
WHERE cloud_type = ? AND is_active = 1
AND consecutive_failures < 5
ORDER BY last_used_at ASC NULLS FIRST
LIMIT 1`
).get(cloudType) as CloudConfig | undefined;
if (!config) {
return {
valid: false,
errorCode: 'NO_AVAILABLE_DRIVE',
message: `Cloud type "${cloudType}" is not configured or no available drives`,
};
}
if (!config.cookie) {
return {
valid: false,
errorCode: 'COOKIE_MISSING',
message: `Cookie not configured for ${cloudType} drive #${config.id}`,
};
}
try {
let cookieValid = false;
if (cloudType === 'baidu') {
const { BaiduDriver } = require('./drivers/baidu.driver');
const driver = new BaiduDriver({ cookie: config.cookie, nickname: config.nickname });
cookieValid = await driver.validate();
} else {
const { QuarkDriver } = require('./drivers/quark.driver');
const driver = new QuarkDriver({ cookie: config.cookie, nickname: config.nickname });
cookieValid = await driver.validate();
}
if (!cookieValid) {
db.prepare(
`UPDATE cloud_configs SET verification_status = 'invalid', updated_at = ? WHERE id = ?`
).run(localTimestamp(), config.id);
return {
valid: false,
errorCode: 'COOKIE_EXPIRED',
message: `Cookie expired or invalid for ${cloudType} drive #${config.id}`,
};
}
return {
valid: true,
config,
message: 'ok',
};
} catch (err: any) {
return {
valid: false,
errorCode: 'VALIDATION_ERROR',
message: `Credential validation failed: ${err.message}`,
};
}
}

View File

@@ -0,0 +1,113 @@
// Native fetch available in Node 20+
export interface AliyunConfig {
cookie?: string;
nickname?: string;
}
export class AliyunDriver {
private config: AliyunConfig;
private baseUrl = 'https://api.aliyundrive.com';
constructor(config: AliyunConfig = {}) {
this.config = config;
}
/**
* Extract share_id from an Aliyun share URL.
* Supports:
* https://www.aliyundrive.com/s/XXXYYY
* https://www.alipan.com/s/XXXYYY
* https://api.aliyundrive.com/v2/share_link/XXXYYY
*/
private extractShareId(shareUrl: string): string | null {
try {
const url = new URL(shareUrl);
const pathMatch = url.pathname.match(/\/s\/([a-zA-Z0-9]+)/);
if (pathMatch) return pathMatch[1];
const shareMatch = url.pathname.match(/\/share_link\/([a-zA-Z0-9]+)/);
if (shareMatch) return shareMatch[1];
return null;
} catch {
return null;
}
}
/**
* Validate a share link using Aliyun's public anonymous API.
* No cookie or token required — this endpoint is open.
*
* API:
* POST https://api.aliyundrive.com/v2/share_link/get_share_by_anonymous
* Body: { "share_id": "XXXYYY", "share_pwd": "" }
*
* Success: returns share_name, file_infos, creator info
* Failure: returns error code (ShareLinkExpired, ShareLinkCancelled, etc.)
*/
async validateShareLink(shareUrl: string): Promise<{
valid: boolean;
message: string;
fileCount?: number;
shareName?: string;
}> {
const shareId = this.extractShareId(shareUrl);
if (!shareId) {
return { valid: false, message: '无法解析阿里云盘链接格式' };
}
try {
const response = await fetch(
`${this.baseUrl}/v2/share_link/get_share_by_anonymous`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Referer': 'https://www.aliyundrive.com/',
'Accept-Language': 'zh-CN,zh;q=0.9',
},
body: JSON.stringify({
share_id: shareId,
share_pwd: '',
}),
signal: AbortSignal.timeout(10000),
}
);
if (!response.ok) {
return { valid: false, message: `HTTP ${response.status}: API 请求失败` };
}
const data = await response.json() as any;
// Check for error codes
if (data.code) {
switch (data.code) {
case 'ShareLinkExpired':
return { valid: false, message: '分享已失效(已过期)' };
case 'ShareLinkCancelled':
return { valid: false, message: '分享已被取消' };
case 'NotFound.ShareLink':
return { valid: false, message: '分享链接不存在' };
case 'ShareLinkPasswordIncorrect':
return { valid: true, message: '需要提取码(链接有效)' };
default:
return { valid: false, message: data.message || `未知错误 (${data.code})` };
}
}
// Success — valid share link
const fileInfos = data.file_infos || [];
return {
valid: true,
message: `有效链接(${fileInfos.length} 个文件)`,
fileCount: fileInfos.length,
shareName: data.share_name || '',
};
} catch (err: any) {
return { valid: false, message: `网络错误: ${err.message || err}` };
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,70 @@
// Standard error codes for all cloud drivers
export const ErrCode = {
COOKIE_EXPIRED: 'COOKIE_EXPIRED',
COOKIE_INVALID: 'COOKIE_INVALID',
TOKEN_EXPIRED: 'TOKEN_EXPIRED',
SHARE_NOT_FOUND: 'SHARE_NOT_FOUND',
SHARE_EXPIRED: 'SHARE_EXPIRED',
PASSWORD_REQUIRED: 'PASSWORD_REQUIRED',
PASSWORD_WRONG: 'PASSWORD_WRONG',
CAPACITY_FULL: 'CAPACITY_FULL',
FILE_EXISTS: 'FILE_EXISTS',
RATE_LIMITED: 'RATE_LIMITED',
TRANSFER_FAILED: 'TRANSFER_FAILED',
NETWORK_ERROR: 'NETWORK_ERROR',
UNSUPPORTED: 'UNSUPPORTED',
UNKNOWN: 'UNKNOWN',
} as const;
export type ErrorCode = typeof ErrCode[keyof typeof ErrCode];
const messages: Record<string, string> = {
[ErrCode.COOKIE_EXPIRED]: 'Cookie已过期请重新登录',
[ErrCode.COOKIE_INVALID]: 'Cookie无效请检查配置',
[ErrCode.TOKEN_EXPIRED]: 'Token已过期请刷新',
[ErrCode.SHARE_NOT_FOUND]: '分享链接不存在或已被删除',
[ErrCode.SHARE_EXPIRED]: '分享链接已过期',
[ErrCode.PASSWORD_REQUIRED]: '需要提取码',
[ErrCode.PASSWORD_WRONG]: '提取码错误',
[ErrCode.CAPACITY_FULL]: '网盘容量不足',
[ErrCode.RATE_LIMITED]: '请求过于频繁,请稍后重试',
[ErrCode.TRANSFER_FAILED]: '转存失败',
[ErrCode.NETWORK_ERROR]: '网络请求失败',
[ErrCode.UNKNOWN]: '未知错误',
};
export function errorResponse(code: ErrorCode, detail?: string) {
return {
success: false,
code,
message: messages[code] + (detail ? ': ' + detail : ''),
};
}
export class TransferError extends Error {
code: ErrorCode;
detail?: string;
cookieExpired: boolean;
constructor(code: ErrorCode, detail?: string) {
super(messages[code] + (detail ? ': ' + detail : ''));
this.code = code;
this.detail = detail;
this.cookieExpired = (code === ErrCode.COOKIE_EXPIRED || code === ErrCode.COOKIE_INVALID);
}
}
/** Detect error code from driver result message (for untagged drivers) */
export function detectErrorCode(result: { message?: string; cookieExpired?: boolean }): ErrorCode | null {
if (!result || !result.message) return null;
if (result.cookieExpired) return ErrCode.COOKIE_EXPIRED;
const msg = result.message.toLowerCase();
if (msg.includes('cookie') || msg.includes('登录') || msg.includes('bdstoken')) return ErrCode.COOKIE_EXPIRED;
if (msg.includes('不存在') || msg.includes('not found') || msg.includes('已删除')) return ErrCode.SHARE_NOT_FOUND;
if (msg.includes('过期') || msg.includes('expired')) return ErrCode.SHARE_EXPIRED;
if (msg.includes('提取码') || msg.includes('密码') || msg.includes('password')) return ErrCode.PASSWORD_WRONG;
if (msg.includes('容量') || msg.includes('空间') || msg.includes('capacity')) return ErrCode.CAPACITY_FULL;
if (msg.includes('频繁') || msg.includes('稍后') || msg.includes('rate')) return ErrCode.RATE_LIMITED;
if (msg.includes('网络') || msg.includes('fetch') || msg.includes('timeout')) return ErrCode.NETWORK_ERROR;
return ErrCode.TRANSFER_FAILED;
}

View File

@@ -0,0 +1,31 @@
/**
* IP 归属地查询工具
* 通过系统配置中的 IP 地理接口查询
*/
import { getSystemConfig } from '../admin/system-config.service';
export async function lookupIpLocation(ip: string): Promise<string | null> {
if (!ip || ip === '127.0.0.1' || ip === '::1' || ip.startsWith('192.168.') || ip.startsWith('10.')) {
return null;
}
try {
const apiUrlTemplate = getSystemConfig('ip_geo_api_url');
if (!apiUrlTemplate) return null;
const url = apiUrlTemplate.replace('{ip}', encodeURIComponent(ip));
const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
if (!res.ok) return null;
const data = await res.json() as {
code: number; sheng?: string; shi?: string; qu?: string;
isp?: string; msg?: string; guo?: string;
};
if (data.code !== 200) return null;
// Format: "四川 绵阳 江油 中国联通" — strip 省/市/区/州 suffixes for compact display
const stripSuffix = (s: string | undefined) => s?.replace(/[省市州区]$/, '');
const parts = [stripSuffix(data.sheng), stripSuffix(data.shi), stripSuffix(data.qu), data.isp].filter(Boolean);
return parts.join(' ');
} catch {
return null;
}
}

View File

@@ -0,0 +1,95 @@
// Native fetch available in Node 20+
import { getSystemConfig } from '../admin/system-config.service';
type NotifyLevel = 'info' | 'warn' | 'error';
interface NotifyChannel {
send(title: string, content: string, level: NotifyLevel): Promise<void>;
}
// ---- Feishu Webhook Channel ----
class FeishuChannel implements NotifyChannel {
private webhookUrl: string;
constructor(webhookUrl: string) {
this.webhookUrl = webhookUrl;
}
async send(title: string, content: string, _level: NotifyLevel): Promise<void> {
try {
const body = JSON.stringify({
msg_type: 'interactive',
card: {
header: {
title: { tag: 'plain_text', content: title },
template: _level === 'error' ? 'red' : _level === 'warn' ? 'orange' : 'blue',
},
elements: [
{ tag: 'div', text: { tag: 'lark_md', content } },
{
tag: 'note',
elements: [
{ tag: 'plain_text', content: `CloudSearch · ${new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })}` },
],
},
],
},
});
const resp = await fetch(this.webhookUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body,
});
if (!resp.ok) {
console.error(`[Notify] Feishu send failed: ${resp.status}`);
}
} catch (err: any) {
console.error('[Notify] Feishu send error:', err.message);
}
}
}
// ---- Notification Manager ----
let _channel: NotifyChannel | null = null;
function getChannel(): NotifyChannel | null {
const feishuUrl = process.env.FEISHU_WEBHOOK || getSystemConfig('feishu_webhook_url');
if (!feishuUrl) return null;
if (!_channel) {
_channel = new FeishuChannel(feishuUrl);
console.log('[Notify] Feishu webhook configured');
}
return _channel;
}
/**
* Send a notification through configured channels.
* Returns immediately — failures are logged silently.
*/
export function notify(title: string, content: string, level: NotifyLevel = 'info'): void {
const ch = getChannel();
if (!ch) return;
// Fire-and-forget — don't block the caller
ch.send(title, content, level).catch(() => {});
}
/**
* Notify on critical events:
* - Cookie expired / login failed
* - Save/transfer failed repeatedly
* - Storage below threshold
*/
export function notifyError(title: string, detail: string): void {
notify(`⚠️ ${title}`, detail, 'error');
}
export function notifyWarn(title: string, detail: string): void {
notify(`🔔 ${title}`, detail, 'warn');
}
export function notifyInfo(title: string, detail: string): void {
notify(` ${title}`, detail, 'info');
}

View File

@@ -0,0 +1,407 @@
import { chromium, BrowserContext, Page } from 'playwright';
import jsQR from 'jsqr';
import { getDb } from '../database/database';
import { escapeLike } from '../utils/time';
interface QrSession {
id: string;
browserContext: BrowserContext;
page: Page;
createdAt: number;
cookieSnapshot: string;
lastPollAt: number;
qrUrl: string;
status: 'pending' | 'scanned' | 'logged_in' | 'expired' | 'error';
error?: string;
}
const SESSIONS = new Map<string, QrSession>();
const SESSION_TTL = 5 * 60 * 1000; // 5 minutes
const COOKIE_CHECK_INTERVAL = 1500; // 1.5s between cookie checks
const CHROMIUM_PATH = process.env.CHROMIUM_PATH || '/usr/bin/chromium-browser';
// Clean up old sessions periodically
setInterval(() => {
const now = Date.now();
for (const [id, session] of SESSIONS.entries()) {
if (now - session.createdAt > SESSION_TTL) {
cleanupSession(id);
}
}
}, 60000);
function cleanupSession(id: string) {
const session = SESSIONS.get(id);
if (session) {
try {
session.browserContext.close().catch(() => {});
} catch {}
SESSIONS.delete(id);
}
}
/**
* Extract QR code URL from the login page canvas using jsQR.
* The actual login QR code is Canvas #0 (anonymous, 177x177), NOT #react-qrcode-logo.
*/
async function extractQrUrl(page: Page): Promise<string> {
// Run inside Playwright's browser context (as a string to avoid Node TS type errors)
const raw = await page.evaluate(`(() => {
const canvases = document.querySelectorAll('canvas');
var results = [];
for (var i = 0; i < canvases.length; i++) {
try {
var c = canvases[i];
var ctx = c.getContext('2d');
if (!ctx) continue;
var imageData = ctx.getImageData(0, 0, c.width, c.height);
results.push({
index: i,
w: c.width,
h: c.height,
data: Array.from(imageData.data)
});
} catch(e) {}
}
return results;
})()`) as unknown as { index: number; w: number; h: number; data: number[] }[];
if (!raw || raw.length === 0) {
throw new Error('页面没有可用的 canvas');
}
// Try to decode each canvas, preferring the one with su.quark.cn URL
let bestUrl = '';
let bestResult: { index: number; w: number; h: number; data: number[] } | null = null;
for (const canvas of raw) {
const code = jsQR(new Uint8ClampedArray(canvas.data), canvas.w, canvas.h);
if (code && code.data) {
// If this is the login QR code (has su.quark.cn), use it immediately
if (code.data.includes('su.quark.cn')) {
return code.data;
}
// Otherwise keep it as fallback
if (!bestUrl) {
bestUrl = code.data;
bestResult = canvas;
}
}
}
if (bestUrl) {
return bestUrl;
}
throw new Error('无法解析二维码内容');
}
/**
* Start a QR code login session.
* Launches headless Chromium, navigates to Quark login page, extracts QR code URL.
*/
export async function startQrLogin(): Promise<{
sessionId: string;
qrUrl: string;
expiresIn: number;
}> {
// Clean up any existing expired sessions
for (const [id, session] of SESSIONS.entries()) {
if (Date.now() - session.createdAt > SESSION_TTL) {
cleanupSession(id);
}
}
const browser = await chromium.launch({
executablePath: CHROMIUM_PATH,
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-gpu',
'--no-first-run',
'--no-zygote',
],
});
const browserContext = await browser.newContext({
userAgent:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
viewport: { width: 1280, height: 800 },
locale: 'zh-CN',
});
const page = await browserContext.newPage();
const sessionId = Date.now().toString(36) + Math.random().toString(36).slice(2, 8);
try {
// Navigate to Quark login page (now the homepage itself has QR login)
await page.goto('https://pan.quark.cn/', {
waitUntil: 'commit',
timeout: 30000,
});
// Wait for the QR code canvas to appear
await page.waitForSelector('canvas', { timeout: 15000 });
// Extra wait for the QR code to fully render
await page.waitForTimeout(2000);
// Extract the QR code URL from the canvas
const qrUrl = await extractQrUrl(page);
// Take initial cookie snapshot
const cookies = await browserContext.cookies();
const cookieSnapshot = cookies.map(c => `${c.name}=${c.value}`).join('; ');
const session: QrSession = {
id: sessionId,
browserContext,
page,
createdAt: Date.now(),
cookieSnapshot,
lastPollAt: Date.now(),
qrUrl,
status: 'pending',
};
SESSIONS.set(sessionId, session);
// Start background polling for login detection
pollLoginStatus(session);
// Handle page navigation (like redirect after login)
page.on('framenavigated', async (frame) => {
if (frame === page.mainFrame()) {
const url = frame.url();
if (url === 'about:blank') {
await checkAndCaptureCookies(session);
}
}
});
// Handle popups/dialogs
page.on('popup', async (popup) => {
try {
await popup.waitForLoadState('networkidle', { timeout: 10000 });
await checkAndCaptureCookies(session);
} catch {}
});
return {
sessionId,
qrUrl,
expiresIn: SESSION_TTL / 1000,
};
} catch (err: any) {
// Clean up on failure
try { await browserContext.close(); } catch {}
try { browser.close().catch(() => {}); } catch {}
SESSIONS.delete(sessionId);
throw new Error(`启动扫码登录失败: ${err.message}`);
}
}
/**
* Poll login status in background.
* Checks cookies every COOKIE_CHECK_INTERVAL ms for new session tokens.
*/
async function pollLoginStatus(session: QrSession) {
const checkInterval = setInterval(async () => {
try {
const now = Date.now();
// Check if expired
if (now - session.createdAt > SESSION_TTL) {
clearInterval(checkInterval);
session.status = 'expired';
cleanupSession(session.id);
return;
}
session.lastPollAt = now;
// Check cookies
const cookies = await session.browserContext.cookies();
const cookieStr = cookies.map(c => `${c.name}=${c.value}`).join('; ');
// Check for session cookies indicating login
const hasSessionCookie = cookies.some(
c => (c.name === '__st' || c.name === 'pus' || c.name === '__pus' || c.name === '__ktd')
);
if (hasSessionCookie) {
session.cookieSnapshot = cookieStr;
session.status = 'logged_in';
clearInterval(checkInterval);
return;
}
// Check URL change as alternative indicator
const url = session.page.url();
if (!url.includes('login') && !url.includes('qrcode') && url !== 'about:blank' && url !== 'https://pan.quark.cn/' && url.length > 10) {
await checkAndCaptureCookies(session);
}
} catch (err: any) {
// Page might have been closed
clearInterval(checkInterval);
}
}, COOKIE_CHECK_INTERVAL);
}
/**
* Check cookies after navigation/redirect and capture them if login succeeded.
*/
async function checkAndCaptureCookies(session: QrSession) {
try {
const cookies = await session.browserContext.cookies();
const cookieStr = cookies.map(c => `${c.name}=${c.value}`).join('; ');
const hasSessionCookie = cookies.some(
c => (c.name === '__st' || c.name === 'pus' || c.name === '__pus' || c.name === '__ktd')
);
if (hasSessionCookie) {
session.cookieSnapshot = cookieStr;
session.status = 'logged_in';
} else if (cookies.length > 3) {
const newCookies = cookies.filter(
c => !['ctoken', 'b-user-id', '__wpkreporterwid_'].includes(c.name)
);
if (newCookies.length > 0) {
session.cookieSnapshot = cookieStr;
try {
const resp = await session.page.evaluate(async () => {
const r = await fetch('https://pan.quark.cn/account/info', {
credentials: 'include',
});
return await r.text();
});
const data = JSON.parse(resp);
if (data?.data?.nickname) {
session.status = 'logged_in';
}
} catch {}
}
}
} catch {}
}
/**
* Get the login status for a session.
*/
export async function getQrLoginStatus(sessionId: string): Promise<{
status: string;
cookie?: string;
nickname?: string;
storage_used?: string;
storage_total?: string;
autoUpdated?: boolean;
updatedConfigId?: number;
}> {
const session = SESSIONS.get(sessionId);
if (!session) {
return { status: 'expired' };
}
// Check if expired
if (Date.now() - session.createdAt > SESSION_TTL) {
session.status = 'expired';
cleanupSession(sessionId);
return { status: 'expired' };
}
if (session.status === 'logged_in') {
// Try to get nickname too
let nickname = '';
try {
const resp = await session.page.evaluate(async () => {
const r = await fetch('https://pan.quark.cn/account/info', {
credentials: 'include',
});
return await r.text();
});
const data = JSON.parse(resp);
nickname = data?.data?.nickname || '';
} catch {}
// Fetch capacity info from within the browser context (has full JS signing)
let storageTotal = '';
let storageUsed = '';
try {
const capResp = await session.page.evaluate(async () => {
const r = await fetch(
'https://pan.quark.cn/1/clouddrive/capacity/detail?pr=ucpro&fr=pc',
{ credentials: 'include' }
);
return await r.text();
});
const capData = JSON.parse(capResp);
if (capData.status === 200 && capData.data?.capacity_summary) {
const summary = capData.data.capacity_summary;
const total = summary.sum_capacity || 0;
storageTotal = formatBytes(total);
storageUsed = '0 B'; // capacity/detail doesn't return used_size
}
} catch {}
// Build full cookie string including httpOnly cookies
const cookies = await session.browserContext.cookies();
const cookieStr = cookies.map(c => `${c.name}=${c.value}`).join('; ');
// Extract __uid from cookie for duplicate detection
const uidMatch = cookieStr.match(/(?<!\\w)__uid=([a-f0-9-]+)/);
let autoUpdated = false;
let updatedConfigId: number | undefined;
if (uidMatch) {
const uid = uidMatch[1];
try {
const db = getDb();
const existing = db.prepare(
`SELECT id, nickname FROM cloud_configs WHERE cloud_type = 'quark' AND cookie LIKE ?`
).get(`%${escapeLike(uid)}%`) as { id: number; nickname: string } | undefined;
if (existing) {
// Same account → auto-update cookie with capacity info too
const localTimestamp = new Date().toISOString().replace('T', ' ').slice(0, 19);
db.prepare(
`UPDATE cloud_configs SET cookie = ?, storage_used = ?, storage_total = ?, updated_at = ? WHERE id = ?`
).run(cookieStr, storageUsed || null, storageTotal || null, localTimestamp, existing.id);
autoUpdated = true;
updatedConfigId = existing.id;
}
} catch {}
}
// Clean up session after successful login
cleanupSession(sessionId);
return {
status: 'logged_in',
cookie: cookieStr,
nickname,
storage_used: storageUsed,
storage_total: storageTotal,
autoUpdated,
updatedConfigId,
};
}
return { status: session.status };
}
function formatBytes(bytes: number): string {
if (bytes === 0) return '0 B';
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + ' ' + sizes[i];
}
/**
* Cancel a QR login session.
*/
export async function cancelQrLogin(sessionId: string): Promise<void> {
cleanupSession(sessionId);
}