better code
Massimo Melina committed
Feb 8, 2025 at 19:48 UTC
a115dd15f5a0a39a2ad93951f25209788596dee0
1 file changed
+5
-7
plugins/antibrute/plugin.js
+5
-7
@@ -1,6 +1,6 @@
1
exports.version = 3
2
exports.description = "Introduce increasing delays between login attempts."
3
-exports.apiRequired = 8.8 // attemptingLogin
3
+exports.apiRequired = 9.6 // addBlock
4
5
exports.config = {
6
increment: { type: 'number', min: 1, defaultValue: 5, unit: "seconds", helperText: "How longer user must wait for each login attempt" },
@@ -15,11 +15,10 @@ exports.configDialog = {
15
const byIp = {}
16
17
exports.init = api => {
18
- const { getOrSet, isLocalHost, HOUR } = api.require('./misc')
19
- const { block } = api.require('./block')
18
+ const { getOrSet, isLocalHost, HOUR } = api.misc
19
return {
20
unload: api.events.multi({
22
- attemptingLogin: async ({ ctx }) => {
21
+ async attemptingLogin({ ctx }) {
22
const { ip } = ctx
23
const now = new Date
24
const rec = getOrSet(byIp, ip, () => ({ attempts: 0, next: now }))
@@ -29,8 +28,7 @@ exports.init = api => {
28
rec.next = new Date(+rec.next + delay)
29
if (rec.attempts > api.getConfig('blockAfter') && !isLocalHost(ctx)) {
30
const hours = api.getConfig('blockForHours')
32
- const newRule = { ip, comment: "From antibrute plugin", expire: hours ? new Date(now.getTime() + hours * HOUR) : undefined }
33
- block.set(x => [...x, newRule])
31
+ api.addBlock({ ip, comment: "From antibrute plugin", expire: hours ? new Date(now.getTime() + hours * HOUR) : undefined })
32
}
33
clearTimeout(rec.timer)
34
if (wait > 0) {
@@ -44,6 +42,6 @@ exports.init = api => {
42
if (ctx.state.account)
43
delete byIp[ctx.ip] // reset if login was successful
44
}
47
- })
45
+ })
46
}
47
}