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, useBoard, type BoardPanel } from './PanelBoard.tsx' import RemoteAccessDialog from './RemoteAccessDialog.tsx' import Toolbar from './Toolbar.tsx' import WireGuardConfigDialog from './WireGuardConfigDialog.tsx' import WireGuardPeers from './WireGuardPeers.tsx' import { Copyable, Grid, IPSEC_HEADS, Pairs, bytes, ipsecRows, text } from './ServerPanel.tsx' 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 = { 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 = { 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(null) const [status, setStatus] = useState(null) const [error, setError] = useState(null) const [busy, setBusy] = useState(false) 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(null) const [details, setDetails] = useState(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' const refresh = useCallback(async () => { setBusy(true) try { const [nextAccess, nextStatus, nextUsers, nextDetails] = await Promise.all([ request('GET', '/remote-access', REMOTE_ACCESS_SHAPE), request('GET', '/status/full', STATUS_SHAPE), wireguard ? request('GET', '/users', USERS_SHAPE) : null, wireguard || ikev2 ? request('GET', '/config', DETAILS_SHAPE) : null, ]) setAccess(nextAccess) setStatus(nextStatus) setUsers(nextUsers) setDetails(nextDetails) setError(null) } catch (caught) { setError((caught as Error).message) } finally { setBusy(false) } }, [wireguard, ikev2]) useEffect(() => { void refresh() }, [refresh]) async function save(settings: Record) { 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: }, { id: 'state', title: 'State', content: ( ], ...(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: }] : []), ...(shown(ipsec.childSas) ? [{ id: 'ipsec-child-sas', title: 'IPsec child SAs', wide: true, content: }] : []), ...(shown(ipsec.connections) ? [{ id: 'ipsec-connections', title: 'IPsec connections', wide: true, content: }] : []), ...(shown(ipsec.children) ? [{ id: 'ipsec-children', title: 'IPsec connection children', wide: true, content: }] : []), ...(system === 'wireguard' ? [{ id: 'server', title: 'Server', wide: true, content: ( <> ], ['Endpoint', access.wireguard_server.endpoint ?? 'unknown: wan.local_fqdn is not set'], ['Allowed IPs', text(access.wireguard_server.allowed_ips)], ]} /> ), }] : []), ...(system === 'wireguard' ? [{ id: 'peers', title: 'Peers', wide: true, content: void refresh()} />, }] : []), ...(system === 'wireguard' && running ? [{ id: 'connections', title: 'Connections', wide: true, content: ( { // 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 ( {error && {error}} {access && settings && settings.enabled && !running && ( {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.`} )} {access && } {showConfig && access && ( setShowConfig(false)} /> )} {editing && access && ( setEditing(false)} /> )} ) }