admin/monitoring: disconnect command should now be more effective

Massimo Melina committed Jan 6, 2024 at 17:23 UTC 4787fdeb72e45a1e96d2b7f8d890c99a0c17e104
3 files changed +9 -16
admin/src/MonitorPage.ts
+2 -3
@@ -10,7 +10,6 @@ import { formatBytes, ipForUrl, manipulateConfig, CFG, formatSpeed, with_ } from
10 import { IconBtn, IconProgress, iconTooltip, usePauseButton, useBreakpoint, Country } from './mui'
11 import { Field, SelectField } from '@hfs/mui-grid-form'
12 import { StandardCSSProperties } from '@mui/system/styleFunctionSx/StandardCssProperties'
13 -import { toast } from "./dialog"
13 import { agentIcons } from './LogsPage'
14 import { state, useSnapState } from './state'
15
@@ -197,8 +196,8 @@ function Connections() {
196 h(IconBtn, {
197 icon: LinkOff,
198 title: "Disconnect",
200 - onClick: () => apiCall('disconnect', _.pick(row, ['ip', 'port']))
201 - .then(() => toast("Disconnection requested")),
199 + doneMessage: true,
200 + onClick: () => apiCall('disconnect', _.pick(row, ['ip', 'port'])).then(x => x.result > 0)
201 }),
202 h(IconBtn, {
203 icon: Block,
src/api.monitor.ts
+6 -12
@@ -2,7 +2,7 @@
2
3 import _ from 'lodash'
4 import { Connection, getConnections } from './connections'
5 -import { pendingPromise, shortenAgent, wait } from './misc'
5 +import { shortenAgent, wait } from './misc'
6 import { ApiHandlers } from './apiMiddleware'
7 import Koa from 'koa'
8 import { totalGot, totalInSpeed, totalOutSpeed, totalSent } from './throttler'
@@ -11,18 +11,12 @@ import { SendListReadable } from './SendList'
11
12 export default {
13
14 - async disconnect({ ip, port, wait }) {
14 + async disconnect({ ip, port }) {
15 const match = _.matches({ ip, port })
16 - const c = getConnections().find(c => match(getConnAddress(c)))
17 - if (c) {
18 - const waiter = pendingPromise<void>()
19 - c.socket.end(waiter.resolve)
20 - c.ctx?.res.end()
21 - c.ctx?.req.socket.end('')
22 - if (wait)
23 - await waiter
24 - }
25 - return { result: Boolean(c) }
16 + const found = getConnections().filter(c => match(getConnAddress(c)))
17 + for (const c of found)
18 + c.socket.destroy()
19 + return { result: found.length }
20 },
21
22 get_connections({}, ctx) {
src/nat.ts
+1 -1
@@ -7,7 +7,7 @@ import _ from 'lodash'
7 import { httpString } from './util-http'
8 import { Resolver } from 'dns/promises'
9 import { isIP } from 'net'
10 -import { baseUrl, getIps, getServerStatus } from './listen'
10 +import { getIps, getServerStatus } from './listen'
11 import { exec } from 'child_process'
12 import { IS_MAC, IS_WINDOWS } from './const'
13