mirror of
https://github.com/openmhz/trunk-server.git
synced 2026-11-12 16:57:39 -05:00
tested
This commit is contained in:
159
backend/index.js
159
backend/index.js
@@ -60,20 +60,20 @@ if ((typeof mongo_user !== 'undefined') && (typeof mongo_password !== 'undefined
|
||||
|
||||
|
||||
const connect = async () => {
|
||||
// Demonstrate the readyState and on event emitters
|
||||
console.log(mongoose.connection.readyState); //logs 0
|
||||
mongoose.connection.on('connecting', () => {
|
||||
console.log('Mongoose is connecting')
|
||||
});
|
||||
mongoose.connection.on('connected', () => {
|
||||
console.log('Mongoose is connected');
|
||||
});
|
||||
mongoose.connection.on('disconnecting', () => {
|
||||
console.log('Mongoose is disconnecting');
|
||||
});
|
||||
|
||||
await mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true, maxPoolSize: 50 });
|
||||
console.log("All Done");
|
||||
// Demonstrate the readyState and on event emitters
|
||||
console.log(mongoose.connection.readyState); //logs 0
|
||||
mongoose.connection.on('connecting', () => {
|
||||
console.log('Mongoose is connecting')
|
||||
});
|
||||
mongoose.connection.on('connected', () => {
|
||||
console.log('Mongoose is connected');
|
||||
});
|
||||
mongoose.connection.on('disconnecting', () => {
|
||||
console.log('Mongoose is disconnecting');
|
||||
});
|
||||
|
||||
await mongoose.connect(mongoUrl, { useNewUrlParser: true, useUnifiedTopology: true, maxPoolSize: 50 });
|
||||
console.log("All Done");
|
||||
}
|
||||
connect();
|
||||
|
||||
@@ -124,7 +124,7 @@ app.get('/:shortName/calls/newer', calls.get_newer_calls);
|
||||
app.get('/:shortName/calls/older', calls.get_older_calls);
|
||||
app.get('/:shortName/calls/date', calls.get_date_calls);
|
||||
app.get('/:shortName/calls/:time/older', calls.get_iphone_calls);
|
||||
app.get('/:shortName/calls', calls.get_calls);
|
||||
app.get('/:shortName/calls', calls.get_calls);
|
||||
|
||||
|
||||
/*------ UPLOADS ---------- upload.single('call'), uploads.upload,*/
|
||||
@@ -213,40 +213,37 @@ function notify_clients(call) {
|
||||
var client = clients[key];
|
||||
if (client.active) {
|
||||
if ((client.firehose == true) || (client.shortName == call.shortName.toLowerCase())) {
|
||||
// if client is not filtering for stars, or if the client is filtering and the call has stars
|
||||
if (!client.filterStarred || call.star) {
|
||||
if (client.filterCode == "") {
|
||||
sent++;
|
||||
client.socket.emit("new message", JSON.stringify(call));
|
||||
} else if (client.filterType == "unit") {
|
||||
var codeArray = client.filterCode.split(',');
|
||||
var success = false;
|
||||
for (var j = 0; j < codeArray.length; ++j) {
|
||||
for (var k = 0; k < call.srcList.length; k++) {
|
||||
if (codeArray[j] == call.srcList[k]) {
|
||||
sent++;
|
||||
client.socket.emit("new message", JSON.stringify(call));
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (success) {
|
||||
// if client
|
||||
if ((client.firehose == true) || (client.filterType == "all")) {
|
||||
sent++;
|
||||
client.socket.emit("new message", JSON.stringify(call));
|
||||
} else if (client.filterType == "unit") {
|
||||
var codeArray = client.filterCode.split(',');
|
||||
var success = false;
|
||||
for (var j = 0; j < codeArray.length; ++j) {
|
||||
for (var k = 0; k < call.srcList.length; k++) {
|
||||
if (codeArray[j] == call.srcList[k]) {
|
||||
sent++;
|
||||
client.socket.emit("new message", JSON.stringify(call));
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
var codeArray = client.talkgroupNums;
|
||||
for (var j = 0; j < codeArray.length; ++j) {
|
||||
if (codeArray[j] == call.talkgroupNum) {
|
||||
client.socket.emit("new message", JSON.stringify(call));
|
||||
sent++
|
||||
break;
|
||||
}
|
||||
if (success) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var codeArray = client.talkgroupNums;
|
||||
for (var j = 0; j < codeArray.length; ++j) {
|
||||
if (codeArray[j] == call.talkgroupNum) {
|
||||
client.socket.emit("new message", JSON.stringify(call));
|
||||
sent++
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -257,11 +254,24 @@ function notify_clients(call) {
|
||||
}
|
||||
}
|
||||
|
||||
function parseHeader(header) {
|
||||
for (const directive of header.split(",")[0].split(";")) {
|
||||
if (directive.startsWith("for=")) {
|
||||
return directive.substring(4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
io.sockets.on('connection', function (client) {
|
||||
const header_ipAddress = parseHeader(client.handshake.headers["forwarded"] || "");
|
||||
const cf_ipAddress = client.handshake.headers["cf-connecting-ip"];
|
||||
const x_ipAddress = client.handshake.headers["x-forwarded-for"].split(",")[0];
|
||||
const ipAddress = cf_ipAddress || x_ipAddress || header_ipAddress;
|
||||
clients[client.id] = { socket: client, active: false };
|
||||
clients[client.id].timestamp = new Date();
|
||||
client.on('start', async function (data) {
|
||||
if ((typeof clients[client.id] !== "undefined") && data.shortName) {
|
||||
if (typeof clients[client.id] !== "undefined") {
|
||||
clients[client.id].active = true;
|
||||
clients[client.id].shortName = data.shortName.toLowerCase();
|
||||
clients[client.id].filterCode = String(data.filterCode);
|
||||
@@ -271,19 +281,41 @@ io.sockets.on('connection', function (client) {
|
||||
clients[client.id].talkgroupNums = [];
|
||||
clients[client.id].timestamp = new Date();
|
||||
clients[client.id].firehose = false;
|
||||
clients[client.id].ipAddress = ipAddress;
|
||||
|
||||
// Validate the filterCode and filterType
|
||||
if (typeof clients[client.id].filterCode != "string") {
|
||||
console.error("Error - Socket - Invalid filterCode: " + data.filterCode + " ShortName: " + data.shortName.toLowerCase());
|
||||
console.error("Error - Socket - Invalid filterCode: " + data.filterCode + " ShortName: " + data.shortName.toLowerCase() + " ClientID: " + client.id + " IP: " + ipAddress);
|
||||
delete clients[client.id];
|
||||
return;
|
||||
}
|
||||
|
||||
if ((data.filterType == "firehose") && firehose_key && (typeof data.filterCode == "string") && (data.filterCode == firehose_key)) {
|
||||
clients[client.id].firehose = true;
|
||||
|
||||
} else if ((data.filterType == "group") && (typeof data.filterCode == "string") && (data.filterCode.indexOf(',') == -1)) {
|
||||
if (!ObjectId.isValid(data.filterCode)) {
|
||||
console.error("Error - Socket - Invalid Group ID: " + data.filterCode);
|
||||
if (typeof clients[client.id].filterType != "string") {
|
||||
console.error("Error - Socket - Invalid filterType: " + data.filterType + " ShortName: " + data.shortName.toLowerCase() + " ClientID: " + client.id + " IP: " + ipAddress);
|
||||
delete clients[client.id];
|
||||
return;
|
||||
}
|
||||
|
||||
if ((data.filterType != "firehose") && (!data.shortName || (typeof data.shortName != "string"))) { // If it is not firehose, then it must have a shortName
|
||||
console.error("Error - Socket - Invalid ShortName: " + data.shortName + " ClientID: " + client.id + " IP: " + ipAddress);
|
||||
delete clients[client.id];
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Check if the filterType is firehose and the filterCode is the firehose key
|
||||
if (data.filterType == "firehose") {
|
||||
if (firehose_key && (typeof data.filterCode == "string") && (data.filterCode == firehose_key)) {
|
||||
console.log("Enabling Firehose mode for Client: " + client.id + " IP: " + ipAddress);
|
||||
clients[client.id].firehose = true;
|
||||
} else {
|
||||
console.error("Error - Socket - Invalid Firehose Key: " + data.filterCode + " ClientID: " + client.id + " Filter Name: " + client.filterName + " IP: " + ipAddress);
|
||||
delete clients[client.id];
|
||||
return;
|
||||
}
|
||||
} else if ((data.filterType == "group")) { // Handle filterType == "group"
|
||||
if ((typeof data.filterCode != "string") || (data.filterCode.indexOf(',') != -1) || !ObjectId.isValid(data.filterCode)) {
|
||||
console.error("Error - Socket - Invalid Group ID: " + data.filterCode + " ShortName: " + data.shortName.toLowerCase() + " ClientID: " + client.id + " IP: " + ipAddress);
|
||||
delete clients[client.id];
|
||||
return;
|
||||
}
|
||||
@@ -291,29 +323,32 @@ io.sockets.on('connection', function (client) {
|
||||
const group = await Group.findOne({ 'shortName': data.shortName.toLowerCase(), '_id': ObjectId.createFromHexString(data.filterCode) });
|
||||
|
||||
if (typeof clients[client.id] === "undefined") {
|
||||
console.error("How can it be undefined here!!");
|
||||
console.error(client.id);
|
||||
console.error("Error - Socket - Client not found ShortName: " + data.shortName + " ClientID: " + client.id + " IP: " + ipAddress);
|
||||
return;
|
||||
}
|
||||
if (group) {
|
||||
clients[client.id].talkgroupNums = group.talkgroups;
|
||||
} else {
|
||||
console.error("Error - Socket: Invalid group " + data.filterCode + " Shortname: " + data.shortName + " ClientID: " + client.id);
|
||||
console.error("Error - Socket - Invalid group " + data.filterCode + " Shortname: " + data.shortName + " ClientID: " + client.id + " IP: " + ipAddress);
|
||||
delete clients[client.id];
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle filterType == "talkgroup"
|
||||
} else if ((data.filterType == "talkgroup") && Array.isArray(data.filterCode)) {
|
||||
clients[client.id].talkgroupNums = data.filterCode;
|
||||
} else if ((data.filterType == "all") && (data.filterCode == "")) {
|
||||
// Handle filterType == "all"
|
||||
clients[client.id].talkgroupNums = [];
|
||||
} else {
|
||||
// This is for ALL filters
|
||||
//console.error("Invalid Socket Filter - Type: " + data.filterType + " Code: " + data.filterCode);
|
||||
console.error("Error - Socket - Invalid Filter Type: " + data.filterType + " Filter Code: " + data.filterCode + " Filter Name: " + data.filterName + " ShortName: " + data.shortName.toLowerCase() + " ClientID: " + client.id + " IP: " + ipAddress);
|
||||
delete clients[client.id];
|
||||
return;
|
||||
}
|
||||
//console.log("[" + data.shortName.toLowerCase() + "] WebSocket Updating - Client: " + client.id + " code set to: " + data.filterCode + " type set to: " + data.filterType + " TGS: " + clients[client.id].talkgroupNums);
|
||||
} else {
|
||||
console.error("Error - Socket.io [Start] either client not found or no Short Name");
|
||||
console.error("Error - Socket.io [Start] either client not found, shortName:" + data.shortName + " ClientID: " + client.id + " IP: " + ipAddress);
|
||||
}
|
||||
});
|
||||
|
||||
client.on('stop', function (data) {
|
||||
if (clients[client.id]) {
|
||||
clients[client.id].active = false;
|
||||
@@ -329,15 +364,15 @@ io.sockets.on('connection', function (client) {
|
||||
stats.init_stats();
|
||||
// This will run at 3am each day.
|
||||
// IF you don't set the minute to 0, it will run every minute while it is still 3, so at 3:01, 3:02... etc
|
||||
schedule.scheduleJob('0 3 * * *', function() {
|
||||
schedule.scheduleJob('0 3 * * *', function () {
|
||||
db.cleanOldCalls();
|
||||
});
|
||||
|
||||
schedule.scheduleJob('15 3 * * *', function() {
|
||||
schedule.scheduleJob('15 3 * * *', function () {
|
||||
db.cleanOldEvents();
|
||||
});
|
||||
|
||||
schedule.scheduleJob('30 3 * * *', function() {
|
||||
schedule.scheduleJob('30 3 * * *', function () {
|
||||
db.cleanOldPodcasts();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user