mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-10 05:10:58 +00:00
fix(warp): preserve outbound customization when rotating IP (#6052)
changeIp() rebuilt the warp outbound from a fixed default template via collectConfig and replaced the whole in-memory object through onResetOutbound, so only secretKey/address/reserved and the first peer's publicKey/endpoint survived — a user's custom mtu, domainStrategy, noKernelTun, the first peer's keepAlive / allowedIPs / preSharedKey, and any extra peers were silently dropped from the editor. Because the page keeps rendering that in-memory copy and a later page Save persists it to /panel/api/xray/update, the loss could become permanent, even though the backend ChangeWarpIP already patches only the rotated fields and leaves everything else intact in the stored template. Mirror the server-side UpdateWarpXraySetting: merge only the rotated fields (secretKey, address, reserved, first peer publicKey/endpoint) into the existing outbound, preserving the rest. register() (first-time creation, nothing to preserve) still uses collectConfig's full build. Extracted as a pure module-level mergeWarpRotation so it is unit-testable without rendering the modal. Fixes #6019
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { mergeWarpRotation } from '@/pages/xray/overrides/WarpModal';
|
||||
|
||||
const clientId = btoa(String.fromCharCode(1, 2, 3));
|
||||
|
||||
function rotatedConfig(overrides: { public_key?: string; host?: string; v4?: string; v6?: string } = {}) {
|
||||
return {
|
||||
config: {
|
||||
client_id: clientId,
|
||||
interface: { addresses: { v4: overrides.v4 ?? '172.16.0.2', v6: overrides.v6 ?? '2606:4700::2' } },
|
||||
peers: [{ public_key: overrides.public_key ?? 'newPub', endpoint: { host: overrides.host ?? 'engage.cloudflareclient.com:2408' } }],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('mergeWarpRotation', () => {
|
||||
it('patches only rotated fields and preserves user customizations', () => {
|
||||
const existing = {
|
||||
tag: 'warp',
|
||||
protocol: 'wireguard',
|
||||
settings: {
|
||||
mtu: 1280,
|
||||
secretKey: 'oldSecret',
|
||||
address: ['172.16.0.2/32'],
|
||||
reserved: [9, 9, 9],
|
||||
domainStrategy: 'ForceIPv4',
|
||||
noKernelTun: false,
|
||||
peers: [
|
||||
{
|
||||
publicKey: 'oldPub',
|
||||
endpoint: 'engage.cloudflareclient.com:2408',
|
||||
keepAlive: 25,
|
||||
allowedIPs: ['10.0.0.0/24'],
|
||||
preSharedKey: 'psk',
|
||||
},
|
||||
{ publicKey: 'extraPeer', endpoint: 'extra.test:51820' },
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const merged = mergeWarpRotation(
|
||||
existing,
|
||||
{ private_key: 'newSecret' },
|
||||
rotatedConfig({ public_key: 'newPub', host: 'engage.cloudflareclient.com:2408', v4: '172.16.0.9' }),
|
||||
);
|
||||
|
||||
expect(merged).not.toBeNull();
|
||||
const settings = (merged as { settings: Record<string, unknown> }).settings;
|
||||
expect(settings.secretKey).toBe('newSecret');
|
||||
expect(settings.address).toEqual(['172.16.0.9/32', '2606:4700::2/128']);
|
||||
expect(settings.reserved).toEqual([1, 2, 3]);
|
||||
const peers = settings.peers as Array<Record<string, unknown>>;
|
||||
expect(peers[0].publicKey).toBe('newPub');
|
||||
expect(peers[0].endpoint).toBe('engage.cloudflareclient.com:2408');
|
||||
expect(peers[0].keepAlive).toBe(25);
|
||||
expect(peers[0].allowedIPs).toEqual(['10.0.0.0/24']);
|
||||
expect(peers[0].preSharedKey).toBe('psk');
|
||||
expect(peers[1]).toEqual({ publicKey: 'extraPeer', endpoint: 'extra.test:51820' });
|
||||
expect(settings.mtu).toBe(1280);
|
||||
expect(settings.domainStrategy).toBe('ForceIPv4');
|
||||
expect(settings.noKernelTun).toBe(false);
|
||||
});
|
||||
|
||||
it('does not mutate the existing outbound object', () => {
|
||||
const existing = {
|
||||
tag: 'warp',
|
||||
protocol: 'wireguard',
|
||||
settings: {
|
||||
secretKey: 'oldSecret',
|
||||
address: ['172.16.0.2/32'],
|
||||
reserved: [9, 9, 9],
|
||||
peers: [{ publicKey: 'oldPub', endpoint: 'old:1', keepAlive: 25 }],
|
||||
},
|
||||
};
|
||||
const snapshot = JSON.parse(JSON.stringify(existing));
|
||||
|
||||
mergeWarpRotation(existing, { private_key: 'newSecret' }, rotatedConfig());
|
||||
|
||||
expect(existing).toEqual(snapshot);
|
||||
});
|
||||
|
||||
it('returns null when the rotation response has no peer', () => {
|
||||
expect(mergeWarpRotation(undefined, null, { config: { peers: [] } })).toBeNull();
|
||||
expect(mergeWarpRotation(undefined, null, null)).toBeNull();
|
||||
});
|
||||
|
||||
it('seeds a default warp outbound when none existed yet', () => {
|
||||
const merged = mergeWarpRotation(undefined, { private_key: 'newSecret' }, rotatedConfig());
|
||||
expect(merged).not.toBeNull();
|
||||
expect((merged as Record<string, unknown>).tag).toBe('warp');
|
||||
expect((merged as Record<string, unknown>).protocol).toBe('wireguard');
|
||||
const settings = (merged as { settings: Record<string, unknown> }).settings;
|
||||
expect(settings.secretKey).toBe('newSecret');
|
||||
expect(settings.peers).toEqual([
|
||||
{ publicKey: 'newPub', endpoint: 'engage.cloudflareclient.com:2408' },
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user