Skip to content

Commit

Permalink
admin/monitoring: disconnect-all button #797
Browse files Browse the repository at this point in the history
  • Loading branch information
rejetto committed Nov 9, 2024
1 parent c07a87b commit 3a3bf32
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
21 changes: 15 additions & 6 deletions admin/src/MonitorPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
import _ from "lodash"
import { createElement as h, useMemo, Fragment, useState } from "react"
import { apiCall, useApiEvents, useApiEx, useApiList } from "./api"
import { LinkOff, Lock, FolderZip, Upload, Download, ChevronRight, ChevronLeft, History } from '@mui/icons-material'
import { LinkOff as DisconnectIcon, Lock, FolderZip, Upload, Download, ChevronRight, ChevronLeft, History } from '@mui/icons-material'
import { Alert, Box, Chip, ChipProps, Grid } from '@mui/material'
import { DataTable } from './DataTable'
import {
formatBytes, ipForUrl, CFG, formatSpeed, with_, createDurationFormatter, formatTimestamp, formatPerc, md, Callback,
reactJoin,
} from "./misc"
import {
IconBtn, IconProgress, iconTooltip, usePauseButton, useBreakpoint, Country, hTooltip, useToggleButton, Flex
IconBtn, IconProgress, iconTooltip, usePauseButton, useBreakpoint, Country, hTooltip, useToggleButton, Flex, Btn
} from './mui'
import { Field, SelectField } from '@hfs/mui-grid-form'
import { StandardCSSProperties } from '@mui/system/styleFunctionSx/StandardCssProperties'
import { agentIcons, LogFile } from './LogsPage'
import { state, useSnapState } from './state'
import { useBlockIp } from './useBlockIp'
import { alertDialog, confirmDialog } from './dialog'
import { alertDialog, confirmDialog, toast } from './dialog'
import { useInterval } from 'usehooks-ts'
import { PageProps } from './App'

Expand Down Expand Up @@ -129,7 +129,7 @@ function Connections() {
const logSize = logAble && wantLog ? 6 : 0
return h(Fragment, {},
h(Flex, {},
h(Box, { flex: 1 },
h(Flex, { flex: 1 },
h(SelectField as Field<boolean>, {
fullWidth: false,
value: monitorOnlyFiles,
Expand All @@ -147,7 +147,16 @@ function Connections() {
error,
rows,
noRows: monitorOnlyFiles && "No downloads at the moment",
footerSide: () => pauseButton,
footerSide: () => h(Flex, {},
pauseButton,
h(Btn, {
size: 'small',
icon: DisconnectIcon,
labelIf: 'xl',
confirm: "Disconnecting all connections but localhost. Continue?",
onClick: () => apiCall('disconnect', { allButLocalhost: true }).then(x => toast(`Disconnected: ${x.result}`))
}, "Disconnect all")
),
columns: [
{
field: 'ip',
Expand Down Expand Up @@ -249,7 +258,7 @@ function Connections() {
actionsProps: { hideUnder: 'sm' },
actions: ({ row }) => [
h(IconBtn, {
icon: LinkOff,
icon: DisconnectIcon,
title: "Disconnect",
doneMessage: true,
onClick: () => apiCall('disconnect', _.pick(row, ['ip', 'port'])).then(x => x.result > 0)
Expand Down
2 changes: 2 additions & 0 deletions admin/src/mui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ export const Btn = forwardRef(({ icon, title, onClick, disabled, progress, link,
if (link)
onClick = () => window.open(link)
const showLabel = useBreakpoint(_.isString(labelIf) ? labelIf : 'xs') && (_.isBoolean(labelIf) ? labelIf : true)
if (!showLabel)
title = children
const ref = useRefPass<HTMLButtonElement>(forwarded)
const common = _.merge(propsForModifiedValues(modified), {
ref,
Expand Down
7 changes: 4 additions & 3 deletions src/api.monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import _ from 'lodash'
import { Connection, getConnections } from './connections'
import { apiAssertTypes, shortenAgent, try_, wait, wantArray } from './misc'
import { apiAssertTypes, isLocalHost, shortenAgent, try_, wait, wantArray } from './misc'
import { ApiHandlers } from './apiMiddleware'
import Koa from 'koa'
import { totalGot, totalInSpeed, totalOutSpeed, totalSent } from './throttler'
Expand All @@ -12,8 +12,9 @@ import { storedMap } from './persistence'

export default {

async disconnect({ ip, port }) {
const match = _.matches({ ip, port })
async disconnect({ ip, port, allButLocalhost }) {
const match = allButLocalhost ? ((x: any) => !isLocalHost(x.ip))
: _.matches({ ip, port })
const found = getConnections().filter(c => match(getConnAddress(c)))
for (const c of found)
c.socket.destroy()
Expand Down

0 comments on commit 3a3bf32

Please sign in to comment.