359 lines
17 KiB
TypeScript
359 lines
17 KiB
TypeScript
import AccountCircle from '@mui/icons-material/AccountCircle'
|
|
import EditNote from '@mui/icons-material/EditNote'
|
|
import HelpOutline from '@mui/icons-material/HelpOutlineOutlined'
|
|
import Logout from '@mui/icons-material/Logout'
|
|
import { Box, Button, CircularProgress, Container, IconButton, Link, Stack, Tab, Tabs, Tooltip, Typography } from '@mui/material'
|
|
import { useCallback, useEffect, useState } from 'react'
|
|
import AccountPanel from './AccountPanel.tsx'
|
|
import BackupPanel from './BackupPanel.tsx'
|
|
import CertificatesPanel from './CertificatesPanel.tsx'
|
|
import GroupsPanel from './GroupsPanel.tsx'
|
|
import HelpPanel, { HELP_HOME, HELP_WIDTH } from './HelpPanel.tsx'
|
|
import LogsPanel from './LogsPanel.tsx'
|
|
import MetricsPanel from './MetricsPanel.tsx'
|
|
import DockerPanel from './DockerPanel.tsx'
|
|
import DnsPanel from './DnsPanel.tsx'
|
|
import PageMenu, { CA_PATH, DNS_PATH, DOCKER_PATH, REMOTE_ACCESS_PATH, SHELL_PATH, SITES_PATH, SITE_TO_SITE_PATH, USERS_PATH, USER_PATH } from './PageMenu.tsx'
|
|
import NetworkPanel from './NetworkPanel.tsx'
|
|
import RemoteAccessPanel from './RemoteAccessPanel.tsx'
|
|
import ServerPanel from './ServerPanel.tsx'
|
|
import ServiceAccountsPanel from './ServiceAccountsPanel.tsx'
|
|
import SettingsButton from './SettingsButton.tsx'
|
|
import { useBrowserSettings } from './settings.ts'
|
|
import ShellPanel from './ShellPanel.tsx'
|
|
import SignIn from './SignIn.tsx'
|
|
import SystemPanel from './SystemPanel.tsx'
|
|
import SiteFiles from './SiteFiles.tsx'
|
|
import SitesPanel from './SitesPanel.tsx'
|
|
import SiteToSitePanel from './SiteToSitePanel.tsx'
|
|
import SiteToSiteStatusPanel from './SiteToSiteStatusPanel.tsx'
|
|
import ThemeMenu from './ThemeMenu.tsx'
|
|
import UsersPanel from './UsersPanel.tsx'
|
|
import {
|
|
ACCOUNT_SHAPE, DETAILS_SHAPE, INFO_SHAPE, SESSION_EXPIRED, SESSION_SHAPE, request, type Account, type Info, type RemoteAccessSystem,
|
|
type RouterDetails, type Session,
|
|
} from './api.ts'
|
|
|
|
/** The tabs of the Router page, router being the Overview tab, of the Identities page, of the Connectivity page and of the Web Sites page. */
|
|
type View = 'router' | 'metrics' | 'logs' | 'backup' | 'system'
|
|
type UsersView = 'users' | 'groups' | 'service-accounts'
|
|
type SiteToSiteView = 'status' | 'network' | 'connections'
|
|
type SitesView = 'sites' | 'files'
|
|
|
|
/** The help topic of each tab of the Web Sites page. */
|
|
const SITES_TOPICS: Record<SitesView, string> = { sites: 'sites.md', files: 'site-files.md' }
|
|
|
|
/** The prefix of the sessionStorage key of the page the account page was opened from, by its path. */
|
|
const RETURN_KEY = 'linux-manager-ui.return:'
|
|
|
|
/** The pages of an administrator besides Router, which every other path shows. */
|
|
const PAGE_PATHS = [USER_PATH, USERS_PATH, SITE_TO_SITE_PATH, REMOTE_ACCESS_PATH, CA_PATH, SITES_PATH, DNS_PATH, DOCKER_PATH, SHELL_PATH]
|
|
|
|
/** The help topic of each page, a document of /docs; the Router, the Identities and the Web Sites page have one per tab. */
|
|
const HELP_TOPICS: Record<string, string> = {
|
|
[USER_PATH]: 'account.md',
|
|
[SITE_TO_SITE_PATH]: 'site-to-site.md',
|
|
[REMOTE_ACCESS_PATH]: 'remote-access.md',
|
|
[CA_PATH]: 'certificates.md',
|
|
[DNS_PATH]: 'dns.md',
|
|
[DOCKER_PATH]: 'docker.md',
|
|
[SHELL_PATH]: 'shell.md',
|
|
}
|
|
|
|
/** The localStorage key of whether the help is open. */
|
|
const HELP_KEY = 'linux-manager-ui.help'
|
|
|
|
/**
|
|
* The page asks for a sign-in until a session exists. At USER_PATH, and for a user who is not a member of admins at
|
|
* every path, it then shows the user's own account; at USERS_PATH the server's users, groups or service accounts; at
|
|
* SITE_TO_SITE_PATH its site-to-site connections, their state and its network with its routes; at REMOTE_ACCESS_PATH its IKEv2, OpenVPN and
|
|
* WireGuard access; at CA_PATH its certificates; at SITES_PATH its web
|
|
* sites and the files of a site, opened from the sites too; at DNS_PATH its name server, while it is on; at DOCKER_PATH the state of Docker; at SHELL_PATH a shell on the server; otherwise the state of the server, with the update information of its packages in
|
|
* the Status panel, its metrics, its logs, its backups or the updates of its system's packages. A page shows one tab at a time, each read only while shown. An
|
|
* administrator switches pages with the page menu at the right end of the header, whose Shell entry opens the shell in
|
|
* a new tab with the browser setting shellNewTab; the button with the user's name, left of Sign out, opens the user's
|
|
* own account, and pressed on it returns to the page it was opened from. While files are open in the shell's editor, the
|
|
* editor button left of the user's name switches the Shell page between the terminal and the editor. The help button, between the theme switcher and the page menu, opens or closes the help on
|
|
* the right (HelpPanel) at the topic of the page and tab shown (HELP_TOPICS), the page moving left; the browser keeps
|
|
* whether it is open. A call answered 401 ends the session and asks for a sign-in again.
|
|
*/
|
|
export default function App(props: { info: Info }) {
|
|
const [view, setView] = useState<View>('router')
|
|
const [usersView, setUsersView] = useState<UsersView>('users')
|
|
const [siteToSiteView, setSiteToSiteView] = useState<SiteToSiteView>('status')
|
|
const [remoteAccessView, setRemoteAccessView] = useState<RemoteAccessSystem>('p2s')
|
|
const [sitesView, setSitesView] = useState<SitesView>('sites')
|
|
const [filesSite, setFilesSite] = useState<string | null>(null)
|
|
const [session, setSession] = useState<Session | null | undefined>(undefined)
|
|
// /api/info again once signed in, as the version is told only with a session.
|
|
const [info, setInfo] = useState<Info>(props.info)
|
|
const [role, setRole] = useState<{ name: string; admin: boolean } | null>(null)
|
|
// The router and the VPN components as router.enabled and vpn.enabled of /config, and whether Docker is installed;
|
|
// null while not known, when every page shows.
|
|
const [components, setComponents] = useState<{ router: boolean; vpn: boolean; dns: boolean; docker: boolean } | null>(null)
|
|
// Raised by the System tab's components, so that the pages follow a component switched there.
|
|
const [componentsRead, setComponentsRead] = useState(0)
|
|
const routerOff = components?.router === false
|
|
const vpnOff = components?.vpn === false
|
|
const dnsOff = components?.dns === false
|
|
const dockerMissing = components?.docker === false
|
|
const page = PAGE_PATHS.find((path) => path === window.location.pathname) ?? '/'
|
|
const userPage = page === USER_PATH
|
|
const admin = session && role?.name === session.name ? role.admin : undefined
|
|
// The account page is the page of every user who is not an administrator, and of an administrator at USER_PATH.
|
|
const accountShown = Boolean(session) && admin !== undefined && (userPage || !admin)
|
|
const [helpOpen, setHelpOpen] = useState(() => window.localStorage.getItem(HELP_KEY) === 'open')
|
|
const { shellNewTab } = useBrowserSettings()
|
|
// The files open in the Shell page's editor, and whether the editor is shown in place of the terminal.
|
|
const [editorFiles, setEditorFiles] = useState(0)
|
|
const [editorShown, setEditorShown] = useState(false)
|
|
const showFiles = useCallback((count: number) => {
|
|
setEditorFiles(count)
|
|
if (count === 0) {
|
|
setEditorShown(false)
|
|
}
|
|
}, [])
|
|
const showEditor = useCallback(() => setEditorShown(true), [])
|
|
const helpShown = Boolean(session) && helpOpen
|
|
const topic = !admin ? 'account.md'
|
|
: page === '/' ? `${view === 'router' ? 'router' : view}.md`
|
|
: page === USERS_PATH ? `${usersView}.md`
|
|
: page === SITES_PATH ? SITES_TOPICS[sitesView]
|
|
: HELP_TOPICS[page] ?? HELP_HOME
|
|
|
|
/** Opens or closes the help, kept in localStorage. */
|
|
function toggleHelp() {
|
|
window.localStorage.setItem(HELP_KEY, helpOpen ? 'closed' : 'open')
|
|
setHelpOpen(!helpOpen)
|
|
}
|
|
|
|
useEffect(() => {
|
|
request<Session>('GET', '/session', SESSION_SHAPE).then(setSession).catch(() => setSession(null))
|
|
const expired = () => setSession(null)
|
|
window.addEventListener(SESSION_EXPIRED, expired)
|
|
return () => window.removeEventListener(SESSION_EXPIRED, expired)
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
if (!session) {
|
|
return
|
|
}
|
|
request<Account>('GET', '/account', ACCOUNT_SHAPE)
|
|
.then((account) => setRole({ name: session.name, admin: account.admin }))
|
|
.catch(() => setRole({ name: session.name, admin: false }))
|
|
request<Info>('GET', '/info', INFO_SHAPE).then(setInfo).catch(() => undefined)
|
|
}, [session])
|
|
|
|
useEffect(() => {
|
|
if (!admin) {
|
|
return
|
|
}
|
|
request<RouterDetails>('GET', '/config', DETAILS_SHAPE)
|
|
.then((details) => setComponents({
|
|
router: details.configuration.router?.enabled === true,
|
|
vpn: details.configuration.vpn?.enabled === true,
|
|
dns: details.configuration.dns?.enabled === true,
|
|
docker: details.docker.installed,
|
|
}))
|
|
.catch(() => setComponents(null))
|
|
}, [admin, componentsRead])
|
|
|
|
/** Opens the page of the path, keeping the page it was opened from in sessionStorage, or returns to that page from it. */
|
|
function toggle(path: string) {
|
|
if (page === path) {
|
|
window.location.assign(window.sessionStorage.getItem(RETURN_KEY + path) ?? '/')
|
|
return
|
|
}
|
|
window.sessionStorage.setItem(RETURN_KEY + path, window.location.pathname)
|
|
window.location.assign(path)
|
|
}
|
|
|
|
async function signOut() {
|
|
await request('DELETE', '/session', () => true).catch(() => undefined)
|
|
setSession(null)
|
|
}
|
|
|
|
return (
|
|
<Box
|
|
sx={(theme) => ({
|
|
mr: { sm: helpShown ? `${HELP_WIDTH}px` : 0 },
|
|
transition: theme.transitions.create('margin', { duration: theme.transitions.duration.enteringScreen }),
|
|
})}
|
|
>
|
|
{session && <HelpPanel open={helpShown} topic={topic} onClose={toggleHelp} />}
|
|
<Container maxWidth="xl" sx={{ py: 3 }}>
|
|
<Stack direction="row" sx={{ mb: 2, alignItems: 'center', justifyContent: 'space-between', gap: 2 }}>
|
|
<Stack direction="row" sx={{ alignItems: 'center', gap: 2 }}>
|
|
<Box component="img" src="/favicon.svg" alt="" sx={{ width: 56, height: 56 }} />
|
|
<Box>
|
|
<Stack direction="row" sx={{ alignItems: 'baseline', gap: 1 }}>
|
|
<Typography variant="h5" component="h1">
|
|
Ubuntu Linux Manager
|
|
</Typography>
|
|
{session && (
|
|
<Typography variant="caption" color="text.secondary" sx={{ fontFamily: 'monospace' }}>
|
|
{info.version ?? 'development'}
|
|
</Typography>
|
|
)}
|
|
</Stack>
|
|
<Typography variant="body2" color="text.secondary" sx={{ mt: 1 }}>
|
|
Server status page:{' '}
|
|
{info.status_url
|
|
? <Link href={info.status_url} target="_blank" rel="noreferrer">{info.status_url}</Link>
|
|
: <b>not configured</b>}
|
|
</Typography>
|
|
</Box>
|
|
</Stack>
|
|
<Stack direction="row" sx={{ alignItems: 'center', gap: 2 }}>
|
|
{session && admin && page === SHELL_PATH && editorFiles > 0 && (
|
|
<Tooltip title={editorShown ? 'Back to the terminal' : 'Editor'}>
|
|
<IconButton
|
|
aria-label="editor"
|
|
color={editorShown ? 'primary' : 'default'}
|
|
onClick={() => setEditorShown(!editorShown)}
|
|
sx={editorShown ? { bgcolor: 'action.selected' } : undefined}
|
|
>
|
|
<EditNote />
|
|
</IconButton>
|
|
</Tooltip>
|
|
)}
|
|
{session && (
|
|
<>
|
|
<Button variant={userPage ? 'contained' : 'outlined'} startIcon={<AccountCircle />} onClick={() => toggle(USER_PATH)} sx={{ textTransform: 'none' }}>
|
|
{session.name}
|
|
</Button>
|
|
<Button variant="outlined" startIcon={<Logout />} onClick={() => void signOut()}>Sign out</Button>
|
|
</>
|
|
)}
|
|
{session && admin !== undefined && !accountShown && <SettingsButton />}
|
|
<ThemeMenu />
|
|
{session && (
|
|
<Tooltip title={helpShown ? 'Close the help' : 'Help'}>
|
|
<IconButton
|
|
aria-label="help"
|
|
color={helpShown ? 'primary' : 'default'}
|
|
onClick={toggleHelp}
|
|
sx={helpShown ? { bgcolor: 'action.selected' } : undefined}
|
|
>
|
|
<HelpOutline />
|
|
</IconButton>
|
|
</Tooltip>
|
|
)}
|
|
{session && admin && (
|
|
<PageMenu path={page} newTab={shellNewTab ? [SHELL_PATH] : []} hidden={[...(vpnOff ? [REMOTE_ACCESS_PATH] : []), ...(dnsOff ? [DNS_PATH] : []), ...(dockerMissing ? [DOCKER_PATH] : [])]} />
|
|
)}
|
|
</Stack>
|
|
</Stack>
|
|
{session === undefined && <CircularProgress />}
|
|
{session === null && <SignIn onSignedIn={setSession} />}
|
|
{session && admin === undefined && <CircularProgress />}
|
|
{accountShown && <AccountPanel onPasswordChanged={() => void signOut()} />}
|
|
{session && admin && page === CA_PATH && (
|
|
<>
|
|
<Typography variant="h6" component="h2" sx={{ mb: 2 }}>Certificates</Typography>
|
|
<CertificatesPanel />
|
|
</>
|
|
)}
|
|
{session && admin && page === SITES_PATH && (
|
|
<>
|
|
<Tabs value={sitesView} onChange={(_event, next: SitesView) => setSitesView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}>
|
|
<Tab value="sites" label="Sites" />
|
|
<Tab value="files" label="Files" />
|
|
</Tabs>
|
|
{sitesView === 'sites' && (
|
|
<SitesPanel
|
|
onOpenFiles={(name) => {
|
|
setFilesSite(name)
|
|
setSitesView('files')
|
|
}}
|
|
/>
|
|
)}
|
|
{sitesView === 'files' && <SiteFiles key={filesSite ?? ''} site={filesSite} onSite={setFilesSite} />}
|
|
</>
|
|
)}
|
|
{session && admin && page === DNS_PATH && dnsOff && (
|
|
<Typography color="text.secondary">
|
|
The name server is off on this server: it is switched on on the System tab of the Server page.
|
|
</Typography>
|
|
)}
|
|
{session && admin && page === DNS_PATH && !dnsOff && (
|
|
<>
|
|
<Typography variant="h6" component="h2" sx={{ mb: 2 }}>DNS</Typography>
|
|
<DnsPanel />
|
|
</>
|
|
)}
|
|
{session && admin && page === DOCKER_PATH && dockerMissing && (
|
|
<Typography color="text.secondary">Docker is not installed on this server.</Typography>
|
|
)}
|
|
{session && admin && page === DOCKER_PATH && !dockerMissing && (
|
|
<>
|
|
<Typography variant="h6" component="h2" sx={{ mb: 2 }}>Docker</Typography>
|
|
<DockerPanel />
|
|
</>
|
|
)}
|
|
{session && admin && page === SHELL_PATH && (
|
|
<>
|
|
<Typography variant="h6" component="h2" sx={{ mb: 2 }}>Shell</Typography>
|
|
<ShellPanel editorShown={editorShown} onFiles={showFiles} onOpened={showEditor} />
|
|
</>
|
|
)}
|
|
{session && admin && page === REMOTE_ACCESS_PATH && vpnOff && (
|
|
<Typography color="text.secondary">
|
|
The VPN is off on this server: it is switched on on the System tab of the Server page.
|
|
</Typography>
|
|
)}
|
|
{session && admin && page === REMOTE_ACCESS_PATH && !vpnOff && (
|
|
<>
|
|
<Tabs value={remoteAccessView} onChange={(_event, next: RemoteAccessSystem) => setRemoteAccessView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}>
|
|
<Tab value="p2s" label="IKEv2" />
|
|
<Tab value="openvpn" label="OpenVPN" />
|
|
<Tab value="wireguard" label="WireGuard" />
|
|
</Tabs>
|
|
<RemoteAccessPanel key={remoteAccessView} system={remoteAccessView} />
|
|
</>
|
|
)}
|
|
{session && admin && page === SITE_TO_SITE_PATH && (
|
|
<>
|
|
<Tabs value={routerOff ? 'network' : siteToSiteView} onChange={(_event, next: SiteToSiteView) => setSiteToSiteView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}>
|
|
{!routerOff && <Tab value="status" label="Status" />}
|
|
<Tab value="network" label="Network" />
|
|
{!routerOff && <Tab value="connections" label="Connections" />}
|
|
</Tabs>
|
|
{!routerOff && siteToSiteView === 'connections' && <SiteToSitePanel />}
|
|
{!routerOff && siteToSiteView === 'status' && <SiteToSiteStatusPanel />}
|
|
{(routerOff || siteToSiteView === 'network') && <NetworkPanel />}
|
|
</>
|
|
)}
|
|
{session && admin && page === USERS_PATH && (
|
|
<>
|
|
<Tabs value={usersView} onChange={(_event, next: UsersView) => setUsersView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}>
|
|
<Tab value="users" label="Users" />
|
|
<Tab value="groups" label="Groups" />
|
|
<Tab value="service-accounts" label="Service Accounts" />
|
|
</Tabs>
|
|
{usersView === 'users' && <UsersPanel />}
|
|
{usersView === 'groups' && <GroupsPanel />}
|
|
{usersView === 'service-accounts' && <ServiceAccountsPanel />}
|
|
</>
|
|
)}
|
|
{session && admin && page === '/' && (
|
|
<>
|
|
<Tabs value={view} onChange={(_event, next: View) => setView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}>
|
|
<Tab value="router" label="Overview" />
|
|
<Tab value="metrics" label="Metrics" />
|
|
<Tab value="logs" label="Logs" />
|
|
<Tab value="backup" label="Backup" />
|
|
<Tab value="system" label="System" />
|
|
</Tabs>
|
|
{view === 'router' && <ServerPanel />}
|
|
{view === 'metrics' && <MetricsPanel />}
|
|
{view === 'logs' && <LogsPanel />}
|
|
{view === 'backup' && <BackupPanel />}
|
|
{view === 'system' && <SystemPanel onComponentsChange={() => setComponentsRead((value) => value + 1)} />}
|
|
</>
|
|
)}
|
|
</Container>
|
|
</Box>
|
|
)
|
|
}
|