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', + '', + ]); + }); +});