| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145 |
- import { useEffect, useMemo, useState } from 'react';
- import { useTranslation } from 'react-i18next';
- import {
- AutoComplete,
- Button,
- Col,
- Form,
- Input,
- InputNumber,
- Modal,
- Popconfirm,
- Row,
- Select,
- Space,
- Switch,
- Tabs,
- Tag,
- Tooltip,
- Typography,
- message,
- } from 'antd';
- import { DeleteOutlined, EyeOutlined, PlusOutlined, ReloadOutlined, RetweetOutlined } from '@ant-design/icons';
- import dayjs from 'dayjs';
- import type { Dayjs } from 'dayjs';
- import { FormProvider, useForm, useWatch, useFieldArray } from 'react-hook-form';
- import { HttpUtil, RandomUtil, Wireguard } from '@/utils';
- import { formatInboundLabel } from '@/lib/inbounds/label';
- import { generateMtprotoSecret } from '@/lib/xray/inbound-defaults';
- import { normalizeClientIps, type ClientIpInfo } from '@/lib/clients/ip-log';
- import { DateTimePicker, SelectAllClearButtons } from '@/components/form';
- import { FormField } from '@/components/form/rhf';
- import { TLS_FLOW_CONTROL } from '@/schemas/primitives';
- import type { ClientRecord, InboundOption, ExternalLink, ExternalLinkInput } from '@/hooks/useClients';
- import { useFail2banStatusQuery, getLimitIpNotice } from '@/api/queries/useFail2banStatusQuery';
- import { ClientFormSchema, ClientCreateFormSchema, type ClientFormValues } from '@/schemas/client';
- const FLOW_OPTIONS = Object.values(TLS_FLOW_CONTROL);
- const VMESS_SECURITY_OPTIONS = ['auto', 'aes-128-gcm', 'chacha20-poly1305'] as const;
- const MULTI_CLIENT_PROTOCOLS = new Set([
- 'shadowsocks', 'vless', 'vmess', 'trojan', 'hysteria', 'wireguard', 'mtproto',
- ]);
- const CLIENT_FORM_MODAL_Z_INDEX = 1000;
- const CLIENT_IP_LOG_MODAL_Z_INDEX = CLIENT_FORM_MODAL_Z_INDEX + 1;
- interface ExternalLinkRow {
- kind: 'link' | 'subscription';
- value: string;
- remark: string;
- }
- interface ApiMsg<T = unknown> {
- success?: boolean;
- msg?: string;
- obj?: T;
- }
- interface ClientHwidInfo {
- id: number;
- firstSeen: number;
- lastSeen: number;
- userAgent: string;
- deviceOs: string;
- osVersion: string;
- deviceModel: string;
- }
- type Mode = 'add' | 'edit';
- interface SaveMetaEdit {
- isEdit: true;
- email: string;
- attach: number[];
- detach: number[];
- externalLinks: ExternalLinkInput[];
- }
- interface SaveMetaCreate {
- isEdit: false;
- email: string;
- externalLinks: ExternalLinkInput[];
- }
- interface SaveCreatePayload {
- client: Record<string, unknown>;
- inboundIds: number[];
- }
- interface ClientFormModalProps {
- open: boolean;
- mode: Mode;
- client: ClientRecord | null;
- inbounds: InboundOption[];
- attachedExternalLinks?: ExternalLink[];
- attachedIds?: number[];
- tgBotEnable?: boolean;
- groups?: string[];
- save: (
- payload: Record<string, unknown> | SaveCreatePayload,
- meta: SaveMetaEdit | SaveMetaCreate,
- ) => Promise<ApiMsg | null>;
- resetTraffic?: (client: ClientRecord) => Promise<ApiMsg | null>;
- onOpenChange: (open: boolean) => void;
- }
- type Values = ClientFormValues & {
- expiryDate: number;
- limitHwid: number;
- externalLinks: ExternalLinkRow[];
- wgPrivateKey: string;
- wgPublicKey: string;
- wgPreSharedKey: string;
- wgAllowedIPs: string;
- secret: string;
- adTag: string;
- };
- const EMPTY: Values = {
- email: '',
- subId: '',
- uuid: '',
- password: '',
- auth: '',
- flow: '',
- security: 'auto',
- reverseTag: '',
- totalGB: 0,
- expiryDate: 0,
- delayedStart: false,
- delayedDays: 0,
- reset: 0,
- resetMax: 0,
- limitIp: 0,
- limitHwid: 0,
- tgId: 0,
- group: '',
- comment: '',
- enable: true,
- inboundIds: [],
- externalLinks: [],
- wgPrivateKey: '',
- wgPublicKey: '',
- wgPreSharedKey: '',
- wgAllowedIPs: '',
- secret: '',
- adTag: '',
- };
- function toExternalLinkRows(links: ExternalLink[] | undefined): ExternalLinkRow[] {
- return (links || []).map((l) => ({
- kind: l.kind === 'subscription' ? 'subscription' : 'link',
- value: l.value || '',
- remark: l.remark || '',
- }));
- }
- function bytesToGB(bytes: number): number {
- if (!bytes || bytes <= 0) return 0;
- return Math.round((bytes / (1024 * 1024 * 1024)) * 100) / 100;
- }
- export function gbToBytes(gb: number): number {
- if (!gb || gb <= 0) return 0;
- return Math.round(gb * 1024 * 1024 * 1024);
- }
- export function resolveTotalBytes(originalBytes: number | null | undefined, displayedGB: number): number {
- if (originalBytes != null && displayedGB === bytesToGB(originalBytes)) {
- return originalBytes;
- }
- return gbToBytes(displayedGB);
- }
- export default function ClientFormModal({
- open,
- mode,
- client,
- inbounds,
- attachedExternalLinks = [],
- attachedIds = [],
- tgBotEnable = false,
- groups = [],
- save,
- resetTraffic,
- onOpenChange,
- }: ClientFormModalProps) {
- const { t } = useTranslation();
- const [messageApi, messageContextHolder] = message.useMessage();
- const isEdit = mode === 'edit';
- const methods = useForm<Values>({ defaultValues: EMPTY });
- const inboundIds = useWatch({ control: methods.control, name: 'inboundIds' });
- const delayedStart = useWatch({ control: methods.control, name: 'delayedStart' });
- const expiryDate = useWatch({ control: methods.control, name: 'expiryDate' });
- const enable = useWatch({ control: methods.control, name: 'enable' });
- const flow = useWatch({ control: methods.control, name: 'flow' });
- const reverseTag = useWatch({ control: methods.control, name: 'reverseTag' });
- const secret = useWatch({ control: methods.control, name: 'secret' });
- const email = useWatch({ control: methods.control, name: 'email' });
- const uuid = useWatch({ control: methods.control, name: 'uuid' });
- const password = useWatch({ control: methods.control, name: 'password' });
- const subId = useWatch({ control: methods.control, name: 'subId' });
- const limitHwid = useWatch({ control: methods.control, name: 'limitHwid' });
- const auth = useWatch({ control: methods.control, name: 'auth' });
- const wgPrivateKey = useWatch({ control: methods.control, name: 'wgPrivateKey' });
- const limitIp = useWatch({ control: methods.control, name: 'limitIp' });
- const {
- fields: externalLinkFields,
- append: appendExternalLink,
- remove: removeExternalLink,
- } = useFieldArray({ control: methods.control, name: 'externalLinks' });
- const [submitting, setSubmitting] = useState(false);
- const [resetting, setResetting] = useState(false);
- const [clientIps, setClientIps] = useState<ClientIpInfo[]>([]);
- const [ipsLoading, setIpsLoading] = useState(false);
- const [ipsClearing, setIpsClearing] = useState(false);
- const [ipsModalOpen, setIpsModalOpen] = useState(false);
- const [clientHwids, setClientHwids] = useState<ClientHwidInfo[]>([]);
- const [hwidsLoading, setHwidsLoading] = useState(false);
- const [hwidsClearing, setHwidsClearing] = useState(false);
- const [hwidsModalOpen, setHwidsModalOpen] = useState(false);
- const fail2ban = useFail2banStatusQuery();
- const limitIpDisabled = !fail2ban.usable;
- const limitIpNotice = getLimitIpNotice(fail2ban, t);
- function addExternalLinkRow(kind: 'link' | 'subscription') {
- appendExternalLink({ kind, value: '', remark: '' });
- }
- useEffect(() => {
- if (!open) return;
- setIpsModalOpen(false);
- setHwidsModalOpen(false);
- if (isEdit && client) {
- const et = Number(client.expiryTime) || 0;
- const seed: Values = {
- ...EMPTY,
- email: client.email || '',
- subId: client.subId || '',
- uuid: client.uuid || '',
- password: client.password || '',
- auth: client.auth || '',
- flow: client.flow || '',
- security: !client.security || client.security === 'none' || client.security === 'zero'
- ? 'auto'
- : client.security,
- reverseTag: client.reverse?.tag || '',
- totalGB: bytesToGB(client.totalGB || 0),
- reset: Number(client.reset) || 0,
- resetMax: Number(client.resetMax) || 0,
- limitIp: client.limitIp || 0,
- limitHwid: client.limitHwid || 0,
- tgId: Number(client.tgId) || 0,
- group: client.group || '',
- comment: client.comment || '',
- enable: !!client.enable,
- inboundIds: Array.isArray(attachedIds) ? [...attachedIds] : [],
- externalLinks: toExternalLinkRows(attachedExternalLinks),
- wgPrivateKey: client.privateKey || '',
- wgPublicKey: client.publicKey || '',
- wgPreSharedKey: client.preSharedKey || '',
- wgAllowedIPs: client.allowedIPs || '',
- secret: client.secret || '',
- adTag: client.adTag || '',
- };
- if (et < 0) {
- seed.delayedStart = true;
- seed.delayedDays = Math.round(et / -86400000);
- seed.expiryDate = 0;
- } else {
- seed.delayedStart = false;
- seed.delayedDays = 0;
- seed.expiryDate = et > 0 ? et : 0;
- }
- methods.reset(seed);
- void loadIps();
- void loadHwids();
- } else {
- const wgKeypair = Wireguard.generateKeypair();
- methods.reset({
- ...EMPTY,
- email: RandomUtil.randomLowerAndNum(10),
- uuid: RandomUtil.randomUUID(),
- subId: RandomUtil.randomLowerAndNum(16),
- password: RandomUtil.randomLowerAndNum(16),
- auth: RandomUtil.randomLowerAndNum(16),
- wgPrivateKey: wgKeypair.privateKey,
- wgPublicKey: wgKeypair.publicKey,
- });
- }
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [open, isEdit]);
- const flowCapableIds = useMemo(() => {
- const ids = new Set<number>();
- for (const row of inbounds || []) {
- if (row?.tlsFlowCapable) ids.add(row.id);
- }
- return ids;
- }, [inbounds]);
- const vlessLikeIds = useMemo(() => {
- const ids = new Set<number>();
- for (const row of inbounds || []) {
- if (row && row.protocol === 'vless') ids.add(row.id);
- }
- return ids;
- }, [inbounds]);
- const vmessIds = useMemo(() => {
- const ids = new Set<number>();
- for (const row of inbounds || []) {
- if (row && row.protocol === 'vmess') ids.add(row.id);
- }
- return ids;
- }, [inbounds]);
- const wireguardIds = useMemo(() => {
- const ids = new Set<number>();
- for (const row of inbounds || []) {
- if (row && row.protocol === 'wireguard') ids.add(row.id);
- }
- return ids;
- }, [inbounds]);
- const mtprotoIds = useMemo(() => {
- const ids = new Set<number>();
- for (const row of inbounds || []) {
- if (row && row.protocol === 'mtproto') ids.add(row.id);
- }
- return ids;
- }, [inbounds]);
- const mtprotoDomain = useMemo(() => {
- for (const id of inboundIds || []) {
- const ib = (inbounds || []).find((row) => row.id === id);
- if (ib?.protocol === 'mtproto' && ib.mtprotoDomain) return ib.mtprotoDomain;
- }
- return 'www.cloudflare.com';
- }, [inboundIds, inbounds]);
- const ss2022Method = useMemo(() => {
- for (const id of inboundIds || []) {
- const ib = (inbounds || []).find((row) => row.id === id);
- const method = ib?.ssMethod;
- if (method && method.substring(0, 4) === '2022') return method;
- }
- return '';
- }, [inboundIds, inbounds]);
- function regeneratePassword() {
- methods.setValue('password', ss2022Method
- ? RandomUtil.randomShadowsocksPassword(ss2022Method)
- : RandomUtil.randomLowerAndNum(16));
- }
- const showFlow = useMemo(
- () => (inboundIds || []).some((id) => flowCapableIds.has(id)),
- [inboundIds, flowCapableIds],
- );
- const showReverseTag = useMemo(
- () => (inboundIds || []).some((id) => vlessLikeIds.has(id)),
- [inboundIds, vlessLikeIds],
- );
- const showSecurity = useMemo(
- () => (inboundIds || []).some((id) => vmessIds.has(id)),
- [inboundIds, vmessIds],
- );
- const showWireguard = useMemo(
- () => (inboundIds || []).some((id) => wireguardIds.has(id)),
- [inboundIds, wireguardIds],
- );
- const showMtproto = useMemo(
- () => (inboundIds || []).some((id) => mtprotoIds.has(id)),
- [inboundIds, mtprotoIds],
- );
- function regenerateWireguardKeys() {
- const kp = Wireguard.generateKeypair();
- methods.setValue('wgPrivateKey', kp.privateKey);
- methods.setValue('wgPublicKey', kp.publicKey);
- }
- function regenerateMtprotoSecret() {
- methods.setValue('secret', generateMtprotoSecret(mtprotoDomain));
- }
- useEffect(() => {
- // Only clear the flow once we actually have inbound options to judge
- // capability from. While the options list is momentarily empty (e.g. the
- // options query is (re)loading and `inbounds` falls back to `[]`), showFlow
- // is a false negative, so clearing here would silently drop a valid
- // xtls-rprx-vision flow the user picked for a Reality/TLS inbound.
- if (inbounds.length > 0 && !showFlow && flow) {
- methods.setValue('flow', '');
- }
- }, [inbounds, showFlow, flow, methods]);
- useEffect(() => {
- if (!showReverseTag && reverseTag) {
- methods.setValue('reverseTag', '');
- }
- }, [showReverseTag, reverseTag, methods]);
- useEffect(() => {
- if (!ss2022Method) return;
- const current = methods.getValues('password');
- if (!RandomUtil.isShadowsocks2022Password(current, ss2022Method)) {
- methods.setValue('password', RandomUtil.randomShadowsocksPassword(ss2022Method));
- }
- }, [ss2022Method, methods]);
- useEffect(() => {
- if (showMtproto && !secret) {
- methods.setValue('secret', generateMtprotoSecret(mtprotoDomain));
- }
- }, [showMtproto, secret, mtprotoDomain, methods]);
- const inboundOptions = useMemo(
- () => (inbounds || [])
- .filter((ib) => MULTI_CLIENT_PROTOCOLS.has(ib.protocol || ''))
- .filter((ib) => ib.enable || (inboundIds || []).includes(ib.id))
- .map((ib) => ({
- label: formatInboundLabel(ib.tag, ib.remark),
- value: ib.id,
- title: formatInboundLabel(ib.tag, ib.remark),
- })),
- [inbounds, inboundIds],
- );
- const expiryDayjs = useMemo<Dayjs | null>(
- () => (expiryDate > 0 ? dayjs(expiryDate) : null),
- [expiryDate],
- );
- const linkRows = externalLinkFields
- .map((field, index) => ({ field, index }))
- .filter((row) => row.field.kind === 'link');
- const subscriptionRows = externalLinkFields
- .map((field, index) => ({ field, index }))
- .filter((row) => row.field.kind === 'subscription');
- async function loadIps() {
- if (!isEdit || !client?.email) return;
- setIpsLoading(true);
- try {
- const msg = await HttpUtil.post(`/panel/api/clients/ips/${encodeURIComponent(client.email)}`) as ApiMsg<unknown[]>;
- if (!msg?.success) { setClientIps([]); return; }
- setClientIps(normalizeClientIps(msg.obj));
- } finally {
- setIpsLoading(false);
- }
- }
- function openIpsModal() {
- setIpsModalOpen(true);
- if (clientIps.length === 0) void loadIps();
- }
- async function clearIps() {
- if (!isEdit || !client?.email) return;
- setIpsClearing(true);
- try {
- const msg = await HttpUtil.post(`/panel/api/clients/clearIps/${encodeURIComponent(client.email)}`) as ApiMsg;
- if (msg?.success) setClientIps([]);
- } finally {
- setIpsClearing(false);
- }
- }
- async function loadHwids() {
- if (!isEdit || !client?.email) return;
- setHwidsLoading(true);
- try {
- const msg = await HttpUtil.post(`/panel/api/clients/hwids/${encodeURIComponent(client.email)}`) as ApiMsg<unknown[]>;
- if (!msg?.success || !Array.isArray(msg.obj)) { setClientHwids([]); return; }
- setClientHwids(msg.obj.filter((x): x is ClientHwidInfo => !!x && typeof x === 'object' && typeof (x as ClientHwidInfo).id === 'number'));
- } finally {
- setHwidsLoading(false);
- }
- }
- function openHwidsModal() {
- setHwidsModalOpen(true);
- if (clientHwids.length === 0) void loadHwids();
- }
- async function clearHwids() {
- if (!isEdit || !client?.email) return;
- setHwidsClearing(true);
- try {
- const msg = await HttpUtil.delete(`/panel/api/clients/hwids/${encodeURIComponent(client.email)}`) as ApiMsg;
- if (msg?.success) setClientHwids([]);
- } finally {
- setHwidsClearing(false);
- }
- }
- function close() {
- onOpenChange(false);
- }
- async function onResetTraffic() {
- if (!isEdit || !client?.email || !resetTraffic) return;
- setResetting(true);
- try {
- const msg = await resetTraffic(client);
- if (msg?.success) {
- messageApi.success(t('pages.clients.toasts.trafficReset'));
- } else {
- messageApi.error(msg?.msg || t('somethingWentWrong'));
- }
- } finally {
- setResetting(false);
- }
- }
- async function onSubmit() {
- const values = methods.getValues();
- const schema = isEdit ? ClientFormSchema : ClientCreateFormSchema;
- const validated = schema.safeParse({
- email: values.email,
- subId: values.subId,
- uuid: values.uuid,
- password: values.password,
- auth: values.auth,
- flow: values.flow,
- security: values.security,
- reverseTag: values.reverseTag,
- totalGB: values.totalGB,
- delayedStart: values.delayedStart,
- delayedDays: values.delayedDays,
- reset: values.reset,
- resetMax: values.resetMax,
- limitIp: values.limitIp,
- limitHwid: values.limitHwid,
- tgId: values.tgId,
- group: values.group,
- comment: values.comment,
- enable: values.enable,
- inboundIds: values.inboundIds,
- });
- if (!validated.success) {
- const issue = validated.error.issues[0];
- messageApi.error(t(issue?.message ?? 'somethingWentWrong'));
- return;
- }
- const expiryTime = values.delayedStart
- ? -86400000 * (Number(values.delayedDays) || 0)
- : (values.expiryDate || 0);
- const totalBytes = resolveTotalBytes(client ? (client.totalGB ?? 0) : null, values.totalGB);
- const clientPayload: Record<string, unknown> = {
- email: values.email.trim(),
- subId: values.subId,
- id: values.uuid,
- password: values.password,
- auth: values.auth,
- flow: showFlow ? (values.flow || '') : '',
- security: showSecurity ? (values.security || 'auto') : 'auto',
- totalGB: totalBytes,
- expiryTime,
- reset: Number(values.reset) || 0,
- resetMax: Number(values.resetMax) || 0,
- limitIp: Number(values.limitIp) || 0,
- limitHwid: Number(values.limitHwid) || 0,
- tgId: Number(values.tgId) || 0,
- group: values.group,
- comment: values.comment,
- enable: !!values.enable,
- };
- const reverseTagValue = showReverseTag ? (values.reverseTag || '').trim() : '';
- if (reverseTagValue) {
- clientPayload.reverse = { tag: reverseTagValue };
- }
- if (showWireguard) {
- clientPayload.privateKey = values.wgPrivateKey;
- clientPayload.publicKey = values.wgPublicKey;
- if (values.wgPreSharedKey) {
- clientPayload.preSharedKey = values.wgPreSharedKey;
- }
- const allowedIPs = values.wgAllowedIPs
- .split(',')
- .map((s) => s.trim())
- .filter((s) => s !== '');
- if (allowedIPs.length > 0) {
- clientPayload.allowedIPs = allowedIPs;
- }
- }
- if (showMtproto) {
- const adTag = values.adTag.trim();
- if (adTag !== '' && !/^[0-9a-fA-F]{32}$/.test(adTag)) {
- messageApi.error(t('pages.inbounds.form.mtgAdTagInvalid'));
- return;
- }
- clientPayload.secret = values.secret;
- clientPayload.adTag = adTag;
- }
- const externalLinks: ExternalLinkInput[] = values.externalLinks
- .map((r) => ({ kind: r.kind, value: r.value.trim(), remark: (r.remark || '').trim() }))
- .filter((r) => r.value !== '');
- setSubmitting(true);
- try {
- let msg;
- if (isEdit && client) {
- const original = new Set(attachedIds || []);
- const next = new Set(values.inboundIds || []);
- const toAttach = [...next].filter((id) => !original.has(id));
- const toDetach = [...original].filter((id) => !next.has(id));
- msg = await save(clientPayload, {
- isEdit: true,
- email: client.email,
- attach: toAttach,
- detach: toDetach,
- externalLinks,
- });
- } else {
- msg = await save(
- { client: clientPayload, inboundIds: values.inboundIds },
- { isEdit: false, email: clientPayload.email as string, externalLinks },
- );
- }
- if (msg?.success) close();
- } finally {
- setSubmitting(false);
- }
- }
- return (
- <>
- {messageContextHolder}
- <Modal
- open={open}
- title={isEdit ? t('pages.clients.editClient') : t('pages.clients.addClient')}
- destroyOnHidden
- width={720}
- zIndex={CLIENT_FORM_MODAL_Z_INDEX}
- style={{ top: 20 }}
- styles={{ body: { maxHeight: 'calc(100vh - 160px)', overflowY: 'auto', overflowX: 'hidden' } }}
- onCancel={close}
- footer={
- <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
- {isEdit && resetTraffic && (
- <Popconfirm
- title={t('pages.inbounds.resetTraffic')}
- description={t('pages.inbounds.resetTrafficContent')}
- okText={t('reset')}
- cancelText={t('cancel')}
- zIndex={CLIENT_IP_LOG_MODAL_Z_INDEX}
- onConfirm={onResetTraffic}
- >
- <Button color="danger" variant="filled" icon={<RetweetOutlined />} loading={resetting}>
- {t('pages.inbounds.resetTraffic')}
- </Button>
- </Popconfirm>
- )}
- <div style={{ marginInlineStart: 'auto', display: 'flex', gap: 8 }}>
- <Button onClick={close}>{t('cancel')}</Button>
- <Button type="primary" loading={submitting} onClick={onSubmit}>
- {isEdit ? t('save') : t('create')}
- </Button>
- </div>
- </div>
- }
- >
- <FormProvider {...methods}>
- <Form layout="vertical">
- <Tabs
- defaultActiveKey="basic"
- items={[
- {
- key: 'basic',
- label: t('pages.clients.tabBasics'),
- children: (
- <>
- <Row gutter={16}>
- <Col xs={24} md={12}>
- <Form.Item label={t('pages.clients.email')} required>
- <Space.Compact style={{ display: 'flex' }}>
- <Input
- value={email}
- placeholder={t('pages.clients.email')}
- style={{ flex: 1 }}
- onChange={(e) => methods.setValue('email', e.target.value)}
- />
- {!isEdit && (
- <Button aria-label={t('regenerate')} icon={<ReloadOutlined />} onClick={() => methods.setValue('email', RandomUtil.randomLowerAndNum(12))} />
- )}
- </Space.Compact>
- </Form.Item>
- </Col>
- <Col xs={24} md={6}>
- <FormField
- name="totalGB"
- label={t('pages.clients.totalGB')}
- tooltip={t('pages.clients.totalGBDesc')}
- transform={{ output: (v) => Number(v) || 0 }}
- >
- <InputNumber min={0} step={1} style={{ width: '100%' }} />
- </FormField>
- </Col>
- <Col xs={24} md={6}>
- <Form.Item label={t('pages.clients.limitIp')} tooltip={t('pages.clients.limitIpDesc')}>
- <Tooltip title={limitIpNotice || undefined}>
- <span style={{ display: 'flex', width: '100%' }}>
- <Space.Compact style={{ display: 'flex', flex: 1 }}>
- <InputNumber value={limitIp} min={0} disabled={limitIpDisabled}
- style={{ flex: 1, ...(limitIpDisabled ? { pointerEvents: 'none' } : null) }}
- onChange={(v) => methods.setValue('limitIp', Number(v) || 0)} />
- {isEdit && (
- <Tooltip title={t('pages.clients.ipLog')}>
- <Button aria-label={t('pages.clients.ipLog')} icon={<EyeOutlined />} loading={ipsLoading} onClick={openIpsModal}>
- {clientIps.length > 0 ? clientIps.length : ''}
- </Button>
- </Tooltip>
- )}
- </Space.Compact>
- </span>
- </Tooltip>
- </Form.Item>
- </Col>
- <Col xs={24} md={6}>
- <Form.Item label={t('pages.clients.limitHwid')} tooltip={t('pages.clients.limitHwidDesc')}>
- <Space.Compact style={{ display: 'flex' }}>
- <InputNumber value={limitHwid} min={0} style={{ flex: 1 }}
- onChange={(v) => methods.setValue('limitHwid', Number(v) || 0)} />
- {isEdit && (
- <Tooltip title={t('pages.clients.hwidLog')}>
- <Button aria-label={t('pages.clients.hwidLog')} icon={<EyeOutlined />} loading={hwidsLoading} onClick={openHwidsModal}>
- {clientHwids.length > 0 ? clientHwids.length : ''}
- </Button>
- </Tooltip>
- )}
- </Space.Compact>
- </Form.Item>
- </Col>
- </Row>
- <Row gutter={16}>
- <Col xs={24} md={12}>
- {delayedStart ? (
- <FormField
- name="delayedDays"
- label={t('pages.clients.expireDays')}
- transform={{ output: (v) => Number(v) || 0 }}
- >
- <InputNumber min={0} style={{ width: '100%' }} />
- </FormField>
- ) : (
- <Form.Item label={t('pages.clients.expiryTime')}>
- <DateTimePicker
- value={expiryDayjs}
- onChange={(d) => methods.setValue('expiryDate', d ? d.valueOf() : 0)}
- />
- </Form.Item>
- )}
- </Col>
- <Col xs={12} md={6}>
- <Form.Item label={t('pages.clients.delayedStart')}>
- <Switch
- checked={delayedStart}
- onChange={(v) => {
- methods.setValue('delayedStart', v);
- if (v) methods.setValue('expiryDate', 0);
- else methods.setValue('delayedDays', 0);
- }}
- />
- </Form.Item>
- </Col>
- <Col xs={12} md={6}>
- <FormField
- name="reset"
- label={t('pages.clients.renewDays')}
- tooltip={t('pages.clients.renewDesc')}
- transform={{ output: (v) => Number(v) || 0 }}
- >
- <InputNumber min={0} style={{ width: '100%' }} />
- </FormField>
- </Col>
- <Col xs={12} md={6}>
- <FormField
- name="resetMax"
- label={t('pages.clients.renewMax')}
- tooltip={t('pages.clients.renewMaxDesc')}
- transform={{ output: (v) => Number(v) || 0 }}
- >
- <InputNumber min={0} style={{ width: '100%' }} />
- </FormField>
- </Col>
- </Row>
- <Row gutter={16}>
- <Col xs={24} md={12}>
- <FormField name="comment" label={t('pages.clients.comment')}>
- <Input />
- </FormField>
- </Col>
- <Col xs={24} md={12}>
- <FormField
- name="group"
- label={t('pages.clients.group')}
- tooltip={t('pages.clients.groupDesc')}
- transform={{ output: (v) => v ?? '' }}
- >
- <AutoComplete
- placeholder={t('pages.clients.groupPlaceholder')}
- options={groups.map((g) => ({ value: g }))}
- allowClear
- />
- </FormField>
- </Col>
- </Row>
- {(tgBotEnable || showReverseTag) && (
- <Row gutter={16}>
- {tgBotEnable && (
- <Col xs={24} md={12}>
- <FormField
- name="tgId"
- label={t('pages.clients.telegramId')}
- transform={{ output: (v) => Number(v) || 0 }}
- >
- <InputNumber min={0} controls={false}
- placeholder={t('pages.clients.telegramIdPlaceholder')} style={{ width: '100%' }} />
- </FormField>
- </Col>
- )}
- {showReverseTag && (
- <Col xs={24} md={12}>
- <FormField name="reverseTag" label={t('pages.clients.reverseTag')}>
- <Input placeholder={t('pages.clients.reverseTagPlaceholder')} />
- </FormField>
- </Col>
- )}
- </Row>
- )}
- <Form.Item label={t('pages.clients.attachedInbounds')} required={!isEdit}>
- <SelectAllClearButtons
- options={inboundOptions}
- value={inboundIds}
- onChange={(v) => methods.setValue('inboundIds', v)}
- />
- <Select
- mode="multiple"
- value={inboundIds}
- onChange={(v) => methods.setValue('inboundIds', v)}
- options={inboundOptions}
- placeholder={t('pages.clients.selectInbound')}
- maxTagCount="responsive"
- placement="topLeft"
- listHeight={220}
- showSearch={{
- filterOption: (input, option) => ((option?.label as string) || '').toLowerCase().includes(input.toLowerCase()),
- }}
- />
- </Form.Item>
- <Form.Item>
- <Switch aria-label={t('enable')} checked={enable} onChange={(v) => methods.setValue('enable', v)} />
- <span style={{ marginLeft: 8 }}>{t('enable')}</span>
- </Form.Item>
- </>
- ),
- },
- {
- key: 'config',
- label: t('pages.clients.tabCredentials'),
- children: (
- <>
- <Form.Item label={t('pages.clients.uuid')}>
- <Space.Compact style={{ display: 'flex' }}>
- <Input value={uuid} style={{ flex: 1 }} onChange={(e) => methods.setValue('uuid', e.target.value)} />
- <Button aria-label={t('regenerate')} icon={<ReloadOutlined />} onClick={() => methods.setValue('uuid', RandomUtil.randomUUID())} />
- </Space.Compact>
- </Form.Item>
- <Form.Item label={t('pages.clients.password')} tooltip={t('pages.clients.passwordDesc')}>
- <Space.Compact style={{ display: 'flex' }}>
- <Input value={password} style={{ flex: 1 }} onChange={(e) => methods.setValue('password', e.target.value)} />
- <Button aria-label={t('regenerate')} icon={<ReloadOutlined />} onClick={regeneratePassword} />
- </Space.Compact>
- </Form.Item>
- <Form.Item label={t('pages.clients.subId')}>
- <Space.Compact style={{ display: 'flex' }}>
- <Input value={subId} style={{ flex: 1 }} onChange={(e) => methods.setValue('subId', e.target.value)} />
- <Button aria-label={t('regenerate')} icon={<ReloadOutlined />} onClick={() => methods.setValue('subId', RandomUtil.randomLowerAndNum(16))} />
- </Space.Compact>
- </Form.Item>
- <Form.Item label={t('pages.clients.hysteriaAuth')} tooltip={t('pages.clients.hysteriaAuthDesc')}>
- <Space.Compact style={{ display: 'flex' }}>
- <Input value={auth} style={{ flex: 1 }} onChange={(e) => methods.setValue('auth', e.target.value)} />
- <Button aria-label={t('regenerate')} icon={<ReloadOutlined />} onClick={() => methods.setValue('auth', RandomUtil.randomLowerAndNum(16))} />
- </Space.Compact>
- </Form.Item>
- {showFlow && (
- <FormField name="flow" label={t('pages.clients.flow')}>
- <Select
- options={[
- { value: '', label: t('none') },
- ...FLOW_OPTIONS.map((k) => ({ value: k, label: k })),
- ]}
- />
- </FormField>
- )}
- {showSecurity && (
- <FormField name="security" label={t('pages.clients.vmessSecurity')}>
- <Select
- options={VMESS_SECURITY_OPTIONS.map((k) => ({ value: k, label: k }))}
- />
- </FormField>
- )}
- {showWireguard && (
- <>
- <Form.Item label={t('pages.clients.wireguardPrivateKey')}>
- <Space.Compact style={{ display: 'flex' }}>
- <Input
- value={wgPrivateKey}
- style={{ flex: 1 }}
- onChange={(e) => {
- const priv = e.target.value;
- methods.setValue('wgPrivateKey', priv);
- methods.setValue('wgPublicKey', priv ? Wireguard.generateKeypair(priv).publicKey : '');
- }}
- />
- <Button aria-label={t('regenerate')} icon={<ReloadOutlined />} onClick={regenerateWireguardKeys} />
- </Space.Compact>
- </Form.Item>
- <FormField name="wgPublicKey" label={t('pages.clients.wireguardPublicKey')}>
- <Input disabled />
- </FormField>
- <FormField name="wgPreSharedKey" label={t('pages.clients.wireguardPreSharedKey')}>
- <Input />
- </FormField>
- <FormField
- name="wgAllowedIPs"
- label={t('pages.clients.wireguardAllowedIPs')}
- extra={t('pages.clients.wireguardAllowedIPsHint')}
- >
- <Input placeholder="10.0.0.2/32" />
- </FormField>
- </>
- )}
- {showMtproto && (
- <>
- <Form.Item label={t('pages.clients.mtprotoSecret')} extra={t('pages.clients.mtprotoSecretHint')}>
- <Space.Compact style={{ display: 'flex' }}>
- <Input value={secret} style={{ flex: 1 }} onChange={(e) => methods.setValue('secret', e.target.value)} />
- <Button aria-label={t('regenerate')} icon={<ReloadOutlined />} onClick={regenerateMtprotoSecret} />
- </Space.Compact>
- </Form.Item>
- <FormField
- name="adTag"
- label={t('pages.clients.mtprotoAdTag')}
- extra={t('pages.clients.mtprotoAdTagHint')}
- >
- <Input
- allowClear
- placeholder="0123456789abcdef0123456789abcdef"
- />
- </FormField>
- </>
- )}
- </>
- ),
- },
- {
- key: 'links',
- label: t('pages.clients.tabLinks'),
- children: (
- <>
- <Typography.Paragraph type="secondary" style={{ marginTop: 4 }}>
- {t('pages.clients.linksHint')}
- </Typography.Paragraph>
- <Button type="primary" icon={<PlusOutlined />} onClick={() => addExternalLinkRow('link')}>
- {t('pages.clients.addExternalLink')}
- </Button>
- <div style={{ marginTop: 12, marginBottom: 24 }}>
- {linkRows.length === 0 ? (
- <Typography.Text type="secondary">{t('pages.clients.noExternalLinks')}</Typography.Text>
- ) : linkRows.map(({ field, index }) => (
- <div key={field.id} style={{ display: 'flex', gap: 8, marginBottom: 8 }}>
- <FormField name={`externalLinks.${index}.value`} noStyle>
- <Input
- style={{ flex: 1 }}
- aria-label="vless:// · vmess:// · trojan:// · ss:// · hysteria2:// · wireguard://"
- placeholder="vless:// · vmess:// · trojan:// · ss:// · hysteria2:// · wireguard://"
- />
- </FormField>
- <FormField name={`externalLinks.${index}.remark`} noStyle>
- <Input
- style={{ width: 140 }}
- aria-label={t('remark')}
- placeholder={t('remark')}
- />
- </FormField>
- <Tooltip title={t('delete')}>
- <Button aria-label={t('delete')} danger icon={<DeleteOutlined />} onClick={() => removeExternalLink(index)} />
- </Tooltip>
- </div>
- ))}
- </div>
- <Button type="primary" icon={<PlusOutlined />} onClick={() => addExternalLinkRow('subscription')}>
- {t('pages.clients.addExternalSubscription')}
- </Button>
- <div style={{ marginTop: 12 }}>
- {subscriptionRows.length === 0 ? (
- <Typography.Text type="secondary">{t('pages.clients.noExternalSubscriptions')}</Typography.Text>
- ) : subscriptionRows.map(({ field, index }) => (
- <div key={field.id} style={{ display: 'flex', gap: 8, marginBottom: 8 }}>
- <FormField name={`externalLinks.${index}.value`} noStyle>
- <Input
- style={{ flex: 1 }}
- aria-label="https://provider.example/sub/…"
- placeholder="https://provider.example/sub/…"
- />
- </FormField>
- <Tooltip title={t('delete')}>
- <Button aria-label={t('delete')} danger icon={<DeleteOutlined />} onClick={() => removeExternalLink(index)} />
- </Tooltip>
- </div>
- ))}
- </div>
- </>
- ),
- },
- ]}
- />
- </Form>
- </FormProvider>
- </Modal>
- <Modal
- open={ipsModalOpen}
- title={`${t('pages.clients.ipLog')}${client?.email ? ` — ${client.email}` : ''}`}
- width={440}
- zIndex={CLIENT_IP_LOG_MODAL_Z_INDEX}
- onCancel={() => setIpsModalOpen(false)}
- footer={[
- <Button key="refresh" icon={<ReloadOutlined />} loading={ipsLoading} onClick={loadIps}>
- {t('refresh')}
- </Button>,
- <Button key="clear" danger loading={ipsClearing} disabled={clientIps.length === 0} onClick={clearIps}>
- {t('pages.clients.clearAll')}
- </Button>,
- <Button key="close" type="primary" onClick={() => setIpsModalOpen(false)}>
- {t('close')}
- </Button>,
- ]}
- >
- {clientIps.length > 0 ? (
- <div style={{ maxHeight: 360, overflowY: 'auto' }}>
- {clientIps.map((entry, idx) => (
- <Tag
- key={idx}
- color="blue"
- style={{
- display: 'block',
- width: 'fit-content',
- maxWidth: '100%',
- marginBottom: 6,
- padding: '2px 8px',
- fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace',
- }}
- >
- {entry.ip}{entry.time ? ` (${entry.time})` : ''}
- {entry.node ? (
- <span style={{ marginInlineStart: 6, opacity: 0.85, fontWeight: 600 }}>@ {entry.node}</span>
- ) : null}
- </Tag>
- ))}
- </div>
- ) : (
- <Tag>{t('tgbot.noIpRecord')}</Tag>
- )}
- </Modal>
- <Modal
- open={hwidsModalOpen}
- title={`${t('pages.clients.hwidLog')}${client?.email ? ` — ${client.email}` : ''}`}
- width={520}
- zIndex={CLIENT_IP_LOG_MODAL_Z_INDEX}
- onCancel={() => setHwidsModalOpen(false)}
- footer={[
- <Button key="refresh" icon={<ReloadOutlined />} loading={hwidsLoading} onClick={loadHwids}>
- {t('refresh')}
- </Button>,
- <Button key="clear" danger loading={hwidsClearing} disabled={clientHwids.length === 0} onClick={clearHwids}>
- {t('pages.clients.clearAll')}
- </Button>,
- <Button key="close" type="primary" onClick={() => setHwidsModalOpen(false)}>
- {t('close')}
- </Button>,
- ]}
- >
- {clientHwids.length > 0 ? (
- <div style={{ maxHeight: 360, overflowY: 'auto' }}>
- {clientHwids.map((entry) => (
- <div key={entry.id} style={{ borderBottom: '1px solid var(--ant-color-border-secondary)', padding: '8px 0' }}>
- <Typography.Text strong>{entry.deviceModel || entry.userAgent || t('pages.clients.hwidDevice')}</Typography.Text>
- <br />
- <Typography.Text type="secondary">
- {[entry.deviceOs, entry.osVersion].filter(Boolean).join(' ')}
- </Typography.Text>
- <br />
- <Typography.Text type="secondary">
- {t('pages.clients.firstSeen')}: {entry.firstSeen ? dayjs(entry.firstSeen).format('YYYY-MM-DD HH:mm') : '-'}
- </Typography.Text>
- <br />
- <Typography.Text type="secondary">
- {t('pages.clients.lastSeen')}: {entry.lastSeen ? dayjs(entry.lastSeen).format('YYYY-MM-DD HH:mm') : '-'}
- </Typography.Text>
- {entry.userAgent && (
- <>
- <br />
- <Typography.Text type="secondary" style={{ wordBreak: 'break-all' }}>{entry.userAgent}</Typography.Text>
- </>
- )}
- </div>
- ))}
- </div>
- ) : (
- <Tag>{t('pages.clients.noHwids')}</Tag>
- )}
- </Modal>
- </>
- );
- }
|