neat logging
This commit is contained in:
parent
bbe46961b5
commit
9f177f325e
5 changed files with 50 additions and 33 deletions
11
inc/console.coffee
Normal file
11
inc/console.coffee
Normal 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
|
|
@ -1,6 +1,7 @@
|
||||||
# libraries
|
# libraries
|
||||||
bcrypt = (require 'bcrypt')
|
bcrypt = (require 'bcrypt')
|
||||||
sqlite3 = (require 'sqlite3').verbose()
|
sqlite3 = (require 'sqlite3').verbose()
|
||||||
|
FFTCGLOG = new (require './console')('FFTCGDB')
|
||||||
|
|
||||||
# bruteforce countermeasure
|
# bruteforce countermeasure
|
||||||
saltRounds = 13
|
saltRounds = 13
|
||||||
|
@ -11,17 +12,17 @@ FFTCGDB = (filename, truncate) ->
|
||||||
|
|
||||||
@db = new sqlite3.Database @filename, (err) ->
|
@db = new sqlite3.Database @filename, (err) ->
|
||||||
if err
|
if err
|
||||||
console.error err.message
|
FFTCGLOG.error err.message
|
||||||
|
|
||||||
else
|
else
|
||||||
console.log "[FFTCGDB] Connected to '#{that.filename}'"
|
FFTCGLOG.log "Connected to '#{that.filename}'"
|
||||||
|
|
||||||
that.db.run 'PRAGMA foreign_keys = ON;', (err) ->
|
that.db.run 'PRAGMA foreign_keys = ON;', (err) ->
|
||||||
console.error err.message if err
|
FFTCGLOG.error err.message if err
|
||||||
|
|
||||||
if truncate == true
|
if truncate == true
|
||||||
that.db.run 'DROP TABLE IF EXISTS users;', (err) ->
|
that.db.run 'DROP TABLE IF EXISTS users;', (err) ->
|
||||||
console.error err.message if err
|
FFTCGLOG.error err.message if err
|
||||||
that.db.run '''
|
that.db.run '''
|
||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
user integer PRIMARY KEY,
|
user integer PRIMARY KEY,
|
||||||
|
@ -30,10 +31,10 @@ FFTCGDB = (filename, truncate) ->
|
||||||
UNIQUE(login)
|
UNIQUE(login)
|
||||||
);
|
);
|
||||||
''', (err) ->
|
''', (err) ->
|
||||||
console.error err.message if err
|
FFTCGLOG.error err.message if err
|
||||||
|
|
||||||
that.db.run 'DROP TABLE IF EXISTS decks;', (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 '''
|
that.db.run '''
|
||||||
CREATE TABLE decks (
|
CREATE TABLE decks (
|
||||||
deck integer PRIMARY KEY,
|
deck integer PRIMARY KEY,
|
||||||
|
@ -42,10 +43,10 @@ FFTCGDB = (filename, truncate) ->
|
||||||
ON DELETE CASCADE
|
ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
''', (err) ->
|
''', (err) ->
|
||||||
console.error err.message if err
|
FFTCGLOG.error err.message if err
|
||||||
|
|
||||||
that.db.run 'DROP TABLE IF EXISTS decks_cards;', (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 '''
|
that.db.run '''
|
||||||
CREATE TABLE decks_cards (
|
CREATE TABLE decks_cards (
|
||||||
deck integer NOT NULL,
|
deck integer NOT NULL,
|
||||||
|
@ -56,9 +57,9 @@ FFTCGDB = (filename, truncate) ->
|
||||||
ON DELETE CASCADE
|
ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
''', (err) ->
|
''', (err) ->
|
||||||
console.error err.message if err
|
FFTCGLOG.error err.message if err
|
||||||
|
|
||||||
console.log "[FFTCGDB] recreated DB"
|
FFTCGLOG.log 'recreated DB'
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -66,9 +67,11 @@ FFTCGDB::close = ->
|
||||||
new Promise (resolve, reject) ->
|
new Promise (resolve, reject) ->
|
||||||
@db.close (err) ->
|
@db.close (err) ->
|
||||||
if err
|
if err
|
||||||
resolve "[FFTCGDB] Error closing: '#{err.message}'"
|
FFTCGLOG.log "Error closing: '#{err.message}'"
|
||||||
|
resolve 'ok'
|
||||||
else
|
else
|
||||||
reject "[FFTCGDB] Closed '#{@filename}'"
|
FFTCGLOG.error "Closed '#{@filename}'"
|
||||||
|
reject 'db'
|
||||||
|
|
||||||
FFTCGDB::register = (login, password) ->
|
FFTCGDB::register = (login, password) ->
|
||||||
that = @
|
that = @
|
||||||
|
@ -77,26 +80,26 @@ FFTCGDB::register = (login, password) ->
|
||||||
# validate user input
|
# validate user input
|
||||||
if login == '' or password == ''
|
if login == '' or password == ''
|
||||||
# no user name or password given
|
# 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'
|
reject 'invalid'
|
||||||
|
|
||||||
# hash password
|
# hash password
|
||||||
bcrypt.hash password, saltRounds, (err, hash) ->
|
bcrypt.hash password, saltRounds, (err, hash) ->
|
||||||
if err
|
if err
|
||||||
console.log "[FFTCGDB] reg: hash fail for name '#{login}'"
|
FFTCGLOG.log "reg: hash fail for name '#{login}'"
|
||||||
reject 'hash'
|
reject 'hash'
|
||||||
|
|
||||||
# try creating row in users table
|
# try creating row in users table
|
||||||
stmt = that.db.prepare 'INSERT INTO users (login, pwdhash) VALUES (?, ?)'
|
stmt = that.db.prepare 'INSERT INTO users (login, pwdhash) VALUES (?, ?)'
|
||||||
stmt.run [login, hash], (err) ->
|
stmt.run [login, hash], (err) ->
|
||||||
if 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()
|
stmt.finalize()
|
||||||
# reduce attack surface, don't disclose user names
|
# reduce attack surface, don't disclose user names
|
||||||
reject 'db' # user already exists
|
reject 'db' # user already exists
|
||||||
|
|
||||||
else
|
else
|
||||||
console.log "[FFTCGDB] reg: OK '#{login}'"
|
FFTCGLOG.log "reg: OK '#{login}'"
|
||||||
stmt.finalize()
|
stmt.finalize()
|
||||||
# registration successful
|
# registration successful
|
||||||
resolve
|
resolve
|
||||||
|
@ -111,14 +114,14 @@ FFTCGDB::login = (login, password) ->
|
||||||
stmt = that.db.prepare 'SELECT user, login, pwdhash FROM users WHERE login = ?'
|
stmt = that.db.prepare 'SELECT user, login, pwdhash FROM users WHERE login = ?'
|
||||||
stmt.get [login], (err, row) ->
|
stmt.get [login], (err, row) ->
|
||||||
if err
|
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()
|
stmt.finalize()
|
||||||
reject 'db'
|
reject 'db'
|
||||||
|
|
||||||
else if not row
|
else if not row
|
||||||
# hash the password for timing attack reasons
|
# hash the password for timing attack reasons
|
||||||
bcrypt.hash password, saltRounds, (err, hash) ->
|
bcrypt.hash password, saltRounds, (err, hash) ->
|
||||||
console.log "[FFTCGDB] login: nonexistent '#{login}'"
|
FFTCGLOG.log "login: nonexistent '#{login}'"
|
||||||
stmt.finalize()
|
stmt.finalize()
|
||||||
# reduce attack surface, don't disclose user names
|
# reduce attack surface, don't disclose user names
|
||||||
reject 'login' # user doesnt exist
|
reject 'login' # user doesnt exist
|
||||||
|
@ -126,11 +129,11 @@ FFTCGDB::login = (login, password) ->
|
||||||
else
|
else
|
||||||
bcrypt.compare password, row.pwdhash, (err, res) ->
|
bcrypt.compare password, row.pwdhash, (err, res) ->
|
||||||
if err
|
if err
|
||||||
console.log "[FFTCGDB] login: hash fail for name '#{login}'"
|
FFTCGLOG.log "login: hash fail for name '#{login}'"
|
||||||
reject 'hash'
|
reject 'hash'
|
||||||
|
|
||||||
if res == true
|
if res == true
|
||||||
console.log "[FFTCGDB] login: OK '#{row.login}'"
|
FFTCGLOG.log "login: OK '#{row.login}'"
|
||||||
stmt.finalize()
|
stmt.finalize()
|
||||||
# login successful
|
# login successful
|
||||||
resolve
|
resolve
|
||||||
|
@ -138,7 +141,7 @@ FFTCGDB::login = (login, password) ->
|
||||||
login: row.login
|
login: row.login
|
||||||
|
|
||||||
else
|
else
|
||||||
console.log "[FFTCGDB] login: wrong password for '#{login}'"
|
FFTCGLOG.log "login: wrong password for '#{login}'"
|
||||||
stmt.finalize()
|
stmt.finalize()
|
||||||
# login failed
|
# login failed
|
||||||
reject 'login'
|
reject 'login'
|
||||||
|
@ -151,7 +154,7 @@ FFTCGDB::addDeck = (user, deckCards) ->
|
||||||
stmt = that.db.prepare 'INSERT INTO decks (user) VALUES (?)'
|
stmt = that.db.prepare 'INSERT INTO decks (user) VALUES (?)'
|
||||||
stmt.run [user], (err) ->
|
stmt.run [user], (err) ->
|
||||||
if 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()
|
stmt.finalize()
|
||||||
reject 'db'
|
reject 'db'
|
||||||
|
|
||||||
|
@ -167,14 +170,14 @@ FFTCGDB::addDeck = (user, deckCards) ->
|
||||||
deckCards.forEach (card) ->
|
deckCards.forEach (card) ->
|
||||||
stmt.run [deckID, card.id, card.quant], (err) ->
|
stmt.run [deckID, card.id, card.quant], (err) ->
|
||||||
if 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()
|
stmt.finalize()
|
||||||
reject 'db'
|
reject 'db'
|
||||||
else
|
else
|
||||||
# check if all queries are done
|
# check if all queries are done
|
||||||
promiseCount -= 1
|
promiseCount -= 1
|
||||||
if promiseCount == 0
|
if promiseCount == 0
|
||||||
console.log "[FFTCGDB] addDeck: OK '#{user}'"
|
FFTCGLOG.log "addDeck: OK '#{user}'"
|
||||||
stmt.finalize()
|
stmt.finalize()
|
||||||
resolve deckID
|
resolve deckID
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ path = (require 'path')
|
||||||
|
|
||||||
# my libraries
|
# my libraries
|
||||||
FFTCGDB = (require './db')
|
FFTCGDB = (require './db')
|
||||||
|
FFTCGLOG = new (require './console')('FFTCGROUTER')
|
||||||
|
|
||||||
# open fftcg db
|
# open fftcg db
|
||||||
fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db')
|
fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db')
|
||||||
|
@ -14,9 +15,9 @@ FFTCGROUTER = express.Router()
|
||||||
# request logging
|
# request logging
|
||||||
FFTCGROUTER.use (req, res, next) ->
|
FFTCGROUTER.use (req, res, next) ->
|
||||||
if req.session.user
|
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
|
else
|
||||||
console.log "[FFTCGROUTER] requested '#{req.url}'"
|
FFTCGLOG.log "requested '#{req.url}'"
|
||||||
|
|
||||||
next()
|
next()
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# node libraries
|
# node libraries
|
||||||
socketio = (require 'socket.io')
|
socketio = (require 'socket.io')
|
||||||
path = (require 'path')
|
path = (require 'path')
|
||||||
|
FFTCGLOG = new (require './console')('FFTCGSOCKET')
|
||||||
|
|
||||||
# my libraries
|
# my libraries
|
||||||
|
|
||||||
|
@ -14,17 +15,17 @@ FFTCGSOCKET = (http, session) ->
|
||||||
# on new connection
|
# on new connection
|
||||||
@io.on 'connection', (socket) ->
|
@io.on 'connection', (socket) ->
|
||||||
@session = socket.handshake.session
|
@session = socket.handshake.session
|
||||||
console.log "session '#{@session.id}' connected"
|
FFTCGLOG.log "session '#{@session.id}' connected"
|
||||||
console.log "is user '#{@session.userID}'" if @session.userID
|
FFTCGLOG.log "is user '#{@session.userID}'" if @session.userID
|
||||||
|
|
||||||
socket.on 'disconnect', ->
|
socket.on 'disconnect', ->
|
||||||
console.log "session '#{that.session.id}' disconnected"
|
FFTCGLOG.log "session '#{that.session.id}' disconnected"
|
||||||
console.log "is user '#{that.session.userID}'" if that.session.userID
|
FFTCGLOG.log "is user '#{that.session.userID}'" if that.session.userID
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
FFTCGSOCKET::close = ->
|
FFTCGSOCKET::close = ->
|
||||||
console.log '[FFTCGSOCKET] shutting down'
|
FFTCGLOG.log 'shutting down'
|
||||||
if @db
|
if @db
|
||||||
@db.close()
|
@db.close()
|
||||||
.then (msg) ->
|
.then (msg) ->
|
||||||
|
|
|
@ -10,6 +10,7 @@ path = (require 'path')
|
||||||
FFTCGSOCKET = (require './inc/socket')
|
FFTCGSOCKET = (require './inc/socket')
|
||||||
FFTCGSESSION = (require './inc/session')
|
FFTCGSESSION = (require './inc/session')
|
||||||
FFTCGROUTER = (require './inc/router')
|
FFTCGROUTER = (require './inc/router')
|
||||||
|
FFTCGLOG = new (require './inc/console')('FFTCG')
|
||||||
|
|
||||||
# express framework
|
# express framework
|
||||||
app = express()
|
app = express()
|
||||||
|
@ -30,10 +31,10 @@ socket = new FFTCGSOCKET web, sharedSession sessionMiddleware
|
||||||
|
|
||||||
# Create server
|
# Create server
|
||||||
web.listen 3000, ->
|
web.listen 3000, ->
|
||||||
console.log '[FFTCG] Listening on port 3000 ...'
|
FFTCGLOG.log 'Listening on port 3000 ...'
|
||||||
|
|
||||||
# Handle termination
|
# Handle termination
|
||||||
process.on 'SIGINT', ->
|
process.on 'SIGINT', ->
|
||||||
socket.close()
|
socket.close()
|
||||||
console.log '[FFTCG] shutting down after SIGINT'
|
FFTCGLOG.log 'shutting down after SIGINT'
|
||||||
process.exit()
|
process.exit()
|
||||||
|
|
Reference in a new issue