2018-12-16 01:40:01 +00:00
|
|
|
# node libraries
|
2019-02-14 16:47:47 +00:00
|
|
|
redis = (require 'redis')
|
|
|
|
crypto = (require 'crypto')
|
|
|
|
logger = (require 'logging').default 'session'
|
|
|
|
|
|
|
|
# expiry times in seconds
|
|
|
|
EXPIRY =
|
2019-02-16 18:03:21 +00:00
|
|
|
# games expire 1 week after creation
|
2019-02-15 02:50:11 +00:00
|
|
|
game: 1 * 60 * 60 * 24 * 7
|
2019-02-14 16:47:47 +00:00
|
|
|
# logins expire 1 month after last action
|
2019-02-15 02:50:11 +00:00
|
|
|
login: 1 * 60 * 60 * 24 * 7 * 4
|
2019-02-14 16:47:47 +00:00
|
|
|
|
|
|
|
|
2019-02-16 18:15:49 +00:00
|
|
|
class FFTCGSESSION
|
|
|
|
constructor: ->
|
|
|
|
@db = redis.createClient
|
|
|
|
host: 'redis'
|
|
|
|
port: 6379
|
|
|
|
|
2019-02-19 20:36:14 +00:00
|
|
|
@db.on 'error', (err) =>
|
2019-02-16 18:15:49 +00:00
|
|
|
logger.error err.message
|
|
|
|
|
|
|
|
start: (data) ->
|
|
|
|
new Promise (resolve) =>
|
|
|
|
# hash data
|
|
|
|
hmac = crypto.createHmac 'sha256', Math.random().toString()
|
|
|
|
hmac.update (JSON.stringify data)
|
|
|
|
digest = hmac.digest 'base64'
|
|
|
|
logger.debug 'digest', digest
|
|
|
|
|
|
|
|
# push (hash, data) into DB for the configured timespan
|
|
|
|
@db.setex digest, EXPIRY.login, (JSON.stringify data), (err) ->
|
2019-02-19 18:57:22 +00:00
|
|
|
logger.info "OK '#{digest}' created"
|
2019-02-16 18:15:49 +00:00
|
|
|
resolve digest
|
|
|
|
|
|
|
|
check: (digest) ->
|
|
|
|
new Promise (resolve, reject) =>
|
|
|
|
# refresh expiry timer on digest
|
|
|
|
@db.expire digest, EXPIRY.login, (err, res) =>
|
|
|
|
if res == 0
|
|
|
|
reject null
|
|
|
|
|
|
|
|
else
|
|
|
|
@db.get digest, (err, res) ->
|
2019-02-19 18:57:22 +00:00
|
|
|
logger.info "OK '#{digest}' resumed"
|
2019-02-16 18:15:49 +00:00
|
|
|
resolve JSON.parse res
|
2019-02-14 16:47:47 +00:00
|
|
|
|
2019-02-19 18:57:22 +00:00
|
|
|
module.exports = new FFTCGSESSION
|