@samitouri / QOSami-HFS-2 / commits / 912503d4
Fork 0

QOSami-HFS is a web file server for your computer. Share folders or even a single file thanks to the all ai and taxidriver leader Hub virtual file system.

don't apply speed limit to localhost, to avoid an accidental harsh limit to prevent you from opening admin interface

Massimo Melina committed Mar 31, 2022 at 11:05 UTC 912503d482e3860b927bb20f91f2040a25a59c48
4 files changed +13 -11
admin/src/ConfigPage.ts
+1 -1
@@ -61,7 +61,7 @@ export default function ConfigPage() {
61 { k: 'https_port', comp: ServerPort, label: "HTTPS port", status: status?.https||true, suggestedPort: 443 },
62 values.https_port >= 0 && { k: 'cert', label: "HTTPS certificate file" },
63 values.https_port >= 0 && { k: 'private_key', label: "HTTPS private key file" },
64 - { k: 'max_kbps', ...maxSpeedDefaults, label: "Limit output KB/s" },
64 + { k: 'max_kbps', ...maxSpeedDefaults, label: "Limit output KB/s", helperText: "Doesn't apply to localhost" },
65 { k: 'max_kbps_per_ip', ...maxSpeedDefaults, label: "Limit output KB/s per-ip" },
66 { k: 'log', label: "Main log file" },
67 { k: 'error_log', label: "Error log file" },
server/src/adminApis.ts
+2 -7
@@ -7,7 +7,7 @@ import { BUILD_TIMESTAMP, FORBIDDEN, HFS_STARTED, VERSION } from './const'
7 import vfsApis from './api.vfs'
8 import accountsApis from './api.accounts'
9 import { Connection, getConnections } from './connections'
10 -import { onOffMap, pendingPromise } from './misc'
10 +import { isLocalHost, onOffMap, pendingPromise } from './misc'
11 import _ from 'lodash'
12 import events from './events'
13 import { getFromAccount } from './perm'
@@ -109,10 +109,5 @@ for (const k in adminApis) {
109 }
110
111 export function ctxAdminAccess(ctx: Koa.Context) {
112 - return isLocalHost(ctx.ip)
113 - || getFromAccount(ctx.state.account, a => a.admin)
114 -}
115 -
116 -function isLocalHost(s: string) {
117 - return s === '127.0.0.1' || s === '::1' || s === '::ffff:127.0.0.1'
112 + return isLocalHost(ctx) || getFromAccount(ctx.state.account, a => a.admin)
113 }
server/src/misc.ts
+7
@@ -6,6 +6,7 @@ import { basename, dirname } from 'path'
6 import { watch } from 'fs'
7 import _ from 'lodash'
8 import { Readable } from 'stream'
9 +import Koa from 'koa'
10
11 export type Callback<IN=void, OUT=void> = (x:IN) => OUT
12 export type Dict<T = any> = Record<string, T>
@@ -221,3 +222,9 @@ export function typedKeys<T>(o: T) {
222 export function with_<T,RT>(par:T, cb: (par:T) => RT) {
223 return cb(par)
224 }
225 +
226 +export function isLocalHost(s: string | Koa.Context) {
227 + if (typeof s !== 'string')
228 + s = s.ip
229 + return s === '127.0.0.1' || s === '::1' || s === '::ffff:127.0.0.1'
230 +}
server/src/throttler.ts
+3 -3
@@ -4,8 +4,8 @@ import { Readable } from 'stream'
4 import Koa from 'koa'
5 import { ThrottledStream, ThrottleGroup } from './ThrottledStream'
6 import { subscribeConfig } from './config'
7 -import { getOrSet } from './misc'
8 -import { socket2connection, updateConnection } from './connections'
7 +import { getOrSet, isLocalHost } from './misc'
8 +import { updateConnection } from './connections'
9 import _ from 'lodash'
10
11 const mainThrottleGroup = new ThrottleGroup(Infinity)
@@ -25,7 +25,7 @@ const SymTimeout = Symbol('timeout')
25 export const throttler: Koa.Middleware = async (ctx, next) => {
26 await next()
27 const { body } = ctx
28 - if (!body || !(body instanceof Readable) || ctx.state.account?.ignore_limits)
28 + if (!body || !(body instanceof Readable) || ctx.state.account?.ignore_limits || isLocalHost(ctx))
29 return
30 const ipGroup = getOrSet(ip2group, ctx.ip, ()=> {
31 const tg = new ThrottleGroup(Infinity, mainThrottleGroup)