Filtering with whitelist
This commit is contained in:
53
mqtt.js
53
mqtt.js
@@ -7,6 +7,7 @@ const express = require('express');
|
|||||||
const app = express(); // http-express framework laden (macht routing, etc.)
|
const app = express(); // http-express framework laden (macht routing, etc.)
|
||||||
const http = require('http').Server(app); // http-server module laden
|
const http = require('http').Server(app); // http-server module laden
|
||||||
const io = require('socket.io')(http); // socket.io einbinden
|
const io = require('socket.io')(http); // socket.io einbinden
|
||||||
|
var whitelist=[];
|
||||||
|
|
||||||
app.use('/jquery', express.static(path.join(__dirname, 'node_modules', 'jquery', 'dist')));
|
app.use('/jquery', express.static(path.join(__dirname, 'node_modules', 'jquery', 'dist')));
|
||||||
|
|
||||||
@@ -40,21 +41,25 @@ mqttC.on('message', function (topic, message) { // Handler, wenn mqtt-message ko
|
|||||||
} else {
|
} else {
|
||||||
msg.content=message.toString(); // Ist nix json? dann ab in "content" damit
|
msg.content=message.toString(); // Ist nix json? dann ab in "content" damit
|
||||||
}
|
}
|
||||||
if (topic.startsWith('wavelog/qso/logged')) {
|
if (whitelist.whitelist.includes(msg.content.user_name)) {
|
||||||
tobrowser=parse_qso_msg(msg.content);
|
if (topic.startsWith('wavelog/qso/logged')) {
|
||||||
if (tobrowser.qso_time) {
|
tobrowser=parse_qso_msg(msg.content);
|
||||||
tobrowser.qso_age=dinmin(tobrowser.qso_time);
|
if (tobrowser.qso_time) {
|
||||||
if (tobrowser.qso_age<=10) {
|
tobrowser.qso_age=dinmin(tobrowser.qso_time);
|
||||||
io.emit("mqtt",tobrowser); // und raus an den Browser (nur fuer DIESES Socket, nicht fuer alle Clients) damit
|
if (tobrowser.qso_age<=10) {
|
||||||
|
io.emit("mqtt",tobrowser); // und raus an den Browser (nur fuer DIESES Socket, nicht fuer alle Clients) damit
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log("No Timestamp!");
|
||||||
}
|
}
|
||||||
|
console.log(topic+' / QSO from: '+tobrowser.station_call+' with '+tobrowser.call+' in Mode: '+tobrowser.mode+' at '+tobrowser.qso_time);
|
||||||
} else {
|
} else {
|
||||||
console.log("No Timestamp!");
|
tobrowser=parse_cat_msg(topic,msg.content);
|
||||||
|
// io.emit("cat",tobrowser); // und raus an den Browser (nur fuer DIESES Socket, nicht fuer alle Clients) damit
|
||||||
|
console.log(topic+' / CAT for User '+tobrowser.user_id+' at '+tobrowser.qrg+' in Mode '+tobrowser.mode);
|
||||||
}
|
}
|
||||||
console.log(topic+' / QSO from: '+tobrowser.station_call+' with '+tobrowser.call+' in Mode: '+tobrowser.mode+' at '+tobrowser.qso_time);
|
|
||||||
} else {
|
} else {
|
||||||
tobrowser=parse_cat_msg(topic,msg.content);
|
console.log(msg.content.user_name+' not in Whitelist');
|
||||||
// io.emit("cat",tobrowser); // und raus an den Browser (nur fuer DIESES Socket, nicht fuer alle Clients) damit
|
|
||||||
console.log(topic+' / CAT for User '+tobrowser.user_id+' at '+tobrowser.qrg+' in Mode '+tobrowser.mode);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -75,6 +80,19 @@ function parse_cat_msg(topic,msg) {
|
|||||||
return retmsg;
|
return retmsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getWhitelist() {
|
||||||
|
try {
|
||||||
|
const response = await fetch(config.whitelist_url);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
|
}
|
||||||
|
const data = await response.json();
|
||||||
|
whitelist = data;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching JSON data:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function parse_qso_msg(msg) {
|
function parse_qso_msg(msg) {
|
||||||
let retmsg={};
|
let retmsg={};
|
||||||
retmsg.call=msg.COL_CALL;
|
retmsg.call=msg.COL_CALL;
|
||||||
@@ -91,9 +109,14 @@ function parse_qso_msg(msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const dinmin = (timestamp) => {
|
const dinmin = (timestamp) => {
|
||||||
return Math.floor((Date.now() - new Date(timestamp).getTime()) / 60000);
|
return Math.floor((Date.now() - new Date(timestamp).getTime()) / 60000);
|
||||||
}
|
}
|
||||||
|
|
||||||
http.listen(8000,'127.0.0.1', () => { // Webserver starten
|
function startup() {
|
||||||
console.log(`Socket.IO server running at http://localhost:8000/`); // debug
|
getWhitelist();
|
||||||
});
|
http.listen(8000,'127.0.0.1', () => { // Webserver starten
|
||||||
|
console.log(`Socket.IO server running at http://localhost:8000/`); // debug
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
startup();
|
||||||
|
|||||||
Reference in New Issue
Block a user