193 lines
9.7 KiB
TypeScript
193 lines
9.7 KiB
TypeScript
import Settings from '@mui/icons-material/Settings'
|
|
import { Alert, Box, Button, Chip, IconButton, Stack, Tooltip, Typography } from '@mui/material'
|
|
import { useCallback, useEffect, useState, type ReactNode } from 'react'
|
|
import AcmeSettingsDialog from './AcmeSettingsDialog.tsx'
|
|
import { PROVIDER_LABELS } from './https.ts'
|
|
import { when } from './format.ts'
|
|
import HttpsSettingsDialog from './HttpsSettingsDialog.tsx'
|
|
import { CERTIFICATE_LABELS } from './https.ts'
|
|
import ImportDialog from './ImportDialog.tsx'
|
|
import PfxDialog from './PfxDialog.tsx'
|
|
import { Pairs, Section } from './ServerPanel.tsx'
|
|
import { text } from './format.ts'
|
|
import { saveFile } from './saveFile.ts'
|
|
import {
|
|
DETAILS_SHAPE, HTTPS_CSR_SHAPE, HTTPS_IMPORTED_SHAPE, HTTPS_SETTINGS_SHAPE, request, type HttpsCertificate, type HttpsChange,
|
|
type HttpsSettings, type RouterDetails,
|
|
} from './api.ts'
|
|
|
|
/**
|
|
* The Server tab of the Certificates page, the server's HTTPS from /config and /https/settings: the names and addresses
|
|
* HTTPS is served for, with a chip while the certificate has another source than ACME and a button that opens
|
|
* HttpsSettingsDialog; the certificate of a source other than ACME, and, for one of another CA, Generate CSR, Import
|
|
* certificate and Import PFX; the ACME CA with a chip while it is Let's Encrypt's staging environment and a button that
|
|
* opens AcmeSettingsDialog; and Caddy, its version and the state of caddy.service. Until the first setup
|
|
* chooses the source, the HTTPS row says so and the served self-signed certificate is shown. Read when it is shown; the
|
|
* page's Refresh shows it again.
|
|
*/
|
|
export default function ServerCertificatePanel() {
|
|
const [details, setDetails] = useState<RouterDetails | null>(null)
|
|
const [certificate, setCertificate] = useState<HttpsCertificate | null>(null)
|
|
const [reading, setReading] = useState(true)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [note, setNote] = useState<string | null>(null)
|
|
const [settings, setSettings] = useState<{ dialog: 'https' | 'acme'; settings: HttpsSettings } | null>(null)
|
|
const [importing, setImporting] = useState<'certificate' | 'pfx' | null>(null)
|
|
|
|
/** Reads the server's details and HTTPS certificate; the state changes only once the server answers. */
|
|
const read = useCallback(async () => {
|
|
await Promise.all([
|
|
request<RouterDetails>('GET', '/config', DETAILS_SHAPE),
|
|
request<{ certificate?: HttpsCertificate }>('GET', '/https/settings', HTTPS_SETTINGS_SHAPE),
|
|
])
|
|
.then(([nextDetails, nextHttps]) => {
|
|
setDetails(nextDetails)
|
|
setCertificate(nextHttps.certificate ?? null)
|
|
setError(null)
|
|
}, (caught) => setError(`The server did not give its HTTPS settings: ${(caught as Error).message}`))
|
|
.finally(() => setReading(false))
|
|
}, [])
|
|
|
|
/** Reads the details and the certificate again, reading until the server answers. */
|
|
const refresh = useCallback(() => {
|
|
setReading(true)
|
|
return read()
|
|
}, [read])
|
|
|
|
useEffect(() => {
|
|
void read()
|
|
}, [read])
|
|
|
|
/** Reads the server's https section from /https/settings and opens HttpsSettingsDialog or AcmeSettingsDialog with it. */
|
|
async function open(dialog: 'https' | 'acme') {
|
|
setError(null)
|
|
try {
|
|
setSettings({ dialog, settings: (await request<{ settings: HttpsSettings }>('GET', '/https/settings', HTTPS_SETTINGS_SHAPE)).settings })
|
|
} catch (caught) {
|
|
setError(`The server did not give its HTTPS settings: ${(caught as Error).message}`)
|
|
}
|
|
}
|
|
|
|
/** Sends a change of HttpsSettingsDialog or AcmeSettingsDialog to /https/settings and closes the dialog. */
|
|
async function save(values: HttpsChange) {
|
|
await request('PUT', '/https/settings', HTTPS_SETTINGS_SHAPE, values)
|
|
setSettings(null)
|
|
setNote('The server runs its setup again, which applies the change.')
|
|
void refresh()
|
|
}
|
|
|
|
/** Saves a new CSR for the server's FQDN, to be signed by another CA. */
|
|
async function downloadCsr() {
|
|
setError(null)
|
|
try {
|
|
const { csr } = await request<{ csr: string }>('POST', '/https/csr', HTTPS_CSR_SHAPE)
|
|
saveFile(`${certificate?.subject ?? 'server'}.csr`, csr, 'application/pkcs10')
|
|
void refresh()
|
|
} catch (caught) {
|
|
setError(`The server made no CSR: ${(caught as Error).message}`)
|
|
}
|
|
}
|
|
|
|
const https = details?.configuration.https
|
|
const hosts = [...(details?.network.https_names ?? []), ...(details?.network.https_addresses ?? [])]
|
|
const chosen = certificate !== null && certificate.source !== ''
|
|
const other = certificate !== null && certificate.source !== 'acme'
|
|
|
|
return (
|
|
<Box>
|
|
{error && <Alert severity="warning" sx={{ mb: 2 }} onClose={() => setError(null)}>{error}</Alert>}
|
|
{note && <Alert severity="info" sx={{ mb: 2 }} onClose={() => setNote(null)}>{note}</Alert>}
|
|
{details === null ? (
|
|
<Typography variant="body2" color="text.secondary">{reading ? 'Reading the server\'s HTTPS settings.' : 'The server\'s HTTPS settings are not read.'}</Typography>
|
|
) : (
|
|
<Section title="HTTPS">
|
|
<Pairs
|
|
rows={[
|
|
['HTTPS', (
|
|
<Stack key="https" direction="row" sx={{ alignItems: 'center', gap: 1, flexWrap: 'wrap' }}>
|
|
<span>{hosts.length > 0 ? `for ${hosts.join(', ')}` : 'on'}</span>
|
|
{other && certificate.source !== '' && <Chip size="small" label={CERTIFICATE_LABELS[certificate.source]} color={certificate.served === certificate.source ? 'default' : 'warning'} />}
|
|
{!chosen && <Typography variant="body2" color="text.secondary">certificate chosen by the next setup</Typography>}
|
|
<Tooltip title="HTTPS certificate">
|
|
<IconButton size="small" aria-label="HTTPS certificate" onClick={() => void open('https')}>
|
|
<Settings fontSize="small" />
|
|
</IconButton>
|
|
</Tooltip>
|
|
</Stack>
|
|
)],
|
|
...(other ? [['HTTPS certificate', (
|
|
<Stack key="certificate" sx={{ gap: 0.5 }}>
|
|
<span>
|
|
{certificate.subject
|
|
? `${certificate.served === certificate.source || !chosen ? '' : 'self-signed until the chosen one is available: '}${certificate.subject}, issued by ${certificate.issuer ?? '-'}, expires ${when(certificate.expires)}`
|
|
: 'none yet'}
|
|
</span>
|
|
{certificate.source === 'external' && (
|
|
<Stack direction="row" sx={{ gap: 1, flexWrap: 'wrap' }}>
|
|
<Button size="small" variant="outlined" onClick={() => void downloadCsr()}>
|
|
{certificate.pending ? 'New CSR' : 'Generate CSR'}
|
|
</Button>
|
|
<Button size="small" variant="outlined" disabled={!certificate.pending} onClick={() => setImporting('certificate')}>
|
|
Import certificate
|
|
</Button>
|
|
<Button size="small" variant="outlined" onClick={() => setImporting('pfx')}>
|
|
Import PFX
|
|
</Button>
|
|
</Stack>
|
|
)}
|
|
</Stack>
|
|
)] as [string, ReactNode]] : []),
|
|
['ACME CA', (
|
|
<Stack key="acme" direction="row" sx={{ alignItems: 'center', gap: 1, flexWrap: 'wrap' }}>
|
|
<span>{PROVIDER_LABELS[https?.acme_provider as keyof typeof PROVIDER_LABELS] ?? text(https?.acme_provider)}</span>
|
|
{https?.acme_provider === 'letsencrypt' && https?.acme_staging === true && <Chip size="small" label="Let's Encrypt staging" color="warning" />}
|
|
<Tooltip title="ACME CA settings">
|
|
<IconButton size="small" aria-label="ACME CA settings" onClick={() => void open('acme')}>
|
|
<Settings fontSize="small" />
|
|
</IconButton>
|
|
</Tooltip>
|
|
</Stack>
|
|
)],
|
|
['Caddy', (
|
|
<Stack key="caddy" direction="row" sx={{ alignItems: 'center', gap: 1, flexWrap: 'wrap' }}>
|
|
<span>{details.caddy ? details.caddy.version : 'not installed'}</span>
|
|
{details.caddy?.service && <Chip size="small" label={details.caddy.service} color={details.caddy.service === 'active' ? 'success' : 'warning'} />}
|
|
</Stack>
|
|
)],
|
|
]}
|
|
/>
|
|
</Section>
|
|
)}
|
|
|
|
{settings?.dialog === 'https' && <HttpsSettingsDialog current={settings.settings} onCancel={() => setSettings(null)} onSave={save} />}
|
|
{settings?.dialog === 'acme' && <AcmeSettingsDialog current={settings.settings} onCancel={() => setSettings(null)} onSave={save} />}
|
|
{importing === 'certificate' && (
|
|
<ImportDialog
|
|
title="Import the HTTPS certificate"
|
|
message="The certificate another CA signed for the server's CSR, followed by its chain."
|
|
label="Certificate"
|
|
begin="-----BEGIN CERTIFICATE-----"
|
|
onCancel={() => setImporting(null)}
|
|
onSave={async (value) => {
|
|
await request('PUT', '/https/certificate', HTTPS_IMPORTED_SHAPE, { certificate: value })
|
|
setImporting(null)
|
|
setNote('The server runs its setup again, which serves the certificate.')
|
|
void refresh()
|
|
}}
|
|
/>
|
|
)}
|
|
{importing === 'pfx' && (
|
|
<PfxDialog
|
|
onCancel={() => setImporting(null)}
|
|
onSave={async (pfx, password) => {
|
|
await request('PUT', '/https/pfx', HTTPS_IMPORTED_SHAPE, { pfx, password })
|
|
setImporting(null)
|
|
setNote('The server runs its setup again, which serves the certificate.')
|
|
void refresh()
|
|
}}
|
|
/>
|
|
)}
|
|
</Box>
|
|
)
|
|
}
|