RuleFormModal.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. import { useEffect, useMemo, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { Button, Form, Input, Modal, Select, Space, Tooltip } from 'antd';
  4. import { PlusOutlined, MinusOutlined, QuestionCircleOutlined } from '@ant-design/icons';
  5. import { InputAddon } from '@/components/ui';
  6. import { useInboundOptions } from '@/api/queries/useInboundOptions';
  7. import { RuleFormSchema, type RuleFormValues } from '@/schemas/xray';
  8. import { buildRemarkByTag, formatInboundTag } from './helpers';
  9. export interface RoutingRule {
  10. type?: string;
  11. domain?: string | string[];
  12. ip?: string | string[];
  13. port?: string;
  14. sourcePort?: string;
  15. vlessRoute?: string;
  16. network?: string;
  17. sourceIP?: string | string[];
  18. user?: string | string[];
  19. inboundTag?: string[];
  20. protocol?: string[];
  21. attrs?: Record<string, string>;
  22. outboundTag?: string;
  23. balancerTag?: string;
  24. [key: string]: unknown;
  25. }
  26. interface RuleFormModalProps {
  27. open: boolean;
  28. rule: RoutingRule | null;
  29. inboundTags: string[];
  30. outboundTags: string[];
  31. balancerTags: string[];
  32. onClose: () => void;
  33. onConfirm: (rule: Record<string, unknown>) => void;
  34. }
  35. type FormState = RuleFormValues;
  36. const initialForm = (): FormState => ({
  37. domain: '',
  38. ip: '',
  39. port: '',
  40. sourcePort: '',
  41. vlessRoute: '',
  42. network: '',
  43. sourceIP: '',
  44. user: '',
  45. inboundTag: [],
  46. protocol: [],
  47. attrs: [],
  48. outboundTag: '',
  49. balancerTag: '',
  50. });
  51. const NETWORKS = ['', 'TCP', 'UDP', 'TCP,UDP'];
  52. const PROTOCOLS = ['http', 'tls', 'bittorrent', 'quic'];
  53. function csv(value: string): string[] {
  54. if (!value) return [];
  55. return value.split(',').map((s) => s.trim()).filter(Boolean);
  56. }
  57. export default function RuleFormModal({
  58. open,
  59. rule,
  60. inboundTags,
  61. outboundTags,
  62. balancerTags,
  63. onClose,
  64. onConfirm,
  65. }: RuleFormModalProps) {
  66. const { t } = useTranslation();
  67. const [form, setForm] = useState<FormState>(initialForm);
  68. const isEdit = rule != null;
  69. const { data: inboundOptions } = useInboundOptions();
  70. const remarkByTag = useMemo(() => buildRemarkByTag(inboundOptions || []), [inboundOptions]);
  71. useEffect(() => {
  72. if (!open) return;
  73. if (rule) {
  74. setForm({
  75. domain: Array.isArray(rule.domain) ? rule.domain.join(',') : rule.domain || '',
  76. ip: Array.isArray(rule.ip) ? rule.ip.join(',') : rule.ip || '',
  77. port: rule.port || '',
  78. sourcePort: rule.sourcePort || '',
  79. vlessRoute: rule.vlessRoute || '',
  80. network: rule.network || '',
  81. sourceIP: Array.isArray(rule.sourceIP) ? rule.sourceIP.join(',') : rule.sourceIP || '',
  82. user: Array.isArray(rule.user) ? rule.user.join(',') : rule.user || '',
  83. inboundTag: rule.inboundTag || [],
  84. protocol: rule.protocol || [],
  85. attrs: rule.attrs ? Object.entries(rule.attrs) : [],
  86. outboundTag: rule.outboundTag || '',
  87. balancerTag: rule.balancerTag || '',
  88. });
  89. } else {
  90. setForm(initialForm());
  91. }
  92. }, [open, rule]);
  93. const update = <K extends keyof FormState>(key: K, value: FormState[K]) =>
  94. setForm((prev) => ({ ...prev, [key]: value }));
  95. function submit() {
  96. const validated = RuleFormSchema.safeParse(form);
  97. if (!validated.success) return;
  98. const v = validated.data;
  99. const built: Record<string, unknown> = {
  100. type: 'field',
  101. domain: csv(v.domain),
  102. ip: csv(v.ip),
  103. port: v.port,
  104. sourcePort: v.sourcePort,
  105. vlessRoute: v.vlessRoute,
  106. network: v.network,
  107. sourceIP: csv(v.sourceIP),
  108. user: csv(v.user),
  109. inboundTag: v.inboundTag,
  110. protocol: v.protocol,
  111. attrs: Object.fromEntries(v.attrs.filter(([k]) => k)),
  112. outboundTag: v.outboundTag === '' ? undefined : v.outboundTag,
  113. balancerTag: v.balancerTag === '' ? undefined : v.balancerTag,
  114. };
  115. const out: Record<string, unknown> = {};
  116. for (const [k, v] of Object.entries(built)) {
  117. if (v == null) continue;
  118. if (Array.isArray(v) && v.length === 0) continue;
  119. if (typeof v === 'object' && !Array.isArray(v) && Object.keys(v).length === 0) continue;
  120. if (v === '') continue;
  121. out[k] = v;
  122. }
  123. onConfirm(out);
  124. }
  125. const title = isEdit
  126. ? `${t('edit')} ${t('pages.xray.Routings')}`
  127. : `+ ${t('pages.xray.Routings')}`;
  128. const okText = isEdit ? t('pages.clients.submitEdit') : t('create');
  129. return (
  130. <Modal
  131. open={open}
  132. title={title}
  133. okText={okText}
  134. cancelText={t('close')}
  135. mask={{ closable: false }}
  136. width={640}
  137. onOk={submit}
  138. onCancel={onClose}
  139. >
  140. <Form colon={false} labelCol={{ md: { span: 8 } }} wrapperCol={{ md: { span: 14 } }}>
  141. <Form.Item
  142. label={
  143. <Tooltip title={t('pages.xray.rules.useComma')}>
  144. {t('pages.xray.ruleForm.sourceIps')} <QuestionCircleOutlined />
  145. </Tooltip>
  146. }
  147. >
  148. <Input value={form.sourceIP} onChange={(e) => update('sourceIP', e.target.value)} placeholder="0.0.0.0/8, fc00::/7, geoip:ir" />
  149. </Form.Item>
  150. <Form.Item
  151. label={
  152. <Tooltip title={t('pages.xray.rules.useComma')}>
  153. {t('pages.xray.ruleForm.sourcePort')} <QuestionCircleOutlined />
  154. </Tooltip>
  155. }
  156. >
  157. <Input value={form.sourcePort} onChange={(e) => update('sourcePort', e.target.value)} placeholder="53,443,1000-2000" />
  158. </Form.Item>
  159. <Form.Item
  160. label={
  161. <Tooltip title={t('pages.xray.rules.useComma')}>
  162. {t('pages.xray.ruleForm.vlessRoute')} <QuestionCircleOutlined />
  163. </Tooltip>
  164. }
  165. >
  166. <Input value={form.vlessRoute} onChange={(e) => update('vlessRoute', e.target.value)} placeholder="53,443,1000-2000" />
  167. </Form.Item>
  168. <Form.Item label={t('pages.inbounds.network')}>
  169. <Select
  170. value={form.network}
  171. onChange={(v) => update('network', v)}
  172. options={NETWORKS.map((n) => ({ value: n, label: n || '(any)' }))}
  173. />
  174. </Form.Item>
  175. <Form.Item label={t('pages.inbounds.protocol')}>
  176. <Select
  177. mode="multiple"
  178. value={form.protocol}
  179. onChange={(v) => update('protocol', v)}
  180. options={PROTOCOLS.map((p) => ({ value: p, label: p }))}
  181. />
  182. </Form.Item>
  183. <Form.Item label={t('pages.xray.ruleForm.attributes')}>
  184. <Button size="small" icon={<PlusOutlined />} onClick={() => update('attrs', [...form.attrs, ['', '']])} />
  185. </Form.Item>
  186. <Form.Item wrapperCol={{ span: 24 }}>
  187. {form.attrs.map((attr, idx) => (
  188. <Space.Compact key={idx} block className="mb-8">
  189. <InputAddon>{`${idx + 1}`}</InputAddon>
  190. <Input
  191. value={attr[0]}
  192. placeholder={t('pages.nodes.name')}
  193. onChange={(e) => {
  194. const next = form.attrs.map((a, i) => (i === idx ? ([e.target.value, a[1]] as [string, string]) : a));
  195. update('attrs', next);
  196. }}
  197. />
  198. <Input
  199. value={attr[1]}
  200. placeholder={t('pages.xray.ruleForm.value')}
  201. onChange={(e) => {
  202. const next = form.attrs.map((a, i) => (i === idx ? ([a[0], e.target.value] as [string, string]) : a));
  203. update('attrs', next);
  204. }}
  205. />
  206. <Button
  207. icon={<MinusOutlined />}
  208. onClick={() => update('attrs', form.attrs.filter((_, i) => i !== idx))}
  209. />
  210. </Space.Compact>
  211. ))}
  212. </Form.Item>
  213. <Form.Item
  214. label={
  215. <Tooltip title={t('pages.xray.rules.useComma')}>
  216. IP <QuestionCircleOutlined />
  217. </Tooltip>
  218. }
  219. >
  220. <Input value={form.ip} onChange={(e) => update('ip', e.target.value)} placeholder="0.0.0.0/8, fc00::/7, geoip:ir" />
  221. </Form.Item>
  222. <Form.Item
  223. label={
  224. <Tooltip title={t('pages.xray.rules.useComma')}>
  225. {t('domainName')} <QuestionCircleOutlined />
  226. </Tooltip>
  227. }
  228. >
  229. <Input value={form.domain} onChange={(e) => update('domain', e.target.value)} placeholder="google.com, geosite:cn" />
  230. </Form.Item>
  231. <Form.Item
  232. label={
  233. <Tooltip title={t('pages.xray.rules.useComma')}>
  234. {t('pages.xray.ruleForm.user')} <QuestionCircleOutlined />
  235. </Tooltip>
  236. }
  237. >
  238. <Input value={form.user} onChange={(e) => update('user', e.target.value)} placeholder="email address" />
  239. </Form.Item>
  240. <Form.Item
  241. label={
  242. <Tooltip title={t('pages.xray.rules.useComma')}>
  243. {t('pages.inbounds.port')} <QuestionCircleOutlined />
  244. </Tooltip>
  245. }
  246. >
  247. <Input value={form.port} onChange={(e) => update('port', e.target.value)} placeholder="53,443,1000-2000" />
  248. </Form.Item>
  249. <Form.Item label={t('pages.xray.ruleForm.inboundTags')}>
  250. <Select
  251. mode="multiple"
  252. value={form.inboundTag}
  253. onChange={(v) => update('inboundTag', v)}
  254. options={inboundTags.map((tag) => ({ value: tag, label: formatInboundTag(tag, remarkByTag) }))}
  255. />
  256. </Form.Item>
  257. <Form.Item label={t('pages.xray.ruleForm.outboundTag')}>
  258. <Select
  259. value={form.outboundTag}
  260. onChange={(v) => update('outboundTag', v)}
  261. options={outboundTags.map((tag) => ({ value: tag, label: tag || '(none)' }))}
  262. />
  263. </Form.Item>
  264. <Form.Item
  265. label={
  266. <Tooltip title={t('pages.xray.ruleForm.balancerTagTooltip')}>
  267. {t('pages.xray.ruleForm.balancerTag')} <QuestionCircleOutlined />
  268. </Tooltip>
  269. }
  270. >
  271. <Select
  272. value={form.balancerTag}
  273. onChange={(v) => update('balancerTag', v)}
  274. options={balancerTags.map((tag) => ({ value: tag, label: tag || '(none)' }))}
  275. />
  276. </Form.Item>
  277. </Form>
  278. </Modal>
  279. );
  280. }