neat logging

This commit is contained in:
Jörn-Michael Miehe 2018-12-27 13:03:58 +01:00
parent bbe46961b5
commit 9f177f325e
5 changed files with 50 additions and 33 deletions

11
inc/console.coffee Normal file
View file

@ -0,0 +1,11 @@
FFTCGLOG = (unit) ->
@unit = unit
return
FFTCGLOG::log = (msg) ->
console.log "[#{@unit}] #{msg}"
FFTCGLOG::error = (msg) ->
console.error "[#{@unit}] #{msg}"
module.exports = FFTCGLOG

View file

@ -1,6 +1,7 @@
# libraries
bcrypt = (require 'bcrypt')
sqlite3 = (require 'sqlite3').verbose()
FFTCGLOG = new (require './console')('FFTCGDB')
# bruteforce countermeasure
saltRounds = 13
@ -11,17 +12,17 @@ FFTCGDB = (filename, truncate) ->
@db = new sqlite3.Database @filename, (err) ->
if err
console.error err.message
FFTCGLOG.error err.message
else
console.log "[FFTCGDB] Connected to '#{that.filename}'"
FFTCGLOG.log "Connected to '#{that.filename}'"
that.db.run 'PRAGMA foreign_keys = ON;', (err) ->
console.error err.message if err
FFTCGLOG.error err.message if err
if truncate == true
that.db.run 'DROP TABLE IF EXISTS users;', (err) ->
console.error err.message if err
FFTCGLOG.error err.message if err
that.db.run '''
CREATE TABLE users (
user integer PRIMARY KEY,
@ -30,10 +31,10 @@ FFTCGDB = (filename, truncate) ->
UNIQUE(login)
);
''', (err) ->
console.error err.message if err
FFTCGLOG.error err.message if err
that.db.run 'DROP TABLE IF EXISTS decks;', (err) ->
console.error err.message if err
FFTCGLOG.error err.message if err
that.db.run '''
CREATE TABLE decks (
deck integer PRIMARY KEY,
@ -42,10 +43,10 @@ FFTCGDB = (filename, truncate) ->
ON DELETE CASCADE
);
''', (err) ->
console.error err.message if err
FFTCGLOG.error err.message if err
that.db.run 'DROP TABLE IF EXISTS decks_cards;', (err) ->
console.error err.message if err
FFTCGLOG.error err.message if err
that.db.run '''
CREATE TABLE decks_cards (
deck integer NOT NULL,
@ -56,9 +57,9 @@ FFTCGDB = (filename, truncate) ->
ON DELETE CASCADE
);
''', (err) ->
console.error err.message if err
FFTCGLOG.error err.message if err
console.log "[FFTCGDB] recreated DB"
FFTCGLOG.log 'recreated DB'
return
@ -66,9 +67,11 @@ FFTCGDB::close = ->
new Promise (resolve, reject) ->
@db.close (err) ->
if err
resolve "[FFTCGDB] Error closing: '#{err.message}'"
FFTCGLOG.log "Error closing: '#{err.message}'"
resolve 'ok'
else
reject "[FFTCGDB] Closed '#{@filename}'"
FFTCGLOG.error "Closed '#{@filename}'"
reject 'db'
FFTCGDB::register = (login, password) ->
that = @
@ -77,26 +80,26 @@ FFTCGDB::register = (login, password) ->
# validate user input
if login == '' or password == ''
# no user name or password given
console.log "[FFTCGDB] reg: user name '#{login}' or password empty"
FFTCGLOG.log "reg: user name '#{login}' or password empty"
reject 'invalid'
# hash password
bcrypt.hash password, saltRounds, (err, hash) ->
if err
console.log "[FFTCGDB] reg: hash fail for name '#{login}'"
FFTCGLOG.log "reg: hash fail for name '#{login}'"
reject 'hash'
# try creating row in users table
stmt = that.db.prepare 'INSERT INTO users (login, pwdhash) VALUES (?, ?)'
stmt.run [login, hash], (err) ->
if err
console.log "[FFTCGDB] reg: DB fail '#{err.code}' for name '#{login}'"
FFTCGLOG.log "reg: DB fail '#{err.code}' for name '#{login}'"
stmt.finalize()
# reduce attack surface, don't disclose user names
reject 'db' # user already exists
else
console.log "[FFTCGDB] reg: OK '#{login}'"
FFTCGLOG.log "reg: OK '#{login}'"
stmt.finalize()
# registration successful
resolve
@ -111,14 +114,14 @@ FFTCGDB::login = (login, password) ->
stmt = that.db.prepare 'SELECT user, login, pwdhash FROM users WHERE login = ?'
stmt.get [login], (err, row) ->
if err
console.log "[FFTCGDB] login: DB fail '#{err.code}' for name '#{login}'"
FFTCGLOG.log "login: DB fail '#{err.code}' for name '#{login}'"
stmt.finalize()
reject 'db'
else if not row
# hash the password for timing attack reasons
bcrypt.hash password, saltRounds, (err, hash) ->
console.log "[FFTCGDB] login: nonexistent '#{login}'"
FFTCGLOG.log "login: nonexistent '#{login}'"
stmt.finalize()
# reduce attack surface, don't disclose user names
reject 'login' # user doesnt exist
@ -126,11 +129,11 @@ FFTCGDB::login = (login, password) ->
else
bcrypt.compare password, row.pwdhash, (err, res) ->
if err
console.log "[FFTCGDB] login: hash fail for name '#{login}'"
FFTCGLOG.log "login: hash fail for name '#{login}'"
reject 'hash'
if res == true
console.log "[FFTCGDB] login: OK '#{row.login}'"
FFTCGLOG.log "login: OK '#{row.login}'"
stmt.finalize()
# login successful
resolve
@ -138,7 +141,7 @@ FFTCGDB::login = (login, password) ->
login: row.login
else
console.log "[FFTCGDB] login: wrong password for '#{login}'"
FFTCGLOG.log "login: wrong password for '#{login}'"
stmt.finalize()
# login failed
reject 'login'
@ -151,7 +154,7 @@ FFTCGDB::addDeck = (user, deckCards) ->
stmt = that.db.prepare 'INSERT INTO decks (user) VALUES (?)'
stmt.run [user], (err) ->
if err
console.log "[FFTCGDB] addDeck: DB fail '#{err.code}' for id '#{user}'"
FFTCGLOG.log "addDeck: DB fail '#{err.code}' for id '#{user}'"
stmt.finalize()
reject 'db'
@ -167,14 +170,14 @@ FFTCGDB::addDeck = (user, deckCards) ->
deckCards.forEach (card) ->
stmt.run [deckID, card.id, card.quant], (err) ->
if err
console.log "[FFTCGDB] addDeck: DB fail '#{err.code}' for card '#{deckID}', '#{card.id}', '#{card.quant}'"
FFTCGLOG.log "addDeck: DB fail '#{err.code}' for card '#{deckID}', '#{card.id}', '#{card.quant}'"
stmt.finalize()
reject 'db'
else
# check if all queries are done
promiseCount -= 1
if promiseCount == 0
console.log "[FFTCGDB] addDeck: OK '#{user}'"
FFTCGLOG.log "addDeck: OK '#{user}'"
stmt.finalize()
resolve deckID

View file

@ -4,6 +4,7 @@ path = (require 'path')
# my libraries
FFTCGDB = (require './db')
FFTCGLOG = new (require './console')('FFTCGROUTER')
# open fftcg db
fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db')
@ -14,9 +15,9 @@ FFTCGROUTER = express.Router()
# request logging
FFTCGROUTER.use (req, res, next) ->
if req.session.user
console.log "[FFTCGROUTER] user '#{req.session.user.login}' requested '#{req.url}'"
FFTCGLOG.log "user '#{req.session.user.login}' requested '#{req.url}'"
else
console.log "[FFTCGROUTER] requested '#{req.url}'"
FFTCGLOG.log "requested '#{req.url}'"
next()

View file

@ -1,6 +1,7 @@
# node libraries
socketio = (require 'socket.io')
path = (require 'path')
FFTCGLOG = new (require './console')('FFTCGSOCKET')
# my libraries
@ -14,17 +15,17 @@ FFTCGSOCKET = (http, session) ->
# on new connection
@io.on 'connection', (socket) ->
@session = socket.handshake.session
console.log "session '#{@session.id}' connected"
console.log "is user '#{@session.userID}'" if @session.userID
FFTCGLOG.log "session '#{@session.id}' connected"
FFTCGLOG.log "is user '#{@session.userID}'" if @session.userID
socket.on 'disconnect', ->
console.log "session '#{that.session.id}' disconnected"
console.log "is user '#{that.session.userID}'" if that.session.userID
FFTCGLOG.log "session '#{that.session.id}' disconnected"
FFTCGLOG.log "is user '#{that.session.userID}'" if that.session.userID
return
FFTCGSOCKET::close = ->
console.log '[FFTCGSOCKET] shutting down'
FFTCGLOG.log 'shutting down'
if @db
@db.close()
.then (msg) ->

View file

@ -10,6 +10,7 @@ path = (require 'path')
FFTCGSOCKET = (require './inc/socket')
FFTCGSESSION = (require './inc/session')
FFTCGROUTER = (require './inc/router')
FFTCGLOG = new (require './inc/console')('FFTCG')
# express framework
app = express()
@ -30,10 +31,10 @@ socket = new FFTCGSOCKET web, sharedSession sessionMiddleware
# Create server
web.listen 3000, ->
console.log '[FFTCG] Listening on port 3000 ...'
FFTCGLOG.log 'Listening on port 3000 ...'
# Handle termination
process.on 'SIGINT', ->
socket.close()
console.log '[FFTCG] shutting down after SIGINT'
FFTCGLOG.log 'shutting down after SIGINT'
process.exit()