104 lines
5.1 KiB
TypeScript
104 lines
5.1 KiB
TypeScript
import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, Stack, TextField, Typography } 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 but whether the name server runs, which the System tab switches: 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. The secondary is optional and is not the server's FQDN. onSave sends the
|
|
* settings 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: Omit<DnsSettings, 'enabled'>, publicAddress: string) => Promise<void>
|
|
onCancel: () => void
|
|
}) {
|
|
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 === '' ? ''
|
|
: !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({ 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>}
|
|
<Typography variant="body2" color="text.secondary">
|
|
The name server is {props.current.enabled ? 'on' : 'off'}; it is switched on and off on the System tab.
|
|
</Typography>
|
|
<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 || 'Optional: the second NS of every domain; the addresses it resolves to may transfer the domains. Empty: the server is the only name server.'}
|
|
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 default TTL of the records: used where neither the record nor its domain has one.'}
|
|
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>
|
|
)
|
|
}
|