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(null) const [certificate, setCertificate] = useState(null) const [reading, setReading] = useState(true) const [error, setError] = useState(null) const [note, setNote] = useState(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('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 ( {error && setError(null)}>{error}} {note && setNote(null)}>{note}} {details === null ? ( {reading ? 'Reading the server\'s HTTPS settings.' : 'The server\'s HTTPS settings are not read.'} ) : (
{hosts.length > 0 ? `for ${hosts.join(', ')}` : 'on'} {other && certificate.source !== '' && } {!chosen && certificate chosen by the next setup} void open('https')}> )], ...(other ? [['HTTPS certificate', ( {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'} {certificate.source === 'external' && ( )} )] as [string, ReactNode]] : []), ['ACME CA', ( {PROVIDER_LABELS[https?.acme_provider as keyof typeof PROVIDER_LABELS] ?? text(https?.acme_provider)} {https?.acme_provider === 'letsencrypt' && https?.acme_staging === true && } void open('acme')}> )], ['Caddy', ( {details.caddy ? details.caddy.version : 'not installed'} {details.caddy?.service && } )], ]} />
)} {settings?.dialog === 'https' && setSettings(null)} onSave={save} />} {settings?.dialog === 'acme' && setSettings(null)} onSave={save} />} {importing === 'certificate' && ( 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' && ( 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() }} /> )}
) }