Files
linux-cloud-router/web/src/RemoteAccessDialog.tsx
T

128 lines
6.2 KiB
TypeScript

import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, MenuItem, Stack, Switch, TextField } from '@mui/material'
import { useState, type FormEvent } from 'react'
import { OPENVPN_PROTOCOLS, REMOTE_ACCESS_LABELS, type RemoteAccess, type RemoteAccessSystem } from './api.ts'
import { routerAddressProblem } from './wireguard.ts'
/** An IPv4 network with its prefix length, as the server takes an address pool. */
const CIDR_PATTERN = /^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$/
/** A domain of IKEv2's DNS domains, as the server takes it, and the most domains. */
const DOMAIN_PATTERN = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/
const DOMAINS_MAX = 16
/** A port as text: 1 to 65535. */
function portProblem(text: string) {
return /^\d{1,5}$/.test(text) && Number(text) >= 1 && Number(text) <= 65535 ? '' : 'A port from 1 to 65535.'
}
/**
* A dialog that edits the settings of one remote access system: whether it is enabled and its address pool, for
* WireGuard wg0's address, for IKEv2 the DNS domains its clients resolve with the server's DNS, one per line, and the
* port, with OpenVPN's protocol. An enabled system needs its address. onSave sends
* the section; the dialog closes when it succeeds and shows the server's message when it fails.
*/
export default function RemoteAccessDialog(props: {
system: RemoteAccessSystem
current: RemoteAccess
onSave: (settings: Record<string, unknown>) => Promise<void>
onCancel: () => void
}) {
const { p2s, openvpn, wireguard } = props.current
const section = props.current[props.system]
const [enabled, setEnabled] = useState(section.enabled)
const [address, setAddress] = useState(props.system === 'wireguard' ? wireguard.address : props.system === 'p2s' ? p2s.address_pool : openvpn.address_pool)
const [port, setPort] = useState(String(props.system === 'wireguard' ? wireguard.listen_port : openvpn.port))
const [protocol, setProtocol] = useState(openvpn.protocol)
const [domains, setDomains] = useState(p2s.dns_domains.join('\n'))
const [saving, setSaving] = useState(false)
const [error, setError] = useState<string | null>(null)
const addressLabel = props.system === 'wireguard' ? 'Address of wg0' : 'Address pool'
const addressProblem = address.trim() === ''
? (enabled ? 'Required while enabled.' : '')
: props.system === 'wireguard' ? routerAddressProblem(address)
: CIDR_PATTERN.test(address.trim()) ? '' : 'An IPv4 address with a prefix length, such as 10.10.0.0/24.'
const portError = props.system === 'p2s' ? '' : portProblem(port)
const domainList = domains.split('\n').map((line) => line.trim().toLowerCase()).filter((line) => line !== '')
const badDomain = domainList.find((domain) => !DOMAIN_PATTERN.test(domain))
const domainsProblem = props.system !== 'p2s' ? ''
: badDomain ? `${badDomain} is not a DNS name.`
: domainList.length > DOMAINS_MAX || new Set(domainList).size !== domainList.length ? `At most ${DOMAINS_MAX} different domains.` : ''
const blocked = addressProblem !== '' || portError !== '' || domainsProblem !== ''
async function submit(event: FormEvent) {
event.preventDefault()
if (blocked) {
return
}
setSaving(true)
setError(null)
const settings: Record<string, unknown> = props.system === 'wireguard'
? { enabled, address: address.trim(), listen_port: Number(port) }
: props.system === 'openvpn'
? { enabled, address_pool: address.trim(), port: Number(port), protocol }
: { enabled, address_pool: address.trim(), dns_domains: domainList }
try {
await props.onSave(settings)
} catch (caught) {
setError((caught as Error).message)
setSaving(false)
}
}
return (
<Dialog open onClose={(_event, reason) => reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}>
<DialogTitle>{`${REMOTE_ACCESS_LABELS[props.system]} settings`}</DialogTitle>
<DialogContent>
<Stack sx={{ gap: 2, pt: 1 }}>
{error && <Alert severity="error" sx={{ whiteSpace: 'pre-line' }}>{error}</Alert>}
<FormControlLabel control={<Switch checked={enabled} onChange={(event) => setEnabled(event.target.checked)} />} label="Enabled" />
<TextField
label={addressLabel}
value={address}
autoFocus
autoComplete="off"
error={addressProblem !== ''}
helperText={addressProblem || (props.system === 'wireguard'
? 'The server\'s address and prefix on the WireGuard network, such as 192.168.200.1/24.'
: 'The IPv4 network the clients get their addresses from.')}
onChange={(event) => setAddress(event.target.value)}
/>
{props.system === 'p2s' && (
<TextField
label="DNS domains"
value={domains}
autoComplete="off"
multiline
minRows={2}
error={domainsProblem !== ''}
helperText={domainsProblem || 'The domains the clients resolve with the server\'s DNS, one per line.'}
onChange={(event) => setDomains(event.target.value)}
/>
)}
{props.system !== 'p2s' && (
<TextField
label={props.system === 'wireguard' ? 'UDP port' : 'Port'}
value={port}
autoComplete="off"
error={portError !== ''}
helperText={portError || 'A cloud firewall in front of the server must allow this port.'}
slotProps={{ htmlInput: { inputMode: 'numeric' } }}
onChange={(event) => setPort(event.target.value.trim())}
/>
)}
{props.system === 'openvpn' && (
<TextField select label="Protocol" value={protocol} onChange={(event) => setProtocol(event.target.value)}>
{OPENVPN_PROTOCOLS.map((item) => <MenuItem key={item} value={item}>{item.toUpperCase()}</MenuItem>)}
</TextField>
)}
</Stack>
</DialogContent>
<DialogActions>
<Button onClick={props.onCancel} disabled={saving}>Cancel</Button>
<Button type="submit" variant="contained" disabled={saving || blocked}>Save</Button>
</DialogActions>
</Dialog>
)
}