"use client" import { useEffect, useState, useCallback, useRef } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card" import { useToast } from "@/components/ui/use-toast" import { useRouter } from "next/navigation" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog" import { Switch } from "@/components/ui/switch" import { Slider } from "@/components/ui/slider" import { Plus, Trash2, Edit, Save, Download, Upload } from "lucide-react" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog" interface PathMapping { DefaultTarget: string ExtensionMap?: Record SizeThreshold?: number // 最小文件大小阈值 MaxSize?: number // 最大文件大小阈值 } interface CompressionConfig { Enabled: boolean Level: number } interface Config { MAP: Record Compression: { Gzip: CompressionConfig Brotli: CompressionConfig } MetricsSaveInterval?: number // 指标保存间隔(分钟) MetricsMaxFiles?: number // 保留的最大统计文件数量 } export default function ConfigPage() { const [config, setConfig] = useState(null) const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const { toast } = useToast() const router = useRouter() // 使用 ref 来保存滚动位置 const scrollPositionRef = useRef(0) // 对话框状态 const [pathDialogOpen, setPathDialogOpen] = useState(false) const [newPathData, setNewPathData] = useState({ path: "", defaultTarget: "", extensionMap: {} as Record, sizeThreshold: 0, maxSize: 0, sizeThresholdUnit: 'MB' as 'B' | 'KB' | 'MB' | 'GB', maxSizeUnit: 'MB' as 'B' | 'KB' | 'MB' | 'GB', }) const [extensionMapDialogOpen, setExtensionMapDialogOpen] = useState(false) const [editingPath, setEditingPath] = useState(null) const [editingExtension, setEditingExtension] = useState<{ext: string, target: string} | null>(null) const [newExtension, setNewExtension] = useState({ ext: "", target: "" }) const [editingPathData, setEditingPathData] = useState<{ path: string; defaultTarget: string; sizeThreshold: number; maxSize: number; sizeThresholdUnit: 'B' | 'KB' | 'MB' | 'GB'; maxSizeUnit: 'B' | 'KB' | 'MB' | 'GB'; } | null>(null); const [deletingPath, setDeletingPath] = useState(null) const [deletingExtension, setDeletingExtension] = useState<{path: string, ext: string} | null>(null) const fetchConfig = useCallback(async () => { try { const token = localStorage.getItem("token") if (!token) { router.push("/login") return } const response = await fetch("/admin/api/config/get", { headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' } }) if (response.status === 401) { localStorage.removeItem("token") router.push("/login") return } if (!response.ok) { throw new Error("获取配置失败") } const data = await response.json() // 设置默认值 if (data.MetricsSaveInterval === undefined || data.MetricsSaveInterval === 0) { data.MetricsSaveInterval = 15; // 默认15分钟 } if (data.MetricsMaxFiles === undefined || data.MetricsMaxFiles === 0) { data.MetricsMaxFiles = 10; // 默认10个文件 } setConfig(data) } catch (error) { const message = error instanceof Error ? error.message : "获取配置失败" toast({ title: "错误", description: message, variant: "destructive", }) } finally { setLoading(false) } }, [router, toast]) useEffect(() => { fetchConfig() }, [fetchConfig]) const handleSave = async () => { if (!config) return setSaving(true) try { const token = localStorage.getItem("token") if (!token) { router.push("/login") return } const response = await fetch("/admin/api/config/save", { method: "POST", headers: { "Content-Type": "application/json", 'Authorization': `Bearer ${token}` }, body: JSON.stringify(config), }) if (response.status === 401) { localStorage.removeItem("token") router.push("/login") return } if (!response.ok) { const data = await response.json().catch(() => ({})) throw new Error(data.message || "保存配置失败") } toast({ title: "成功", description: "配置已保存", }) } catch (error) { toast({ title: "错误", description: error instanceof Error ? error.message : "保存配置失败", variant: "destructive", }) } finally { setSaving(false) } } // 处理对话框打开和关闭时的滚动位置 const handleDialogOpenChange = useCallback((open: boolean, handler: (open: boolean) => void) => { if (open) { // 对话框打开时,保存当前滚动位置 scrollPositionRef.current = window.scrollY } else { // 对话框关闭时,恢复滚动位置 handler(open) requestAnimationFrame(() => { window.scrollTo(0, scrollPositionRef.current) }) } }, []) const handlePathDialogOpenChange = useCallback((open: boolean) => { handleDialogOpenChange(open, (isOpen) => { setPathDialogOpen(isOpen) if (!isOpen) { setEditingPathData(null) setNewPathData({ path: "", defaultTarget: "", extensionMap: {}, sizeThreshold: 0, maxSize: 0, sizeThresholdUnit: 'MB', maxSizeUnit: 'MB', }) } }) }, [handleDialogOpenChange]) const handleExtensionMapDialogOpenChange = useCallback((open: boolean) => { handleDialogOpenChange(open, (isOpen) => { setExtensionMapDialogOpen(isOpen) if (!isOpen) { setEditingPath(null) setEditingExtension(null) setNewExtension({ ext: "", target: "" }) } }) }, [handleDialogOpenChange]) const addOrUpdatePath = () => { if (!config) return const data = editingPathData || newPathData const { path, defaultTarget, sizeThreshold, maxSize, sizeThresholdUnit, maxSizeUnit } = data if (!path || !defaultTarget) { toast({ title: "错误", description: "路径和默认目标不能为空", variant: "destructive", }) return } // 转换大小为字节 const sizeThresholdBytes = convertToBytes(sizeThreshold, sizeThresholdUnit) const maxSizeBytes = convertToBytes(maxSize, maxSizeUnit) // 验证阈值 if (maxSizeBytes > 0 && sizeThresholdBytes >= maxSizeBytes) { toast({ title: "错误", description: "最大文件大小阈值必须大于最小文件大小阈值", variant: "destructive", }) return } const newConfig = { ...config } const pathConfig: PathMapping = { DefaultTarget: defaultTarget, ExtensionMap: {}, SizeThreshold: sizeThresholdBytes, MaxSize: maxSizeBytes } // 如果是编辑现有路径,保留原有的扩展名映射 if (editingPathData && typeof config.MAP[path] === 'object') { const existingConfig = config.MAP[path] as PathMapping pathConfig.ExtensionMap = existingConfig.ExtensionMap } newConfig.MAP[path] = pathConfig setConfig(newConfig) if (editingPathData) { setEditingPathData(null) } else { setNewPathData({ path: "", defaultTarget: "", extensionMap: {}, sizeThreshold: 0, maxSize: 0, sizeThresholdUnit: 'MB', maxSizeUnit: 'MB', }) } setPathDialogOpen(false) toast({ title: "成功", description: `${editingPathData ? '更新' : '添加'}路径配置成功`, }) } const deletePath = (path: string) => { setDeletingPath(path) } const confirmDeletePath = () => { if (!config || !deletingPath) return const newConfig = { ...config } delete newConfig.MAP[deletingPath] setConfig(newConfig) setDeletingPath(null) toast({ title: "成功", description: "路径映射已删除", }) } const updateCompression = (type: 'Gzip' | 'Brotli', field: 'Enabled' | 'Level', value: boolean | number) => { if (!config) return const newConfig = { ...config } if (field === 'Enabled') { newConfig.Compression[type].Enabled = value as boolean } else { newConfig.Compression[type].Level = value as number } setConfig(newConfig) } const updateMetricsSettings = (field: 'MetricsSaveInterval' | 'MetricsMaxFiles', value: number) => { if (!config) return const newConfig = { ...config } newConfig[field] = value setConfig(newConfig) } const handleExtensionMapEdit = (path: string, ext?: string, target?: string) => { setEditingPath(path) if (ext && target) { setEditingExtension({ ext, target }) setNewExtension({ ext, target }) } else { setEditingExtension(null) setNewExtension({ ext: "", target: "" }) } setExtensionMapDialogOpen(true) } const addOrUpdateExtensionMap = () => { if (!config || !editingPath) return const { ext, target } = newExtension // 验证输入 if (!ext.trim() || !target.trim()) { toast({ title: "错误", description: "扩展名和目标不能为空", variant: "destructive", }) return } // 验证扩展名格式 const extensions = ext.split(',').map(e => e.trim()) if (extensions.some(e => !e || e.includes('.'))) { toast({ title: "错误", description: "扩展名格式不正确,不需要包含点号", variant: "destructive", }) return } // 验证URL格式 try { new URL(target) } catch { toast({ title: "错误", description: "目标URL格式不正确", variant: "destructive", }) return } const newConfig = { ...config } const mapping = newConfig.MAP[editingPath] if (typeof mapping === "string") { newConfig.MAP[editingPath] = { DefaultTarget: mapping, ExtensionMap: { [ext]: target } } } else { // 如果是编辑现有的扩展名映射,先删除旧的 if (editingExtension) { const newExtMap = { ...mapping.ExtensionMap } delete newExtMap[editingExtension.ext] mapping.ExtensionMap = newExtMap } // 添加新的映射 mapping.ExtensionMap = { ...mapping.ExtensionMap, [ext]: target } } setConfig(newConfig) setExtensionMapDialogOpen(false) setEditingExtension(null) setNewExtension({ ext: "", target: "" }) toast({ title: "成功", description: "扩展名映射已更新", }) } const deleteExtensionMap = (path: string, ext: string) => { setDeletingExtension({ path, ext }) } const confirmDeleteExtensionMap = () => { if (!config || !deletingExtension) return const newConfig = { ...config } const mapping = newConfig.MAP[deletingExtension.path] if (typeof mapping !== "string" && mapping.ExtensionMap) { const newExtensionMap = { ...mapping.ExtensionMap } delete newExtensionMap[deletingExtension.ext] mapping.ExtensionMap = newExtensionMap } setConfig(newConfig) setDeletingExtension(null) toast({ title: "成功", description: "扩展名映射已删除", }) } const openAddPathDialog = () => { setEditingPathData(null) setNewPathData({ path: "", defaultTarget: "", extensionMap: {}, sizeThreshold: 0, maxSize: 0, sizeThresholdUnit: 'MB', maxSizeUnit: 'MB', }) setPathDialogOpen(true) } const exportConfig = () => { if (!config) return const blob = new Blob([JSON.stringify(config, null, 2)], { type: 'application/json' }) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = 'proxy-config.json' document.body.appendChild(a) a.click() document.body.removeChild(a) URL.revokeObjectURL(url) } const importConfig = (event: React.ChangeEvent) => { const file = event.target.files?.[0] if (!file) return const reader = new FileReader() reader.onload = (e) => { try { const content = e.target?.result as string const newConfig = JSON.parse(content) // 验证配置结构 if (!newConfig.MAP || typeof newConfig.MAP !== 'object') { throw new Error('配置文件缺少 MAP 字段或格式不正确') } if (!newConfig.Compression || typeof newConfig.Compression !== 'object' || !newConfig.Compression.Gzip || !newConfig.Compression.Brotli) { throw new Error('配置文件压缩设置格式不正确') } // 验证路径映射 for (const [path, target] of Object.entries(newConfig.MAP)) { if (!path.startsWith('/')) { throw new Error(`路径 ${path} 必须以/开头`) } if (typeof target === 'string') { try { new URL(target) } catch { throw new Error(`路径 ${path} 的目标URL格式不正确`) } } else if (target && typeof target === 'object') { const mapping = target as PathMapping if (!mapping.DefaultTarget || typeof mapping.DefaultTarget !== 'string') { throw new Error(`路径 ${path} 的默认目标格式不正确`) } try { new URL(mapping.DefaultTarget) } catch { throw new Error(`路径 ${path} 的默认目标URL格式不正确`) } } else { throw new Error(`路径 ${path} 的目标格式不正确`) } } setConfig(newConfig) toast({ title: "成功", description: "配置已导入", }) } catch (error) { toast({ title: "错误", description: error instanceof Error ? error.message : "配置文件格式错误", variant: "destructive", }) } } reader.readAsText(file) } const handleEditPath = (path: string, target: PathMapping | string) => { if (typeof target === 'string') { setEditingPathData({ path, defaultTarget: target, sizeThreshold: 0, maxSize: 0, sizeThresholdUnit: 'MB', maxSizeUnit: 'MB' }) } else { const { value: thresholdValue, unit: thresholdUnit } = convertBytesToUnit(target.SizeThreshold || 0) const { value: maxValue, unit: maxUnit } = convertBytesToUnit(target.MaxSize || 0) setEditingPathData({ path, defaultTarget: target.DefaultTarget, sizeThreshold: thresholdValue, maxSize: maxValue, sizeThresholdUnit: thresholdUnit, maxSizeUnit: maxUnit }) } setPathDialogOpen(true) } // 处理删除对话框的滚动位置 const handleDeleteDialogOpenChange = useCallback((open: boolean, setter: (value: null) => void) => { if (open) { scrollPositionRef.current = window.scrollY } else { setter(null) requestAnimationFrame(() => { window.scrollTo(0, scrollPositionRef.current) }) } }, []) if (loading) { return (
加载中...
正在获取配置数据
) } return (
Proxy Go配置
路径映射 压缩设置 指标设置
{editingPathData ? "编辑路径映射" : "添加路径映射"}
editingPathData ? setEditingPathData({ ...editingPathData, path: e.target.value }) : setNewPathData({ ...newPathData, path: e.target.value }) } placeholder="/example" />

请输入需要代理的路径

editingPathData ? setEditingPathData({ ...editingPathData, defaultTarget: e.target.value }) : setNewPathData({ ...newPathData, defaultTarget: e.target.value }) } placeholder="https://example.com" />

默认的回源地址,所有请求都会转发到这个地址

{ if (editingPathData) { setEditingPathData({ ...editingPathData, sizeThreshold: Number(e.target.value), }) } else { setNewPathData({ ...newPathData, sizeThreshold: Number(e.target.value), }) } }} />
{ if (editingPathData) { setEditingPathData({ ...editingPathData, maxSize: Number(e.target.value), }) } else { setNewPathData({ ...newPathData, maxSize: Number(e.target.value), }) } }} />
路径 默认目标 最小阈值 最大阈值 扩展名映射 操作 {config && Object.entries(config.MAP).map(([path, target]) => ( <> {path} {typeof target === 'string' ? target : target.DefaultTarget} {typeof target === 'object' && target.SizeThreshold ? ( {formatBytes(target.SizeThreshold)} ) : '-'} {typeof target === 'object' && target.MaxSize ? ( {formatBytes(target.MaxSize)} ) : '-'}
{typeof target === 'object' && target.ExtensionMap && Object.keys(target.ExtensionMap).length > 0 && (
扩展名 目标地址 操作 {Object.entries(target.ExtensionMap).map(([ext, url]) => ( {ext} {truncateUrl(url)}
))}
)} ))} Gzip 压缩
updateCompression('Gzip', 'Enabled', checked)} />
updateCompression('Gzip', 'Level', value[0])} />
Brotli 压缩
updateCompression('Brotli', 'Enabled', checked)} />
updateCompression('Brotli', 'Level', value[0])} />
指标设置 配置指标收集和保存的相关设置
updateMetricsSettings('MetricsSaveInterval', parseInt(e.target.value) || 15)} className="w-24" /> 分钟(默认:15分钟)

系统将按此间隔定期保存统计数据到文件

updateMetricsSettings('MetricsMaxFiles', parseInt(e.target.value) || 10)} className="w-24" /> 个文件(默认:10个)

超过此数量的旧统计文件将被自动清理

{editingExtension ? "编辑扩展名映射" : "添加扩展名映射"}
setNewExtension({ ...newExtension, ext: e.target.value })} placeholder="jpg,png,webp" />

多个扩展名用逗号分隔,不需要包含点号

setNewExtension({ ...newExtension, target: e.target.value })} placeholder="https://example.com" />

当文件大小超过阈值且扩展名匹配时,将使用此地址

handleDeleteDialogOpenChange(open, setDeletingPath)} > 确认删除 确定要删除路径 “{deletingPath}” 的映射吗?此操作无法撤销。 取消 删除 handleDeleteDialogOpenChange(open, setDeletingExtension)} > 确认删除 确定要删除扩展名 “{deletingExtension?.ext}” 的映射吗?此操作无法撤销。 取消 删除 ) } // 辅助函数:格式化字节大小 const formatBytes = (bytes: number) => { if (bytes === 0) return '0 B' const k = 1024 const sizes = ['B', 'KB', 'MB', 'GB'] const i = Math.floor(Math.log(bytes) / Math.log(k)) return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}` } // 辅助函数:截断 URL const truncateUrl = (url: string) => { if (url.length > 30) { return url.substring(0, 27) + '...' } return url } // 辅助函数:单位转换 const convertToBytes = (value: number, unit: 'B' | 'KB' | 'MB' | 'GB'): number => { if (value < 0) return 0 const multipliers = { 'B': 1, 'KB': 1024, 'MB': 1024 * 1024, 'GB': 1024 * 1024 * 1024 } return Math.floor(value * multipliers[unit]) } const convertBytesToUnit = (bytes: number): { value: number, unit: 'B' | 'KB' | 'MB' | 'GB' } => { if (bytes <= 0) return { value: 0, unit: 'MB' } const k = 1024 const sizes: Array<'B' | 'KB' | 'MB' | 'GB'> = ['B', 'KB', 'MB', 'GB'] const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1) return { value: Number((bytes / Math.pow(k, i)).toFixed(2)), unit: sizes[i] } }