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 = { 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 = { [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('router') const [usersView, setUsersView] = useState('users') const [siteToSiteView, setSiteToSiteView] = useState('status') const [remoteAccessView, setRemoteAccessView] = useState('p2s') const [sitesView, setSitesView] = useState('sites') const [filesSite, setFilesSite] = useState(null) const [session, setSession] = useState(undefined) // /api/info again once signed in, as the version is told only with a session. const [info, setInfo] = useState(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('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('GET', '/account', ACCOUNT_SHAPE) .then((account) => setRole({ name: session.name, admin: account.admin })) .catch(() => setRole({ name: session.name, admin: false })) request('GET', '/info', INFO_SHAPE).then(setInfo).catch(() => undefined) }, [session]) useEffect(() => { if (!admin) { return } request('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 ( ({ mr: { sm: helpShown ? `${HELP_WIDTH}px` : 0 }, transition: theme.transitions.create('margin', { duration: theme.transitions.duration.enteringScreen }), })} > {session && } Ubuntu Linux Manager {session && ( {info.version ?? 'development'} )} Server status page:{' '} {info.status_url ? {info.status_url} : not configured} {session && admin && page === SHELL_PATH && editorFiles > 0 && ( setEditorShown(!editorShown)} sx={editorShown ? { bgcolor: 'action.selected' } : undefined} > )} {session && ( <> )} {session && admin !== undefined && !accountShown && } {session && ( )} {session && admin && ( {session === undefined && } {session === null && } {session && admin === undefined && } {accountShown && void signOut()} />} {session && admin && page === CA_PATH && ( <> Certificates )} {session && admin && page === SITES_PATH && ( <> setSitesView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}> {sitesView === 'sites' && ( { setFilesSite(name) setSitesView('files') }} /> )} {sitesView === 'files' && } )} {session && admin && page === DNS_PATH && dnsOff && ( The name server is off on this server: it is switched on on the System tab of the Server page. )} {session && admin && page === DNS_PATH && !dnsOff && ( <> DNS )} {session && admin && page === DOCKER_PATH && dockerMissing && ( Docker is not installed on this server. )} {session && admin && page === DOCKER_PATH && !dockerMissing && ( <> Docker )} {session && admin && page === SHELL_PATH && ( <> Shell )} {session && admin && page === REMOTE_ACCESS_PATH && vpnOff && ( The VPN is off on this server: it is switched on on the System tab of the Server page. )} {session && admin && page === REMOTE_ACCESS_PATH && !vpnOff && ( <> setRemoteAccessView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}> )} {session && admin && page === SITE_TO_SITE_PATH && ( <> setSiteToSiteView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}> {!routerOff && } {!routerOff && } {!routerOff && siteToSiteView === 'connections' && } {!routerOff && siteToSiteView === 'status' && } {(routerOff || siteToSiteView === 'network') && } )} {session && admin && page === USERS_PATH && ( <> setUsersView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}> {usersView === 'users' && } {usersView === 'groups' && } {usersView === 'service-accounts' && } )} {session && admin && page === '/' && ( <> setView(next)} sx={{ mb: 3, borderBottom: 1, borderColor: 'divider' }}> {view === 'router' && } {view === 'metrics' && } {view === 'logs' && } {view === 'backup' && } {view === 'system' && setComponentsRead((value) => value + 1)} />} )} ) }