import { useEffect, useState } from 'react'; import type { ProxyRoutePoWConfig } from '@/features/proxy-routes/types'; import { ResourceField, ResourceInput, ResourceTextarea, ToggleField, } from '@/features/shared/components/resource-primitives'; import { listToText, parseTextareaList } from './helpers'; export function PowTabPanel({ enabled, config, onChange, }: { enabled: boolean; config: ProxyRoutePoWConfig; onChange: (enabled: boolean, config: ProxyRoutePoWConfig) => void; }) { const [draft, setDraft] = useState(() => ({ whitelist: { ips: listToText(config.whitelist?.ips), ip_cidrs: listToText(config.whitelist?.ip_cidrs), paths: listToText(config.whitelist?.paths), path_regexes: listToText(config.whitelist?.path_regexes), user_agents: listToText(config.whitelist?.user_agents), }, blacklist: { ips: listToText(config.blacklist?.ips), ip_cidrs: listToText(config.blacklist?.ip_cidrs), paths: listToText(config.blacklist?.paths), path_regexes: listToText(config.blacklist?.path_regexes), user_agents: listToText(config.blacklist?.user_agents), }, })); useEffect(() => { setDraft({ whitelist: { ips: listToText(config.whitelist?.ips), ip_cidrs: listToText(config.whitelist?.ip_cidrs), paths: listToText(config.whitelist?.paths), path_regexes: listToText(config.whitelist?.path_regexes), user_agents: listToText(config.whitelist?.user_agents), }, blacklist: { ips: listToText(config.blacklist?.ips), ip_cidrs: listToText(config.blacklist?.ip_cidrs), paths: listToText(config.blacklist?.paths), path_regexes: listToText(config.blacklist?.path_regexes), user_agents: listToText(config.blacklist?.user_agents), }, }); }, [config]); const updateConfig = ( newEnabled: boolean, newConfig: Partial, newDraft?: typeof draft, ) => { const nextConfig = { ...config, ...newConfig }; if (newDraft) { setDraft(newDraft); nextConfig.whitelist = { ips: parseTextareaList(newDraft.whitelist.ips), ip_cidrs: parseTextareaList(newDraft.whitelist.ip_cidrs), paths: parseTextareaList(newDraft.whitelist.paths), path_regexes: parseTextareaList(newDraft.whitelist.path_regexes), user_agents: parseTextareaList(newDraft.whitelist.user_agents), }; nextConfig.blacklist = { ips: parseTextareaList(newDraft.blacklist.ips), ip_cidrs: parseTextareaList(newDraft.blacklist.ip_cidrs), paths: parseTextareaList(newDraft.blacklist.paths), path_regexes: parseTextareaList(newDraft.blacklist.path_regexes), user_agents: parseTextareaList(newDraft.blacklist.user_agents), }; } onChange(newEnabled, nextConfig); }; const updateList = ( scope: 'whitelist' | 'blacklist', key: keyof ProxyRoutePoWConfig['whitelist'], value: string, ) => { const nextDraft = { ...draft, [scope]: { ...draft[scope], [key]: value, }, }; updateConfig(enabled, {}, nextDraft); }; return (
updateConfig(newEnabled, {})} />
updateConfig(enabled, { difficulty: Number(event.target.value) }) } /> updateConfig(enabled, { session_ttl: Number(event.target.value) }) } /> updateConfig(enabled, { challenge_ttl: Number(event.target.value) }) } />
{(['whitelist', 'blacklist'] as const).map((scope) => (

{scope === 'whitelist' ? '白名单(跳过 PoW)' : '黑名单(必须 PoW)'}

updateList(scope, 'ips', event.target.value) } /> updateList(scope, 'ip_cidrs', event.target.value) } /> updateList(scope, 'paths', event.target.value) } /> updateList(scope, 'path_regexes', event.target.value) } /> updateList(scope, 'user_agents', event.target.value) } />
))}
); }