'use client'; import { useState, useEffect } from 'react'; import { X, Plus, FileText, Sparkles, Check, AlertCircle } from 'lucide-react'; import { toast } from 'react-hot-toast'; import { proxyService } from '@/lib/services'; import { ProxyStatus } from '@/types'; interface ProxyAddModalProps { isOpen: boolean; onClose: () => void; onSuccess: () => void; existingPools?: string[]; } const EMPTY_POOLS: string[] = []; interface ParsedProxy { pool_name: string; proto: string; ip: string; port: number; username?: string | null; password?: string | null; status: ProxyStatus; } export default function ProxyAddModal({ isOpen, onClose, onSuccess, existingPools = EMPTY_POOLS, }: ProxyAddModalProps) { const [mode, setMode] = useState<'single' | 'batch'>('single'); // 单条表单状态 const [singleForm, setSingleForm] = useState({ pool_name: '', proto: 'http', ip: '', port: 8080, username: '', password: '', status: 'active' as ProxyStatus, }); // 批量导入状态 const [batchPool, setBatchPool] = useState(''); const [batchDefaultProto, setBatchDefaultProto] = useState('http'); const [batchDefaultStatus, setBatchDefaultStatus] = useState('active'); const [batchText, setBatchText] = useState(''); const [submitting, setSubmitting] = useState(false); useEffect(() => { if (isOpen) { const defaultPool = existingPools.length > 0 ? existingPools[0] : ''; setSingleForm({ pool_name: defaultPool, proto: 'http', ip: '', port: 8080, username: '', password: '', status: 'active', }); setBatchPool(defaultPool); setBatchDefaultProto('http'); setBatchDefaultStatus('active'); setBatchText(''); setMode('single'); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [isOpen]); if (!isOpen) return null; const parseBatchText = (): ParsedProxy[] => { if (!batchText.trim()) return []; const lines = batchText.split('\n'); const result: ParsedProxy[] = []; const poolName = batchPool.trim() || 'default'; for (const rawLine of lines) { const line = rawLine.trim(); if (!line || line.startsWith('#') || line.startsWith('//')) continue; try { if (line.includes('://')) { const url = new URL(line); const proto = url.protocol.replace(':', ''); const ip = url.hostname; const port = Number(url.port) || (proto === 'https' ? 443 : 80); const username = url.username ? decodeURIComponent(url.username) : null; const password = url.password ? decodeURIComponent(url.password) : null; if (ip && port > 0) { result.push({ pool_name: poolName, proto, ip, port, username, password, status: batchDefaultStatus, }); continue; } } const parts = line.split(':'); if (parts.length >= 2) { const ip = parts[0].trim(); const port = Number(parts[1].trim()); const username = parts.length >= 3 ? parts[2].trim() : null; const password = parts.length >= 4 ? parts[3].trim() : null; if (ip && port > 0 && !isNaN(port)) { result.push({ pool_name: poolName, proto: batchDefaultProto, ip, port, username: username || null, password: password || null, status: batchDefaultStatus, }); } } } catch { // ignore parse line error } } return result; }; const parsedBatchList = parseBatchText(); const handleSingleSubmit = async () => { if (!singleForm.pool_name.trim()) { toast.error('请填写代理池名称'); return; } if (!singleForm.ip.trim()) { toast.error('请填写代理 IP 地址'); return; } if (!singleForm.port || singleForm.port <= 0 || singleForm.port > 65535) { toast.error('端口号必须在 1-65535 之间'); return; } setSubmitting(true); try { const res = await proxyService.create({ pool_name: singleForm.pool_name.trim(), proto: singleForm.proto, ip: singleForm.ip.trim(), port: Number(singleForm.port), username: singleForm.username.trim() || undefined, password: singleForm.password.trim() || undefined, status: singleForm.status, }); if (res?.code === 0 || res?.data) { toast.success(`代理 ${singleForm.ip}:${singleForm.port} 添加成功`); onSuccess(); onClose(); } else { toast.error(res?.msg || '添加失败'); } } catch (e: any) { toast.error('添加失败: ' + (e.response?.data?.msg || e.message || '网络错误')); } finally { setSubmitting(false); } }; const handleBatchSubmit = async () => { if (parsedBatchList.length === 0) { toast.error('未能从文本中解析出有效的代理记录'); return; } setSubmitting(true); let successCount = 0; let failCount = 0; try { for (const item of parsedBatchList) { try { const res = await proxyService.create({ pool_name: item.pool_name, proto: item.proto, ip: item.ip, port: item.port, username: item.username || undefined, password: item.password || undefined, status: item.status, }); if (res?.code === 0 || res?.data) { successCount++; } else { failCount++; } } catch { failCount++; } } toast.success(`批量导入完成: 成功 ${successCount} 个, 失败 ${failCount} 个`); onSuccess(); onClose(); } catch { toast.error('导入过程中发生异常'); } finally { setSubmitting(false); } }; return (
{/* Header */}

[ ADD PROXY NODE // 登记代理出口节点 ]

{/* Mode Tabs */}
{/* Body */}
{mode === 'single' ? ( <> {/* Pool & Status */}
setSingleForm({ ...singleForm, pool_name: e.target.value })} /> {existingPools.map((p) => (
{/* Protocol, IP & Port */}
setSingleForm({ ...singleForm, ip: e.target.value })} />
setSingleForm({ ...singleForm, port: Number(e.target.value) })} />
{/* Username & Password */}
setSingleForm({ ...singleForm, username: e.target.value })} />
setSingleForm({ ...singleForm, password: e.target.value })} />
) : ( /* 批量文本导入模式 */
setBatchPool(e.target.value)} /> {existingPools.map((p) => (
已识别: {parsedBatchList.length} 条