90 lines
3.1 KiB
TypeScript
90 lines
3.1 KiB
TypeScript
import Refresh from '@mui/icons-material/Refresh'
|
|
import { Alert, Box, Button } from '@mui/material'
|
|
import { Board, BoardButtons, type BoardPanel } from './PanelBoard.tsx'
|
|
import { useBoard } from './useBoard.ts'
|
|
import { Grid, Pairs } from './ServerPanel.tsx'
|
|
import { text } from './format.ts'
|
|
import Toolbar from './Toolbar.tsx'
|
|
import { DETAILS_SHAPE, request, type RouterDetails } from './api.ts'
|
|
import { useRead } from './useRead.ts'
|
|
|
|
/** The localStorage key of the tab's layout. */
|
|
const LAYOUT_KEY = 'linux-manager-ui.network-layout'
|
|
|
|
/** Reads the server's /config. */
|
|
const readDetails = () => request<RouterDetails>('GET', '/config', DETAILS_SHAPE)
|
|
|
|
/**
|
|
* The server's network, from its /config: the interfaces but loopback, external when they hold an IPv4 default route
|
|
* and internal otherwise; the internal gateway and the local CIDRs while the router is on; and the IPv4 routes. The
|
|
* panels are on a board whose layout is kept in the browser.
|
|
*/
|
|
export default function NetworkPanel() {
|
|
const { data: details, error, busy, refresh } = useRead(readDetails)
|
|
const board = useBoard(LAYOUT_KEY)
|
|
|
|
const network = details?.network
|
|
const router = details?.configuration.router?.enabled === true
|
|
// An interface holding an IPv4 default route is external, every other one internal.
|
|
const external = new Set(network?.routes?.filter((route) => route.dst === 'default').map((route) => route.dev))
|
|
const panels: BoardPanel[] = network ? [
|
|
{
|
|
id: 'interfaces',
|
|
title: 'Interfaces',
|
|
content: (
|
|
<Grid
|
|
head={['Name', 'Type', 'State', 'IPv4 addresses', 'IPv6 addresses', 'MAC address']}
|
|
rows={network.interfaces?.map((link) => [
|
|
link.name,
|
|
external.has(link.name) ? 'external' : 'internal',
|
|
text(link.state),
|
|
text(link.ipv4),
|
|
text(link.ipv6),
|
|
text(link.mac),
|
|
])}
|
|
/>
|
|
),
|
|
},
|
|
...(router ? [{
|
|
id: 'router',
|
|
title: 'Router',
|
|
content: (
|
|
<Pairs
|
|
rows={[
|
|
['Internal gateway', text(network.internal_gateway)],
|
|
['Local CIDRs', text(network.local_cidrs)],
|
|
]}
|
|
/>
|
|
),
|
|
}] : []),
|
|
{
|
|
id: 'routes',
|
|
title: 'Routes',
|
|
content: (
|
|
<Grid
|
|
head={['Destination', 'Gateway', 'Device', 'Source', 'Protocol', 'Metric']}
|
|
rows={network.routes?.map((route) => [
|
|
route.dst,
|
|
text(route.gateway),
|
|
route.flags.length > 0 ? `${route.dev} (${route.flags.join(', ')})` : route.dev,
|
|
text(route.prefsrc),
|
|
text(route.protocol),
|
|
text(route.metric),
|
|
])}
|
|
/>
|
|
),
|
|
},
|
|
] : []
|
|
|
|
return (
|
|
<Box component="section">
|
|
<Toolbar description="Interfaces, addresses and routes.">
|
|
<BoardButtons board={board} />
|
|
<Button variant="outlined" startIcon={<Refresh />} disabled={busy} onClick={() => void refresh()}>Refresh</Button>
|
|
</Toolbar>
|
|
{error && <Alert severity="error" sx={{ mb: 2, whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
<Board board={board} panels={panels} />
|
|
</Box>
|
|
)
|
|
}
|