fix: antibrute plugin not working in exe distribution

Massimo Melina committed Apr 12, 2022 at 21:22 UTC 1ba07effe50716bd568216ceb2b876321c958245
1 file changed +21 -20
plugins/antibrute/plugin.js
+21 -20
@@ -1,5 +1,3 @@
1 -const { API_URI } = require('@hfs/server/src/const')
2 -
1 exports.version = 1
2 exports.description = "Introduce increasing delays between login attempts."
3
@@ -7,26 +5,29 @@ exports.description = "Introduce increasing delays between login attempts."
5 const INCREMENT = 5_000
6 const CAP = 60_000
7
10 -const LOGIN_URI = API_URI + 'loginSrp1'
8 const byIp = {}
9
13 -exports.init = api => ({
14 - async middleware(ctx) {
15 - if (ctx.path !== LOGIN_URI) return
16 - const k = ctx.ip
17 - const now = Date.now()
18 - const rec = byIp[k]
19 - if (rec) {
20 - const wait = rec.when - now
21 - if (wait > 0) {
22 - console.log('plugin antibrute is delaying', k, 'for', Math.round(wait/1000))
23 - await new Promise(resolve => setTimeout(resolve, wait))
10 +exports.init = api => {
11 + const { API_URI } = api.require(api.srcDir + '/const')
12 + const LOGIN_URI = API_URI + 'loginSrp1'
13 + return ({
14 + async middleware(ctx) {
15 + if (ctx.path !== LOGIN_URI) return
16 + const k = ctx.ip
17 + const now = Date.now()
18 + const rec = byIp[k]
19 + if (rec) {
20 + const wait = rec.when - now
21 + if (wait > 0) {
22 + console.log('plugin antibrute is delaying', k, 'for', Math.round(wait / 1000))
23 + await new Promise(resolve => setTimeout(resolve, wait))
24 + }
25 + ctx.set('x-anti-brute-force', wait)
26 }
25 - ctx.set('x-anti-brute-force', wait)
27 + const delay = Math.min(CAP, (rec?.delay || 0) + INCREMENT)
28 + byIp[k] = { delay, when: now + delay }
29 + setTimeout(() => delete byIp[k], delay * 10) // no memory leak
30 }
27 - const delay = Math.min(CAP, (rec?.delay || 0) + INCREMENT)
28 - byIp[k] = { delay, when: now + delay }
29 - setTimeout(() => delete byIp[k], delay * 10) // no memory leak
30 - }
31 -})
31 + })
32 +}
33