import Add from '@mui/icons-material/Add' import Cancel from '@mui/icons-material/Cancel' import CheckCircle from '@mui/icons-material/CheckCircle' import Delete from '@mui/icons-material/Delete' import Edit from '@mui/icons-material/Edit' import Refresh from '@mui/icons-material/Refresh' import { Alert, Box, Button, IconButton, Table, TableBody, TableCell, TableHead, TableRow, Tooltip, Typography, } from '@mui/material' import { useCallback, useEffect, useState } from 'react' import ConfirmDialog, { type ConfirmDialogProps } from './ConfirmDialog.tsx' import { Section } from './ServerPanel.tsx' import SiteToSiteDialog from './SiteToSiteDialog.tsx' import Toolbar from './Toolbar.tsx' import { ApiError, REMOVED_SHAPE, SITE_TO_SITE_CONNECTION_SHAPE, SITE_TO_SITE_SHAPE, request, type SiteToSiteConnection, } from './api.ts' /** The note shown after a change, which the server applies when linux-manager-setup has run again. */ const APPLIED = 'The server applies the change in the background; Established follows once the tunnel is up again.' /** * The server's site-to-site connections: list with their remote peers, subnets, local identity mode, authentication * (whether a pre-shared key is set, or which CA the peer's certificate comes from) and whether the tunnel is * established; add, edit and remove. The server applies a change by * running its setup again, so the list is read again after each. */ export default function SiteToSitePanel() { const [connections, setConnections] = useState(null) const [error, setError] = useState(null) const [unavailable, setUnavailable] = useState(false) const [note, setNote] = useState(null) const [busy, setBusy] = useState(true) const [adding, setAdding] = useState(false) const [editing, setEditing] = useState(null) const [confirm, setConfirm] = useState(null) /** Reads the connections; the state changes only once the server answers. */ const read = useCallback(async () => { await request<{ connections: SiteToSiteConnection[] }>('GET', '/site-to-site', SITE_TO_SITE_SHAPE) .then((found) => { setConnections(found.connections) setError(null) setUnavailable(false) }, (caught) => { const failure = caught as ApiError setConnections(null) setUnavailable(failure.status === 404) setError(failure.message) }) .finally(() => setBusy(false)) }, []) /** Reads the connections again, busy until the server answers. */ const refresh = useCallback(() => { setBusy(true) return read() }, [read]) useEffect(() => { void read() }, [read]) function remove(connection: SiteToSiteConnection) { setConfirm({ title: 'Remove site-to-site connection', message: `Remove ${connection.name}? Its tunnel is taken down.`, confirmLabel: 'Remove', destructive: true, onCancel: () => setConfirm(null), onConfirm: () => { setConfirm(null) setBusy(true) request('DELETE', `/site-to-site/${connection.name}`, REMOVED_SHAPE) .then(() => { setError(null) setNote(APPLIED) }) .catch((caught: Error) => setError(caught.message)) .finally(() => void refresh()) }, }) } return ( {error && {error}} {note && setNote(null)}>{note}} {connections && (
Name Remote peer Remote identity Remote CIDRs Local CIDRs Local identity mode Authentication Established Actions {connections.length === 0 && ( None. )} {connections.map((connection) => ( {connection.name} {connection.addrs.join(', ') || '-'} {connection.id || '-'} {connection.cidrs.join(', ') || '-'} {connection.local_cidrs.join(', ') || '-'} {connection.local_id_mode} {connection.auth === 'certificate' ? `certificate, ${connection.remote_ca ? 'remote CA' : 'site-to-site CA'}` : connection.psk_file ? key from file : connection.psk_set ? 'key set' : key missing} {connection.established === null ? - : connection.established ? : } setEditing(connection)}> remove(connection)}> ))}
)} {adding && ( setAdding(false)} onSave={async (name, settings, psk) => { await request('POST', '/site-to-site', SITE_TO_SITE_CONNECTION_SHAPE, { name, ...settings, psk }) setAdding(false) setNote(APPLIED) await refresh() }} /> )} {editing && ( setEditing(null)} onSave={async (_name, settings, psk) => { await request('PUT', `/site-to-site/${editing.name}`, SITE_TO_SITE_CONNECTION_SHAPE, psk === '' ? settings : { ...settings, psk }) setEditing(null) setNote(APPLIED) await refresh() }} /> )} {confirm && }
) }