feature: the package installs the Router Manager, which is no longer a package of its own.
Release / release (push) Failing after 4s
Release / release (push) Failing after 4s
This commit is contained in:
1 parent
6d90441bee
commit
8efe21bfa6
129 files changed
+17381
-104
No files matched your search
@@ -0,0 +1,260 @@
|
||||
import Add from '@mui/icons-material/Add'
|
||||
import Block from '@mui/icons-material/Block'
|
||||
import CheckCircle from '@mui/icons-material/CheckCircle'
|
||||
import Delete from '@mui/icons-material/Delete'
|
||||
import Edit from '@mui/icons-material/Edit'
|
||||
import FolderOpen from '@mui/icons-material/FolderOpen'
|
||||
import Refresh from '@mui/icons-material/Refresh'
|
||||
import Settings from '@mui/icons-material/Settings'
|
||||
import { Alert, Box, Button, Checkbox, Chip, FormControlLabel, IconButton, Link, Stack, Table, TableBody, TableCell, TableHead, TableRow, Tooltip } from '@mui/material'
|
||||
import { useCallback, useEffect, useState, type ReactNode } from 'react'
|
||||
import ConfirmDialog, { type ConfirmDialogProps } from './ConfirmDialog.tsx'
|
||||
import { Copyable, Pairs, Section } from './RouterPanel.tsx'
|
||||
import SearchField, { matches } from './SearchField.tsx'
|
||||
import SiteDialog, { type SiteSettings } from './SiteDialog.tsx'
|
||||
import SitesSettingsDialog from './SitesSettingsDialog.tsx'
|
||||
import Toolbar from './Toolbar.tsx'
|
||||
import {
|
||||
ApiError, DETAILS_SHAPE, GROUPS_SHAPE, REMOVED_SHAPE, SITES_SETTINGS_SHAPE, SITES_SHAPE, SITE_DNS_ADDRESS_LABELS,
|
||||
SITE_DNS_LABELS, SITE_SHAPE, SITE_TLS_LABELS, request, type Group, type RouterDetails, type Site, type Sites,
|
||||
} from './api.ts'
|
||||
|
||||
/** The URLs of a site: its scheme, each host name and its port when it is not the scheme's default. */
|
||||
function siteUrls(site: Site) {
|
||||
const scheme = site.tls === 'none' ? 'http' : 'https'
|
||||
const port = site.port === (site.tls === 'none' ? 80 : 443) ? '' : `:${site.port}`
|
||||
return site.hosts.filter((host) => !host.startsWith('*.')).map((host) => `${scheme}://${host.includes(':') ? `[${host}]` : host}${port}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* The web sites of the router's /sites, which Caddy serves from folders on the router. The Settings panel shows the
|
||||
* sites section, whether they are served, the largest upload and WebDAV with its URL, edited with Settings. The list
|
||||
* shows each site with its URLs, port, interfaces, TLS, editors group, DNS and state: add, edit, enable, disable,
|
||||
* remove, with its folder when asked, and open its files on the Files tab with onOpenFiles.
|
||||
*/
|
||||
export default function SitesPanel(props: { onOpenFiles: (name: string) => void }) {
|
||||
const [sites, setSites] = useState<Sites | null>(null)
|
||||
const [fqdn, setFqdn] = useState('')
|
||||
const [cas, setCas] = useState<string[]>([])
|
||||
const [groups, setGroups] = useState<string[]>([])
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [unavailable, setUnavailable] = useState(false)
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [adding, setAdding] = useState(false)
|
||||
const [editing, setEditing] = useState<Site | null>(null)
|
||||
const [settings, setSettings] = useState(false)
|
||||
const [confirm, setConfirm] = useState<ConfirmDialogProps | null>(null)
|
||||
const [search, setSearch] = useState('')
|
||||
|
||||
const refresh = useCallback(async () => {
|
||||
setBusy(true)
|
||||
try {
|
||||
const [found, groupList, details] = await Promise.all([
|
||||
request<Sites>('GET', '/sites', SITES_SHAPE),
|
||||
request<{ groups: Group[] }>('GET', '/groups', GROUPS_SHAPE),
|
||||
request<RouterDetails>('GET', '/config', DETAILS_SHAPE),
|
||||
])
|
||||
setSites(found)
|
||||
setGroups(groupList.groups.map((group) => group.name))
|
||||
const wan = details.configuration.wan ?? {}
|
||||
const pki = details.configuration.pki ?? {}
|
||||
setFqdn(typeof wan.local_fqdn === 'string' ? wan.local_fqdn : '')
|
||||
setCas(typeof pki.issuing_cas === 'object' && pki.issuing_cas !== null ? Object.keys(pki.issuing_cas) : [])
|
||||
setError(null)
|
||||
setUnavailable(false)
|
||||
} catch (caught) {
|
||||
const failure = caught as ApiError
|
||||
setSites(null)
|
||||
setUnavailable(failure.status === 404)
|
||||
setError(failure.message)
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
void refresh()
|
||||
}, [refresh])
|
||||
|
||||
/** Runs a change of a site, then reads the list again; a failure is shown above the list. */
|
||||
async function change(work: () => Promise<unknown>) {
|
||||
setBusy(true)
|
||||
try {
|
||||
await work()
|
||||
setError(null)
|
||||
} catch (caught) {
|
||||
setError((caught as Error).message)
|
||||
}
|
||||
await refresh()
|
||||
}
|
||||
|
||||
function remove(site: Site) {
|
||||
let content = false
|
||||
setConfirm({
|
||||
title: 'Remove site',
|
||||
message: `Remove ${site.name}?`,
|
||||
confirmLabel: 'Remove',
|
||||
destructive: true,
|
||||
children: (
|
||||
<FormControlLabel
|
||||
control={<Checkbox onChange={(event) => { content = event.target.checked }} />}
|
||||
label={`Remove its folder ${site.directory}`}
|
||||
/>
|
||||
),
|
||||
onCancel: () => setConfirm(null),
|
||||
onConfirm: () => {
|
||||
setConfirm(null)
|
||||
void change(() => request('DELETE', `/sites/${site.name}${content ? '?content=true' : ''}`, REMOVED_SHAPE))
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const section = sites?.settings
|
||||
const shown = (sites?.sites ?? []).filter((site) => matches(search, [site.name, ...site.hosts, site.editors]))
|
||||
|
||||
return (
|
||||
<Box component="section">
|
||||
<Toolbar description="Static web sites Caddy serves from folders on the router.">
|
||||
<SearchField value={search} onChange={setSearch} />
|
||||
<Button variant="outlined" startIcon={<Settings />} disabled={busy || !section} onClick={() => setSettings(true)}>
|
||||
Settings
|
||||
</Button>
|
||||
<Button variant="contained" startIcon={<Add />} disabled={busy || sites === null} onClick={() => setAdding(true)}>
|
||||
Add
|
||||
</Button>
|
||||
<Button variant="outlined" startIcon={<Refresh />} disabled={busy} onClick={() => void refresh()}>
|
||||
Refresh
|
||||
</Button>
|
||||
</Toolbar>
|
||||
|
||||
{error && <Alert severity={unavailable ? 'info' : 'error'} sx={{ mb: 2, whiteSpace: 'pre-line' }}>{error}</Alert>}
|
||||
|
||||
{section && (
|
||||
<Box sx={{ mb: 2 }}>
|
||||
<Section title="Settings">
|
||||
<Pairs
|
||||
rows={[
|
||||
['State', <Chip key="state" size="small" label={section.enabled ? 'served' : 'not served'} color={section.enabled ? 'success' : 'default'} />],
|
||||
['Largest upload', `${section.upload_max_mb} MiB`],
|
||||
['WebDAV', <Chip key="webdav" size="small" label={section.webdav ? 'enabled' : 'disabled'} color={section.webdav ? 'success' : 'default'} />],
|
||||
...(section.webdav && fqdn
|
||||
? [['WebDAV URL', <Copyable key="url" value={`https://${fqdn}:${section.webdav_port}/`} label="WebDAV URL" />]] as [string, ReactNode][]
|
||||
: []),
|
||||
]}
|
||||
/>
|
||||
</Section>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
{sites && (
|
||||
<Section title="Sites">
|
||||
<Table size="small">
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
<TableCell>Name</TableCell>
|
||||
<TableCell>URLs</TableCell>
|
||||
<TableCell>Port</TableCell>
|
||||
<TableCell>Listen</TableCell>
|
||||
<TableCell>TLS</TableCell>
|
||||
<TableCell>Editors</TableCell>
|
||||
<TableCell>DNS</TableCell>
|
||||
<TableCell>State</TableCell>
|
||||
<TableCell align="right">Actions</TableCell>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{shown.length === 0 && (
|
||||
<TableRow>
|
||||
<TableCell colSpan={9} sx={{ color: 'text.secondary' }}>None.</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
{shown.map((site) => (
|
||||
<TableRow key={site.name} hover>
|
||||
<TableCell>{site.name}</TableCell>
|
||||
<TableCell>
|
||||
<Stack>
|
||||
{siteUrls(site).map((url) => <Link key={url} href={url} target="_blank" rel="noreferrer">{url}</Link>)}
|
||||
{site.hosts.length === 0 && 'any host name'}
|
||||
</Stack>
|
||||
</TableCell>
|
||||
<TableCell>{site.port}</TableCell>
|
||||
<TableCell>{site.listen}</TableCell>
|
||||
<TableCell>{SITE_TLS_LABELS[site.tls]}{site.tls === 'ca' ? ` (${site.ca})` : ''}</TableCell>
|
||||
<TableCell>{site.editors || '-'}</TableCell>
|
||||
<TableCell>{SITE_DNS_LABELS[site.dns]}{site.dns === 'router' ? `, ${SITE_DNS_ADDRESS_LABELS[site.dns_address]}` : ''}</TableCell>
|
||||
<TableCell sx={{ color: site.enabled ? 'success.main' : 'text.secondary' }}>{site.enabled ? 'enabled' : 'disabled'}</TableCell>
|
||||
<TableCell align="right" sx={{ whiteSpace: 'nowrap' }}>
|
||||
<Tooltip title="Files">
|
||||
<span>
|
||||
<IconButton size="small" onClick={() => props.onOpenFiles(site.name)}><FolderOpen fontSize="small" /></IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
<Tooltip title="Edit">
|
||||
<span>
|
||||
<IconButton size="small" disabled={busy} onClick={() => setEditing(site)}><Edit fontSize="small" /></IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
<Tooltip title={site.enabled ? 'Disable' : 'Enable'}>
|
||||
<span>
|
||||
<IconButton
|
||||
size="small"
|
||||
disabled={busy}
|
||||
onClick={() => void change(() => request('PUT', `/sites/${site.name}`, SITE_SHAPE, { enabled: !site.enabled }))}
|
||||
>
|
||||
{site.enabled ? <Block fontSize="small" /> : <CheckCircle fontSize="small" />}
|
||||
</IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
<Tooltip title="Remove">
|
||||
<span>
|
||||
<IconButton size="small" color="error" disabled={busy} onClick={() => remove(site)}><Delete fontSize="small" /></IconButton>
|
||||
</span>
|
||||
</Tooltip>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</Section>
|
||||
)}
|
||||
|
||||
{adding && (
|
||||
<SiteDialog
|
||||
cas={cas}
|
||||
groups={groups}
|
||||
onCancel={() => setAdding(false)}
|
||||
onSave={async (name, values: SiteSettings) => {
|
||||
await request('POST', '/sites', SITE_SHAPE, { name, ...values })
|
||||
setAdding(false)
|
||||
await refresh()
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{editing && (
|
||||
<SiteDialog
|
||||
site={editing}
|
||||
cas={cas}
|
||||
groups={groups}
|
||||
onCancel={() => setEditing(null)}
|
||||
onSave={async (name, values: SiteSettings) => {
|
||||
await request('PUT', `/sites/${name}`, SITE_SHAPE, values)
|
||||
setEditing(null)
|
||||
await refresh()
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{settings && section && (
|
||||
<SitesSettingsDialog
|
||||
current={section}
|
||||
onCancel={() => setSettings(false)}
|
||||
onSave={async (values) => {
|
||||
await request('PUT', '/sites', SITES_SETTINGS_SHAPE, values)
|
||||
setSettings(false)
|
||||
await refresh()
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{confirm && <ConfirmDialog {...confirm} />}
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user