diff --git a/routes/pow-router.ts b/routes/pow-router.ts index 5f3d046c..9b032ef6 100644 --- a/routes/pow-router.ts +++ b/routes/pow-router.ts @@ -1,5 +1,7 @@ import Koa from 'koa' import Router from 'koa-router' +import moment from 'moment' +import NoSql from '../service/util/nosql' import Pow from '../service/pow-service' import config from '../service/util/config-parser' @@ -9,9 +11,15 @@ const pow = new Pow() router.prefix('/pow') +const nosql: NoSql = NoSql.getInstance() + router.get('/', async (ctx: Koa.ParameterizedContext) => { const { prefix } = pow.getProblem() - await Object.assign(ctx.session, { difficulty: config.difficulty, prefix }) + await Object.assign(ctx.session, { + difficulty: config.difficulty, + prefix, + assigned: moment().toISOString(), + }) await ctx.render('pow', { difficulty: config.difficulty, prefix, @@ -24,6 +32,11 @@ router.post('/', async (ctx: Koa.ParameterizedContext) => { return } if (await pow.parseAndVerify(ctx.request.body, ctx.session)) { + await nosql.incrBy( + `stats:ttl_solve_time`, + moment().diff(moment(ctx.session.assigned), 'milliseconds') + ) + await nosql.incr(`stats:prob_solved`) ctx.session.authorized = true ctx.status = 200 } else { diff --git a/service/controllers/blacklist.ts b/service/controllers/blacklist.ts index ec4a5761..329ca0d5 100644 --- a/service/controllers/blacklist.ts +++ b/service/controllers/blacklist.ts @@ -23,7 +23,7 @@ class Blacklist { await this.nosql.setNX(`ban:${ip}`, '1', true, minutes * 60) if (config.socket) { Client.getInstance().send( - JSON.stringify({ method: 'ban', arguments: [ip, minutes * 60] }) + JSON.stringify({ method: 'phlx_ban_ip', arguments: [ip, minutes * 60] }) ) } } diff --git a/service/util/nosql.ts b/service/util/nosql.ts index c32ab2fd..aa19950e 100644 --- a/service/util/nosql.ts +++ b/service/util/nosql.ts @@ -48,6 +48,9 @@ class NoSql { public incr = async (key: string) => { await this.dbInstance.incr(key) } + public incrBy = async (key: string, value:number) => { + await this.dbInstance.incrby(key, value) + } public del = async (key: string) => { await this.dbInstance.del(key) } diff --git a/service/util/socket.ts b/service/util/socket.ts index c4831eed..b538c43d 100644 --- a/service/util/socket.ts +++ b/service/util/socket.ts @@ -41,29 +41,31 @@ export default class Client { const obj = JSON.parse(message) if (obj.method) { switch (obj.method) { - case 'set_config': + case 'shld_set_config': config[obj.arguments[0]] = obj.arguments[1] break - case 'fetch_stats': + case 'shld_fetch_stats': this.send( JSON.stringify({ - method: 'update_stats', + method: 'phlx_update_stats', arguments: [ (await this.nosql.get('stats:legit_req')) || '0', //legit_req (await this.nosql.get('stats:ttl_req')) || '0', //ttl_req (await this.nosql.get('stats:bad_nonce')) || '0', //bad_nonce (await this.nosql.get('stats:ttl_waf')) || '0', //ttl_waf + (await this.nosql.get('stats:ttl_solve_time')) || '0', //ttl_solve_time + (await this.nosql.get('stats:prob_solved')) || '0', //prob_solved ], }) ) break - case 'add_whitelist': + case 'shld_add_whitelist': await this.nosql.setNX(`wht:${obj.arguments[0]}`, '1') break - case 'remove_whitelist': + case 'shld_remove_whitelist': await this.nosql.del(`wht:${obj.arguments[0]}`) break - case 'ban': + case 'shld_ban_ip': await this.nosql.setNX( `ban:${obj.arguments[0]}`, '1', @@ -71,7 +73,7 @@ export default class Client { obj.arguments[1] ) break - case 'update_model': + case 'shld_update_model': //TODO break }