diff --git a/e2e/trace-links.spec.ts b/e2e/trace-links.spec.ts index f2ed62a7..191af1b1 100644 --- a/e2e/trace-links.spec.ts +++ b/e2e/trace-links.spec.ts @@ -28,6 +28,24 @@ test('a connection opens the trace of its target and source', async ({page}) => await expect(page).toHaveURL(/#\/connections\?tab=list&id=1$/); }); +test('a connection traces with its source port and process, so process rules are decided', async ({page}) => { + const {api, handlers, requests} = await mockBackend(page); + handlers['GET connections'] = async () => { + const list = await api.connections({detail: 'full', limit: 1000}); + return {...list, tcp: list.tcp.map(row => (row.id === '1' ? {...row, src: '10.0.0.12:51234', pname: 'Telegram'} : row))}; + }; + handlers['POST routing/trace'] = request => api.routingTrace(request.postDataJSON()); + await page.goto('/#/connections?tab=list&id=1'); + await moreAction(detail(page), 'Trace this connection'); + await expect(page).toHaveURL(/&src_ip=10\.0\.0\.12&src_port=51234&pname=Telegram$/); + await expect(page.getByLabel('Source port', {exact: true})).toHaveValue('51234'); + await expect(page.getByLabel('Process name', {exact: true})).toHaveValue('Telegram'); + await page.getByRole('button', {name: 'Run trace', exact: true}).click(); + await expect.poll(() => requests.filter(request => request.method() === 'POST' && request.url().endsWith('/routing/trace')).length).toBe(1); + const traced = requests.find(request => request.method() === 'POST' && request.url().endsWith('/routing/trace'))!; + expect(traced.postDataJSON().input).toMatchObject({src_ip: '10.0.0.12', src_port: 51234, pname: 'Telegram'}); +}); + const runTrace = async (page: import('@playwright/test').Page, query: string) => { await page.goto('/#/rules?tab=trace&' + query); await page.getByRole('button', {name: 'Run trace', exact: true}).click(); diff --git a/src/features/connections/view.test.ts b/src/features/connections/view.test.ts index 11b176d4..3a472427 100644 --- a/src/features/connections/view.test.ts +++ b/src/features/connections/view.test.ts @@ -264,8 +264,14 @@ it('prepares fallback flow links and exports only visible raw counters', () => { const model = connectionsView([row], {...connections, visibility: 'partial'}, undefined, 'all', 'all', 'en-US', t); expect(connectionDetail(row, 'en-US', t, new Map(), false)?.flowQuery).toBe('tab=records&connection_id=a%2Fb'); expect( - connectionDetail({...row, domain: 'example.com', dst: '[2001:db8::1]:443', src: '10.0.0.2:5353', network: 'udp'}, 'en-US', t, new Map(), false)?.traceQuery - ).toBe('tab=trace&network=udp&domain=example.com&dst_ip=2001%3Adb8%3A%3A1&dst_port=443&src_ip=10.0.0.2'); + connectionDetail( + {...row, domain: 'example.com', dst: '[2001:db8::1]:443', src: '10.0.0.2:5353', network: 'udp', pname: 'curl'}, + 'en-US', + t, + new Map(), + false + )?.traceQuery + ).toBe('tab=trace&network=udp&domain=example.com&dst_ip=2001%3Adb8%3A%3A1&dst_port=443&src_ip=10.0.0.2&src_port=5353&pname=curl'); expect(connectionDetail({...row, domain: null, dst: undefined}, 'en-US', t, new Map(), false)?.traceQuery).toBeNull(); expect(connectionsExport([row], new Map())).toContain('9007199254740993'); expect(model.visibility).toBe(t('conn.visibilityPartial')); diff --git a/src/features/connections/view.ts b/src/features/connections/view.ts index 3fa98093..623b748b 100644 --- a/src/features/connections/view.ts +++ b/src/features/connections/view.ts @@ -347,7 +347,8 @@ export function connectionDetail( ([key, value]) => [t(key), typeof value === 'string' ? value : t(value.key, value.params)] as [string, string] ), flowQuery: within('', {tab: 'records', ...(current.flow_id ? {id: current.flow_id} : {connection_id: current.id})}), - // The trace of this connection's target, from its source; null when it has neither a domain nor an address. + // The trace of this connection's target, from its source and process; null when it has neither a domain nor an + // address. traceQuery: current.domain || sourceIp(current.dst) ? traceQuery({ @@ -355,7 +356,9 @@ export function connectionDetail( domain: current.domain ?? '', dst_ip: sourceIp(current.dst), dst_port: addressPort(current.dst), - src_ip: sourceIp(current.src) + src_ip: sourceIp(current.src), + src_port: addressPort(current.src), + pname: current.pname ?? '' }) : null, source: current.src ? (sourceIp(current.src) ?? current.src) : null, diff --git a/src/features/rules/useRoutingTrace.ts b/src/features/rules/useRoutingTrace.ts index 5f1b004c..6e257002 100644 --- a/src/features/rules/useRoutingTrace.ts +++ b/src/features/rules/useRoutingTrace.ts @@ -33,15 +33,15 @@ const blankForm = { resolve: null as TraceResolve | null }; // Held by the rules page rather than the trace tab, so what was typed survives a tab switch. A link that names a -// target fills the form in, opening the advanced fields when it names the source; it does not run the trace. +// target fills the form in, opening the advanced fields when it names the source or process; it does not run the trace. export function useTraceForm(query: string) { const linked = useMemo(() => parseTraceLink(query), [query]); const [form, setForm] = useState(() => (linked ? {...blankForm, ...linked} : blankForm)); - const [advanced, setAdvanced] = useState(!!linked?.src_ip); + const [advanced, setAdvanced] = useState(!!(linked?.src_ip || linked?.src_port || linked?.pname)); useLinked(linked && JSON.stringify(linked), () => { if (!linked) return; setForm({...blankForm, ...linked}); - setAdvanced(!!linked.src_ip); + setAdvanced(!!(linked.src_ip || linked.src_port || linked.pname)); }); return {form, setForm, advanced, setAdvanced}; } diff --git a/src/features/shared/link.test.ts b/src/features/shared/link.test.ts index cc029f76..ec2a1919 100644 --- a/src/features/shared/link.test.ts +++ b/src/features/shared/link.test.ts @@ -59,12 +59,20 @@ it('focuses a group by its backend id, and opens the page unfocused before the l expect(groupQuery(undefined, 'proxy')).toBe(''); }); -it('carries a trace target and source through the address and back', () => { - const link = {network: 'udp', domain: 'example.com', dst_ip: '2001:db8::1', dst_port: '443', src_ip: '10.0.0.2'} as const; +it('carries a trace target, source and process through the address and back', () => { + const link = {network: 'udp', domain: 'example.com', dst_ip: '2001:db8::1', dst_port: '443', src_ip: '10.0.0.2', src_port: '5353', pname: 'curl'} as const; expect(parseTraceLink(traceQuery(link))).toEqual(link); // Empty values stay out of the address; a missing network is TCP. expect(traceQuery({dst_ip: '1.1.1.1', domain: '', dst_port: '53'})).toBe('tab=trace&dst_ip=1.1.1.1&dst_port=53'); - expect(parseTraceLink('tab=trace&dst_ip=1.1.1.1')).toEqual({network: 'tcp', domain: '', dst_ip: '1.1.1.1', dst_port: '', src_ip: ''}); + expect(parseTraceLink('tab=trace&dst_ip=1.1.1.1')).toEqual({ + network: 'tcp', + domain: '', + dst_ip: '1.1.1.1', + dst_port: '', + src_ip: '', + src_port: '', + pname: '' + }); expect(parseTraceLink('tab=trace&src_ip=10.0.0.2')).toBeNull(); expect(parseTraceLink('tab=trace')).toBeNull(); }); diff --git a/src/features/shared/link.ts b/src/features/shared/link.ts index 03f13f4d..d5268c5e 100644 --- a/src/features/shared/link.ts +++ b/src/features/shared/link.ts @@ -19,10 +19,10 @@ export function groupQuery(groups: ReadonlyArray<{id: string; name: string}> | u return id ? within('', {group: id}) : ''; } -// The routing trace with its form filled in from a link: the target, and the source when the link knows it. The person -// runs the trace. -export type TraceLink = {network: 'tcp' | 'udp'; domain: string; dst_ip: string; dst_port: string; src_ip: string}; -const traceKeys = ['network', 'domain', 'dst_ip', 'dst_port', 'src_ip'] as const; +// The routing trace with its form filled in from a link: the target, and the source and process when the link knows +// them. The person runs the trace. +export type TraceLink = {network: 'tcp' | 'udp'; domain: string; dst_ip: string; dst_port: string; src_ip: string; src_port: string; pname: string}; +const traceKeys = ['network', 'domain', 'dst_ip', 'dst_port', 'src_ip', 'src_port', 'pname'] as const; export const traceQuery = (link: Partial) => within('', {tab: 'trace', ...Object.fromEntries(traceKeys.map(key => [key, link[key] || null]))}); // Null when the link names no target, so a plain visit to the tab keeps what was typed. export function parseTraceLink(query: string): TraceLink | null { @@ -34,7 +34,9 @@ export function parseTraceLink(query: string): TraceLink | null { domain: value('domain'), dst_ip: value('dst_ip'), dst_port: value('dst_port'), - src_ip: value('src_ip') + src_ip: value('src_ip'), + src_port: value('src_port'), + pname: value('pname') }; }