"use client"; import { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { Loader2, Save, ShieldOff } from "lucide-react"; import { Shell } from "@/components/Shell"; import { useToast } from "@/components/Toast"; import { fetchJson, fmtTs } from "@/lib/api"; type Settings = Record; const FIELDS: Array<{ key: string; label: string; hint: string; step?: number }> = [ { key: "max_concurrency", label: "最大并发", hint: "同时检查的直播间数,建议 1–3", step: 1 }, { key: "min_request_gap_sec", label: "最小请求间隔(秒)", hint: "任意两次请求之间的最小间隔", step: 0.1 }, { key: "request_jitter_sec", label: "随机抖动(秒)", hint: "在间隔上叠加随机延迟", step: 0.1 }, { key: "offline_interval_sec", label: "未开播复查间隔(秒)", hint: "离线房间下次检查等待", step: 1 }, { key: "live_interval_sec", label: "在播复查间隔(秒)", hint: "直播中更勤,便于捕捉停播", step: 1 }, { key: "error_backoff_base_sec", label: "错误退避基数(秒)", hint: "失败后指数退避", step: 1 }, { key: "error_backoff_max_sec", label: "错误退避上限(秒)", hint: "退避封顶", step: 1 }, { key: "global_pause_on_risk_sec", label: "风控全局暂停(秒)", hint: "触发验证码后全体停检", step: 1 }, ]; export default function SettingsPage() { const toast = useToast(); const [settings, setSettings] = useState({}); const [status, setStatus] = useState | null>(null); const [ytStatus, setYtStatus] = useState | null>(null); const [shortsStatus, setShortsStatus] = useState | null>(null); const [loading, setLoading] = useState(false); const [msg, setMsg] = useState(""); const [error, setError] = useState(""); const [notifPerm, setNotifPerm] = useState("default"); const load = useCallback(async () => { setLoading(true); try { const [s, m, yt, sh] = await Promise.all([ fetchJson("/api/settings"), fetchJson("/api/monitor/status"), fetchJson("/api/youtube-monitor/status").catch(() => null), fetchJson("/api/shorts/status").catch(() => null), ]); setSettings(s); setStatus(m); setYtStatus(yt); setShortsStatus(sh); if (typeof window !== "undefined" && "Notification" in window) { setNotifPerm(Notification.permission); } } catch (e) { setError((e as Error).message); } finally { setLoading(false); } }, []); useEffect(() => { void load(); }, [load]); async function save() { setMsg(""); setError(""); try { const body: Record = {}; for (const f of FIELDS) body[f.key] = Number(settings[f.key]); body.capture_cover_on_start = Boolean(settings.capture_cover_on_start); body.monitor_enabled = Boolean(settings.monitor_enabled); body.dy_cookie = String(settings.dy_cookie || ""); body.http_proxy = String(settings.http_proxy || ""); body.notify_webhook_url = String(settings.notify_webhook_url || ""); body.notify_on_live = Boolean(settings.notify_on_live); body.notify_on_offline = Boolean(settings.notify_on_offline); body.notify_ntfy_on_douyin_live = Boolean(settings.notify_ntfy_on_douyin_live); body.notify_on_risk = Boolean(settings.notify_on_risk); body.notify_on_shorts_fail = settings.notify_on_shorts_fail !== false; body.notify_on_shorts_ok = Boolean(settings.notify_on_shorts_ok); body.notify_on_shorts_quota = settings.notify_on_shorts_quota !== false; body.quiet_hours_enabled = Boolean(settings.quiet_hours_enabled); body.quiet_hours_start = Number(settings.quiet_hours_start ?? 2); body.quiet_hours_end = Number(settings.quiet_hours_end ?? 7); body.quiet_hours_allow_priority = settings.quiet_hours_allow_priority !== false; body.priority_interval_sec = Number(settings.priority_interval_sec ?? 25); body.yt_monitor_enabled = Boolean(settings.yt_monitor_enabled); body.yt_offline_interval_sec = Number(settings.yt_offline_interval_sec ?? 180); body.yt_live_interval_sec = Number(settings.yt_live_interval_sec ?? 90); body.yt_batch_size = Number(settings.yt_batch_size ?? 8); body.yt_request_gap_sec = Number(settings.yt_request_gap_sec ?? 2); body.shorts_enabled = Boolean(settings.shorts_enabled); body.shorts_clip_sec = Number(settings.shorts_clip_sec ?? 15); body.shorts_trigger = String(settings.shorts_trigger || "tagged"); body.shorts_tag = String(settings.shorts_tag || "shorts"); body.shorts_dedupe_room_day = settings.shorts_dedupe_room_day !== false; body.shorts_max_concurrent = Number(settings.shorts_max_concurrent ?? 1); body.shorts_auto_upload = settings.shorts_auto_upload !== false; body.shorts_live_sample = settings.shorts_live_sample !== false; body.shorts_live_sample_interval_sec = Number(settings.shorts_live_sample_interval_sec ?? 180); body.shorts_live_sample_batch = Number(settings.shorts_live_sample_batch ?? 1); body.shorts_sample_any_live = settings.shorts_sample_any_live !== false; body.shorts_upload_spread = settings.shorts_upload_spread !== false; body.shorts_upload_min_gap_sec = Number(settings.shorts_upload_min_gap_sec ?? 720); body.shorts_channel_min_gap_sec = Number(settings.shorts_channel_min_gap_sec ?? 2400); body.shorts_delete_clip_after_upload = settings.shorts_delete_clip_after_upload !== false; body.shorts_auto_retry_failed = settings.shorts_auto_retry_failed !== false; body.shorts_auto_retry_max_attempts = Number(settings.shorts_auto_retry_max_attempts ?? 3); body.shorts_auto_retry_interval_sec = Number(settings.shorts_auto_retry_interval_sec ?? 300); body.shorts_oauth_check_interval_sec = Number(settings.shorts_oauth_check_interval_sec ?? 3600); body.shorts_oauth_client_id = String(settings.shorts_oauth_client_id || ""); const gSecret = String(settings.shorts_oauth_client_secret || ""); if (gSecret && !gSecret.startsWith("***")) body.shorts_oauth_client_secret = gSecret; body.shorts_oauth_redirect_uri = String( settings.shorts_oauth_redirect_uri || "https://dylive.nomadro.com/api/shorts/oauth/callback" ); const profiles = Array.isArray(settings.shorts_oauth_profiles) ? settings.shorts_oauth_profiles : []; body.shorts_upload_socks_enabled = Boolean(settings.shorts_upload_socks_enabled); body.shorts_upload_socks_profile_id = String(settings.shorts_upload_socks_profile_id || ""); body.shorts_upload_socks_profiles = Array.isArray(settings.shorts_upload_socks_profiles) ? settings.shorts_upload_socks_profiles : []; const oauthProfiles = profiles.map((p: any) => ({ id: String(p.id || "").toUpperCase(), label: String(p.label || ""), client_id: String(p.client_id || ""), client_secret: String(p.client_secret || ""), })); body.shorts_oauth_profiles = oauthProfiles; const studioCookiesRaw = settings.yt_studio_cookies_by_profile && typeof settings.yt_studio_cookies_by_profile === "object" ? (settings.yt_studio_cookies_by_profile as Record) : {}; const studioPatch: Record = {}; for (const pid of ["A", "B", "C", "D"]) { const row = studioCookiesRaw[pid]; if (row && typeof row === "object") { studioPatch[pid] = { SID: String(row.SID || ""), HSID: String(row.HSID || ""), SSID: String(row.SSID || ""), APISID: String(row.APISID || ""), SAPISID: String(row.SAPISID || ""), LOGIN_INFO: String(row.LOGIN_INFO || ""), SESSION_TOKEN: String(row.SESSION_TOKEN || ""), }; } } if (Object.keys(studioPatch).length) body.yt_studio_cookies_by_profile = studioPatch; body.settings_audit_enabled = settings.settings_audit_enabled !== false; body.db_backup_enabled = Boolean(settings.db_backup_enabled); body.db_backup_interval_hours = Number(settings.db_backup_interval_hours ?? 24); body.db_backup_keep_days = Number(settings.db_backup_keep_days ?? 7); body.ops_alerts_enabled = Boolean(settings.ops_alerts_enabled); body.ops_alert_disk_enabled = Boolean(settings.ops_alert_disk_enabled); body.ops_alert_quota_enabled = Boolean(settings.ops_alert_quota_enabled); body.ops_alert_interval_sec = Number(settings.ops_alert_interval_sec ?? 900); body.ops_alert_cooldown_sec = Number(settings.ops_alert_cooldown_sec ?? 3600); body.ops_alert_failed_jobs_threshold = Number(settings.ops_alert_failed_jobs_threshold ?? 50); body.notify_on_tiktok_unaudited = settings.notify_on_tiktok_unaudited !== false; body.notify_webhook_standard_json = Boolean(settings.notify_webhook_standard_json); body.clip_archive_to_s3_enabled = Boolean(settings.clip_archive_to_s3_enabled); body.readonly_username = String(settings.readonly_username || ""); const roPw = String(settings.readonly_password || ""); if (roPw && !roPw.startsWith("***")) body.readonly_password = roPw; // Keep legacy global client in sync with profile A when A is filled const profA = oauthProfiles.find((p) => p.id === "A"); if (profA?.client_id) body.shorts_oauth_client_id = profA.client_id; if (profA?.client_secret && !String(profA.client_secret).startsWith("***")) { body.shorts_oauth_client_secret = profA.client_secret; } const next = await fetchJson("/api/settings", { method: "PATCH", body: JSON.stringify(body) }); setSettings(next); setMsg("已保存"); if (typeof window !== "undefined") { window.dispatchEvent(new CustomEvent("dy-settings-changed")); } await load(); } catch (e) { setError((e as Error).message); } } async function toggleMonitor(enabled: boolean) { try { await fetchJson(enabled ? "/api/monitor/start" : "/api/monitor/stop", { method: "POST" }); toast.push(enabled ? "抖音监控已启用" : "抖音监控已暂停", "ok"); await load(); } catch (e) { toast.push((e as Error).message, "err"); } } async function toggleYtMonitor(enabled: boolean) { try { await fetchJson(enabled ? "/api/youtube-monitor/start" : "/api/youtube-monitor/stop", { method: "POST" }); toast.push(enabled ? "YouTube 分组轮询已开启" : "YouTube 分组轮询已暂停", "ok"); await load(); } catch (e) { toast.push((e as Error).message, "err"); } } async function toggleShorts(enabled: boolean) { try { await fetchJson(enabled ? "/api/shorts/start" : "/api/shorts/stop", { method: "POST" }); toast.push(enabled ? "Shorts 旁路已启用" : "Shorts 旁路已关闭", "ok"); await load(); } catch (e) { toast.push((e as Error).message, "err"); } } async function clearRisk() { try { await fetchJson("/api/monitor/clear-risk", { method: "POST" }); setMsg("已清除风控暂停"); toast.push("已清除风控暂停", "ok"); await load(); } catch (e) { toast.push((e as Error).message, "err"); } } return (

⚙️ 设置中心

防风控参数、Cookie/代理、Webhook 通知。

监控开关: 状态:{status?.running ? "循环运行中" : "未运行"} · 上次检查 {fmtTs(Number(status?.last_check_at || 0))}
YouTube 分组轮询: {ytStatus?.running ? "循环运行中" : "未运行"} · 直播 {String((ytStatus?.groups as any)?.live ?? "—")} · 启用{" "} {String((ytStatus?.groups as any)?.enabled ?? "—")} · 上次 {fmtTs(Number(ytStatus?.last_check_at || 0))}
{( [ ["yt_offline_interval_sec", "离线复查(秒)", 180], ["yt_live_interval_sec", "在播复查(秒)", 90], ["yt_batch_size", "每批数量", 8], ["yt_request_gap_sec", "请求间隔(秒)", 2], ] as const ).map(([key, label, fallback]) => ( ))}

开播/停播沿用上方「Webhook / 浏览器通知」开关;静默时段内暂停 YouTube 轮询。

Shorts 旁路(默认关,不影响监控): 管理频道 / 任务 → {shortsStatus?.running ? "worker 运行中" : "worker 未运行"} ·{" "} {shortsStatus?.enabled ? "已启用" : "已关闭"} · 今日{" "} {JSON.stringify((shortsStatus?.stats as any)?.by_status || {})}

Shorts 上传 SOCKS5 代理

仅作用于 YouTube Shorts 上传(含 OAuth token 刷新),默认关闭。不影响抖音监控与其它业务。

{(() => { const socksList = Array.isArray(settings.shorts_upload_socks_profiles) ? ([...(settings.shorts_upload_socks_profiles as any[])] as any[]) : []; const selectedId = String(settings.shorts_upload_socks_profile_id || ""); const selected = socksList.find((p) => String(p.id) === selectedId) || socksList[0] || null; const setSocksList = (next: any[]) => setSettings((prev) => ({ ...prev, shorts_upload_socks_profiles: next, shorts_upload_socks_profile_id: next.some((p) => String(p.id) === String(prev.shorts_upload_socks_profile_id)) ? prev.shorts_upload_socks_profile_id : String(next[0]?.id || ""), })); const patchSelected = (patch: Record) => { if (!selected) return; const next = socksList.map((p) => String(p.id) === String(selected.id) ? { ...p, ...patch } : p ); setSocksList(next); }; return (
{selected ? ( ) : null}
{selected ? (
) : (

暂无 SOCKS 配置,请点「新增配置」。

)}
); })()}

OAuth 配置档(A / B / C / D)

每个谷歌 Cloud 项目填一套 Client,保存一次即可。Shorts 频道只需选择 A/B/C/D,不必再手填密钥。 各项目均须登记同一回调 URI。密钥留空或显示 *** 表示不修改已有值。 长视频 YPP 自动开广告还需填写下方 Studio Cookie(与 OAuth 不同,从 studio.youtube.com 浏览器复制)。

{(["A", "B", "C", "D"] as const).map((pid) => { const profiles = Array.isArray(settings.shorts_oauth_profiles) ? (settings.shorts_oauth_profiles as any[]) : []; const row = profiles.find((p) => String(p.id || "").toUpperCase() === pid) || { id: pid, label: `谷歌账号 ${pid}`, client_id: pid === "A" ? String(settings.shorts_oauth_client_id || "") : "", client_secret: pid === "A" ? String(settings.shorts_oauth_client_secret || "") : "", }; const setProfile = (patch: Record) => { setSettings((prev) => { const prevList = Array.isArray(prev.shorts_oauth_profiles) ? [...(prev.shorts_oauth_profiles as any[])] : []; const idx = prevList.findIndex((p) => String(p.id || "").toUpperCase() === pid); const nextRow = { id: pid, label: row.label || `谷歌账号 ${pid}`, client_id: row.client_id || "", client_secret: row.client_secret || "", ...patch, }; if (idx >= 0) prevList[idx] = { ...prevList[idx], ...nextRow }; else prevList.push(nextRow); // Ensure A-D exist for (const id of ["A", "B", "C", "D"]) { if (!prevList.some((p) => String(p.id || "").toUpperCase() === id)) { prevList.push({ id, label: `谷歌账号 ${id}`, client_id: "", client_secret: "", }); } } const next: Record = { ...prev, shorts_oauth_profiles: prevList }; if (pid === "A" && patch.client_id !== undefined) next.shorts_oauth_client_id = patch.client_id; if (pid === "A" && patch.client_secret !== undefined && !String(patch.client_secret).startsWith("***")) { next.shorts_oauth_client_secret = patch.client_secret; } return next; }); }; const ready = Boolean(String(row.client_id || "").trim() && String(row.client_secret || "").trim()); const studioAll = settings.yt_studio_cookies_by_profile && typeof settings.yt_studio_cookies_by_profile === "object" ? (settings.yt_studio_cookies_by_profile as Record) : {}; const studioRow = (studioAll[pid] as Record) || {}; const setStudioCookie = (key: string, val: string) => { setSettings((prev) => { const prevMap = prev.yt_studio_cookies_by_profile && typeof prev.yt_studio_cookies_by_profile === "object" ? { ...(prev.yt_studio_cookies_by_profile as Record) } : {}; const prevRow = { ...(prevMap[pid] || {}) }; prevRow[key] = val; prevMap[pid] = prevRow; return { ...prev, yt_studio_cookies_by_profile: prevMap }; }); }; const studioReady = Boolean( String(studioRow.SID || "").trim() && String(studioRow.SAPISID || "").trim() && String(studioRow.SESSION_TOKEN || "").trim() ); return (
{pid} setProfile({ label: e.target.value })} className="min-w-[10rem] flex-1 rounded-lg border border-white/10 bg-slate-900 px-2 py-1 text-xs" placeholder={`谷歌账号 ${pid}`} /> {ready ? "已配置" : "未配置"}
Studio Cookie(长视频开广告) {studioReady ? "已填" : "未填"}

登录 studio.youtube.com(选对应品牌频道)→ F12 → Application → Cookies → 复制 SID/HSID/SSID/APISID/SAPISID/LOGIN_INFO。 再改任意视频标题 → Network → metadata_update → Payload → context.request.sessionInfo.token 复制到 SESSION_TOKEN。 Nomadro Hub 用账号 A + 品牌 nomad Immigration。

{(["SID", "HSID", "SSID", "APISID", "SAPISID"] as const).map((k) => ( ))}
); })}

Shorts 页新建/编辑频道时选择配置档 A/B/C/D,再点「Google 授权」登录对应谷歌账号即可。

{FIELDS.map((f) => ( ))}

🌙 静默时段 / 重点加速(北京时间)

点「收藏」会打上「收藏 + 重点」标签,优先检查、间隔更短;也可在各页快捷拉黑。

Cookie / 代理(降低风控)