mirror of
https://github.com/fatedier/frp.git
synced 2026-07-17 09:49:16 +08:00
feat(server): add typed frps v2 proxy specs (#5405)
This commit is contained in:
@@ -6,6 +6,9 @@ import type {
|
||||
ProxyListV2Params,
|
||||
ProxyStatsInfo,
|
||||
ProxyV2Info,
|
||||
ProxyV2Spec,
|
||||
ProxyV2SpecBlocks,
|
||||
ProxyV2Type,
|
||||
TrafficResponse,
|
||||
} from '../types/proxy'
|
||||
|
||||
@@ -39,19 +42,53 @@ export const getProxiesV2 = async (params: ProxyListV2Params = {}) => {
|
||||
}
|
||||
}
|
||||
|
||||
export const toLegacyProxyStats = (proxy: ProxyV2Info): ProxyStatsInfo => ({
|
||||
name: proxy.name,
|
||||
type: proxy.type,
|
||||
conf: proxy.spec,
|
||||
user: proxy.user,
|
||||
clientID: proxy.clientID,
|
||||
todayTrafficIn: proxy.status.todayTrafficIn,
|
||||
todayTrafficOut: proxy.status.todayTrafficOut,
|
||||
curConns: proxy.status.curConns,
|
||||
lastStartTime: formatUnixSeconds(proxy.status.lastStartAt),
|
||||
lastCloseTime: formatUnixSeconds(proxy.status.lastCloseAt),
|
||||
status: proxy.status.phase,
|
||||
})
|
||||
const getActiveProxySpec = (
|
||||
spec: ProxyV2Spec,
|
||||
): ProxyV2SpecBlocks[ProxyV2Type] => {
|
||||
switch (spec.type) {
|
||||
case 'tcp':
|
||||
return spec.tcp
|
||||
case 'udp':
|
||||
return spec.udp
|
||||
case 'http':
|
||||
return spec.http
|
||||
case 'https':
|
||||
return spec.https
|
||||
case 'tcpmux':
|
||||
return spec.tcpmux
|
||||
case 'stcp':
|
||||
return spec.stcp
|
||||
case 'sudp':
|
||||
return spec.sudp
|
||||
case 'xtcp':
|
||||
return spec.xtcp
|
||||
default:
|
||||
return assertNever(spec)
|
||||
}
|
||||
}
|
||||
|
||||
const assertNever = (value: never): never => {
|
||||
throw new Error(`Unsupported proxy spec: ${JSON.stringify(value)}`)
|
||||
}
|
||||
|
||||
export const toLegacyProxyStats = (proxy: ProxyV2Info): ProxyStatsInfo => {
|
||||
const type = proxy.spec.type
|
||||
const activeSpec = getActiveProxySpec(proxy.spec)
|
||||
|
||||
return {
|
||||
name: proxy.name,
|
||||
type,
|
||||
conf: proxy.status.phase === 'offline' ? null : activeSpec,
|
||||
user: proxy.user,
|
||||
clientID: proxy.clientID,
|
||||
todayTrafficIn: proxy.status.todayTrafficIn,
|
||||
todayTrafficOut: proxy.status.todayTrafficOut,
|
||||
curConns: proxy.status.curConns,
|
||||
lastStartTime: formatUnixSeconds(proxy.status.lastStartAt),
|
||||
lastCloseTime: formatUnixSeconds(proxy.status.lastCloseAt),
|
||||
status: proxy.status.phase,
|
||||
}
|
||||
}
|
||||
|
||||
export const getProxy = (type: string, name: string) => {
|
||||
return http.get<ProxyStatsInfo>(`../api/proxy/${type}/${name}`)
|
||||
|
||||
@@ -28,13 +28,84 @@ export interface ProxyListV2Params {
|
||||
|
||||
export interface ProxyV2Info {
|
||||
name: string
|
||||
type: string
|
||||
user: string
|
||||
clientID: string
|
||||
spec: any
|
||||
spec: ProxyV2Spec
|
||||
status: ProxyV2Status
|
||||
}
|
||||
|
||||
export interface ProxyV2BaseSpec {
|
||||
annotations?: Record<string, string>
|
||||
metadatas?: Record<string, string>
|
||||
transport?: {
|
||||
useEncryption: boolean
|
||||
useCompression: boolean
|
||||
bandwidthLimit: string
|
||||
bandwidthLimitMode: string
|
||||
}
|
||||
loadBalancer?: {
|
||||
group: string
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProxyV2TCPBlock extends ProxyV2BaseSpec {
|
||||
remotePort?: number
|
||||
}
|
||||
|
||||
export interface ProxyV2UDPBlock extends ProxyV2BaseSpec {
|
||||
remotePort?: number
|
||||
}
|
||||
|
||||
export interface ProxyV2HTTPBlock extends ProxyV2BaseSpec {
|
||||
customDomains?: string[]
|
||||
subdomain?: string
|
||||
locations?: string[]
|
||||
hostHeaderRewrite?: string
|
||||
}
|
||||
|
||||
export interface ProxyV2HTTPSBlock extends ProxyV2BaseSpec {
|
||||
customDomains?: string[]
|
||||
subdomain?: string
|
||||
}
|
||||
|
||||
export interface ProxyV2TCPMuxBlock extends ProxyV2BaseSpec {
|
||||
customDomains?: string[]
|
||||
subdomain?: string
|
||||
multiplexer?: string
|
||||
routeByHTTPUser?: string
|
||||
}
|
||||
|
||||
export type ProxyV2STCPBlock = ProxyV2BaseSpec
|
||||
|
||||
export type ProxyV2SUDPBlock = ProxyV2BaseSpec
|
||||
|
||||
export type ProxyV2XTCPBlock = ProxyV2BaseSpec
|
||||
|
||||
export interface ProxyV2SpecBlocks {
|
||||
tcp: ProxyV2TCPBlock
|
||||
udp: ProxyV2UDPBlock
|
||||
http: ProxyV2HTTPBlock
|
||||
https: ProxyV2HTTPSBlock
|
||||
tcpmux: ProxyV2TCPMuxBlock
|
||||
stcp: ProxyV2STCPBlock
|
||||
sudp: ProxyV2SUDPBlock
|
||||
xtcp: ProxyV2XTCPBlock
|
||||
}
|
||||
|
||||
export type ProxyV2Type = keyof ProxyV2SpecBlocks
|
||||
|
||||
type ProxyV2SpecFor<T extends ProxyV2Type> = {
|
||||
type: T
|
||||
} & {
|
||||
[K in T]: ProxyV2SpecBlocks[K]
|
||||
} & {
|
||||
[K in Exclude<ProxyV2Type, T>]?: never
|
||||
}
|
||||
|
||||
export type ProxyV2Spec = {
|
||||
[T in ProxyV2Type]: ProxyV2SpecFor<T>
|
||||
}[ProxyV2Type]
|
||||
|
||||
export interface ProxyV2Status {
|
||||
phase: 'online' | 'offline'
|
||||
todayTrafficIn: number
|
||||
|
||||
Reference in New Issue
Block a user