Files
linux-cloud-router/web/src/ipsec.ts
T

67 lines
3.0 KiB
TypeScript

import type { RouterDetails } from './api.ts'
import { bytes, duration, text } from './format.ts'
/** The column titles of the IPsec tables of ipsecRows. */
export const IPSEC_HEADS = {
sas: ['Connection', 'State', 'Local', 'Remote', 'Algorithms', 'Up'],
childSas: ['Child SA', 'State', 'Traffic selectors', 'In', 'Out', 'Up'],
connections: ['Connection', 'Version', 'Local address', 'Local identity', 'Remote address', 'Remote identity', 'Authentication'],
children: ['Child', 'Mode', 'Local traffic selectors', 'Remote traffic selectors'],
}
/**
* The rows of the IPsec tables of the server's /config for the connections whose names keep accepts: the IKE SAs, their
* child SAs, the connections and their children; undefined where the server could not list them.
*/
export function ipsecRows(details: RouterDetails | null, keep: (name: string) => boolean) {
const sas = details?.ipsec.sas?.filter((sa) => keep(sa.name))
const connections = details?.ipsec.connections?.filter((connection) => keep(connection.name))
return {
sas: sas?.map((sa) => [
sa.name,
sa.state,
`${sa['local-id']} @ ${sa['local-host']}`,
`${sa['remote-id']} @ ${sa['remote-host']}`,
[`${text(sa['encr-alg'])}-${text(sa['encr-keysize'])}`, sa['integ-alg'], sa['dh-group']].filter(Boolean).join(', '),
sa.established === undefined ? '-' : duration(Number(sa.established)),
]),
childSas: sas?.flatMap((sa) =>
Object.values(sa['child-sas'] ?? {}).map((child) => [
`${sa.name}/${child.name}`,
`${child.state}, ${child.mode}`,
`${child['local-ts'].join(', ')} <-> ${child['remote-ts'].join(', ')}`,
`${bytes(Number(child['bytes-in']))}, ${child['packets-in']} packets`,
`${bytes(Number(child['bytes-out']))}, ${child['packets-out']} packets`,
child['install-time'] === undefined ? '-' : duration(Number(child['install-time'])),
]),
),
connections: connections?.map((connection) => {
const local = authOf(connection, 'local')
const remote = authOf(connection, 'remote')
return [
connection.name,
connection.version,
text(connection.local_addrs),
text(local.id),
text(connection.remote_addrs),
text(remote.id),
local.class === remote.class ? text(local.class) : `${text(local.class)} / ${text(remote.class)}`,
]
}),
children: connections?.flatMap((connection) =>
Object.entries(connection.children ?? {}).map(([name, child]) => [
`${connection.name}/${name}`,
child.mode,
child['local-ts'].join(', '),
child['remote-ts'].join(', '),
]),
),
}
}
/** The authentication round of one side of a connection: swanctl names them local-1, remote-1 and so on. */
function authOf(connection: Record<string, unknown>, side: 'local' | 'remote'): { class?: string; id?: string } {
const round = Object.entries(connection).find(([key, value]) => key.startsWith(`${side}-`) && typeof value === 'object' && value !== null)
return (round?.[1] ?? {}) as { class?: string; id?: string }
}