1
0

ClientBulkAddModal.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import { useEffect, useMemo, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { AutoComplete, Button, Form, Input, InputNumber, Modal, Select, Space, Switch, message } from 'antd';
  4. import { ReloadOutlined } from '@ant-design/icons';
  5. import dayjs from 'dayjs';
  6. import type { Dayjs } from 'dayjs';
  7. import { RandomUtil, SizeFormatter } from '@/utils';
  8. import { TLS_FLOW_CONTROL } from '@/schemas/primitives';
  9. import DateTimePicker from '@/components/DateTimePicker';
  10. import { useClients, type InboundOption } from '@/hooks/useClients';
  11. import { ClientBulkAddFormSchema, type ClientBulkAddFormValues } from '@/schemas/client';
  12. const FLOW_OPTIONS = Object.values(TLS_FLOW_CONTROL);
  13. const MULTI_CLIENT_PROTOCOLS = new Set([
  14. 'shadowsocks', 'vless', 'vmess', 'trojan', 'hysteria',
  15. ]);
  16. interface ClientBulkAddModalProps {
  17. open: boolean;
  18. inbounds: InboundOption[];
  19. ipLimitEnable?: boolean;
  20. groups?: string[];
  21. onOpenChange: (open: boolean) => void;
  22. onSaved?: () => void;
  23. }
  24. type FormState = ClientBulkAddFormValues;
  25. function emptyForm(): FormState {
  26. return {
  27. emailMethod: 0,
  28. firstNum: 1,
  29. lastNum: 1,
  30. emailPrefix: '',
  31. emailPostfix: '',
  32. quantity: 1,
  33. subId: '',
  34. group: '',
  35. comment: '',
  36. flow: '',
  37. limitIp: 0,
  38. totalGB: 0,
  39. expiryTime: 0,
  40. reset: 0,
  41. inboundIds: [],
  42. };
  43. }
  44. export default function ClientBulkAddModal({
  45. open,
  46. inbounds,
  47. ipLimitEnable = false,
  48. groups = [],
  49. onOpenChange,
  50. onSaved,
  51. }: ClientBulkAddModalProps) {
  52. const { t } = useTranslation();
  53. const [messageApi, messageContextHolder] = message.useMessage();
  54. const { bulkCreate } = useClients();
  55. const [form, setForm] = useState<FormState>(emptyForm);
  56. const [delayedStart, setDelayedStart] = useState(false);
  57. const [saving, setSaving] = useState(false);
  58. useEffect(() => {
  59. if (!open) return;
  60. setForm(emptyForm());
  61. setDelayedStart(false);
  62. }, [open]);
  63. function update<K extends keyof FormState>(key: K, value: FormState[K]) {
  64. setForm((prev) => ({ ...prev, [key]: value }));
  65. }
  66. const flowCapableIds = useMemo(() => {
  67. const ids = new Set<number>();
  68. for (const row of inbounds || []) {
  69. if (row?.tlsFlowCapable) ids.add(row.id);
  70. }
  71. return ids;
  72. }, [inbounds]);
  73. const showFlow = useMemo(
  74. () => (form.inboundIds || []).some((id) => flowCapableIds.has(id)),
  75. [form.inboundIds, flowCapableIds],
  76. );
  77. useEffect(() => {
  78. if (!showFlow && form.flow) {
  79. update('flow', '');
  80. }
  81. }, [showFlow, form.flow]);
  82. const inboundOptions = useMemo(
  83. () => (inbounds || [])
  84. .filter((ib) => MULTI_CLIENT_PROTOCOLS.has(ib.protocol || ''))
  85. .map((ib) => ({
  86. label: `${ib.remark || `#${ib.id}`} · ${ib.protocol}:${ib.port}`,
  87. value: ib.id,
  88. })),
  89. [inbounds],
  90. );
  91. const expiryDate = useMemo<Dayjs | null>(
  92. () => (form.expiryTime > 0 ? dayjs(form.expiryTime) : null),
  93. [form.expiryTime],
  94. );
  95. const delayedExpireDays = form.expiryTime < 0 ? form.expiryTime / -86400000 : 0;
  96. function buildEmails(): string[] {
  97. const method = form.emailMethod;
  98. const out: string[] = [];
  99. let start: number;
  100. let end: number;
  101. if (method > 1) {
  102. start = form.firstNum;
  103. end = form.lastNum + 1;
  104. } else {
  105. start = 0;
  106. end = form.quantity;
  107. }
  108. const prefix = method > 0 && form.emailPrefix.length > 0 ? form.emailPrefix : '';
  109. const useNum = method > 1;
  110. const postfix = method > 2 && form.emailPostfix.length > 0 ? form.emailPostfix : '';
  111. for (let i = start; i < end; i++) {
  112. let email = '';
  113. if (method !== 4) email = RandomUtil.randomLowerAndNum(10);
  114. email += useNum ? prefix + String(i) + postfix : prefix + postfix;
  115. out.push(email);
  116. }
  117. return out;
  118. }
  119. async function submit() {
  120. const validated = ClientBulkAddFormSchema.safeParse(form);
  121. if (!validated.success) {
  122. messageApi.error(t(validated.error.issues[0]?.message ?? 'somethingWentWrong'));
  123. return;
  124. }
  125. const emails = buildEmails();
  126. if (emails.length === 0) return;
  127. setSaving(true);
  128. try {
  129. const payloads = emails.map((email) => ({
  130. client: {
  131. email,
  132. subId: form.subId || RandomUtil.randomLowerAndNum(16),
  133. id: RandomUtil.randomUUID(),
  134. password: RandomUtil.randomLowerAndNum(16),
  135. auth: RandomUtil.randomLowerAndNum(16),
  136. flow: showFlow ? (form.flow || '') : '',
  137. totalGB: Math.round((form.totalGB || 0) * SizeFormatter.ONE_GB),
  138. expiryTime: form.expiryTime,
  139. reset: Number(form.reset) || 0,
  140. limitIp: Number(form.limitIp) || 0,
  141. group: form.group,
  142. comment: form.comment,
  143. enable: true,
  144. },
  145. inboundIds: form.inboundIds,
  146. }));
  147. const msg = await bulkCreate(payloads);
  148. const ok = msg?.obj?.created ?? 0;
  149. const skipped = msg?.obj?.skipped ?? [];
  150. const failed = skipped.length;
  151. const firstError = skipped[0]?.reason ?? msg?.msg ?? '';
  152. if (failed === 0 && msg?.success) {
  153. messageApi.success(t('pages.clients.toasts.bulkCreated', { count: ok }));
  154. } else {
  155. messageApi.warning(firstError
  156. ? `${t('pages.clients.toasts.bulkCreatedMixed', { ok, failed })} — ${firstError}`
  157. : t('pages.clients.toasts.bulkCreatedMixed', { ok, failed }));
  158. }
  159. onSaved?.();
  160. onOpenChange(false);
  161. } finally {
  162. setSaving(false);
  163. }
  164. }
  165. return (
  166. <>
  167. {messageContextHolder}
  168. <Modal
  169. open={open}
  170. title={t('pages.clients.bulk')}
  171. okText={t('create')}
  172. cancelText={t('close')}
  173. confirmLoading={saving}
  174. mask={{ closable: false }}
  175. width={640}
  176. onOk={submit}
  177. onCancel={() => onOpenChange(false)}
  178. >
  179. <Form colon={false} labelCol={{ sm: { span: 8 } }} wrapperCol={{ sm: { span: 14 } }}>
  180. <Form.Item label={t('pages.clients.attachedInbounds')} required>
  181. <Select
  182. mode="multiple"
  183. value={form.inboundIds}
  184. onChange={(v) => update('inboundIds', v)}
  185. options={inboundOptions}
  186. placeholder={t('pages.clients.selectInbound')}
  187. showSearch={{
  188. filterOption: (input, option) => ((option?.label as string) || '').toLowerCase().includes(input.toLowerCase()),
  189. }}
  190. />
  191. </Form.Item>
  192. <Form.Item label={t('pages.clients.method')}>
  193. <Select
  194. value={form.emailMethod}
  195. onChange={(v) => update('emailMethod', v)}
  196. options={[
  197. { value: 0, label: 'Random' },
  198. { value: 1, label: 'Random + Prefix' },
  199. { value: 2, label: 'Random + Prefix + Num' },
  200. { value: 3, label: 'Random + Prefix + Num + Postfix' },
  201. { value: 4, label: 'Prefix + Num + Postfix' },
  202. ]}
  203. />
  204. </Form.Item>
  205. {form.emailMethod > 1 && (
  206. <>
  207. <Form.Item label={t('pages.clients.first')}>
  208. <InputNumber value={form.firstNum} min={1} onChange={(v) => update('firstNum', Number(v) || 1)} />
  209. </Form.Item>
  210. <Form.Item label={t('pages.clients.last')}>
  211. <InputNumber value={form.lastNum} min={form.firstNum} onChange={(v) => update('lastNum', Number(v) || 1)} />
  212. </Form.Item>
  213. </>
  214. )}
  215. {form.emailMethod > 0 && (
  216. <Form.Item label={t('pages.clients.prefix')}>
  217. <Input value={form.emailPrefix} onChange={(e) => update('emailPrefix', e.target.value)} />
  218. </Form.Item>
  219. )}
  220. {form.emailMethod > 2 && (
  221. <Form.Item label={t('pages.clients.postfix')}>
  222. <Input value={form.emailPostfix} onChange={(e) => update('emailPostfix', e.target.value)} />
  223. </Form.Item>
  224. )}
  225. {form.emailMethod < 2 && (
  226. <Form.Item label={t('pages.clients.clientCount')}>
  227. <InputNumber value={form.quantity} min={1} max={100} onChange={(v) => update('quantity', Number(v) || 1)} />
  228. </Form.Item>
  229. )}
  230. <Form.Item label={t('pages.clients.subId')}>
  231. <Space.Compact style={{ display: 'flex' }}>
  232. <Input
  233. value={form.subId}
  234. onChange={(e) => update('subId', e.target.value)}
  235. style={{ flex: 1 }}
  236. />
  237. <Button
  238. icon={<ReloadOutlined />}
  239. onClick={() => update('subId', RandomUtil.randomLowerAndNum(16))}
  240. />
  241. </Space.Compact>
  242. </Form.Item>
  243. <Form.Item label={t('pages.clients.group')} tooltip={t('pages.clients.groupDesc')}>
  244. <AutoComplete
  245. value={form.group}
  246. placeholder={t('pages.clients.groupPlaceholder')}
  247. options={groups.map((g) => ({ value: g }))}
  248. onChange={(v) => update('group', v ?? '')}
  249. filterOption={(input, option) =>
  250. String(option?.value ?? '').toLowerCase().includes((input || '').toLowerCase())
  251. }
  252. allowClear
  253. style={{ width: '100%' }}
  254. />
  255. </Form.Item>
  256. <Form.Item label={t('comment')}>
  257. <Input value={form.comment} onChange={(e) => update('comment', e.target.value)} />
  258. </Form.Item>
  259. {showFlow && (
  260. <Form.Item label={t('pages.clients.flow')}>
  261. <Select
  262. value={form.flow}
  263. onChange={(v) => update('flow', v)}
  264. style={{ width: 220 }}
  265. options={[
  266. { value: '', label: t('none') },
  267. ...FLOW_OPTIONS.map((k) => ({ value: k, label: k })),
  268. ]}
  269. />
  270. </Form.Item>
  271. )}
  272. {ipLimitEnable && (
  273. <Form.Item label={t('pages.clients.limitIp')}>
  274. <InputNumber value={form.limitIp} min={0} onChange={(v) => update('limitIp', Number(v) || 0)} />
  275. </Form.Item>
  276. )}
  277. <Form.Item label={t('pages.clients.totalGB')}>
  278. <InputNumber value={form.totalGB} min={0} step={1} onChange={(v) => update('totalGB', Number(v) || 0)} />
  279. </Form.Item>
  280. <Form.Item label={t('pages.clients.delayedStart')}>
  281. <Switch
  282. checked={delayedStart}
  283. onClick={() => { setDelayedStart(!delayedStart); update('expiryTime', 0); }}
  284. />
  285. </Form.Item>
  286. {delayedStart ? (
  287. <Form.Item label={t('pages.clients.expireDays')}>
  288. <InputNumber
  289. value={delayedExpireDays}
  290. min={0}
  291. onChange={(v) => update('expiryTime', -86400000 * (Number(v) || 0))}
  292. />
  293. </Form.Item>
  294. ) : (
  295. <Form.Item label={t('pages.inbounds.expireDate')}>
  296. <DateTimePicker
  297. value={expiryDate}
  298. onChange={(next) => update('expiryTime', next ? next.valueOf() : 0)}
  299. />
  300. </Form.Item>
  301. )}
  302. <Form.Item
  303. label={t('pages.clients.renew')}
  304. tooltip={t('pages.clients.renewDesc')}
  305. >
  306. <InputNumber
  307. value={form.reset}
  308. min={0}
  309. onChange={(v) => update('reset', Number(v) || 0)}
  310. />
  311. </Form.Item>
  312. </Form>
  313. </Modal>
  314. </>
  315. );
  316. }