From 1645664f0354b46a718d81b6757c0ce79b3d95b9 Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Sat, 30 May 2026 15:17:16 +0200 Subject: [PATCH] refactor(frontend): move shared protocol enums to schemas/protocols/shared Decouple Outbound from Inbound schemas: SSMethodSchema and VmessSecuritySchema (shared between inbound & outbound) now live in a neutral schemas/protocols/shared/ module. Outbound no longer reaches into schemas/protocols/inbound/*. Pure relocation + import rewiring; schema values identical, snapshots & golden tests unchanged. --- frontend/src/lib/xray/inbound-link.ts | 2 +- .../pages/inbounds/form/InboundFormModal.tsx | 2 +- .../xray/outbounds/OutboundFormModal.tsx | 2 +- frontend/src/schemas/forms/outbound-form.ts | 4 ++-- .../schemas/protocols/inbound/shadowsocks.ts | 11 +---------- .../src/schemas/protocols/inbound/vmess.ts | 18 +----------------- .../schemas/protocols/outbound/shadowsocks.ts | 2 +- .../src/schemas/protocols/outbound/vmess.ts | 2 +- .../src/schemas/protocols/shared/index.ts | 2 ++ .../schemas/protocols/shared/shadowsocks.ts | 12 ++++++++++++ .../src/schemas/protocols/shared/vmess.ts | 19 +++++++++++++++++++ 11 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 frontend/src/schemas/protocols/shared/index.ts create mode 100644 frontend/src/schemas/protocols/shared/shadowsocks.ts create mode 100644 frontend/src/schemas/protocols/shared/vmess.ts diff --git a/frontend/src/lib/xray/inbound-link.ts b/frontend/src/lib/xray/inbound-link.ts index a30cf81f..314c125b 100644 --- a/frontend/src/lib/xray/inbound-link.ts +++ b/frontend/src/lib/xray/inbound-link.ts @@ -2,7 +2,7 @@ import { Base64, Wireguard } from '@/utils'; import type { Inbound } from '@/schemas/api/inbound'; import type { VlessClient } from '@/schemas/protocols/inbound/vless'; -import type { VmessSecurity } from '@/schemas/protocols/inbound/vmess'; +import type { VmessSecurity } from '@/schemas/protocols/shared/vmess'; import type { WireguardInboundPeer, WireguardInboundSettings, diff --git a/frontend/src/pages/inbounds/form/InboundFormModal.tsx b/frontend/src/pages/inbounds/form/InboundFormModal.tsx index 6216cd5e..563f3031 100644 --- a/frontend/src/pages/inbounds/form/InboundFormModal.tsx +++ b/frontend/src/pages/inbounds/form/InboundFormModal.tsx @@ -45,7 +45,7 @@ import { canEnableTls, isSS2022, } from '@/lib/xray/protocol-capabilities'; -import { SSMethodSchema } from '@/schemas/protocols/inbound/shadowsocks'; +import { SSMethodSchema } from '@/schemas/protocols/shared/shadowsocks'; import { getRandomRealityTarget } from '@/models/reality-targets'; import { InboundFormBaseSchema, diff --git a/frontend/src/pages/xray/outbounds/OutboundFormModal.tsx b/frontend/src/pages/xray/outbounds/OutboundFormModal.tsx index c56661c4..df2260bc 100644 --- a/frontend/src/pages/xray/outbounds/OutboundFormModal.tsx +++ b/frontend/src/pages/xray/outbounds/OutboundFormModal.tsx @@ -60,7 +60,7 @@ import { canEnableTls, canEnableTlsFlow, } from '@/lib/xray/protocol-capabilities'; -import { SSMethodSchema } from '@/schemas/protocols/inbound/shadowsocks'; +import { SSMethodSchema } from '@/schemas/protocols/shared/shadowsocks'; import { antdRule } from '@/utils/zodForm'; import './OutboundFormModal.css'; diff --git a/frontend/src/schemas/forms/outbound-form.ts b/frontend/src/schemas/forms/outbound-form.ts index eb73e423..0c6733dc 100644 --- a/frontend/src/schemas/forms/outbound-form.ts +++ b/frontend/src/schemas/forms/outbound-form.ts @@ -1,8 +1,8 @@ import { z } from 'zod'; import { PortSchema } from '@/schemas/primitives'; -import { VmessSecuritySchema } from '@/schemas/protocols/inbound/vmess'; -import { SSMethodSchema } from '@/schemas/protocols/inbound/shadowsocks'; +import { SSMethodSchema } from '@/schemas/protocols/shared/shadowsocks'; +import { VmessSecuritySchema } from '@/schemas/protocols/shared/vmess'; import { SecuritySettingsSchema } from '@/schemas/protocols/security'; import { NetworkSettingsSchema, StreamExtrasSchema } from '@/schemas/protocols/stream'; import { diff --git a/frontend/src/schemas/protocols/inbound/shadowsocks.ts b/frontend/src/schemas/protocols/inbound/shadowsocks.ts index e431a2e1..9a8e0316 100644 --- a/frontend/src/schemas/protocols/inbound/shadowsocks.ts +++ b/frontend/src/schemas/protocols/inbound/shadowsocks.ts @@ -1,15 +1,6 @@ import { z } from 'zod'; -export const SSMethodSchema = z.enum([ - 'aes-256-gcm', - 'chacha20-poly1305', - 'chacha20-ietf-poly1305', - 'xchacha20-ietf-poly1305', - '2022-blake3-aes-128-gcm', - '2022-blake3-aes-256-gcm', - '2022-blake3-chacha20-poly1305', -]); -export type SSMethod = z.infer; +import { SSMethodSchema } from '../shared/shadowsocks'; export const SSNetworkSchema = z.enum(['tcp', 'udp', 'tcp,udp']); export type SSNetwork = z.infer; diff --git a/frontend/src/schemas/protocols/inbound/vmess.ts b/frontend/src/schemas/protocols/inbound/vmess.ts index 2aef59e1..b16aded1 100644 --- a/frontend/src/schemas/protocols/inbound/vmess.ts +++ b/frontend/src/schemas/protocols/inbound/vmess.ts @@ -1,22 +1,6 @@ import { z } from 'zod'; -const VmessSecurityEnum = z.enum([ - 'aes-128-gcm', - 'chacha20-poly1305', - 'auto', - 'none', - 'zero', -]); - -// Legacy rows persisted `security: ""` (especially on VMess inbounds -// created before the enum was nailed down). Preprocess maps the empty -// string back to the documented default so existing data parses cleanly -// — subsequent writes serialize the normalized value. -export const VmessSecuritySchema = z.preprocess( - (val) => (val === '' ? 'auto' : val), - VmessSecurityEnum, -); -export type VmessSecurity = z.infer; +import { VmessSecuritySchema } from '../shared/vmess'; export const VmessClientSchema = z.object({ id: z.uuid(), diff --git a/frontend/src/schemas/protocols/outbound/shadowsocks.ts b/frontend/src/schemas/protocols/outbound/shadowsocks.ts index 64ed0094..5802e233 100644 --- a/frontend/src/schemas/protocols/outbound/shadowsocks.ts +++ b/frontend/src/schemas/protocols/outbound/shadowsocks.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; import { PortSchema } from '@/schemas/primitives'; -import { SSMethodSchema } from '@/schemas/protocols/inbound/shadowsocks'; +import { SSMethodSchema } from '@/schemas/protocols/shared/shadowsocks'; // Shadowsocks outbound persists as { servers: [{ ... }] }, with UDP-over-TCP // knobs (uot, UoTVersion) attached per-server when the user enabled them. diff --git a/frontend/src/schemas/protocols/outbound/vmess.ts b/frontend/src/schemas/protocols/outbound/vmess.ts index d381a913..6c82e939 100644 --- a/frontend/src/schemas/protocols/outbound/vmess.ts +++ b/frontend/src/schemas/protocols/outbound/vmess.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; import { PortSchema } from '@/schemas/primitives'; -import { VmessSecuritySchema } from '@/schemas/protocols/inbound/vmess'; +import { VmessSecuritySchema } from '@/schemas/protocols/shared/vmess'; // Vmess outbound persists in the standard Xray `vnext` shape: // { vnext: [{ address, port, users: [{ id, security }] }] } diff --git a/frontend/src/schemas/protocols/shared/index.ts b/frontend/src/schemas/protocols/shared/index.ts new file mode 100644 index 00000000..9ee79f3b --- /dev/null +++ b/frontend/src/schemas/protocols/shared/index.ts @@ -0,0 +1,2 @@ +export * from './shadowsocks'; +export * from './vmess'; diff --git a/frontend/src/schemas/protocols/shared/shadowsocks.ts b/frontend/src/schemas/protocols/shared/shadowsocks.ts new file mode 100644 index 00000000..227fc181 --- /dev/null +++ b/frontend/src/schemas/protocols/shared/shadowsocks.ts @@ -0,0 +1,12 @@ +import { z } from 'zod'; + +export const SSMethodSchema = z.enum([ + 'aes-256-gcm', + 'chacha20-poly1305', + 'chacha20-ietf-poly1305', + 'xchacha20-ietf-poly1305', + '2022-blake3-aes-128-gcm', + '2022-blake3-aes-256-gcm', + '2022-blake3-chacha20-poly1305', +]); +export type SSMethod = z.infer; diff --git a/frontend/src/schemas/protocols/shared/vmess.ts b/frontend/src/schemas/protocols/shared/vmess.ts new file mode 100644 index 00000000..1e273455 --- /dev/null +++ b/frontend/src/schemas/protocols/shared/vmess.ts @@ -0,0 +1,19 @@ +import { z } from 'zod'; + +const VmessSecurityEnum = z.enum([ + 'aes-128-gcm', + 'chacha20-poly1305', + 'auto', + 'none', + 'zero', +]); + +// Legacy rows persisted `security: ""` (especially on VMess inbounds +// created before the enum was nailed down). Preprocess maps the empty +// string back to the documented default so existing data parses cleanly +// — subsequent writes serialize the normalized value. +export const VmessSecuritySchema = z.preprocess( + (val) => (val === '' ? 'auto' : val), + VmessSecurityEnum, +); +export type VmessSecurity = z.infer;