passport, log to db

This commit is contained in:
Robert Richter 2019-02-01 21:14:31 +01:00
parent 74c5e5ed8a
commit 828109e2c4
7 changed files with 155 additions and 87 deletions

View File

@ -9,6 +9,7 @@ app_cfg.global = {
defaultuser: 'me',
defaultpass: '123',
saltRounds: 10,
sessionsecret: '0987654321abcdef#xyz',
// TODO: eindeutige ID/Version für Anwendung hinterlegen
// TODO: Karten-URL für Client festlegen
app_id: process.pid

View File

@ -1,6 +1,26 @@
module.exports = function(app_cfg, db, bcrypt, passport, LocalStrategy) {
module.exports = function (app, app_cfg, db, bcrypt, passport, LocalStrategy) {
// setting up user authentication
var session = require('express-session');
var SQLiteStore = require('connect-sqlite3')(session);
var LocalStrategy = require('passport-local').Strategy;
app.use(session({
store: new SQLiteStore({
db: app_cfg.global.database,
concurrentDB: true
}),
secret: app_cfg.global.sessionsecret,
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 60 * 60 * 1000
} // 1 Stunde
}));
app.use(passport.initialize());
app.use(passport.session());
// Benutzerauthentifizierung
passport.use(new LocalStrategy({
usernameField: 'user'
}, function (user, password, done) {
@ -9,7 +29,6 @@ module.exports = function(app_cfg, db, bcrypt, passport, LocalStrategy) {
bcrypt.compare(password, row.password, function (err, res) {
if (!res) return done(null, false);
db.get('SELECT user, id FROM waip_users WHERE user = ?', user, function (err, row) {
//console.log('got user: '+row+' err: '+ err);
return done(null, row);
});
});
@ -29,4 +48,17 @@ module.exports = function(app_cfg, db, bcrypt, passport, LocalStrategy) {
});
});
// Funktion die prueft ob der Benutzer angemeldet ist
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
// req.user is available for use here
return next(); }
// denied. redirect to login
res.redirect('/login')
}
return{
ensureAuthenticated: ensureAuthenticated
};
};

View File

@ -1,4 +1,4 @@
module.exports = function(app, sql, app_cfg, passport) {
module.exports = function (app, sql, app_cfg, passport, auth) {
// get index
app.get('/', function (req, res) {
@ -71,6 +71,17 @@ module.exports = function(app, sql, app_cfg, passport) {
});
});
// get /show_log
app.get('/show_log', auth.ensureAuthenticated, function (req, res) {
sql.db_get_log(function (data) {
res.render('show_log', {
title: 'Log-Datei',
user: req.user,
dataSet: data
});
});
});
// get /login
app.get('/login', function (req, res) {
res.render('login', {

View File

@ -84,6 +84,11 @@ module.exports = function(bcrypt, app_cfg) {
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
einsatzmittel_typ TEXT,
einsatzmittel_rufname TEXT)`);
db.run(`CREATE TABLE waip_log (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
log_time DATETIME DEFAULT CURRENT_TIMESTAMP,
log_typ TEXT,
log_text TEXT)`);
// Default-Wachen speichern
db.run(`INSERT OR REPLACE INTO waip_wachen (
nr_wache, nr_traeger, nr_kreis, name_wache, name_traeger, name_kreis)
@ -793,7 +798,7 @@ module.exports = function(bcrypt, app_cfg) {
bcrypt.hash(app_cfg.global.defaultpass, app_cfg.global.saltRounds, function (err, hash) {
db.run(`INSERT INTO waip_users ( user, password, permissions ) VALUES( ?, ?, 'admin' )`, app_cfg.global.defaultuser, hash, function (err) {
if (err) {
console.log(err);
console.error(err.message);
};
});
});

View File

@ -338,6 +338,24 @@ module.exports = function(db) {
});
};
function db_log(typ, text) {
db.run(`INSERT INTO waip_log (log_typ, log_text)
VALUES (
\'` + typ + `\',
\'` + text + `\')`);
//TODO: Log auf 20.000 Datensätze begrenzen
};
function db_get_log(callback) {
db.all('select * from waip_log order by log_time DESC', function(err, rows) {
if (err == null && rows) {
callback && callback(rows);
} else {
callback && callback(null);
};
});
};
return {
db_einsatz_speichern: db_einsatz_speichern,
db_einsatz_laden: db_einsatz_laden,
@ -358,7 +376,9 @@ module.exports = function(db) {
db_tts_einsatzmittel: db_tts_einsatzmittel,
db_get_socket_by_id: db_get_socket_by_id,
db_update_client_status: db_update_client_status,
db_check_client_waipid: db_check_client_waipid
db_check_client_waipid: db_check_client_waipid,
db_log: db_log,
db_get_log: db_get_log
};
};

View File

@ -1,4 +1,4 @@
module.exports = function(app_cfg, waip_io){
module.exports = function(app_cfg, waip_io, sql){
// Module laden
var dgram = require('dgram');
@ -18,16 +18,16 @@ module.exports = function(app_cfg, waip_io){
udp_server.bind(app_cfg.global.udpport);
udp_server.on('listening', function() {
var address = udp_server.address();
console.log('UDP Server auf ' + address.address + ':' + address.port + ' gestartet.');
sql.db_log('Anwendung', 'UDP Server auf ' + address.address + ':' + address.port + ' gestartet.');
});
// Warten auf Einsatzdaten
udp_server.on('message', function(message, remote) {
if (isValidJSON(message)) {
console.log('Neuer Einsatz von ' + remote.address + ':' + remote.port + ': ' + message);
sql.db_log('WAIP','Neuer Einsatz von ' + remote.address + ':' + remote.port + ': ' + message);
waip_io.einsatz_speichern(message);
} else {
console.log('Fehler: Einsatz von ' + remote.address + ':' + remote.port + ' Fehlerhaft: ' + message);
sql.db_log('Fehler-WAIP', 'Fehler: Einsatz von ' + remote.address + ':' + remote.port + ' Fehlerhaft: ' + message);
}
});
};

View File

@ -2,16 +2,16 @@ module.exports = function(io, sql, async, app_cfg) {
// Socket.IO
io.on('connection', function (socket) {
console.log('Anwendung von ' + socket.request.connection.remoteAddress + ' (' + socket.id + ') geoeffnet');
sql.db_log('WAIP', 'Anwendung von ' + socket.request.connection.remoteAddress + ' (' + socket.id + ') geoeffnet');
io.sockets.to(socket.id).emit('io.version', app_cfg.global.app_id);
// disconnect
socket.on('disconnect', function () {
console.log('Alarmmonitor von ' + socket.request.connection.remoteAddress + ' (' + socket.id + ') geschlossen');
sql.db_log('WAIP', 'Alarmmonitor von ' + socket.request.connection.remoteAddress + ' (' + socket.id + ') geschlossen');
sql.db_client_delete(socket.id);
});
// Aufruf des Alarmmonitors einer bestimmten Wache verarbeiten
socket.on('wachen_id', function (wachen_id) {
console.log('Alarmmonitor Nr. ' + wachen_id + ' von ' + socket.request.connection.remoteAddress + ' (' + socket.id + ') aufgerufen');
sql.db_log('WAIP', 'Alarmmonitor Nr. ' + wachen_id + ' von ' + socket.request.connection.remoteAddress + ' (' + socket.id + ') aufgerufen');
// prüfen ob Wachenummer in der Datenbank hinterlegt ist
sql.db_wache_vorhanden(wachen_id, function (result) {
// wenn die Wachennummer vorhanden/plausibel dann weiter
@ -23,12 +23,12 @@ module.exports = function(io, sql, async, app_cfg) {
// prüfen ob für diese Wache ein Einsatz vorhanden ist
sql.db_einsatz_vorhanden(wachen_id, function (result_einsatz) {
if (result_einsatz) {
console.log('Einsatz ' + result_einsatz[0].waip_einsaetze_ID + ' fuer Wache ' + wachen_id + ' vorhanden');
sql.db_log('WAIP', 'Einsatz ' + result_einsatz[0].waip_einsaetze_ID + ' fuer Wache ' + wachen_id + ' vorhanden');
//letzten Einsatz verteilen
einsatz_verteilen(result_einsatz[0].waip_einsaetze_ID, socket.id, wachen_id);
sql.db_update_client_status(socket.id, result_einsatz[0].waip_einsaetze_ID);
} else {
console.log('Kein Einsatz fuer Wache ' + wachen_id + ' vorhanden, Standby');
sql.db_log('WAIP', 'Kein Einsatz fuer Wache ' + wachen_id + ' vorhanden, Standby');
//oder falls kein Einsatz vorhanden ist, dann
io.sockets.to(socket.id).emit('io.standby', null);
sql.db_update_client_status(socket.id, 'standby');
@ -36,7 +36,7 @@ module.exports = function(io, sql, async, app_cfg) {
});
});
} else {
console.log('Fehler: Wachnnummer ' + wachen_id + 'nicht vorhanden');
sql.db_log('Fehler-WAIP', 'Fehler: Wachnnummer ' + wachen_id + 'nicht vorhanden');
io.sockets.to(socket.id).emit('io.error', 'Fehler: Wachnnummer \'' + wachen_id + '\' nicht vorhanden!');
};
});
@ -49,7 +49,7 @@ module.exports = function(io, sql, async, app_cfg) {
// Einsatzmeldung (JSON) speichern
sql.db_einsatz_speichern(JSON.parse(message), function (waip_id) {
// nach dem Speichern anhand der waip_id die beteiligten Wachennummern zum Einsatz ermitteln
console.log('DEBUG: ' + waip_id);
sql.db_log('WAIP', 'DEBUG: ' + waip_id);
sql.db_get_einsatzwachen(waip_id, function (data) {
if (data) {
data.forEach(function (row) {
@ -58,12 +58,12 @@ module.exports = function(io, sql, async, app_cfg) {
if (typeof room_stockets !== 'undefined') {
Object.keys(room_stockets.sockets).forEach(function (socketId) {
einsatz_verteilen(waip_id, socketId, row.room);
console.log('Einsatz ' + waip_id + ' wird an ' + socketId + ' (' + row.room + ') gesendet');
sql.db_log('WAIP', 'Einsatz ' + waip_id + ' wird an ' + socketId + ' (' + row.room + ') gesendet');
});
};
});
} else {
console.log('Fehler: Wache für waip_id ' + waip_id + ' nicht vorhanden!');
sql.db_log('Fehler-WAIP', 'Fehler: Wache für waip_id ' + waip_id + ' nicht vorhanden!');
};
});
});
@ -75,19 +75,19 @@ module.exports = function(io, sql, async, app_cfg) {
sql.db_get_einsatzdaten(waip_id, wachen_nr, function (einsatzdaten) {
if (einsatzdaten) {
// Einsatz senden
console.log('Einsatz ' + waip_id + ' fuer Wache ' + wachen_nr + ' an Socket ' + socket_id + ' gesendet');
sql.db_log('WAIP', 'Einsatz ' + waip_id + ' fuer Wache ' + wachen_nr + ' an Socket ' + socket_id + ' gesendet');
io.sockets.to(socket_id).emit('io.neuerEinsatz', einsatzdaten);
sql.db_update_client_status(socket_id, waip_id);
// Sound erstellen
tts_erstellen(app_cfg, socket_id, einsatzdaten, function (tts) {
// Sound senden
console.log('ttsfile: ' + tts);
sql.db_log('WAIP', 'ttsfile: ' + tts);
io.sockets.to(socket_id).emit('io.playtts', tts);
});
} else {
// Standby senden
io.sockets.to(socket_id).emit('io.standby', null);
console.log('Kein Einsatz fuer Wache ' + wachen_nr + ' vorhanden, Standby an Socket ' + socket_id + ' gesendet..');
sql.db_log('WAIP', 'Kein Einsatz fuer Wache ' + wachen_nr + ' vorhanden, Standby an Socket ' + socket_id + ' gesendet..');
sql.db_update_client_status(socket_id, 'standby');
};
});
@ -116,7 +116,6 @@ module.exports = function(io, sql, async, app_cfg) {
} else {
var tts_text = tts_text + '. ' + einsatzdaten.ort + ', ' + einsatzdaten.ortsteil;
};
//console.log('DEBUG: '+ tts_text);
// Einsatzmittel
tts_text = tts_text + '. Für ' + einsatzmittel.join(", ");
// Unterscheidung nach Sondersignal
@ -153,7 +152,7 @@ module.exports = function(io, sql, async, app_cfg) {
childD.stdin.setEncoding('ascii');
childD.stderr.setEncoding('ascii');
childD.stderr.on('data', function (data) {
console.log(new Error(data));
sql.db_log('Fehler-TTS', data);
callback && callback(null);
});
childD.on('exit', function () {
@ -161,7 +160,7 @@ module.exports = function(io, sql, async, app_cfg) {
});
childD.stdin.end();
} else {
console.log(new Error('OS ist nicht Windows'));
sql.db_log('Fehler-TTS', 'OS ist nicht Windows');
callback && callback(null);
};
});
@ -172,7 +171,7 @@ module.exports = function(io, sql, async, app_cfg) {
// Nach alten Einsaetzen suchen und diese ggf. loeschen
sql.db_get_alte_einsaetze('5', function (waip_id) {
if (waip_id) {
console.log('Einsatz mit der ID ' + waip_id + ' ist veraltet und kann gelöscht werden.')
sql.db_log('WAIP', 'Einsatz mit der ID ' + waip_id + ' ist veraltet und kann gelöscht werden.')
//beteiligte Wachen ermitteln
sql.db_get_einsatzwachen(waip_id, function (data) {
if (data) {
@ -187,7 +186,7 @@ module.exports = function(io, sql, async, app_cfg) {
if (same_id) {
io.sockets.to(socketId).emit('io.standby', null);
io.sockets.to(socketId).emit('io.stopaudio', null);
console.log('Standby an Socket ' + socketId + ' gesendet');
sql.db_log('WAIP', 'Standby an Socket ' + socketId + ' gesendet');
sql.db_update_client_status(socketId, 'standby');
};
});
@ -196,7 +195,7 @@ module.exports = function(io, sql, async, app_cfg) {
});
};
// Einsatz löschen
console.log('Einsatz ' + waip_id + ' wird gelöscht');
sql.db_log('WAIP', 'Einsatz ' + waip_id + ' wird gelöscht');
sql.db_einsatz_loeschen(waip_id);
});
};
@ -211,8 +210,8 @@ module.exports = function(io, sql, async, app_cfg) {
sql.db_get_socket_by_id(file.substring(0, file.length - 4), function (data) {
if (!data) {
fs.unlink(process.cwd() + app_cfg.global.soundpath + file, function (err) {
if(err) return console.log(err);
console.log(file + ' wurde erfolgreich geloescht');
if (err) return sql.db_log('Fehler-WAIP', err);
sql.db_log('WAIP', file + ' wurde erfolgreich geloescht');
});
};
});