Commit dae64f00 authored by NGPixel's avatar NGPixel

fix: brute-knex refactor

parent 2213ba2c
...@@ -53,7 +53,6 @@ ...@@ -53,7 +53,6 @@
"bcryptjs-then": "1.0.1", "bcryptjs-then": "1.0.1",
"bluebird": "3.7.2", "bluebird": "3.7.2",
"body-parser": "1.19.0", "body-parser": "1.19.0",
"brute-knex": "4.0.0",
"chalk": "4.0.0", "chalk": "4.0.0",
"cheerio": "1.0.0-rc.3", "cheerio": "1.0.0-rc.3",
"chokidar": "3.3.1", "chokidar": "3.3.1",
...@@ -69,8 +68,8 @@ ...@@ -69,8 +68,8 @@
"dotize": "0.3.0", "dotize": "0.3.0",
"elasticsearch6": "npm:@elastic/elasticsearch@6", "elasticsearch6": "npm:@elastic/elasticsearch@6",
"elasticsearch7": "npm:@elastic/elasticsearch@7", "elasticsearch7": "npm:@elastic/elasticsearch@7",
"eventemitter2": "6.0.0",
"emoji-regex": "9.0.0", "emoji-regex": "9.0.0",
"eventemitter2": "6.0.0",
"express": "4.17.1", "express": "4.17.1",
"express-brute": "1.0.1", "express-brute": "1.0.1",
"express-session": "1.17.1", "express-session": "1.17.1",
...@@ -146,8 +145,8 @@ ...@@ -146,8 +145,8 @@
"pem-jwk": "2.0.0", "pem-jwk": "2.0.0",
"pg": "8.0.2", "pg": "8.0.2",
"pg-hstore": "2.3.3", "pg-hstore": "2.3.3",
"pg-query-stream": "3.0.6",
"pg-pubsub": "0.5.0", "pg-pubsub": "0.5.0",
"pg-query-stream": "3.0.6",
"pg-tsquery": "8.1.0", "pg-tsquery": "8.1.0",
"pug": "2.0.4", "pug": "2.0.4",
"punycode": "2.1.1", "punycode": "2.1.1",
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
const express = require('express') const express = require('express')
const ExpressBrute = require('express-brute') const ExpressBrute = require('express-brute')
const BruteKnex = require('brute-knex') const BruteKnex = require('../helpers/brute-knex')
const router = express.Router() const router = express.Router()
const moment = require('moment') const moment = require('moment')
const _ = require('lodash') const _ = require('lodash')
......
const AbstractClientStore = require('express-brute/lib/AbstractClientStore')
const KnexStore = module.exports = function (options) {
options = options || Object.create(null)
AbstractClientStore.apply(this, arguments)
this.options = Object.assign(Object.create(null), KnexStore.defaults, options)
if (this.options.knex) {
this.knex = this.options.knex
} else {
this.knex = require('knex')(KnexStore.defaultsKnex)
}
if (options.createTable === false) {
this.ready = Promise.resolve()
} else {
this.ready = this.knex.schema.hasTable(this.options.tablename)
.then((exists) => {
if (exists) {
return
}
return this.knex.schema.createTable(this.options.tablename, (table) => {
table.string('key')
table.bigInteger('firstRequest').nullable()
table.bigInteger('lastRequest').nullable()
table.bigInteger('lifetime').nullable()
table.integer('count')
})
})
}
}
KnexStore.prototype = Object.create(AbstractClientStore.prototype)
KnexStore.prototype.set = async function (key, value, lifetime, callback) {
try {
lifetime = lifetime || 0
await this.ready
const resp = await this.knex.transaction((trx) => {
return trx
.select('*')
.forUpdate()
.from(this.options.tablename)
.where('key', '=', key)
.then((foundKeys) => {
if (foundKeys.length === 0) {
return trx.from(this.options.tablename)
.insert({
key: key,
lifetime: new Date(Date.now() + lifetime * 1000).getTime(),
lastRequest: new Date(value.lastRequest).getTime(),
firstRequest: new Date(value.firstRequest).getTime(),
count: value.count
})
} else {
return trx(this.options.tablename)
.where('key', '=', key)
.update({
lifetime: new Date(Date.now() + lifetime * 1000).getTime(),
count: value.count,
lastRequest: new Date(value.lastRequest).getTime()
})
}
})
})
callback(null, resp)
} catch (err) {
callback(err, null)
}
}
KnexStore.prototype.get = async function (key, callback) {
try {
await this.ready
await this.clearExpired()
const resp = await this.knex.select('*')
.from(this.options.tablename)
.where('key', '=', key)
let o = null
if (resp[0]) {
o = {}
o.lastRequest = new Date(resp[0].lastRequest)
o.firstRequest = new Date(resp[0].firstRequest)
o.count = resp[0].count
}
callback(null, o)
} catch (err) {
callback(err, null)
}
}
KnexStore.prototype.reset = async function (key, callback) {
try {
await this.ready
const resp = await this.knex(this.options.tablename)
.where('key', '=', key)
.del()
callback(null, resp)
} catch (err) {
callback(err, null)
}
}
KnexStore.prototype.increment = async function (key, lifetime, callback) {
try {
const result = await this.get(key)
let resp = null
if (result) {
resp = await this.knex(this.options.tablename)
.increment('count', 1)
.where('key', '=', key)
} else {
resp = await this.knex(this.options.tablename)
.insert({
key: key,
firstRequest: new Date().getTime(),
lastRequest: new Date().getTime(),
lifetime: new Date(Date.now() + lifetime * 1000).getTime(),
count: 1
})
}
callback(null, resp)
} catch (err) {
callback(err, null)
}
}
KnexStore.prototype.clearExpired = async function (callback) {
await this.ready
return this.knex(this.options.tablename)
.del()
.where('lifetime', '<', new Date().getTime())
}
KnexStore.defaults = {
tablename: 'brute',
createTable: true
}
KnexStore.defaultsKnex = {
client: 'sqlite3',
// debug: true,
connection: {
filename: './brute-knex.sqlite'
}
}
This diff was suppressed by a .gitattributes entry.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment