import { NativeSelect } from '@/components/form/NativeSelect'; import { useTranslation } from 'react-i18next'; import { AutoComplete, Input, InputNumber, Switch } from 'antd'; import { useFormContext, useWatch } from 'react-hook-form'; import { HeaderMapEditor } from '@/components/form'; import { FormField } from '@/components/form/rhf'; import { XHTTP_SESSION_ID_TABLES, XMUX_FRESH_DEFAULTS } from '@/schemas/protocols/stream/xhttp'; import { validateSessionIDLength, validateSessionIDTable } from '@/lib/xray/xhttp-session-id'; import { int32RangeUpper } from '@/lib/xray/stream-wire-normalize'; function antdValidatorToRhf(fn: (rule: unknown, value: unknown) => Promise) { return async (value: unknown): Promise => { try { await fn(undefined, value); return true; } catch (e) { return (e as Error).message; } }; } export default function XhttpForm() { const { t } = useTranslation(); const { control, getValues, setValue } = useFormContext(); const xhttpMode = useWatch({ control, name: 'streamSettings.xhttpSettings.mode' }) as | string | undefined; const xhttpObfsMode = !!useWatch({ control, name: 'streamSettings.xhttpSettings.xPaddingObfsMode', }); const xhttpSessionIDPlacement = useWatch({ control, name: 'streamSettings.xhttpSettings.sessionIDPlacement', }) as string | undefined; const xhttpSessionIDTable = useWatch({ control, name: 'streamSettings.xhttpSettings.sessionIDTable', }); const xhttpSeqPlacement = useWatch({ control, name: 'streamSettings.xhttpSettings.seqPlacement', }) as string | undefined; const xhttpUplinkPlacement = useWatch({ control, name: 'streamSettings.xhttpSettings.uplinkDataPlacement', }) as string | undefined; const enableXmux = !!useWatch({ control, name: 'streamSettings.xhttpSettings.enableXmux' }); function onXmuxToggle(checked: boolean) { if (!checked) return; const existing = getValues('streamSettings.xhttpSettings.xmux'); const hasValues = existing && typeof existing === 'object' && Object.keys(existing).length > 0; if (hasValues) return; setValue('streamSettings.xhttpSettings.xmux', { ...XMUX_FRESH_DEFAULTS }); } function onXmuxMaxConcurrencyChange(value: unknown) { if (int32RangeUpper(value) <= 0) return; if (int32RangeUpper(getValues('streamSettings.xhttpSettings.xmux.maxConnections')) > 0) { setValue('streamSettings.xhttpSettings.xmux.maxConnections', 0); } } function onXmuxMaxConnectionsChange(value: unknown) { if (int32RangeUpper(value) <= 0) return; if (int32RangeUpper(getValues('streamSettings.xhttpSettings.xmux.maxConcurrency')) > 0) { setValue('streamSettings.xhttpSettings.xmux.maxConcurrency', ''); } } return ( <> ({ value: m, label: m, }))} /> {(xhttpMode === 'packet-up' || xhttpMode === 'auto') && ( <> )} {xhttpMode === 'stream-up' && ( <> )} {xhttpObfsMode && ( <> )} {xhttpSessionIDPlacement && xhttpSessionIDPlacement !== 'path' && ( )} ({ value: v }))} placeholder="Base62" /> {!!xhttpSessionIDTable && ( )} {xhttpSeqPlacement && xhttpSeqPlacement !== 'path' && ( )} {xhttpMode === 'packet-up' && ( <> {xhttpUplinkPlacement && xhttpUplinkPlacement !== 'body' && ( )} )} {/* XMUX is the connection-multiplexing layer xHTTP uses to fan out parallel requests over a small pool of upstream connections. UI-only toggle (enableXmux) hides the 6 nested knobs when off. */} onXmuxToggle(v as boolean)} > {enableXmux && ( <> )} ); }