191 lines
8.2 KiB
TypeScript
191 lines
8.2 KiB
TypeScript
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<SiteToSiteConnection[] | null>(null)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [unavailable, setUnavailable] = useState(false)
|
|
const [note, setNote] = useState<string | null>(null)
|
|
const [busy, setBusy] = useState(true)
|
|
const [adding, setAdding] = useState(false)
|
|
const [editing, setEditing] = useState<SiteToSiteConnection | null>(null)
|
|
const [confirm, setConfirm] = useState<ConfirmDialogProps | null>(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 (
|
|
<Box component="section">
|
|
<Toolbar description="IPsec site-to-site connections.">
|
|
<Button variant="contained" startIcon={<Add />} disabled={busy || connections === null} onClick={() => setAdding(true)}>
|
|
Add connection
|
|
</Button>
|
|
<Button variant="outlined" startIcon={<Refresh />} disabled={busy} onClick={() => void refresh()}>
|
|
Refresh
|
|
</Button>
|
|
</Toolbar>
|
|
|
|
{error && <Alert severity={unavailable ? 'info' : 'error'} sx={{ mb: 2, whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
{note && <Alert severity="info" sx={{ mb: 2 }} onClose={() => setNote(null)}>{note}</Alert>}
|
|
|
|
{connections && (
|
|
<Section title="Site-to-site connections">
|
|
<Box sx={{ overflowX: 'auto' }}>
|
|
<Table size="small">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableCell>Name</TableCell>
|
|
<TableCell>Remote peer</TableCell>
|
|
<TableCell>Remote identity</TableCell>
|
|
<TableCell>Remote CIDRs</TableCell>
|
|
<TableCell>Local CIDRs</TableCell>
|
|
<TableCell>Local identity mode</TableCell>
|
|
<TableCell>Authentication</TableCell>
|
|
<TableCell>Established</TableCell>
|
|
<TableCell align="right">Actions</TableCell>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{connections.length === 0 && (
|
|
<TableRow>
|
|
<TableCell colSpan={9} sx={{ color: 'text.secondary' }}>None.</TableCell>
|
|
</TableRow>
|
|
)}
|
|
{connections.map((connection) => (
|
|
<TableRow key={connection.name} hover>
|
|
<TableCell>{connection.name}</TableCell>
|
|
<TableCell>{connection.addrs.join(', ') || '-'}</TableCell>
|
|
<TableCell>{connection.id || '-'}</TableCell>
|
|
<TableCell sx={{ fontFamily: 'monospace' }}>{connection.cidrs.join(', ') || '-'}</TableCell>
|
|
<TableCell sx={{ fontFamily: 'monospace' }}>{connection.local_cidrs.join(', ') || '-'}</TableCell>
|
|
<TableCell>{connection.local_id_mode}</TableCell>
|
|
<TableCell>
|
|
{connection.auth === 'certificate'
|
|
? `certificate, ${connection.remote_ca ? 'remote CA' : 'site-to-site CA'}`
|
|
: connection.psk_file
|
|
? <Tooltip title={connection.psk_file}><span>key from file</span></Tooltip>
|
|
: connection.psk_set ? 'key set' : <Typography variant="body2" component="span" color="error">key missing</Typography>}
|
|
</TableCell>
|
|
<TableCell>
|
|
{connection.established === null
|
|
? <Tooltip title="The server cannot tell."><span>-</span></Tooltip>
|
|
: connection.established
|
|
? <CheckCircle fontSize="small" color="success" aria-label="established" />
|
|
: <Cancel fontSize="small" color="error" aria-label="not established" />}
|
|
</TableCell>
|
|
<TableCell align="right" sx={{ whiteSpace: 'nowrap' }}>
|
|
<Tooltip title="Edit">
|
|
<span>
|
|
<IconButton size="small" disabled={busy} onClick={() => setEditing(connection)}><Edit fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
<Tooltip title="Remove">
|
|
<span>
|
|
<IconButton size="small" color="error" disabled={busy} onClick={() => remove(connection)}><Delete fontSize="small" /></IconButton>
|
|
</span>
|
|
</Tooltip>
|
|
</TableCell>
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
</Box>
|
|
</Section>
|
|
)}
|
|
|
|
{adding && (
|
|
<SiteToSiteDialog
|
|
onCancel={() => 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 && (
|
|
<SiteToSiteDialog
|
|
connection={editing}
|
|
onCancel={() => 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 && <ConfirmDialog {...confirm} />}
|
|
</Box>
|
|
)
|
|
}
|