-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWevQueryServer.js
More file actions
192 lines (152 loc) · 5.48 KB
/
WevQueryServer.js
File metadata and controls
192 lines (152 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";//necessary for old NODE versions. otherwise some functions will not work
/**
* WevQuery server.
* 1. npm install socket.io
* 2. install connect and serve-static
* npm install connect serve-static
* https://github.com/senchalabs/connect
* 2. npm i xsd-schema-validator
* xsd-schema-validator requires having Java installed!!! check https://www.npmjs.com/package/xsd-schema-validator
* 3. node WevQueryServer.js
*/
//Load libraries
var express = require("express");
var serveStatic = require('serve-static');
var auth = require('basic-auth');
const wevQueryOptions = require('./options');
//Load external files
var userCredentials = require('./userCredentials.js');
var socketGeneric = require("./socketHandlers/socketGeneric.js");
var socketXmlQuery = require("./socketHandlers/socketXmlQuery.js");
var socketDataAnalysis = require("./socketHandlers/socketDataAnalysis.js");
var socketDataInfo = require("./socketHandlers/socketDataInfo.js");
//Start Express server
var app = express();
var router = express.Router();
var logFile = "./wevQuery.log";
//Start router
var wevqueryRouter = require("./rest/wevqueryRouter.js");
app.use('/wevqueryrest', wevqueryRouter);
// console.log(userCredentials.userList);
// console.log(userCredentials.email);
// console.log(userCredentials);
//Only add authentication if there are users in the list (apart from default, if still there)
if (Object.keys(userCredentials.userList).length > 1
|| (Object.keys(userCredentials.userList).length == 1 && userCredentials.userList["user"] != "password")) {
app.use(authFunction);
}
else
console.log("AUTHENTICATION DISABLED");
var httpServer = app.use(serveStatic(__dirname)).listen(wevQueryOptions.port, function () {
console.log('WevQuery Server running on ' + wevQueryOptions.port + '...');
});
var io = require('socket.io')(httpServer);
var socketConnection = io.sockets;
//var socket = io.connect();
var fs = require('fs');
var exec = require('child_process').exec;
var path = require('path');
var url = require("url");
var mongoDAO = require("./mongoDAO/mongoDAO.js");
var resultsFolder = "./Results/";
//Create results folder if it doesn't exist
if (!fs.existsSync(resultsFolder)) {
fs.mkdirSync(resultsFolder);
}
// listen for commands from the Web dashboard
socketConnection.on('connection', function (socketInstance) {
socketXmlQuery.initialiseSockets(mongoDAO, socketGeneric, socketConnection, socketInstance);
socketDataAnalysis.initialiseSockets(mongoDAO, socketGeneric, socketConnection, socketInstance, resultsFolder);
socketDataInfo.initialiseSockets(mongoDAO, socketGeneric, socketConnection, socketInstance);
});
function authFunction(req, res, next) {
console.log('Authentication triggered');
var objUser = auth(req)
if (objUser === undefined || userCredentials.userList[objUser.name] !== objUser.pass) {
res.set("WWW-Authenticate", "Basic realm=Authorization Required")
res.status(401).end()
} else { next() }
}
/**
* Sends a mail, using the credentials stored in mail
*/
/*
const nodemailer = require('nodemailer');
console.log(emailConfig);
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: emailConfig.email.service,
auth: {
user: emailConfig.email.user,
pass: emailConfig.email.password
}
});
function sendEmailNotification(email, title, query, result) {
let mailOptions = {
from: emailConfig.email.user + ' <noone@noone.com>', // sender address
to: email, // list of receivers
subject: title, // Subject line
text: query, // plain text body
html: '<b>Hello world ?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});
}
*/
/**
* Log a message into the server's log file
*/
function logText(message) {
var date = new Date(new Date().getTime());
var datevalues = date.getFullYear() + "," +
(date.getMonth() + 1) + "," +
date.getDate() + "," +
date.getHours() + ":" +
date.getMinutes() + ":" +
date.getSeconds();
var logEntry = datevalues + " MESSAGE:" + message + "\n";
console.log("clientlog: " + logEntry);
var log = fs.createWriteStream(logFile, { 'flags': 'a' });
log.write(logEntry);
}
/**
* Empty the log file
*/
function clearLog() {
fs.writeFile(logFile, "");
}
/**
* Rename the log file by appending something
* does not check to see if it overwrites...
*/
function saveLog(id) {
// save log file to new file
fs.rename(logFile, logFile + "." + id);
clearLog();
}
/**
* Error handling
*/
process.stdin.resume();//so the program will not close instantly
function exitHandler(options, err) {
if (options.adminInitiated) {
socketGeneric.sendMessageToUser(-1, "ADMINISTRATOR STOPPED THE SERVER", true, socketConnection);
} else {
socketGeneric.sendMessageToUser(-1, "FATAL ERROR, CONTACT ADMINISTRATOR", true, socketConnection);
}
if (options.cleanup) console.log('clean');
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', exitHandler.bind(null, { cleanup: true }));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, { adminInitiated: true, exit: true }));
//catches uncaught exceptions
//Do we want to close the server if there is an uncaught exception?
process.on('uncaughtException', exitHandler.bind(null, { exit: false }));