fix: brute-knex refactor
This commit is contained in:
		| @@ -53,7 +53,6 @@ | ||||
|     "bcryptjs-then": "1.0.1", | ||||
|     "bluebird": "3.7.2", | ||||
|     "body-parser": "1.19.0", | ||||
|     "brute-knex": "4.0.0", | ||||
|     "chalk": "4.0.0", | ||||
|     "cheerio": "1.0.0-rc.3", | ||||
|     "chokidar": "3.3.1", | ||||
| @@ -69,8 +68,8 @@ | ||||
|     "dotize": "0.3.0", | ||||
|     "elasticsearch6": "npm:@elastic/elasticsearch@6", | ||||
|     "elasticsearch7": "npm:@elastic/elasticsearch@7", | ||||
|     "eventemitter2": "6.0.0", | ||||
|     "emoji-regex": "9.0.0", | ||||
|     "eventemitter2": "6.0.0", | ||||
|     "express": "4.17.1", | ||||
|     "express-brute": "1.0.1", | ||||
|     "express-session": "1.17.1", | ||||
| @@ -146,8 +145,8 @@ | ||||
|     "pem-jwk": "2.0.0", | ||||
|     "pg": "8.0.2", | ||||
|     "pg-hstore": "2.3.3", | ||||
|     "pg-query-stream": "3.0.6", | ||||
|     "pg-pubsub": "0.5.0", | ||||
|     "pg-query-stream": "3.0.6", | ||||
|     "pg-tsquery": "8.1.0", | ||||
|     "pug": "2.0.4", | ||||
|     "punycode": "2.1.1", | ||||
|   | ||||
| @@ -2,7 +2,7 @@ | ||||
|  | ||||
| const express = require('express') | ||||
| const ExpressBrute = require('express-brute') | ||||
| const BruteKnex = require('brute-knex') | ||||
| const BruteKnex = require('../helpers/brute-knex') | ||||
| const router = express.Router() | ||||
| const moment = require('moment') | ||||
| const _ = require('lodash') | ||||
|   | ||||
							
								
								
									
										148
									
								
								server/helpers/brute-knex.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										148
									
								
								server/helpers/brute-knex.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,148 @@ | ||||
| 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' | ||||
|   } | ||||
| } | ||||
							
								
								
									
										70
									
								
								yarn.lock
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								yarn.lock
									
									
									
									
									
								
							| @@ -1359,14 +1359,6 @@ | ||||
|     "@babel/helper-create-regexp-features-plugin" "^7.8.3" | ||||
|     "@babel/helper-plugin-utils" "^7.8.3" | ||||
|  | ||||
| "@babel/polyfill@^7.4.4": | ||||
|   version "7.6.0" | ||||
|   resolved "https://registry.yarnpkg.com/@babel/polyfill/-/polyfill-7.6.0.tgz#6d89203f8b6cd323e8d946e47774ea35dc0619cc" | ||||
|   integrity sha512-q5BZJI0n/B10VaQQvln1IlDK3BTBJFbADx7tv+oXDPIDZuTo37H5Adb9jhlXm/fEN4Y7/64qD9mnrJJG7rmaTw== | ||||
|   dependencies: | ||||
|     core-js "^2.6.5" | ||||
|     regenerator-runtime "^0.13.2" | ||||
|  | ||||
| "@babel/polyfill@^7.8.7": | ||||
|   version "7.8.7" | ||||
|   resolved "https://registry.yarnpkg.com/@babel/polyfill/-/polyfill-7.8.7.tgz#151ec24c7135481336168c3bd8b8bf0cf91c032f" | ||||
| @@ -2149,11 +2141,6 @@ | ||||
|   dependencies: | ||||
|     "@types/babel-types" "*" | ||||
|  | ||||
| "@types/bluebird@^3.5.27": | ||||
|   version "3.5.27" | ||||
|   resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.27.tgz#61eb4d75dc6bfbce51cf49ee9bbebe941b2cb5d0" | ||||
|   integrity sha512-6BmYWSBea18+tSjjSC3QIyV93ZKAeNWGM7R6aYt1ryTZXrlHF+QLV0G2yV0viEGVyRkyQsWfMoJ0k/YghBX5sQ== | ||||
|  | ||||
| "@types/body-parser@*": | ||||
|   version "1.17.1" | ||||
|   resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.17.1.tgz#18fcf61768fb5c30ccc508c21d6fd2e8b3bf7897" | ||||
| @@ -4233,14 +4220,6 @@ browserslist@^4.9.1: | ||||
|     electron-to-chromium "^1.3.363" | ||||
|     node-releases "^1.1.50" | ||||
|  | ||||
| brute-knex@4.0.0: | ||||
|   version "4.0.0" | ||||
|   resolved "https://registry.yarnpkg.com/brute-knex/-/brute-knex-4.0.0.tgz#bb23549017565983e5ed7858214d8d15b690c3bb" | ||||
|   integrity sha512-mMpMvCJjWasupvbcYVPIb6QSWT67U8zKzp+nG6NRsQUeXJHE2fS76EG5r+NSzoQO7xKZ7kONnNkylnivZ9ASmA== | ||||
|   dependencies: | ||||
|     express-brute "^1.0.1" | ||||
|     knex "^0.17" | ||||
|  | ||||
| bser@^2.0.0: | ||||
|   version "2.1.0" | ||||
|   resolved "https://registry.yarnpkg.com/bser/-/bser-2.1.0.tgz#65fc784bf7f87c009b973c12db6546902fa9c7b5" | ||||
| @@ -4951,11 +4930,6 @@ color@^3.0.0: | ||||
|     color-convert "^1.9.1" | ||||
|     color-string "^1.5.2" | ||||
|  | ||||
| colorette@1.0.8: | ||||
|   version "1.0.8" | ||||
|   resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.0.8.tgz#421ff11c80b7414027ebed922396bc1833d1903c" | ||||
|   integrity sha512-X6Ck90ReaF+EfKdVGB7vdIQ3dr651BbIrBwY5YBKg13fjH+940sTtp7/Pkx33C6ntYfQcRumOs/aUQhaRPpbTQ== | ||||
|  | ||||
| colorette@1.1.0: | ||||
|   version "1.1.0" | ||||
|   resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.1.0.tgz#1f943e5a357fac10b4e0f5aaef3b14cdc1af6ec7" | ||||
| @@ -6974,7 +6948,7 @@ expect@^25.3.0: | ||||
|     jest-message-util "^25.3.0" | ||||
|     jest-regex-util "^25.2.6" | ||||
|  | ||||
| express-brute@1.0.1, express-brute@^1.0.1: | ||||
| express-brute@1.0.1: | ||||
|   version "1.0.1" | ||||
|   resolved "https://registry.yarnpkg.com/express-brute/-/express-brute-1.0.1.tgz#9f36d107fe34e40a682593e39bffcc53102b5335" | ||||
|   integrity sha1-nzbRB/405ApoJZPjm//MUxArUzU= | ||||
| @@ -7564,11 +7538,6 @@ get-value@^2.0.3, get-value@^2.0.6: | ||||
|   resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28" | ||||
|   integrity sha1-3BXKHGcjh8p2vTesCjlbogQqLCg= | ||||
|  | ||||
| getopts@2.2.4: | ||||
|   version "2.2.4" | ||||
|   resolved "https://registry.yarnpkg.com/getopts/-/getopts-2.2.4.tgz#3137fe8a5fddf304904059a851bdc1c22f0f54fb" | ||||
|   integrity sha512-Rz7DGyomZjrenu9Jx4qmzdlvJgvrEFHXHvjK0FcZtcTC1U5FmES7OdZHUwMuSnEE6QvBvwse1JODKj7TgbSEjQ== | ||||
|  | ||||
| getopts@2.2.5: | ||||
|   version "2.2.5" | ||||
|   resolved "https://registry.yarnpkg.com/getopts/-/getopts-2.2.5.tgz#67a0fe471cacb9c687d817cab6450b96dde8313b" | ||||
| @@ -8356,7 +8325,7 @@ inquirer@^7.0.0: | ||||
|     strip-ansi "^5.1.0" | ||||
|     through "^2.3.6" | ||||
|  | ||||
| interpret@1.2.0, interpret@^1.2.0: | ||||
| interpret@1.2.0: | ||||
|   version "1.2.0" | ||||
|   resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.2.0.tgz#d5061a6224be58e8083985f5014d844359576296" | ||||
|   integrity sha512-mT34yGKMNceBQUoVn7iCDKDntA7SC6gycMAWzGx1z/CMCTV7b2AAtXlo3nRyHZ1FelRkQbQjprHSYGwzLtkVbw== | ||||
| @@ -9544,29 +9513,6 @@ knex@0.20.15: | ||||
|     uuid "^7.0.1" | ||||
|     v8flags "^3.1.3" | ||||
|  | ||||
| knex@^0.17: | ||||
|   version "0.17.6" | ||||
|   resolved "https://registry.yarnpkg.com/knex/-/knex-0.17.6.tgz#80220cf159cd52768d5b29118c70b18aaf5138fe" | ||||
|   integrity sha512-4SKp8jaBxqlEoaveenmpfnHEv5Kzo6/vhIj8UhW1srGw/FKqARTr+7Fv8C1C1qeVHDjv0coQWuUzN5eermHUsw== | ||||
|   dependencies: | ||||
|     "@babel/polyfill" "^7.4.4" | ||||
|     "@types/bluebird" "^3.5.27" | ||||
|     bluebird "^3.5.5" | ||||
|     colorette "1.0.8" | ||||
|     commander "^2.20.0" | ||||
|     debug "4.1.1" | ||||
|     getopts "2.2.4" | ||||
|     inherits "~2.0.3" | ||||
|     interpret "^1.2.0" | ||||
|     liftoff "3.1.0" | ||||
|     lodash "^4.17.11" | ||||
|     mkdirp "^0.5.1" | ||||
|     pg-connection-string "2.0.0" | ||||
|     tarn "^1.1.5" | ||||
|     tildify "1.2.0" | ||||
|     uuid "^3.3.2" | ||||
|     v8flags "^3.1.3" | ||||
|  | ||||
| kuler@1.0.x: | ||||
|   version "1.0.1" | ||||
|   resolved "https://registry.yarnpkg.com/kuler/-/kuler-1.0.1.tgz#ef7c784f36c9fb6e16dd3150d152677b2b0228a6" | ||||
| @@ -11712,11 +11658,6 @@ pg-connection-string@0.1.3: | ||||
|   resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-0.1.3.tgz#da1847b20940e42ee1492beaf65d49d91b245df7" | ||||
|   integrity sha1-2hhHsglA5C7hSSvq9l1J2RskXfc= | ||||
|  | ||||
| pg-connection-string@2.0.0: | ||||
|   version "2.0.0" | ||||
|   resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.0.0.tgz#3eefe5997e06d94821e4d502e42b6a1c73f8df82" | ||||
|   integrity sha1-Pu/lmX4G2Ugh5NUC5CtqHHP434I= | ||||
|  | ||||
| pg-connection-string@2.1.0: | ||||
|   version "2.1.0" | ||||
|   resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.1.0.tgz#e07258f280476540b24818ebb5dca29e101ca502" | ||||
| @@ -15225,13 +15166,6 @@ through@2, through@^2.3.6: | ||||
|   resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" | ||||
|   integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU= | ||||
|  | ||||
| tildify@1.2.0: | ||||
|   version "1.2.0" | ||||
|   resolved "https://registry.yarnpkg.com/tildify/-/tildify-1.2.0.tgz#dcec03f55dca9b7aa3e5b04f21817eb56e63588a" | ||||
|   integrity sha1-3OwD9V3Km3qj5bBPIYF+tW5jWIo= | ||||
|   dependencies: | ||||
|     os-homedir "^1.0.0" | ||||
|  | ||||
| tildify@2.0.0: | ||||
|   version "2.0.0" | ||||
|   resolved "https://registry.yarnpkg.com/tildify/-/tildify-2.0.0.tgz#f205f3674d677ce698b7067a99e949ce03b4754a" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user