admin/monitor: prevent auto-blocking https://github.com/rejetto/hfs/issues/88

Massimo Melina committed Jan 16, 2023 at 21:09 UTC ba322e75ac904076631bf72692923c6398b24d8c
4 files changed +15 -3
admin/src/MonitorPage.ts
+7 -2
@@ -78,7 +78,7 @@ function MoreInfo() {
78 }
79
80 function Connections() {
81 - const { list, error } = useApiList('get_connections')
81 + const { list, error, props } = useApiList('get_connections')
82 const [filtered, setFiltered] = useState(true)
83 const [paused, setPaused] = useState(false)
84 const rows = useMemo(() =>
@@ -159,12 +159,13 @@ function Connections() {
159 h(IconBtn, {
160 icon: Block,
161 title: "Block IP",
162 + disabled: row.ip === props?.you,
163 onClick: () => blockIp(row.ip),
164 }),
165 )
166 }
167 }
167 - ], [])
168 + ], [props])
169 return h(Fragment, {},
170 h(Box, { display: 'flex', alignItems: 'center' },
171 h(SelectField as Field<boolean>, {
@@ -198,3 +199,7 @@ function blockIp(ip: string) {
199 function formatSpeed(value: number) {
200 return !value ? '' : formatBytes(value * 1000, { post: "B/s", k: 1000, digits: 1 })
201 }
202 +
203 +function isLocalHost(ip: string) {
204 + return ip === '::1' || ip.endsWith('127.0.0.1')
205 +}
\ No newline at end of file
admin/src/api.ts
+4 -1
@@ -146,6 +146,7 @@ export function useApiEvents(cmd: string, params: Dict={}) {
146
147 export function useApiList<T=any>(cmd:string|Falsy, params: Dict={}, { addId=false, map=((x:any)=>x) }={}) {
148 const [list, setList] = useStateMounted<T[]>([])
149 + const [props, setProps] = useStateMounted<any>(undefined)
150 const [error, setError] = useStateMounted<any>(undefined)
151 const [connecting, setConnecting] = useStateMounted(true)
152 const [loading, setLoading] = useStateMounted(false)
@@ -183,6 +184,8 @@ export function useApiList<T=any>(cmd:string|Falsy, params: Dict={}, { addId=fal
184 }
185 if (data.error)
186 return setError(err2msg(data.error))
187 + if (data.props)
188 + return setProps(data.props)
189 if (data.add) {
190 const rec = map(data.add)
191 if (addId)
@@ -236,7 +239,7 @@ export function useApiList<T=any>(cmd:string|Falsy, params: Dict={}, { addId=fal
239 apply.flush()
240 }
241 }, [cmd, JSON.stringify(params)]) //eslint-disable-line
239 - return { list, loading, error, initializing, connecting, setList, updateList }
242 + return { list, props, loading, error, initializing, connecting, setList, updateList }
243
244 function updateList(cb: (toModify: Draft<typeof list>) => void) {
245 setList(produce(list, x => {
src/api.monitor.ts
+1
@@ -25,6 +25,7 @@ const apis: ApiHandlers = {
25 type Change = Partial<Omit<Connection,'ip'>>
26 const throttledUpdate = _.throttle(update, 1000/20) // try to avoid clogging with updates
27 const state = Symbol('state') // undefined=added, Timeout=add-pending, false=removed
28 + list.props({ you: ctx.ip })
29 return list.events(ctx, {
30 connection(conn: Connection) {
31 conn[state] = setTimeout(() => add(conn), 100)
src/apiMiddleware.ts
+3
@@ -100,6 +100,9 @@ export class SendListReadable<T> extends Readable {
100 custom(data: any) {
101 this._push(data)
102 }
103 + props(props: object) {
104 + this._push({ props })
105 + }
106 error(msg: NonNullable<typeof this.lastError>, close=false) {
107 this._push({ error: msg })
108 this.lastError = msg