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) => Promise 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(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 = 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 ( reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> {`${REMOTE_ACCESS_LABELS[props.system]} settings`} {error && {error}} setEnabled(event.target.checked)} />} label="Enabled" /> setAddress(event.target.value)} /> {props.system === 'p2s' && ( setDomains(event.target.value)} /> )} {props.system !== 'p2s' && ( setPort(event.target.value.trim())} /> )} {props.system === 'openvpn' && ( setProtocol(event.target.value)}> {OPENVPN_PROTOCOLS.map((item) => {item.toUpperCase()})} )} ) }