update So 7. Jun 18:31:01 CEST 2020

This commit is contained in:
Robert Richter 2020-06-07 18:31:01 +02:00
parent cde9067dba
commit 98c68cb660
4 changed files with 96 additions and 45 deletions

View File

@ -3,9 +3,8 @@ module.exports = function (io, sql, app_cfg, waip) {
// Module laden
const io_api = require('socket.io-client');
// FIXME eventuellen Zirkel-Bezug abfangen
// Variablen festlegen
var uuid_pattern = new RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$', 'i');
// ###
// Server Socket.IO Empfangs-API (anderer Server stellt Verbindung her und sendet Daten)
@ -20,24 +19,34 @@ module.exports = function (io, sql, app_cfg, waip) {
// versuche Remote-IP zu ermitteln
var remote_ip = socket.handshake.headers["x-real-ip"] || socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
//TODO pruefen ob Verbindung mit passendem Geheimnis und aus IP-Bereich, das Ergebnis loggen
//TODO Verschlüsselung: pruefen ob Verbindung mit passendem Geheimnis und aus IP-Bereich, das Ergebnis loggen
// in Liste der Clients mit aufnehmen
sql.db_client_update_status(socket, 'api');
// Neuen Einsatz speichern
socket.on('from_client_to_server_new_waip', function (data) {
waip.einsatz_speichern(data);
sql.db_log('API', 'Neuer Wachalarm von ' + remote_ip + ': ' + data);
socket.on('from_client_to_server_new_waip', function (raw_data) {
var data = raw_data.data;
var app_id = raw_data.app_id;
// nur speichern wenn app_id nicht eigenen globalen app_id entspricht
if (app_id != app_cfg.global.app_id) {
waip.einsatz_speichern(data, app_id);
sql.db_log('API', 'Neuer Wachalarm von ' + remote_ip + ': ' + data);
};
});
// neue externe Rueckmeldung speichern
socket.on('from_client_to_server_new_rmld', function (data) {
waip.rmld_speichern(data, remote_ip, function (result) {
if (!result) {
sql.db_log('API', 'Fehler beim speichern der Rückmeldung von ' + remote_ip + ': ' + data);
};
});
socket.on('from_client_to_server_new_rmld', function (raw_data) {
var data = raw_data.data;
var app_id = raw_data.app_id;
// nur speichern wenn app_id nicht eigenen globalen app_id entspricht
if (app_id != app_cfg.global.app_id) {
waip.rmld_speichern(data, remote_ip, function (result) {
if (!result) {
sql.db_log('API', 'Fehler beim speichern der Rückmeldung von ' + remote_ip + ': ' + data);
};
});
};
});
// Disconnect
@ -48,18 +57,32 @@ module.exports = function (io, sql, app_cfg, waip) {
});
};
function server_to_client_new_waip(data) {
function server_to_client_new_waip(data, app_id) {
// Rückmeldung an verbundenen Client senden, falls funktion aktiviert
if (app_cfg.api.enabled) {
nsp_api.emit('from_server_to_client_new_waip', data);
// testen ob app_id auch eine uuid ist, falls nicht, eigene app_uuid setzen
if (!uuid_pattern.test(app_id)) {
app_id = app_cfg.global.app_id;
};
nsp_api.emit('from_server_to_client_new_waip', {
data: data,
app_id: app_id
});
sql.db_log('API', 'Einsatz an ' + app_cfg.endpoint.host + ' gesendet: ' + data);
};
};
function server_to_client_new_rmld(data) {
function server_to_client_new_rmld(data, app_id) {
// Rückmeldung an verbundenen Client senden, falls funktion aktiviert
if (app_cfg.api.enabled) {
nsp_api.emit('from_server_to_client_new_rmld', data);
// testen ob app_id auch eine uuid ist, falls nicht, eigene app_uuid setzen
if (!uuid_pattern.test(app_id)) {
app_id = app_cfg.global.app_id;
};
nsp_api.emit('from_server_to_client_new_rmld', {
data: data,
app_id: app_id
});
sql.db_log('API', 'Rückmeldung an ' + app_cfg.endpoint.host + ' gesendet: ' + data);
};
};
@ -70,7 +93,7 @@ module.exports = function (io, sql, app_cfg, waip) {
if (app_cfg.endpoint.enabled) {
// Verbindung zu anderem Server aufbauen
// TODO Verbindungsaufbau mit passendem Geheimnis absichern
// TODO Verschlüsselung: Verbindungsaufbau mit passendem Geheimnis absichern
var remote_api = io_api.connect(app_cfg.endpoint.host, {
reconnect: true
});
@ -91,33 +114,57 @@ module.exports = function (io, sql, app_cfg, waip) {
});
// neuer Einsatz vom Endpoint-Server
remote_api.on('from_server_to_client_new_waip', function (data) {
waip.einsatz_speichern(data);
sql.db_log('API', 'Neuer Wachalarm von ' + app_cfg.endpoint.host + ': ' + data);
remote_api.on('from_server_to_client_new_waip', function (raw_data) {
var data = raw_data.data;
var app_id = raw_data.app_id;
// nur speichern wenn app_id nicht eigenen globalen app_id entspricht
if (app_id != app_cfg.global.app_id) {
waip.einsatz_speichern(data);
sql.db_log('API', 'Neuer Wachalarm von ' + app_cfg.endpoint.host + ': ' + data);
};
});
// neue Rückmeldung vom Endpoint-Server
remote_api.on('from_server_to_client_new_rmld', function (data) {
waip.rmld_speichern(data, app_cfg.endpoint.host, function (result) {
if (!result) {
sql.db_log('API', 'Fehler beim speichern der Rückmeldung von ' + app_cfg.endpoint.host + ': ' + data);
};
});
remote_api.on('from_server_to_client_new_rmld', function (raw_data) {
var data = raw_data.data;
var app_id = raw_data.app_id;
// nur speichern wenn app_id nicht eigenen globalen app_id entspricht
if (app_id != app_cfg.global.app_id) {
waip.rmld_speichern(data, app_cfg.endpoint.host, function (result) {
if (!result) {
sql.db_log('API', 'Fehler beim speichern der Rückmeldung von ' + app_cfg.endpoint.host + ': ' + data);
};
});
};
});
};
function client_to_server_new_waip(data) {
function client_to_server_new_waip(data, app_id) {
// Alarm an Remote-Server senden, falls funktion aktiviert
if (app_cfg.endpoint.enabled) {
remote_api.emit('from_client_to_server_new_waip', data);
// testen ob app_id auch eine uuid ist, falls nicht, eigene app_uuid setzen
if (!uuid_pattern.test(app_id)) {
app_id = app_cfg.global.app_id;
};
remote_api.emit('from_client_to_server_new_waip', {
data: data,
app_id: app_id
});
sql.db_log('API', 'Neuen Wachalarm an ' + app_cfg.endpoint.host + ' gesendet: ' + data);
};
};
function client_to_server_new_rmld(data) {
function client_to_server_new_rmld(data, app_id) {
// Rückmeldung an Remote-Server senden, falls funktion aktiviert
if (app_cfg.endpoint.enabled) {
remote_api.emit('from_client_to_server_new_rmld', data);
// testen ob app_id auch eine uuid ist, falls nicht, eigene app_uuid setzen
if (!uuid_pattern.test(app_id)) {
app_id = app_cfg.global.app_id;
};
remote_api.emit('from_client_to_server_new_rmld', {
data: data,
app_id: app_id
});
sql.db_log('API', 'Rückmeldung an ' + app_cfg.endpoint.host + ' gesendet: ' + data);
};
};

View File

@ -247,7 +247,7 @@ module.exports = function (app, sql, uuidv4, app_cfg, passport, auth, waip, udp)
// Rueckmeldung entgegennehmen
app.post('/rmld/:waip_uuid/:rmld_uuid', function (req, res) {
// TODO Rueckmeldung auf Validiteat pruefen
// TODO Validierung: Rueckmeldung auf Validiteat pruefen
var waip_uuid = req.body.waip_uuid;
var rmld_uuid = req.body.rmld_uuid;
waip.rmld_speichern(req.body, null, function (result) {

View File

@ -25,7 +25,7 @@ module.exports = function(app_cfg, waip, sql) {
udp_server.on('message', function(message, remote) {
if (isValidJSON(message)) {
sql.db_log('WAIP', 'Neuer Einsatz von ' + remote.address + ':' + remote.port + ': ' + message);
waip.einsatz_speichern(message, app_cfg.global.app_id);
waip.einsatz_speichern(message, 'udp');
} else {
sql.db_log('Fehler-WAIP', 'Fehler: Einsatz von ' + remote.address + ':' + remote.port + ' Fehlerhaft: ' + message);
}

View File

@ -1,8 +1,8 @@
module.exports = function (io, sql, brk, async, app_cfg) {
module.exports = function (io, sql, brk, async, app_cfg, api) {
// Einsatzmeldung in Datenbank speichern
function einsatz_speichern(einsatz_rohdaten) {
// FIXME: Einsatzdaten auf Validität prüfen
function einsatz_speichern(einsatz_rohdaten, app_id) {
// TODO Validierung: Einsatzdaten auf Validität prüfen
// Einsatzmeldung (JSON) speichern
sql.db_einsatz_speichern(einsatz_rohdaten, function (waip_id) {
sql.db_log('WAIP', 'DEBUG: Neuer Einsatz mit der ID ' + waip_id);
@ -40,6 +40,9 @@ module.exports = function (io, sql, brk, async, app_cfg) {
};
});
});
// Einsatzdaten per API weiterleiten (entweder zum Server oder zum verbunden Client)
api.server_to_client_new_waip(einsatz_rohdaten, app_id);
api.client_to_server_new_waip(einsatz_rohdaten, app_id);
};
// Einsatz an Client verteilen
@ -82,6 +85,7 @@ module.exports = function (io, sql, brk, async, app_cfg) {
function rmld_speichern(rueckmeldung, host, callback) {
// TODO an api senden
if (!host == null) {
host = ' von ' + host;
};