13 var fs = require(
'fs');
14 var path_module = require(
'path');
15 var module_holder = {};
17 var child_process = require(
'child_process');
19 var util = require(
'util');
20 var log_file = fs.createWriteStream(
'/tmp/server.' + process.env[
"USER"] +
'.log', { flags :
'a' });
21 var log_stdout = process.stdout;
23 var getversion =
function () {
24 console.log(
"Getting Server Version");
25 if (fs.existsSync(
"./version.txt")) {
26 console.log(
"Reading Server Version from File");
27 return "" + fs.readFileSync(
"./version.txt");
30 child_process.exec(
"git describe --tags",
function (error, stdout, stderr) {
31 version = stdout.trim() +
"-Git";
32 child_process.exec(
"git status --porcelain",
function (error, stdout) {
33 if (stdout.length > 0) {
40 var version = getversion();
46 hostname:
"localhost",
49 function loadConfig() {
50 if (fs.existsSync(
"config.json")) {
51 config = JSON.parse(fs.readFileSync(
"config.json"));
53 fs.writeFileSync(
"config.json", JSON.stringify(config));
56 if (process.env.ARTDAQDEMO_BASE_PORT) {
57 config.baseport = parseInt(process.env[
"ARTDAQDEMO_BASE_PORT"]) + config.portOffset;
60 if (config.hostname ===
"localhost" && cluster.isMaster) {
61 console.log(
"Listening only on localhost. To listen on a different address, set \"hostname\" in config.json.\nUse \"0.0.0.0\" to listen on all interfaces.");
67 console.log =
function (d) {
68 log_file.write(util.format(d) +
'\n');
69 log_stdout.write(util.format(d) +
'\n');
72 function LoadCerts(path) {
74 var files = fs.readdirSync(path);
75 for (var i = 0; i < files.length; i++) {
76 if (files[i].search(
".pem") > 0 || files[i].search(
".crt") > 0) {
77 output.push(fs.readFileSync(path +
"/" + files[i]));
85 function LoadModules(path) {
86 var stat = fs.lstatSync(path);
87 if (stat.isDirectory()) {
89 var files = fs.readdirSync(path);
90 var f, l = files.length;
91 for (var i = 0; i < l; i++) {
92 f = path_module.join(path, files[i]);
95 }
else if (path.search(
"_module.js") > 0 && path.search(
"js~") < 0) {
96 console.log(
"Loading Submodule " + path);
99 require(path)(module_holder);
100 console.log(
"Initialized Submodule " + path);
103 var DIR = path_module.join(__dirname,
"modules");
107 var https = require(
'https');
108 var http = require(
'http');
109 var url = require(
'url');
110 var qs = require(
'querystring');
112 for (var name in module_holder) {
113 if (module_holder.hasOwnProperty(name)) {
114 module_holder[name].on(
"message",
function (data) {
120 module_holder[name].MasterInitFunction(workerData, config.module_config[name]);
121 module_holder[name].WorkerInitFunction(workerData);
128 function serve(req, res, readOnly, username) {
131 var pathname = url.parse(req.url,
true).pathname;
132 if (pathname[0] ===
'/') {
133 pathname = pathname.substr(1);
136 var moduleName = pathname.substr(0, pathname.indexOf(
'/'));
137 var functionName = pathname.substr(pathname.indexOf(
'/') + 1);
141 require(
'dns').reverse(req.connection.remoteAddress, function (err, domains) {
144 if (functionName.search(
".min.map") < 0) {
146 console.log(
"Received " + req.method +
", Client: " + domains[0] +
" [" + req.connection.remoteAddress +
"], PID: " + process.pid +
" Module: " + moduleName +
", function: " + functionName);
151 if (functionName.search(
".min.map") < 0) {
153 console.log(
"Received " + req.method +
", Client: " + req.connection.remoteAddress +
", PID: " + process.pid +
" Module: " + moduleName +
", function: " + functionName);
159 if (functionName.search(
"GET_ServerVersion") >= 0) {
160 res.setHeader(
"Content-Type",
"text/plain");
161 res.statusCode = 200;
165 if (moduleName ===
".." || functionName.search(
"\\.\\.") >= 0) {
166 console.log(
"Possible break-in attempt!: " + pathname);
167 res.writeHeader(404, {
'Content-Type':
'text/html' });
171 res.setHeader(
"Content-Type",
"application/json");
172 res.statusCode = 200;
179 if (req.method ===
"POST") {
183 req.on(
'data',
function (data) {
187 req.on(
'end',
function () {
191 post = JSON.parse(body);
193 post = qs.parse(body);
197 if (module_holder[moduleName] != null) {
198 console.log(
"Module " + moduleName +
", function " + functionName +
" accessType " + (readOnly ?
"RO" :
"RW"));
200 module_holder[moduleName].removeAllListeners(
'data').on(
'data',
function (data) {
203 module_holder[moduleName].removeAllListeners(
'end').on(
'end',
function (data) {
205 res.end(JSON.stringify(dataTemp + data));
207 module_holder[moduleName].removeAllListeners(
'stream').on(
'stream',
function (str, hdrs, code) {
208 console.log(
"Stream message received: " + hdrs +
" CODE: " + code);
209 res.writeHead(code, hdrs);
215 data = module_holder[moduleName][
"RO_" + functionName](post, workerData[moduleName]);
218 res.end(JSON.stringify(data));
221 console.log(
"Error caught: " + err.stack);
222 if (err instanceof TypeError) {
224 res.end(JSON.stringify(null));
229 data = module_holder[moduleName][
"RW_" + functionName](post, workerData[moduleName]);
232 res.end(JSON.stringify(data));
235 console.log(
"Error caught; text: " + JSON.stringify(err2));
236 console.log(
"Error caught: " + err2.stack);
237 if (err2 instanceof TypeError) {
239 data = module_holder[moduleName][
"RO_" + functionName](post, workerData[moduleName]);
242 res.end(JSON.stringify(data));
248 console.log(
"Unknown POST URL: " + pathname);
249 res.writeHeader(404, {
'Content-Type':
'text/html' });
255 if (req.method ===
"GET" || req.method ===
"HEAD") {
257 if (functionName.indexOf(
".") > 0) {
259 var ext = functionName.substr(functionName.lastIndexOf(
".") + 1);
260 res.setHeader(
"Content-Type",
"text/plain");
264 res.setHeader(
"Content-Type",
"text/css");
267 res.setHeader(
"Content-Type",
"text/javascript");
270 res.setHeader(
"Content-Type",
"text/html");
273 res.setHeader(
"Content-Type",
"text/html");
276 res.setHeader(
"Content-Type",
"application/root+root.exe");
279 res.setHeader(
"Content-Type",
"image/gif");
283 var filename =
"./modules/" + moduleName +
"/client/" + functionName;
284 if (functionName.search(
"favicon.ico") >= 0) {
285 filename =
"./modules/base/client/images/favicon.ico";
287 if (fs.existsSync(filename)) {
288 res.setHeader(
"Content-Length", fs.statSync(filename)[
"size"]);
289 if (req.headers.range != null) {
290 var range = req.headers.range;
291 var offset = parseInt(range.substr(range.indexOf(
'=') + 1, range.indexOf(
'-') - (range.indexOf(
'=') + 1)));
292 var endOffset = parseInt(range.substr(range.indexOf(
'-') + 1));
293 console.log(
"Reading (" + offset +
", " + endOffset +
")");
295 res.setHeader(
"Content-Length", (endOffset - offset + 1).toString());
296 var readStream = fs.createReadStream(filename, { start: parseInt(offset), end: parseInt(endOffset) });
297 readStream.pipe(res);
299 res.end(fs.readFileSync(filename));
303 console.log(
"File not found: " + filename);
304 res.setHeader(
"Content-Type",
"text/plain");
305 res.end(
"File Not Found.");
307 }
else if (module_holder[moduleName] != null) {
308 console.log(
"Module " + moduleName +
", function GET_" + functionName);
311 module_holder[moduleName].removeAllListeners(
'data').on(
'data',
function (data) {
315 module_holder[moduleName].removeAllListeners(
'end').on(
'end',
function (data) {
317 res.end(JSON.stringify(dataTemp + data));
319 module_holder[moduleName].removeAllListeners(
'stream').on(
'stream',
function (str, hdrs, code) {
320 res.writeHead(code, hdrs);
324 var data = module_holder[moduleName][
"GET_" + functionName](workerData[moduleName]);
327 res.end(JSON.stringify(data));
330 console.log(
"Error caught: " + err.stack);
333 console.log(
"Sending client.html");
335 res.setHeader(
"Content-Type",
"text/html");
336 res.end(fs.readFileSync(
"./client.html"),
'utf-8');
337 console.log(
"Done sending client.html");
342 console.log(
"Setting up options");
344 key: fs.readFileSync(
'./certs/server.key'),
345 cert: fs.readFileSync(
'./certs/server.crt'),
346 ca: LoadCerts(
"./certs/certificates"),
348 rejectUnauthorized:
false
350 var authlist =
" " + fs.readFileSync(
"./certs/authorized_users");
351 console.log(
"Done setting up options");
354 var server = https.createServer(options,
function (req, res) {
356 var clientCertificate = req.connection.getPeerCertificate();
357 var username =
"HTTPS User";
358 if (req.client.authorized) {
359 username = clientCertificate.subject.CN[0];
360 var useremail = clientCertificate.subject.CN[1].substr(4);
361 if (authlist.search(username) > 0 || authlist.search(useremail) > 0) {
367 serve(req, res, readOnly, username);
369 console.trace(
"Unhandled error in serve: " + JSON.stringify(e));
372 var insecureServer = http.createServer(
function (req, res) {
375 serve(req, res,
false,
"HTTP User");
377 console.trace(
"Unhandled error in serve: " + JSON.stringify(e));
382 if (__dirname.search(
"dev") >= 0) {
385 console.log(
"Listening on ports " + baseport +
" and " + (baseport + 1));
386 server.listen(baseport + 1);
387 insecureServer.listen(baseport);