import { createEventSignal } from '@solid-primitives/event-listener' import { useI18n } from '@solid-primitives/i18n' import { makePersisted } from '@solid-primitives/storage' import { createReconnectingWS } from '@solid-primitives/websocket' import { IconCircleX, IconSettings, IconSortAscending, IconSortDescending, } from '@tabler/icons-solidjs' import { ColumnDef, SortingState, createSolidTable, flexRender, getCoreRowModel, getSortedRowModel, } from '@tanstack/solid-table' import byteSize from 'byte-size' import { isIPv6 } from 'is-ip' import { For, createEffect, createSignal } from 'solid-js' import { twMerge } from 'tailwind-merge' import ConnectionsModal from '~/components/ConnectionsModal' import { AccessorKey } from '~/config/enum' import { secret, useRequest, wsEndpointURL } from '~/signals' import type { Connection } from '~/types' type ConnectionWithSpeed = Connection & { downloadSpeed: number uploadSpeed: number } type ColumnVisibility = Partial> type ColumnOrder = AccessorKey[] const initColumnOrder = Object.values(AccessorKey) const initColumnVisibility = { ...Object.fromEntries(initColumnOrder.map((i) => [i, true])), [AccessorKey.ID]: false, } export default () => { const [t] = useI18n() const [columnVisibility, setColumnVisibility] = makePersisted( createSignal(initColumnVisibility), { name: 'columnVisibility', storage: localStorage, }, ) const [columnOrder, setColumnOrder] = makePersisted( createSignal(initColumnOrder), { name: 'columnOrder', storage: localStorage, }, ) const request = useRequest() const [search, setSearch] = createSignal('') const ws = createReconnectingWS( `${wsEndpointURL()}/connections?token=${secret()}`, ) const messageEvent = createEventSignal<{ message: WebSocketEventMap['message'] }>(ws, 'message') const [connectionsWithSpeed, setConnectionsWithSpeed] = createSignal< ConnectionWithSpeed[] >([]) createEffect(() => { const data = messageEvent()?.data if (!data) { return } setConnectionsWithSpeed((prevConnections) => { const prevMap = new Map() prevConnections.forEach((prev) => prevMap.set(prev.id, prev)) const connections = ( JSON.parse(data) as { connections: Connection[] } ).connections.map((connection) => { const prevConn = prevMap.get(connection.id) if (!prevConn) { return { ...connection, downloadSpeed: 0, uploadSpeed: 0 } } return { ...connection, downloadSpeed: prevConn.download ? connection.download - prevConn.download : 0, uploadSpeed: prevConn.upload ? connection.upload - prevConn.upload : 0, } }) return connections.slice(-100) }) }) const onCloseConnection = (id: string) => request.delete(`connections/${id}`) const columns: ColumnDef[] = [ { accessorKey: AccessorKey.Close, header: () => (
), cell: ({ row }) => (
), }, { accessorKey: AccessorKey.ID, accessorFn: (row) => row.id, }, { accessorKey: AccessorKey.Type, accessorFn: (row) => `${row.metadata.type}(${row.metadata.network})`, }, { accessorKey: AccessorKey.Process, accessorFn: (row) => row.metadata.process || row.metadata.processPath.replace(/^.*[/\\](.*)$/, '$1') || '-', }, { accessorKey: AccessorKey.Host, accessorFn: (row) => `${ row.metadata.host ? row.metadata.host : row.metadata.destinationIP }:${row.metadata.destinationPort}`, }, { accessorKey: AccessorKey.Rule, accessorFn: (row) => !row.rulePayload ? row.rule : `${row.rule} :: ${row.rulePayload}`, }, { accessorKey: AccessorKey.Chains, accessorFn: (row) => row.chains.slice().reverse().join(' :: '), }, { accessorKey: AccessorKey.DlSpeed, accessorFn: (row) => `${byteSize(row.downloadSpeed)}/s`, sortingFn: (prev, next) => prev.original.downloadSpeed - next.original.downloadSpeed, }, { accessorKey: AccessorKey.ULSpeed, accessorFn: (row) => `${byteSize(row.uploadSpeed)}/s`, sortingFn: (prev, next) => prev.original.uploadSpeed - next.original.uploadSpeed, }, { accessorKey: AccessorKey.Download, accessorFn: (row) => byteSize(row.download), sortingFn: (prev, next) => prev.original.download - next.original.download, }, { accessorKey: AccessorKey.Upload, accessorFn: (row) => byteSize(row.upload), sortingFn: (prev, next) => prev.original.upload - next.original.upload, }, { accessorKey: AccessorKey.Source, accessorFn: (row) => isIPv6(row.metadata.sourceIP) ? `[${row.metadata.sourceIP}]:${row.metadata.sourcePort}` : `${row.metadata.sourceIP}:${row.metadata.sourcePort}`, }, { accessorKey: AccessorKey.Destination, accessorFn: (row) => row.metadata.remoteDestination || row.metadata.destinationIP || row.metadata.host, }, ] const [sorting, setSorting] = createSignal([ { id: 'ID', desc: true }, ]) const table = createSolidTable({ state: { get columnOrder() { return columnOrder() }, get sorting() { return sorting() }, get columnVisibility() { return columnVisibility() }, }, get data() { return search() ? connectionsWithSpeed().filter((connection) => Object.values(connection).some((conn) => JSON.stringify(conn) .toLowerCase() .includes(search().toLowerCase()), ), ) : connectionsWithSpeed() }, enableHiding: true, columns, onSortingChange: setSorting, getSortedRowModel: getSortedRowModel(), getCoreRowModel: getCoreRowModel(), }) return (
setSearch(e.target.value)} /> { setColumnOrder([...data]) }} onVisibleChange={(data: ColumnVisibility) => setColumnVisibility({ ...data }) } />
{(headerGroup) => ( {(header) => ( )} )} {(row) => ( {(cell) => ( )} )}
{header.column.id === AccessorKey.Close ? ( flexRender( header.column.columnDef.header, header.getContext(), ) ) : ( {t(header.column.id)} )} {{ asc: , desc: , }[header.column.getIsSorted() as string] ?? null}
{flexRender( cell.column.columnDef.cell, cell.getContext(), )}
) }