mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-12 14:21:01 +00:00
feat(wireguard): multi-client support
WireGuard inbounds now manage per-client peers using xray-core's native WireGuard users (AddUser/RemoveUser). Each client lives in settings.clients (canonical, like every other protocol) and is projected to peers[] only when emitting the xray config, at level 0 so the dispatcher's per-user traffic/online counters work with no extra plumbing. Backend: internal/util/wireguard gains KeyToHex (base64 to hex for the gRPC path), PublicKeyFromPrivate and GenerateWireguardPSK; xray/api.go builds a wireguard account in AddUser with hex keys (RemoveUser already worked); client CRUD generates a keypair and allocates a unique tunnel address per client and never rotates keys on edit; an idempotent migration converts legacy settings.peers into managed clients; WireGuard is included in the raw subscription. Frontend: WireGuard in the add-client modal with keys on the credential tab, client schema, per-client QR/link/.conf, inbound form reduced to server settings; i18n added across 13 locales. Fix: guard the settings[clients] assertion in add/update so a legacy WireGuard inbound stored without a clients key no longer panics.
This commit is contained in:
@@ -16,7 +16,7 @@ import { ClientBulkAddFormSchema, type ClientBulkAddFormValues } from '@/schemas
|
||||
const FLOW_OPTIONS = Object.values(TLS_FLOW_CONTROL);
|
||||
|
||||
const MULTI_CLIENT_PROTOCOLS = new Set([
|
||||
'shadowsocks', 'vless', 'vmess', 'trojan', 'hysteria',
|
||||
'shadowsocks', 'vless', 'vmess', 'trojan', 'hysteria', 'wireguard',
|
||||
]);
|
||||
|
||||
interface ClientBulkAddModalProps {
|
||||
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
import { DeleteOutlined, EyeOutlined, PlusOutlined, ReloadOutlined, RetweetOutlined } from '@ant-design/icons';
|
||||
import dayjs from 'dayjs';
|
||||
import type { Dayjs } from 'dayjs';
|
||||
import { HttpUtil, RandomUtil } from '@/utils';
|
||||
import { HttpUtil, RandomUtil, Wireguard } from '@/utils';
|
||||
import { formatInboundLabel } from '@/lib/inbounds/label';
|
||||
import { normalizeClientIps, type ClientIpInfo } from '@/lib/clients/ip-log';
|
||||
import { DateTimePicker, SelectAllClearButtons } from '@/components/form';
|
||||
@@ -35,7 +35,7 @@ const FLOW_OPTIONS = Object.values(TLS_FLOW_CONTROL);
|
||||
const VMESS_SECURITY_OPTIONS = ['auto', 'aes-128-gcm', 'chacha20-poly1305', 'none', 'zero'] as const;
|
||||
|
||||
const MULTI_CLIENT_PROTOCOLS = new Set([
|
||||
'shadowsocks', 'vless', 'vmess', 'trojan', 'hysteria',
|
||||
'shadowsocks', 'vless', 'vmess', 'trojan', 'hysteria', 'wireguard',
|
||||
]);
|
||||
|
||||
const CLIENT_FORM_MODAL_Z_INDEX = 1000;
|
||||
@@ -113,6 +113,10 @@ interface FormState {
|
||||
enable: boolean;
|
||||
inboundIds: number[];
|
||||
externalLinks: ExternalLinkRow[];
|
||||
wgPrivateKey: string;
|
||||
wgPublicKey: string;
|
||||
wgPreSharedKey: string;
|
||||
wgAllowedIPs: string;
|
||||
}
|
||||
|
||||
function emptyForm(): FormState {
|
||||
@@ -137,6 +141,10 @@ function emptyForm(): FormState {
|
||||
enable: true,
|
||||
inboundIds: [],
|
||||
externalLinks: [],
|
||||
wgPrivateKey: '',
|
||||
wgPublicKey: '',
|
||||
wgPreSharedKey: '',
|
||||
wgAllowedIPs: '',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -237,6 +245,10 @@ export default function ClientFormModal({
|
||||
enable: !!client.enable,
|
||||
inboundIds: Array.isArray(attachedIds) ? [...attachedIds] : [],
|
||||
externalLinks: toExternalLinkRows(attachedExternalLinks),
|
||||
wgPrivateKey: client.privateKey || '',
|
||||
wgPublicKey: client.publicKey || '',
|
||||
wgPreSharedKey: client.preSharedKey || '',
|
||||
wgAllowedIPs: client.allowedIPs || '',
|
||||
};
|
||||
if (et < 0) {
|
||||
next.delayedStart = true;
|
||||
@@ -250,6 +262,7 @@ export default function ClientFormModal({
|
||||
setForm(next);
|
||||
void loadIps();
|
||||
} else {
|
||||
const wgKeypair = Wireguard.generateKeypair();
|
||||
setForm({
|
||||
...emptyForm(),
|
||||
email: RandomUtil.randomLowerAndNum(10),
|
||||
@@ -257,6 +270,8 @@ export default function ClientFormModal({
|
||||
subId: RandomUtil.randomLowerAndNum(16),
|
||||
password: RandomUtil.randomLowerAndNum(16),
|
||||
auth: RandomUtil.randomLowerAndNum(16),
|
||||
wgPrivateKey: wgKeypair.privateKey,
|
||||
wgPublicKey: wgKeypair.publicKey,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -287,6 +302,14 @@ export default function ClientFormModal({
|
||||
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 ss2022Method = useMemo(() => {
|
||||
for (const id of form.inboundIds || []) {
|
||||
const ib = (inbounds || []).find((row) => row.id === id);
|
||||
@@ -317,6 +340,16 @@ export default function ClientFormModal({
|
||||
[form.inboundIds, vmessIds],
|
||||
);
|
||||
|
||||
const showWireguard = useMemo(
|
||||
() => (form.inboundIds || []).some((id) => wireguardIds.has(id)),
|
||||
[form.inboundIds, wireguardIds],
|
||||
);
|
||||
|
||||
function regenerateWireguardKeys() {
|
||||
const kp = Wireguard.generateKeypair();
|
||||
setForm((prev) => ({ ...prev, wgPrivateKey: kp.privateKey, wgPublicKey: kp.publicKey }));
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!showFlow && form.flow) {
|
||||
|
||||
@@ -453,6 +486,14 @@ export default function ClientFormModal({
|
||||
clientPayload.reverse = { tag: reverseTag };
|
||||
}
|
||||
|
||||
if (showWireguard) {
|
||||
clientPayload.privateKey = form.wgPrivateKey;
|
||||
clientPayload.publicKey = form.wgPublicKey;
|
||||
if (form.wgPreSharedKey) {
|
||||
clientPayload.preSharedKey = form.wgPreSharedKey;
|
||||
}
|
||||
}
|
||||
|
||||
const externalLinks: ExternalLinkInput[] = form.externalLinks
|
||||
.map((r) => ({ kind: r.kind, value: r.value.trim(), remark: '' }))
|
||||
.filter((r) => r.value !== '');
|
||||
@@ -736,6 +777,38 @@ export default function ClientFormModal({
|
||||
/>
|
||||
</Form.Item>
|
||||
)}
|
||||
{showWireguard && (
|
||||
<>
|
||||
<Form.Item label={t('pages.clients.wireguardPrivateKey')}>
|
||||
<Space.Compact style={{ display: 'flex' }}>
|
||||
<Input
|
||||
value={form.wgPrivateKey}
|
||||
style={{ flex: 1 }}
|
||||
onChange={(e) => {
|
||||
const priv = e.target.value;
|
||||
update('wgPrivateKey', priv);
|
||||
update('wgPublicKey', priv ? Wireguard.generateKeypair(priv).publicKey : '');
|
||||
}}
|
||||
/>
|
||||
<Button icon={<ReloadOutlined />} onClick={regenerateWireguardKeys} />
|
||||
</Space.Compact>
|
||||
</Form.Item>
|
||||
<Form.Item label={t('pages.clients.wireguardPublicKey')}>
|
||||
<Input value={form.wgPublicKey} disabled />
|
||||
</Form.Item>
|
||||
<Form.Item label={t('pages.clients.wireguardPreSharedKey')}>
|
||||
<Input
|
||||
value={form.wgPreSharedKey}
|
||||
onChange={(e) => update('wgPreSharedKey', e.target.value)}
|
||||
/>
|
||||
</Form.Item>
|
||||
{isEdit && form.wgAllowedIPs && (
|
||||
<Form.Item label={t('pages.clients.wireguardAllowedIPs')}>
|
||||
<Input value={form.wgAllowedIPs} disabled />
|
||||
</Form.Item>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
),
|
||||
},
|
||||
|
||||
@@ -278,12 +278,6 @@ export default function InboundFormModal({
|
||||
form.setFieldValue(['settings', 'secretKey'], kp.privateKey);
|
||||
};
|
||||
|
||||
const regenWgPeerKeypair = (peerName: number) => {
|
||||
const kp = Wireguard.generateKeypair();
|
||||
form.setFieldValue(['settings', 'peers', peerName, 'privateKey'], kp.privateKey);
|
||||
form.setFieldValue(['settings', 'peers', peerName, 'publicKey'], kp.publicKey);
|
||||
};
|
||||
|
||||
const matchesVlessAuth = (
|
||||
block: { id?: string; label?: string } | undefined | null,
|
||||
authId: string,
|
||||
@@ -695,7 +689,7 @@ export default function InboundFormModal({
|
||||
|
||||
const protocolTab = (
|
||||
<>
|
||||
{protocol === Protocols.WIREGUARD && <WireguardFields wgPubKey={wgPubKey} regenInboundWg={regenInboundWg} regenWgPeerKeypair={regenWgPeerKeypair} />}
|
||||
{protocol === Protocols.WIREGUARD && <WireguardFields wgPubKey={wgPubKey} regenInboundWg={regenInboundWg} />}
|
||||
|
||||
{protocol === Protocols.TUN && <TunFields />}
|
||||
|
||||
|
||||
@@ -1,44 +1,14 @@
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Button, Divider, Form, Input, InputNumber, Select, Space, Switch } from 'antd';
|
||||
import { MinusOutlined, PlusOutlined, ReloadOutlined } from '@ant-design/icons';
|
||||
|
||||
import { Wireguard } from '@/utils';
|
||||
import { Button, Form, Input, InputNumber, Select, Space, Switch } from 'antd';
|
||||
import { ReloadOutlined } from '@ant-design/icons';
|
||||
|
||||
interface WireguardFieldsProps {
|
||||
wgPubKey: string;
|
||||
regenInboundWg: () => void;
|
||||
regenWgPeerKeypair: (name: number) => void;
|
||||
}
|
||||
|
||||
function nextWgPeerAllowedIP(peers: Array<{ allowedIPs?: string[] }> | undefined): string {
|
||||
const fallback = '10.0.0.2/32';
|
||||
let maxInt = -1;
|
||||
let prefix = 32;
|
||||
for (const peer of peers ?? []) {
|
||||
for (const ip of peer?.allowedIPs ?? []) {
|
||||
const m = /^\s*(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(?:\/(\d{1,2}))?\s*$/.exec(String(ip));
|
||||
if (!m) continue;
|
||||
const octets = [Number(m[1]), Number(m[2]), Number(m[3]), Number(m[4])];
|
||||
if (octets.some((o) => o > 255)) continue;
|
||||
const asInt = octets[0] * 16777216 + octets[1] * 65536 + octets[2] * 256 + octets[3];
|
||||
if (asInt > maxInt) {
|
||||
maxInt = asInt;
|
||||
prefix = m[5] !== undefined ? Math.min(Number(m[5]), 32) : 32;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (maxInt < 0) return fallback;
|
||||
const next = maxInt + 1;
|
||||
const a = Math.floor(next / 16777216) % 256;
|
||||
const b = Math.floor(next / 65536) % 256;
|
||||
const c = Math.floor(next / 256) % 256;
|
||||
const d = next % 256;
|
||||
return `${a}.${b}.${c}.${d}/${prefix}`;
|
||||
}
|
||||
|
||||
export default function WireguardFields({ wgPubKey, regenInboundWg, regenWgPeerKeypair }: WireguardFieldsProps) {
|
||||
export default function WireguardFields({ wgPubKey, regenInboundWg }: WireguardFieldsProps) {
|
||||
const { t } = useTranslation();
|
||||
const form = Form.useFormInstance();
|
||||
return (
|
||||
<>
|
||||
<Form.Item label={t('pages.xray.wireguard.secretKey')}>
|
||||
@@ -74,96 +44,6 @@ export default function WireguardFields({ wgPubKey, regenInboundWg, regenWgPeerK
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.List name={['settings', 'peers']}>
|
||||
{(fields, { add, remove }) => (
|
||||
<>
|
||||
<Form.Item label={t('pages.inbounds.form.peers')}>
|
||||
<Button
|
||||
size="small"
|
||||
onClick={() => {
|
||||
const kp = Wireguard.generateKeypair();
|
||||
const peers = form.getFieldValue(['settings', 'peers']) as Array<{ allowedIPs?: string[] }> | undefined;
|
||||
add({
|
||||
privateKey: kp.privateKey,
|
||||
publicKey: kp.publicKey,
|
||||
allowedIPs: [nextWgPeerAllowedIP(peers)],
|
||||
keepAlive: 0,
|
||||
});
|
||||
}}
|
||||
>
|
||||
<PlusOutlined /> {t('pages.inbounds.form.addPeer')}
|
||||
</Button>
|
||||
</Form.Item>
|
||||
{fields.map((field, idx) => (
|
||||
<div key={field.key} className="wg-peer">
|
||||
<Divider titlePlacement="center">
|
||||
<Space>
|
||||
<span>{t('pages.inbounds.info.peerNumber', { n: idx + 1 })}</span>
|
||||
<Form.Item noStyle shouldUpdate>
|
||||
{() => {
|
||||
const comment = form.getFieldValue(['settings', 'peers', field.name, 'comment']) as string | undefined;
|
||||
return comment ? <span style={{ opacity: 0.65 }}>— {comment}</span> : null;
|
||||
}}
|
||||
</Form.Item>
|
||||
{fields.length > 1 && (
|
||||
<Button
|
||||
size="small"
|
||||
danger
|
||||
icon={<MinusOutlined />}
|
||||
onClick={() => remove(field.name)}
|
||||
/>
|
||||
)}
|
||||
</Space>
|
||||
</Divider>
|
||||
<Form.Item name={[field.name, 'comment']} label={t('comment')}>
|
||||
<Input placeholder="e.g. Alice's laptop" />
|
||||
</Form.Item>
|
||||
<Form.Item label={t('pages.xray.wireguard.secretKey')}>
|
||||
<Space.Compact block>
|
||||
<Form.Item name={[field.name, 'privateKey']} noStyle>
|
||||
<Input style={{ width: 'calc(100% - 32px)' }} />
|
||||
</Form.Item>
|
||||
<Button
|
||||
icon={<ReloadOutlined />}
|
||||
onClick={() => regenWgPeerKeypair(field.name)}
|
||||
/>
|
||||
</Space.Compact>
|
||||
</Form.Item>
|
||||
<Form.Item name={[field.name, 'publicKey']} label={t('pages.xray.wireguard.publicKey')}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name={[field.name, 'preSharedKey']} label="PSK">
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.List name={[field.name, 'allowedIPs']}>
|
||||
{(ipFields, { add: addIp, remove: removeIp }) => (
|
||||
<Form.Item label={t('pages.xray.wireguard.allowedIPs')}>
|
||||
<Button size="small" onClick={() => addIp('')}>
|
||||
<PlusOutlined />
|
||||
</Button>
|
||||
{ipFields.map((ipField) => (
|
||||
<Space.Compact key={ipField.key} block className="mt-4">
|
||||
<Form.Item name={ipField.name} noStyle>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
{ipFields.length > 1 && (
|
||||
<Button size="small" onClick={() => removeIp(ipField.name)}>
|
||||
<MinusOutlined />
|
||||
</Button>
|
||||
)}
|
||||
</Space.Compact>
|
||||
))}
|
||||
</Form.Item>
|
||||
)}
|
||||
</Form.List>
|
||||
<Form.Item name={[field.name, 'keepAlive']} label={t('pages.inbounds.form.keepAlive')}>
|
||||
<InputNumber min={0} />
|
||||
</Form.Item>
|
||||
</div>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
</Form.List>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user