103 lines
5.0 KiB
TypeScript
103 lines
5.0 KiB
TypeScript
import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, MenuItem, Stack, Switch, TextField } from '@mui/material'
|
|
import { useState, type FormEvent } from 'react'
|
|
import { ADDRESS_PATTERN, LISTEN_CHOICES, ZONE_PATTERN, type DnsSettings, type Listen } from './api.ts'
|
|
|
|
/**
|
|
* A dialog that edits the server's dns section: whether the name server runs, the secondary name server of every
|
|
* domain, the interfaces it answers on and the TTL, and wan.public_address, the server's public IP the records of the
|
|
* sites with Public IP take. The primary name server is the server's FQDN, nameserver, shown and not edited. An enabled
|
|
* name server needs a secondary other than the server's FQDN. onSave sends the section and the public address; the
|
|
* dialog closes when it succeeds and shows the server's message when it fails.
|
|
*/
|
|
export default function DnsSettingsDialog(props: {
|
|
current: DnsSettings
|
|
nameserver: string
|
|
publicAddress: string
|
|
onSave: (settings: DnsSettings, publicAddress: string) => Promise<void>
|
|
onCancel: () => void
|
|
}) {
|
|
const [enabled, setEnabled] = useState(props.current.enabled)
|
|
const [secondary, setSecondary] = useState(props.current.secondary)
|
|
const [publicAddress, setPublicAddress] = useState(props.publicAddress)
|
|
const [listen, setListen] = useState<Listen>(props.current.listen)
|
|
const [ttl, setTtl] = useState(String(props.current.ttl))
|
|
const [saving, setSaving] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const secondaryProblem = secondary === '' ? (enabled ? 'Required while enabled.' : '')
|
|
: !ZONE_PATTERN.test(secondary) ? 'A DNS name in lower case.'
|
|
: secondary === props.nameserver ? 'The secondary is not the server\'s FQDN, the primary.' : ''
|
|
const addressProblem = publicAddress === '' || ADDRESS_PATTERN.test(publicAddress) ? '' : 'Not an IP address.'
|
|
const ttlProblem = /^\d{2,5}$/.test(ttl) && Number(ttl) >= 60 && Number(ttl) <= 86400 ? '' : '60 to 86400 seconds.'
|
|
const blocked = secondaryProblem !== '' || addressProblem !== '' || ttlProblem !== ''
|
|
|
|
async function submit(event: FormEvent) {
|
|
event.preventDefault()
|
|
if (blocked) {
|
|
return
|
|
}
|
|
setSaving(true)
|
|
setError(null)
|
|
try {
|
|
await props.onSave({ enabled, secondary, listen, ttl: Number(ttl) }, publicAddress)
|
|
} 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>Name server 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="Primary name server"
|
|
value={props.nameserver || '(the server has no FQDN)'}
|
|
disabled
|
|
helperText="The server's FQDN: the SOA and the first NS of every domain."
|
|
/>
|
|
<TextField
|
|
label="Secondary name server"
|
|
value={secondary}
|
|
autoFocus
|
|
autoComplete="off"
|
|
error={secondaryProblem !== ''}
|
|
helperText={secondaryProblem || 'The second NS of every domain; the addresses it resolves to may transfer the domains.'}
|
|
onChange={(event) => setSecondary(event.target.value.trim().toLowerCase())}
|
|
/>
|
|
<TextField
|
|
label="Public IP address"
|
|
value={publicAddress}
|
|
autoComplete="off"
|
|
error={addressProblem !== ''}
|
|
helperText={addressProblem || 'The server\'s public IP, for the records of the sites with Public IP. Empty: none.'}
|
|
onChange={(event) => setPublicAddress(event.target.value.trim().toLowerCase())}
|
|
/>
|
|
<Stack direction="row" sx={{ gap: 2 }}>
|
|
<TextField select label="Listen" value={listen} helperText="The interfaces port 53 is open on." onChange={(event) => setListen(event.target.value as Listen)} sx={{ flex: 1 }}>
|
|
{LISTEN_CHOICES.map((item) => <MenuItem key={item} value={item}>{item}</MenuItem>)}
|
|
</TextField>
|
|
<TextField
|
|
label="TTL (seconds)"
|
|
value={ttl}
|
|
autoComplete="off"
|
|
error={ttlProblem !== ''}
|
|
helperText={ttlProblem || 'The TTL of the domains without their own.'}
|
|
slotProps={{ htmlInput: { inputMode: 'numeric' } }}
|
|
onChange={(event) => setTtl(event.target.value.trim())}
|
|
sx={{ flex: 1 }}
|
|
/>
|
|
</Stack>
|
|
</Stack>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={props.onCancel} disabled={saving}>Cancel</Button>
|
|
<Button type="submit" variant="contained" disabled={saving || blocked}>Save</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
)
|
|
}
|