213 lines
11 KiB
TypeScript
213 lines
11 KiB
TypeScript
import Description from '@mui/icons-material/Description'
|
|
import Edit from '@mui/icons-material/Edit'
|
|
import Refresh from '@mui/icons-material/Refresh'
|
|
import { Alert, Box, Button, Chip, Stack } from '@mui/material'
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import { Board, BoardButtons, type BoardPanel } from './PanelBoard.tsx'
|
|
import { useBoard } from './useBoard.ts'
|
|
import RemoteAccessDialog from './RemoteAccessDialog.tsx'
|
|
import Toolbar from './Toolbar.tsx'
|
|
import WireGuardConfigDialog from './WireGuardConfigDialog.tsx'
|
|
import WireGuardPeers from './WireGuardPeers.tsx'
|
|
import { Copyable, Grid, Pairs } from './ServerPanel.tsx'
|
|
import { bytes, text } from './format.ts'
|
|
import { IPSEC_HEADS, ipsecRows } from './ipsec.ts'
|
|
import {
|
|
DETAILS_SHAPE, REMOTE_ACCESS_LABELS, REMOTE_ACCESS_SECTION_SHAPES, REMOTE_ACCESS_SHAPE, STATUS_SHAPE, USERS_SHAPE, request,
|
|
type RemoteAccess, type RemoteAccessSystem, type RouterDetails, type RouterStatus, type Users,
|
|
} from './api.ts'
|
|
|
|
/** The localStorage key of each system's layout. */
|
|
const LAYOUT_KEYS: Record<RemoteAccessSystem, string> = {
|
|
p2s: 'linux-manager-ui.remote-access-p2s-layout',
|
|
openvpn: 'linux-manager-ui.remote-access-openvpn-layout',
|
|
wireguard: 'linux-manager-ui.remote-access-wireguard-layout',
|
|
}
|
|
|
|
/** What each system describes, above its panels. */
|
|
const DESCRIPTIONS: Record<RemoteAccessSystem, string> = {
|
|
p2s: 'IKEv2 remote access.',
|
|
openvpn: 'OpenVPN remote access.',
|
|
wireguard: 'WireGuard remote access.',
|
|
}
|
|
|
|
/**
|
|
* The settings and the state of one remote access system of the server, from its /remote-access and /status/full: the
|
|
* settings with Edit, whether setup runs the system, why an enabled one does not run, and its connected clients; for
|
|
* IKEv2, from /config too, the IPsec security associations, child SAs, connections and connection children of all but
|
|
* the site-to-site connections, that is of IKEv2 access; for WireGuard, from /users and /config too, the peers of the users, added and removed here (WireGuardPeers), and each
|
|
* connection with its user, found by the peer's key, its IPs, endpoint, handshake and traffic. The server applies a
|
|
* change in the background; Refresh reads the state again. The panels are on a PanelBoard whose layout
|
|
* is kept in the browser.
|
|
*/
|
|
export default function RemoteAccessPanel(props: { system: RemoteAccessSystem }) {
|
|
const [access, setAccess] = useState<RemoteAccess | null>(null)
|
|
const [status, setStatus] = useState<RouterStatus | null>(null)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [busy, setBusy] = useState(true)
|
|
const [editing, setEditing] = useState(false)
|
|
const [showConfig, setShowConfig] = useState(false)
|
|
const board = useBoard(LAYOUT_KEYS[props.system])
|
|
// WireGuard's: the users with their peers, and the server's details with each peer's key, endpoint and traffic.
|
|
const [users, setUsers] = useState<Users | null>(null)
|
|
const [details, setDetails] = useState<RouterDetails | null>(null)
|
|
const wireguard = props.system === 'wireguard'
|
|
// IKEv2's: the server's details with its IPsec SAs and connections, those of IKEv2 access being all but site to site.
|
|
const ikev2 = props.system === 'p2s'
|
|
|
|
/** Reads the access of the system with what it shows; the state changes only once the server answers. */
|
|
const read = useCallback(async () => {
|
|
await Promise.all([
|
|
request<RemoteAccess>('GET', '/remote-access', REMOTE_ACCESS_SHAPE),
|
|
request<RouterStatus>('GET', '/status/full', STATUS_SHAPE),
|
|
wireguard ? request<Users>('GET', '/users', USERS_SHAPE) : null,
|
|
wireguard || ikev2 ? request<RouterDetails>('GET', '/config', DETAILS_SHAPE) : null,
|
|
])
|
|
.then(([nextAccess, nextStatus, nextUsers, nextDetails]) => {
|
|
setAccess(nextAccess)
|
|
setStatus(nextStatus)
|
|
setUsers(nextUsers)
|
|
setDetails(nextDetails)
|
|
setError(null)
|
|
}, (caught) => setError((caught as Error).message))
|
|
.finally(() => setBusy(false))
|
|
}, [wireguard, ikev2])
|
|
|
|
/** Reads the access again, busy until the server answers. */
|
|
const refresh = useCallback(() => {
|
|
setBusy(true)
|
|
return read()
|
|
}, [read])
|
|
|
|
useEffect(() => {
|
|
void read()
|
|
}, [read])
|
|
|
|
async function save(settings: Record<string, unknown>) {
|
|
await request('PUT', `/remote-access/${props.system}`, REMOTE_ACCESS_SECTION_SHAPES[props.system], settings)
|
|
setEditing(false)
|
|
await refresh()
|
|
}
|
|
|
|
const system = props.system
|
|
const settings = access?.[system]
|
|
const running = access?.running[system] ?? false
|
|
const rows: [string, string][] = !access ? [] : system === 'p2s'
|
|
? [['Enabled', access.p2s.enabled ? 'yes' : 'no'], ['Address pool', text(access.p2s.address_pool)],
|
|
['DNS domains', access.p2s.dns_domains.join(', ') || '-']]
|
|
: system === 'openvpn'
|
|
? [['Enabled', access.openvpn.enabled ? 'yes' : 'no'], ['Address pool', text(access.openvpn.address_pool)],
|
|
['Port', `${access.openvpn.port}/${access.openvpn.protocol}`]]
|
|
: [['Enabled', access.wireguard.enabled ? 'yes' : 'no'], ['Address of wg0', text(access.wireguard.address)],
|
|
['UDP port', String(access.wireguard.listen_port)]]
|
|
const clients = system === 'p2s' ? status?.p2s?.clients : system === 'openvpn' ? status?.openvpn?.clients : undefined
|
|
const peers = status?.wireguard?.peers
|
|
const siteToSite = new Set((details?.network.site_to_site ?? []).map((connection) => connection.name))
|
|
const ipsec = ipsecRows(ikev2 ? details : null, (name) => !siteToSite.has(name))
|
|
// An IPsec table is shown with rows, or with the note that the server could not read them.
|
|
const shown = (rows: string[][] | undefined) => ikev2 && details !== null && (rows === undefined || rows.length > 0)
|
|
|
|
const panels: BoardPanel[] = access ? [
|
|
{ id: 'settings', title: 'Settings', content: <Pairs rows={rows} /> },
|
|
{
|
|
id: 'state',
|
|
title: 'State',
|
|
content: (
|
|
<Pairs
|
|
rows={[
|
|
['Running', <Chip key="running" size="small" label={running ? 'running' : 'not running'} color={running ? 'success' : 'default'} />],
|
|
...(system === 'wireguard'
|
|
? [['Peers', peers === undefined || peers === null ? '-' : String(peers.length)] as [string, string]]
|
|
: [['Connected clients', clients === undefined || clients === null ? '-' : String(clients)] as [string, string]]),
|
|
]}
|
|
/>
|
|
),
|
|
},
|
|
...(shown(ipsec.sas) ? [{ id: 'ipsec-sas', title: 'IPsec security associations', wide: true, content: <Grid head={IPSEC_HEADS.sas} rows={ipsec.sas} /> }] : []),
|
|
...(shown(ipsec.childSas) ? [{ id: 'ipsec-child-sas', title: 'IPsec child SAs', wide: true, content: <Grid head={IPSEC_HEADS.childSas} rows={ipsec.childSas} /> }] : []),
|
|
...(shown(ipsec.connections) ? [{ id: 'ipsec-connections', title: 'IPsec connections', wide: true, content: <Grid head={IPSEC_HEADS.connections} rows={ipsec.connections} /> }] : []),
|
|
...(shown(ipsec.children) ? [{ id: 'ipsec-children', title: 'IPsec connection children', wide: true, content: <Grid head={IPSEC_HEADS.children} rows={ipsec.children} /> }] : []),
|
|
...(system === 'wireguard' ? [{
|
|
id: 'server',
|
|
title: 'Server',
|
|
wide: true,
|
|
content: (
|
|
<>
|
|
<Pairs
|
|
rows={[
|
|
['Public key', access.wireguard_server.public_key === null ? 'not made yet; setup makes it once WireGuard is enabled'
|
|
: <Copyable key="key" value={access.wireguard_server.public_key} label="public key" />],
|
|
['Endpoint', access.wireguard_server.endpoint ?? 'unknown: wan.local_fqdn is not set'],
|
|
['Allowed IPs', text(access.wireguard_server.allowed_ips)],
|
|
]}
|
|
/>
|
|
<Stack direction="row" sx={{ mt: 1, justifyContent: 'flex-end' }}>
|
|
<Button
|
|
size="small"
|
|
variant="outlined"
|
|
startIcon={<Description />}
|
|
disabled={!access.wireguard_server.public_key || !access.wireguard_server.endpoint}
|
|
onClick={() => setShowConfig(true)}
|
|
>
|
|
Show config
|
|
</Button>
|
|
</Stack>
|
|
</>
|
|
),
|
|
}] : []),
|
|
...(system === 'wireguard' ? [{
|
|
id: 'peers',
|
|
title: 'Peers',
|
|
wide: true,
|
|
content: <WireGuardPeers server={access.wireguard_server} users={users} onChanged={() => void refresh()} />,
|
|
}] : []),
|
|
...(system === 'wireguard' && running ? [{
|
|
id: 'connections',
|
|
title: 'Connections',
|
|
wide: true,
|
|
content: (
|
|
<Grid
|
|
head={['User', 'IPs', 'Endpoint', 'Handshake', 'Received', 'Sent']}
|
|
rows={details?.wireguard?.peers.map((peer) => {
|
|
// A peer is a user's by its key; one added to wg0.conf by hand has no user.
|
|
const owner = users?.users.find((user) => user.wireguard_peers.some((own) => own.public_key === peer.public_key))
|
|
return [
|
|
owner?.name ?? `- (${peer.public_key.slice(0, 8)}...)`,
|
|
peer.allowed_ips.join(', '),
|
|
peer.endpoint ?? '-',
|
|
peer.handshake_seconds_ago === null ? 'none' : `${peer.handshake_seconds_ago} s ago`,
|
|
bytes(peer.received_bytes),
|
|
bytes(peer.sent_bytes),
|
|
]
|
|
}) ?? undefined}
|
|
/>
|
|
),
|
|
}] : []),
|
|
] : []
|
|
|
|
return (
|
|
<Box component="section">
|
|
<Toolbar description={DESCRIPTIONS[system]}>
|
|
<Button variant="outlined" startIcon={<Edit />} disabled={!access || busy} onClick={() => setEditing(true)}>Edit</Button>
|
|
<BoardButtons board={board} />
|
|
<Button variant="outlined" startIcon={<Refresh />} disabled={busy} onClick={() => void refresh()}>Refresh</Button>
|
|
</Toolbar>
|
|
{error && <Alert severity="error" sx={{ mb: 2, whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
{access && settings && settings.enabled && !running && (
|
|
<Alert severity="warning" sx={{ mb: 2 }}>
|
|
{system === 'wireguard'
|
|
? 'WireGuard is enabled but does not run yet; the server applies a change within a moment.'
|
|
: `${REMOTE_ACCESS_LABELS[system]} is enabled but does not run yet. It needs wan.local_fqdn${access.local_fqdn ? '' : ', which is not set,'} and the server's VPN server certificate of the P2S issuing CA; the server applies a change within a moment.`}
|
|
</Alert>
|
|
)}
|
|
{access && <Board board={board} panels={panels} />}
|
|
{showConfig && access && (
|
|
<WireGuardConfigDialog title="WireGuard configuration of a device" server={access.wireguard_server} ips={null} onClose={() => setShowConfig(false)} />
|
|
)}
|
|
{editing && access && (
|
|
<RemoteAccessDialog system={system} current={access} onSave={save} onCancel={() => setEditing(false)} />
|
|
)}
|
|
</Box>
|
|
)
|
|
}
|