From f0d48afe6f2faed917dfac976a2f54048f878efb Mon Sep 17 00:00:00 2001 From: Abol <107064351+AbolDev@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:36:42 +0330 Subject: [PATCH] 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. --- frontend/src/lib/hosts/host-port.ts | 19 +++++++++ frontend/src/pages/hosts/HostFormModal.tsx | 7 ++++ frontend/src/test/host-form-port.test.tsx | 47 ++++++++++++++++++++++ frontend/src/test/host-port.test.ts | 31 ++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 frontend/src/lib/hosts/host-port.ts create mode 100644 frontend/src/test/host-form-port.test.tsx create mode 100644 frontend/src/test/host-port.test.ts diff --git a/frontend/src/lib/hosts/host-port.ts b/frontend/src/lib/hosts/host-port.ts new file mode 100644 index 000000000..1f53fca7b --- /dev/null +++ b/frontend/src/lib/hosts/host-port.ts @@ -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}`; + }); +} diff --git a/frontend/src/pages/hosts/HostFormModal.tsx b/frontend/src/pages/hosts/HostFormModal.tsx index 8c6b37b40..d2237dbb1 100644 --- a/frontend/src/pages/hosts/HostFormModal.tsx +++ b/frontend/src/pages/hosts/HostFormModal.tsx @@ -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 & { @@ -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), + ) + } > diff --git a/frontend/src/test/host-form-port.test.tsx b/frontend/src/test/host-form-port.test.tsx new file mode 100644 index 000000000..07403c8e2 --- /dev/null +++ b/frontend/src/test/host-form-port.test.tsx @@ -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( + {}} + />, + ); + 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'], + }); + }); +}); diff --git a/frontend/src/test/host-port.test.ts b/frontend/src/test/host-port.test.ts new file mode 100644 index 000000000..81460fb5c --- /dev/null +++ b/frontend/src/test/host-port.test.ts @@ -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', + '', + ]); + }); +});