mirror of
https://github.com/VaalaCat/frp-panel.git
synced 2025-10-16 12:21:03 +08:00

refactor: change client manager structure [重构:更改客户端管理器结构适配影子客户端] feat: add proxy config table and dao [添加代理配置独立数据表和DAO层] feat: new proxy config entity [新建的代理配置实体] feat: update and delete proxy config [更新和删除代理配置接口] feat: get config api and beautify proxy item [更新获取配置API并美化代理项] feat: change proxy form style [美化修改代理的表单样式] fix: client edit [修复:编辑客户端的问题] fix: shadow copy status error [修复:影子客户端复制状态错误] fix: http proxy bug [修复:HTTP代理类型的错误] fix: cannot update client [修复:无法更新客户端] feat: record trigger refetch [自动重新获取表格内的数据] fix: filter string length [修复:过滤字符串长度] fix: add client error [修复:添加客户端错误] fix: do not notify client when stopped [修复:停止时不通知客户端] fix: delete when proxy duplicate [修复:代理重复时删除] feat: add http proxy location [添加HTTP代理路由路径] chore: edit style [编辑样式美化] fix: remove expired client [修复:自动移除过期客户端] feat: proxy status [新增代理状态提示] fix: build [修复:构建] fix: refetch trigger [修复:重新获取数据的问题] fix: remove all expired client [修复:移除所有过期客户端] feat: i18n for proxy [代理页面的国际化翻译]
146 lines
5.4 KiB
TypeScript
146 lines
5.4 KiB
TypeScript
"use client"
|
|
|
|
import { useEffect, useState } from 'react'
|
|
import { useMutation } from '@tanstack/react-query'
|
|
import { Label } from '@/components/ui/label'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Button } from '@/components/ui/button'
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from '@/components/ui/dialog'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { ServerSelector } from '../base/server-selector'
|
|
import { ClientSelector } from '../base/client-selector'
|
|
import { TypedProxyForm } from '../frpc/proxy_form'
|
|
import { ProxyType, TypedProxyConfig } from '@/types/proxy'
|
|
import { BaseSelector } from '../base/selector'
|
|
import { createProxyConfig } from '@/api/proxy'
|
|
import { ClientConfig } from '@/types/client'
|
|
import { ObjToUint8Array } from '@/lib/utils'
|
|
import { VisitPreview } from '../base/visit-preview'
|
|
import { ProxyConfig, Server } from '@/lib/pb/common'
|
|
import { TypedProxyConfigValid } from '@/lib/consts'
|
|
import { toast } from 'sonner'
|
|
import { $proxyTableRefetchTrigger } from '@/store/refetch-trigger'
|
|
|
|
export type ProxyConfigMutateDialogProps = {
|
|
overwrite?: boolean
|
|
defaultProxyConfig?: TypedProxyConfig
|
|
defaultOriginalProxyConfig?: ProxyConfig
|
|
disableChangeProxyName?: boolean
|
|
}
|
|
|
|
export const ProxyConfigMutateDialog = ({ ...props }: ProxyConfigMutateDialogProps) => {
|
|
const { t } = useTranslation()
|
|
|
|
return (
|
|
<Dialog>
|
|
<DialogTrigger asChild>
|
|
<Button variant="outline" className='w-fit'>
|
|
{t('proxy.config.create')}
|
|
</Button>
|
|
</DialogTrigger>
|
|
<DialogContent className='max-h-screen overflow-auto'>
|
|
<ProxyConfigMutateForm {...props} />
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
export const ProxyConfigMutateForm = ({ overwrite, defaultProxyConfig, defaultOriginalProxyConfig, disableChangeProxyName }: ProxyConfigMutateDialogProps) => {
|
|
const { t } = useTranslation()
|
|
const [newClientID, setNewClientID] = useState<string | undefined>()
|
|
const [newServerID, setNewServerID] = useState<string | undefined>()
|
|
const [proxyConfigs, setProxyConfigs] = useState<TypedProxyConfig[]>([])
|
|
const [proxyName, setProxyName] = useState<string | undefined>('')
|
|
const [proxyType, setProxyType] = useState<ProxyType>('http')
|
|
const [selectedServer, setSelectedServer] = useState<Server | undefined>()
|
|
const supportedProxyTypes: ProxyType[] = ["http", "tcp", "udp"]
|
|
|
|
const createProxyConfigMutation = useMutation({
|
|
mutationKey: ['createProxyConfig', newClientID, newServerID],
|
|
mutationFn: () => createProxyConfig({
|
|
clientId: newClientID!,
|
|
serverId: newServerID!,
|
|
config: ObjToUint8Array({
|
|
proxies: proxyConfigs
|
|
} as ClientConfig),
|
|
overwrite,
|
|
}),
|
|
onSuccess: () => {
|
|
toast(t('proxy.config.create_success'))
|
|
$proxyTableRefetchTrigger.set(Math.random())
|
|
},
|
|
onError: (e) => {
|
|
toast(t('proxy.config.create_failed'), {
|
|
description: JSON.stringify(e),
|
|
})
|
|
$proxyTableRefetchTrigger.set(Math.random())
|
|
}
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (proxyName && proxyType) {
|
|
setProxyConfigs([{ name: proxyName, type: proxyType }])
|
|
}
|
|
}, [proxyName, proxyType])
|
|
|
|
useEffect(() => {
|
|
if (defaultProxyConfig && defaultOriginalProxyConfig) {
|
|
setProxyConfigs([defaultProxyConfig])
|
|
setProxyType(defaultProxyConfig.type)
|
|
setProxyName(defaultProxyConfig.name)
|
|
setNewClientID(defaultOriginalProxyConfig.originClientId)
|
|
setNewServerID(defaultOriginalProxyConfig.serverId)
|
|
}
|
|
}, [defaultProxyConfig, defaultOriginalProxyConfig])
|
|
|
|
return (
|
|
<>
|
|
<Label>{t('proxy.config.select_server')} </Label>
|
|
<ServerSelector setServerID={setNewServerID} serverID={newServerID} setServer={setSelectedServer} />
|
|
<Label>{t('proxy.config.select_client')} </Label>
|
|
<ClientSelector setClientID={setNewClientID} clientID={newClientID} />
|
|
<Label>{t('proxy.config.select_proxy_type')} </Label>
|
|
<BaseSelector
|
|
dataList={supportedProxyTypes.map((type) => ({ value: type, label: type }))}
|
|
value={proxyType}
|
|
setValue={(value) => { setProxyType(value as ProxyType) }}
|
|
/>
|
|
<div className='flex flex-row w-full overflow-auto'>
|
|
{proxyConfigs && selectedServer && proxyConfigs.length > 0 &&
|
|
proxyConfigs[0] && TypedProxyConfigValid(proxyConfigs[0]) &&
|
|
<div className='flex flex-col'>
|
|
<VisitPreview server={selectedServer} typedProxyConfig={proxyConfigs[0]} />
|
|
</div>
|
|
}
|
|
</div>
|
|
<Label>{t('proxy.config.proxy_name')} </Label>
|
|
<Input className='text-sm' defaultValue={proxyName} onChange={(e) => setProxyName(e.target.value)} disabled={disableChangeProxyName} />
|
|
{proxyName && newClientID && newServerID && <TypedProxyForm
|
|
serverID={newServerID}
|
|
clientID={newClientID}
|
|
proxyName={proxyName}
|
|
defaultProxyConfig={proxyConfigs && proxyConfigs.length > 0 ? proxyConfigs[0] : undefined}
|
|
clientProxyConfigs={proxyConfigs}
|
|
setClientProxyConfigs={setProxyConfigs}
|
|
enablePreview={false}
|
|
/>}
|
|
<Button
|
|
disabled={!TypedProxyConfigValid(proxyConfigs[0])}
|
|
onClick={() => {
|
|
if (!TypedProxyConfigValid(proxyConfigs[0])) {
|
|
toast(t('proxy.config.invalid_config'))
|
|
return
|
|
}
|
|
createProxyConfigMutation.mutate()
|
|
}} >{t('proxy.config.submit')}</Button>
|
|
</>
|
|
)
|
|
} |