feat(monitor): 抖音多身份池 —— 目标按登录态串行分片
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

把 44 个博主打散给多个抖音号,降低**单个账号**每轮的请求量(约 35 个博主后开始
出现限流是已知现象)。**默认串行,不是并行**:同一个出口 IP 上并发多个 cookie,
平台视角是一台设备在不停换访客,比单账号高频更容易被判异常。并发数做成了设置项,
但要先给每个账号配独立出口 IP 再考虑调大。

新增:
* `monitor_identity` 表:label / source(stored|browser) / cookie / **user_agent** /
  client_hints / status / enabled / 冷却字段。cookie 与 UA 必须成套存 —— a_bogus
  把 UA 算进签名,空 UA 签出来的请求一定被挡。
* `api/monitor/identities.py`:可用性判定、解析、**Rendezvous Hashing 分片**
  (加一个身份只迁移约 1/(n+1) 的目标)、健康状态与指数退避冷却。
* `douyin_api`:四个接口都接受显式 `identity`,走身份池时**不再回读浏览器**(否则
  浏览器里的 cookie 会把指定账号顶掉);新增 `shared_fingerprint()` 给粘贴 cookie
  的身份兜底 UA。
* `douyin_fetch.collect`:按身份分片执行,分片之间默认串行、可配切换间隔;落盘前
  按 note_id 去重(并发时两个分片可能刷到同一件作品)。
* `runner`:池模式下加载身份、不足 `identity_min_available` 就快速失败(不产生请求),
  结束后按各自分片结果回写健康。
* 设置项:identity_mode / identity_min_available / identity_switch_delay_sec /
  identity_concurrency。
* API:GET/POST/PATCH/DELETE `/api/monitor/identities` + `/{id}/check`(真请求一次,
  没有可探测博主时退化成格式检查并在 verified 里标 false)。
* 前端:设置页(仅抖音)新增「多身份(分片采集)」面板。

测试 9 条新增:分片确定性与覆盖、加身份只迁移一小部分、缺 UA/cookie 拒绝、
两次失败进冷却、成功清零、冷却中不可用、采集按身份分片。
This commit is contained in:
butubb
2026-10-22 16:37:12 +08:00
parent 4665dac667
commit d95e5ec8e2
15 changed files with 1402 additions and 65 deletions
@@ -0,0 +1,236 @@
import { useState } from 'react'
import { CheckCircle2, KeyRound, Plus, Power, Trash2 } from 'lucide-react'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import {
useCheckIdentity,
useCreateIdentity,
useDeleteIdentity,
useMonitorIdentities,
useUpdateIdentity,
} from '@/hooks/useMonitor'
import { formatRelative } from '@/lib/monitorFormat'
import { cn } from '@/lib/utils'
import type { MonitorIdentity } from '@/types/monitor'
/**
* 多身份(登录态)管理。
*
* **这里做的是串行分片,不是并行提速。** 抖音按「账号 + IP + 指纹」联合判定,同一个
* 出口 IP 上并发多个 cookie,比单账号高频请求更容易被判成异常 —— 平台视角是一台设备
* 在不停换访客。所以默认一次只跑一个身份,把目标分给不同账号,降低单个账号的请求量。
*
* 卡片上那句提醒是刻意留在界面上的:这个功能的收益和风险都很容易被误解。
*/
const STATUS: Record<
MonitorIdentity['status'],
{ label: string; className: string }
> = {
ok: {
label: '可用',
className: 'border-cyber-neon-green/40 bg-cyber-neon-green/10 text-cyber-neon-green',
},
cooldown: {
label: '冷却中',
className:
'border-cyber-neon-orange/40 bg-cyber-neon-orange/10 text-cyber-neon-orange',
},
expired: {
label: '已失效',
className: 'border-cyber-neon-pink/40 bg-cyber-neon-pink/10 text-cyber-neon-pink',
},
disabled: {
label: '已停用',
className: 'border-cyber-border-DEFAULT text-cyber-text-muted',
},
}
function clockText(ms: number): string {
return new Date(ms).toLocaleTimeString('zh-CN', {
hour: '2-digit',
minute: '2-digit',
})
}
export function IdentityPanel() {
const { data: identities, isLoading } = useMonitorIdentities()
const create = useCreateIdentity()
const update = useUpdateIdentity()
const remove = useDeleteIdentity()
const check = useCheckIdentity()
const [adding, setAdding] = useState(false)
const [label, setLabel] = useState('')
const [cookie, setCookie] = useState('')
const [userAgent, setUserAgent] = useState('')
const submit = () => {
const name = label.trim()
const value = cookie.trim()
if (!name || !value) return
create.mutate(
{ label: name, cookie: value, user_agent: userAgent.trim() },
{
onSuccess: () => {
setLabel('')
setCookie('')
setUserAgent('')
setAdding(false)
},
},
)
}
return (
<div className="rounded-lg glass-panel float-panel p-4 space-y-3">
<div className="flex items-center gap-2">
<KeyRound className="w-4 h-4 text-cyber-neon-cyan" />
<span className="font-mono text-xs tracking-wider text-cyber-text-primary">
多身份(分片采集)
</span>
</div>
<p className="text-[10px] font-mono text-cyber-text-muted leading-relaxed">
多个登录态
<span className="text-cyber-text-secondary"> 串行分片 </span>
:把博主打散给不同账号,降低单个账号每轮的请求量 —— 这不是并行提速。
同一个出口 IP 上并发多个账号,比单账号高频更容易被风控,所以默认一次只跑一个身份。
采集策略里的「多身份采集」要选 <code className="text-cyber-text-secondary">pool</code>
才会生效。
</p>
<div className="space-y-1.5">
{isLoading && (
<p className="text-[11px] font-mono text-cyber-text-muted">加载中…</p>
)}
{identities?.length === 0 && (
<p className="text-[11px] font-mono text-cyber-text-muted">
还没有身份。在下面加一条:名称 + 该账号的完整 Cookie。
</p>
)}
{identities?.map((identity) => {
const status = STATUS[identity.status]
const cooling =
identity.cooldown_until !== null && identity.cooldown_until > Date.now()
return (
<div
key={identity.id}
className="flex items-center gap-2 flex-wrap rounded border border-cyber-border-subtle bg-cyber-bg-tertiary/40 px-2 py-1.5"
>
<span className="text-[11px] font-mono text-cyber-text-primary">
{identity.label}
</span>
<Badge
variant="outline"
className={cn('text-[9px] px-1.5 py-0', status.className)}
>
{status.label}
</Badge>
<span className="text-[10px] font-mono text-cyber-text-muted">
{identity.last_ok_at
? `最近成功 ${formatRelative(identity.last_ok_at)}`
: '还没成功过'}
</span>
{cooling && identity.cooldown_until !== null && (
<span className="text-[10px] font-mono text-cyber-neon-orange">
冷却到 {clockText(identity.cooldown_until)}
</span>
)}
{identity.last_error && (
<span
className="text-[10px] font-mono text-cyber-text-muted truncate max-w-[220px]"
title={identity.last_error}
>
{identity.last_error}
</span>
)}
<div className="ml-auto flex items-center gap-1">
<Button
variant="ghost"
size="sm"
className="h-6 px-1.5"
title="用这份身份真请求一次"
disabled={check.isPending}
onClick={() => check.mutate(identity.id)}
>
<CheckCircle2 className="w-3 h-3" />
</Button>
<Button
variant="ghost"
size="sm"
className="h-6 px-1.5"
title={identity.enabled ? '停用' : '启用'}
onClick={() =>
update.mutate({
id: identity.id,
payload: { enabled: !identity.enabled },
})
}
>
<Power className="w-3 h-3" />
</Button>
<Button
variant="ghost"
size="sm"
className="h-6 px-1.5 hover:text-cyber-neon-pink"
title="删除"
onClick={() => {
if (window.confirm(`删除身份「${identity.label}」?`)) {
remove.mutate(identity.id)
}
}}
>
<Trash2 className="w-3 h-3" />
</Button>
</div>
</div>
)
})}
</div>
{adding ? (
<div className="space-y-2 pt-2 mt-1 border-t border-cyber-border-subtle">
<input
autoFocus
value={label}
onChange={(event) => setLabel(event.target.value)}
placeholder="名称,比如「小号A」"
className="w-full rounded border border-cyber-border-DEFAULT bg-cyber-bg-tertiary px-1.5 py-0.5 text-[11px] font-mono text-cyber-text-primary outline-none focus:border-cyber-neon-cyan/40"
/>
<textarea
value={cookie}
onChange={(event) => setCookie(event.target.value)}
rows={3}
placeholder="粘贴这个账号的完整 Cookie(必须含 sessionid)"
className="w-full rounded border border-cyber-border-DEFAULT bg-cyber-bg-tertiary px-1.5 py-0.5 text-[11px] font-mono text-cyber-text-primary outline-none focus:border-cyber-neon-cyan/40"
/>
<input
value={userAgent}
onChange={(event) => setUserAgent(event.target.value)}
placeholder="User-Agent(可留空:留空时从 CDP 浏览器借一份)"
className="w-full rounded border border-cyber-border-DEFAULT bg-cyber-bg-tertiary px-1.5 py-0.5 text-[11px] font-mono text-cyber-text-primary outline-none focus:border-cyber-neon-cyan/40"
/>
<div className="flex items-center gap-2">
<Button
size="sm"
disabled={!label.trim() || !cookie.trim() || create.isPending}
onClick={submit}
>
添加
</Button>
<Button size="sm" variant="ghost" onClick={() => setAdding(false)}>
取消
</Button>
</div>
</div>
) : (
<Button size="sm" variant="ghost" onClick={() => setAdding(true)}>
<Plus className="w-3 h-3 mr-1" />
添加身份
</Button>
)}
</div>
)
}
@@ -3,6 +3,7 @@ import { KeyRound, QrCode, Save } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { CookiePanel } from '@/components/monitor/CookiePanel'
import { IdentityPanel } from '@/components/monitor/IdentityPanel'
import { QrLoginPanel } from '@/components/monitor/QrLoginPanel'
import { RemoteBrowserPanel } from '@/components/settings/RemoteBrowserPanel'
import { Section, SettingField } from '@/components/settings/SettingFields'
@@ -78,6 +79,8 @@ export function SettingsView({ onNavigate }: { onNavigate?: (view: AppView) => v
scan lands in the very browser profile the monitor runs reuse. */}
<QrLoginPanel />
<CookiePanel />
{/* 身份池只有抖音接:那套进程内采集才能按账号分片,小红书走子进程。 */}
{platform === 'dy' && <IdentityPanel />}
<div className="pt-3 mt-1 border-t border-cyber-border-subtle space-y-2">
<p className="text-[10px] font-mono text-cyber-text-muted leading-relaxed">