import { useState } from 'react'; import Link from 'next/link'; interface ProxyInfo { id: string; targetUrl: string; port: number; createdAt: Date | string; } interface ProxyListProps { proxies: ProxyInfo[]; onDelete?: (id: string) => void; onRefresh?: () => void; } export default function ProxyList({ proxies, onDelete, onRefresh }: ProxyListProps) { const [deletingId, setDeletingId] = useState(null); const handleDelete = async (id: string) => { setDeletingId(id); try { const response = await fetch(`/api/proxies/close/${id}`, { method: 'DELETE', }); const data = await response.json(); if (!response.ok) { if (response.status === 404) { // 代理不存在,刷新列表 if (onRefresh) onRefresh(); throw new Error('代理服务不存在或已被关闭'); } throw new Error(data.error || '关闭代理服务失败'); } if (onDelete) { onDelete(id); } // 显示成功消息 alert('代理服务已成功关闭'); } catch (error) { console.error('关闭代理服务失败:', error); const errorMessage = error instanceof Error ? error.message : '未知错误'; alert(`关闭代理服务失败: ${errorMessage}`); } finally { setDeletingId(null); } }; const getProxyAddress = (proxy: ProxyInfo) => { if (typeof window === 'undefined') { return `localhost:${proxy.port}`; } // The proxy listener speaks plain HTTP/TCP; use the management page origin // instead of the target's protocol (an HTTPS target is not an HTTPS listener). return `${window.location.protocol}//${window.location.hostname}:${proxy.port}`; }; if (!proxies || proxies.length === 0) { return (

代理服务列表

暂无代理服务。请创建一个新的代理服务。

{onRefresh && ( )}
); } return (

代理服务列表

{onRefresh && ( )}
{proxies.map((proxy) => ( ))}
ID 目标URL 代理地址 创建时间 操作
{proxy.id.substring(0, 8)}... {proxy.targetUrl} {getProxyAddress(proxy)} {new Date(proxy.createdAt).toLocaleString()}
查看日志
); }