feat(monitor): 抖音多身份池 —— 目标按登录态串行分片
把 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:
@@ -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">
|
||||
|
||||
@@ -490,3 +490,81 @@ export function useCheckUpstream() {
|
||||
onError: (error: Error) => toast.error(`检查失败:${error.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
// --- 身份池(抖音多登录态) ------------------------------------------------
|
||||
|
||||
export function useMonitorIdentities() {
|
||||
const platform = usePlatformParam()
|
||||
return useQuery({
|
||||
queryKey: ['monitorIdentities', platform],
|
||||
queryFn: async () => (await monitorApi.listIdentities(platform)).data.identities,
|
||||
// 状态会随着采集轮次变化,慢点刷就够了。
|
||||
refetchInterval: 15_000,
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreateIdentity() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (payload: {
|
||||
label: string
|
||||
cookie: string
|
||||
user_agent?: string
|
||||
platform?: string
|
||||
}) => monitorApi.createIdentity(payload),
|
||||
onSuccess: () => {
|
||||
toast.success('身份已添加')
|
||||
queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] })
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
// 后端会把「拿不到 UA」这类原因写在 detail 里,axios 默认会把它压成状态码。
|
||||
const detail = (error as { response?: { data?: { detail?: string } } })?.response
|
||||
?.data?.detail
|
||||
toast.error(`添加失败:${detail ?? error.message}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateIdentity() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
id,
|
||||
payload,
|
||||
}: {
|
||||
id: number
|
||||
payload: Partial<{ label: string; cookie: string; user_agent: string; enabled: boolean }>
|
||||
}) => monitorApi.updateIdentity(id, payload),
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] }),
|
||||
onError: (error: Error) => toast.error(`更新失败:${error.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteIdentity() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => monitorApi.deleteIdentity(id),
|
||||
onSuccess: () => {
|
||||
toast.success('身份已删除')
|
||||
queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] })
|
||||
},
|
||||
onError: (error: Error) => toast.error(`删除失败:${error.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
export function useCheckIdentity() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => monitorApi.checkIdentity(id),
|
||||
onSuccess: (response) => {
|
||||
const data = response.data
|
||||
if (data.ok) {
|
||||
toast.success(data.verified ? '验证通过' : '格式检查通过(没有可探测的博主)')
|
||||
} else {
|
||||
toast.error(`不可用:${data.error || '未知原因'}`)
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] })
|
||||
},
|
||||
onError: (error: Error) => toast.error(`验证失败:${error.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
MonitorComment,
|
||||
MonitorCreator,
|
||||
MonitorEvent,
|
||||
MonitorIdentity,
|
||||
MonitorNote,
|
||||
MonitorOverview,
|
||||
MonitorRun,
|
||||
@@ -262,6 +263,28 @@ export const monitorApi = {
|
||||
clearCookie: (platform?: string) =>
|
||||
api.delete('/monitor/cookie', { params: { platform } }),
|
||||
|
||||
// --- 身份池(抖音多登录态,串行分片) ------------------------------------
|
||||
listIdentities: (platform?: string) =>
|
||||
api.get<{ identities: MonitorIdentity[] }>('/monitor/identities', {
|
||||
params: { platform },
|
||||
}),
|
||||
createIdentity: (payload: {
|
||||
label: string
|
||||
cookie: string
|
||||
user_agent?: string
|
||||
platform?: string
|
||||
}) => api.post<MonitorIdentity>('/monitor/identities', payload),
|
||||
updateIdentity: (
|
||||
id: number,
|
||||
payload: Partial<{ label: string; cookie: string; user_agent: string; enabled: boolean }>,
|
||||
) => api.patch<MonitorIdentity>(`/monitor/identities/${id}`, payload),
|
||||
deleteIdentity: (id: number) => api.delete(`/monitor/identities/${id}`),
|
||||
/** 真去请求一次,验证这份身份还能不能用。 */
|
||||
checkIdentity: (id: number) =>
|
||||
api.post<{ ok: boolean; verified: boolean; error: string }>(
|
||||
`/monitor/identities/${id}/check`,
|
||||
),
|
||||
|
||||
// task_id is repeated (?task_id=1&task_id=2) rather than comma-joined, so the
|
||||
// query string is built by hand instead of relying on axios array encoding,
|
||||
// which would emit "task_id[]=" that FastAPI does not match.
|
||||
|
||||
@@ -529,3 +529,25 @@ export interface UpstreamStatus {
|
||||
notified?: boolean
|
||||
notify_error?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 一份可用的采集身份(登录态)。
|
||||
*
|
||||
* **接口永远不返回 cookie 原文**,只有 `has_cookie` —— 这是从运营模块沿用下来的
|
||||
* 约定,别为了「方便」把它加回来。
|
||||
*/
|
||||
export interface MonitorIdentity {
|
||||
id: number
|
||||
platform: string
|
||||
label: string
|
||||
source: 'stored' | 'browser'
|
||||
has_cookie: boolean
|
||||
user_agent: string
|
||||
status: 'ok' | 'cooldown' | 'expired' | 'disabled'
|
||||
enabled: boolean
|
||||
consecutive_failures: number
|
||||
cooldown_until: number | null
|
||||
last_checked_at: number | null
|
||||
last_ok_at: number | null
|
||||
last_error: string | null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user