fix(hosts): apply a changed Port field to every address in the form (#7178)

The Address list shows each entry as address:port, and on save a port
inside an address wins over the Port field. After editing Port, the old
port stayed in every address and the field snapped back to its previous
value; the only way to change it was to retype every address.

Changing Port now rewrites the port of every address in the form before
saving, so what is shown is what gets saved. Port 0 leaves the addresses
bare, which inherits the inbound's port.
This commit is contained in:
Abol
2026-11-23 23:36:42 +03:30
committed by GitHub
parent 83535c74e6
commit f0d48afe6f
4 changed files with 104 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
function addressOf(entry: string): string {
const value = entry.trim();
if (value.startsWith('[')) {
const close = value.indexOf(']');
return close === -1 ? value : value.slice(1, close);
}
if (value.split(':').length > 2) return value;
const colon = value.lastIndexOf(':');
return colon !== -1 && /^\d+$/.test(value.slice(colon + 1)) ? value.slice(0, colon) : value;
}
// Port 0 means "inherit the inbound port", so it leaves the addresses bare.
export function withHostPort(entries: string[], port: number): string[] {
return entries.map((entry) => {
const address = addressOf(entry);
if (!port || port <= 0) return address;
return address.includes(':') ? `[${address}]:${port}` : `${address}:${port}`;
});
}
@@ -34,6 +34,7 @@ import { FormField, rhfZodValidate } from '@/components/form/rhf';
import { useNodesQuery } from '@/api/queries/useNodesQuery';
import { useMediaQuery } from '@/hooks/useMediaQuery';
import { catTabLabel } from '@/pages/settings/catTabLabel';
import { withHostPort } from '@/lib/hosts/host-port';
import { HostFinalMaskForm, HostMuxForm, HostSockoptForm } from './json-forms';
type FormShape = Omit<BulkAddHostValues, 'isDisabled'> & {
@@ -318,6 +319,12 @@ export default function HostFormModal({
name="port"
label={t('pages.hosts.fields.port')}
tooltip={t('pages.hosts.hints.port')}
onAfterChange={(value) =>
methods.setValue(
'hosts',
withHostPort(methods.getValues('hosts') ?? [], Number(value) || 0),
)
}
>
<InputNumber min={0} max={65535} />
</FormField>
+47
View File
@@ -0,0 +1,47 @@
import { fireEvent, screen, waitFor } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { HostRecord } from '@/api/queries/useHostsQuery';
import HostFormModal from '@/pages/hosts/HostFormModal';
import { HttpUtil, Msg } from '@/utils';
import { renderWithProviders } from './test-utils';
const host = {
groupId: 'g1',
remark: 'DE WS',
inboundIds: [1],
hosts: ['a.example.com:8443', 'b.example.com:2096'],
port: 8443,
security: 'same',
} as HostRecord;
describe('Host form port', () => {
beforeEach(() => {
vi.spyOn(HttpUtil, 'get').mockResolvedValue(new Msg(true, '', []));
});
it('rewrites every address to the new port when the Port field changes', async () => {
const save = vi.fn().mockResolvedValue({ success: true });
renderWithProviders(
<HostFormModal
open
mode="edit"
host={host}
inboundOptions={[{ id: 1, remark: 'in', tag: 'in', protocol: 'vless' }]}
existingHosts={[host]}
save={save}
onOpenChange={() => {}}
/>,
);
const port = await screen.findByRole('spinbutton');
fireEvent.change(port, { target: { value: '9443' } });
fireEvent.blur(port);
fireEvent.click(screen.getByRole('button', { name: 'Save' }));
await waitFor(() => expect(save).toHaveBeenCalledTimes(1));
expect(save.mock.calls[0][0]).toMatchObject({
port: 9443,
hosts: ['a.example.com:9443', 'b.example.com:9443'],
});
});
});
+31
View File
@@ -0,0 +1,31 @@
import { describe, expect, it } from 'vitest';
import { withHostPort } from '@/lib/hosts/host-port';
describe('withHostPort', () => {
it('replaces the port of every address, including ones that already carry another', () => {
expect(
withHostPort(['a.example.com:8443', 'b.example.com:2096', 'c.example.com'], 9443),
).toEqual(['a.example.com:9443', 'b.example.com:9443', 'c.example.com:9443']);
});
it('brackets bare IPv6 and keeps bracketed IPv6 and CIDR ranges intact', () => {
expect(withHostPort(['2001:db8::1', '[2001:db8::2]:443', '10.0.0.0/24:443'], 9443)).toEqual([
'[2001:db8::1]:9443',
'[2001:db8::2]:9443',
'10.0.0.0/24:9443',
]);
});
it('keeps a port-only entry port-only', () => {
expect(withHostPort([':8443'], 9443)).toEqual([':9443']);
});
it('strips the ports when the new port is 0 (inherit the inbound port)', () => {
expect(withHostPort(['a.example.com:8443', '[2001:db8::1]:443', ':8443'], 0)).toEqual([
'a.example.com',
'2001:db8::1',
'',
]);
});
});