Skip to content

Commit

Permalink
Socket method reorganize (#362)
Browse files Browse the repository at this point in the history
* reorganize socket method naming

* add latency stats
  • Loading branch information
RuiSiang authored Jun 22, 2022
1 parent c9d1691 commit dded91a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
15 changes: 14 additions & 1 deletion routes/pow-router.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion service/controllers/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] })
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions service/util/nosql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 9 additions & 7 deletions service/util/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,39 @@ 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',
true,
obj.arguments[1]
)
break
case 'update_model':
case 'shld_update_model':
//TODO
break
}
Expand Down

0 comments on commit dded91a

Please sign in to comment.