comparison rlgwebd @ 203:5491ca3a335b

Fail cleanly if necessary files can't be opened.
author John "Elwin" Edwards
date Wed, 04 Jan 2017 21:25:43 -0500
parents 7f25bb89b59c
children a200b313870d
comparison
equal deleted inserted replaced
202:7f25bb89b59c 203:5491ca3a335b
23 static_root: "/var/www/" 23 static_root: "/var/www/"
24 }; 24 };
25 25
26 /* Read configuration from a file */ 26 /* Read configuration from a file */
27 var config_file = "/etc/rlgwebd.conf"; 27 var config_file = "/etc/rlgwebd.conf";
28 var config_lines = fs.readFileSync(config_file).toString().split('\n'); 28 var config_lines = read_or_die(config_file, "Configuration file").toString().split('\n');
29
29 for (var i = 0; i < config_lines.length; i++) { 30 for (var i = 0; i < config_lines.length; i++) {
30 if (config_lines[i].length > 0 && config_lines[i][0] != '#') { 31 if (config_lines[i].length > 0 && config_lines[i][0] != '#') {
31 var config_fields = config_lines[i].split('='); 32 var config_fields = config_lines[i].split('=');
32 if (config_fields.length < 2) 33 if (config_fields.length < 2)
33 continue; 34 continue;
576 function tslog() { 577 function tslog() {
577 arguments[0] = new Date().toISOString() + ": " + String(arguments[0]); 578 arguments[0] = new Date().toISOString() + ": " + String(arguments[0]);
578 console.log.apply(console, arguments); 579 console.log.apply(console, arguments);
579 } 580 }
580 581
582 // Read a file synchronously, exiting if anything goes wrong.
583 // Intended only for files required at startup.
584 function read_or_die(filename, description) {
585 var contents;
586 try {
587 contents = fs.readFileSync(filename);
588 }
589 catch (err) {
590 if (err.code == "ENOENT") {
591 tslog("%s %s does not exist", description, filename);
592 }
593 else {
594 console.log(err.stack);
595 }
596 process.exit(1);
597 }
598 return contents;
599 }
600
581 /* Returns a list of the cookies in the request, obviously. */ 601 /* Returns a list of the cookies in the request, obviously. */
582 function getCookies(req) { 602 function getCookies(req) {
583 cookies = []; 603 cookies = [];
584 if ("cookie" in req.headers) { 604 if ("cookie" in req.headers) {
585 cookstrs = req.headers["cookie"].split("; "); 605 cookstrs = req.headers["cookie"].split("; ");
1235 fs.unlinkSync(rlgwebd_options.control_socket); 1255 fs.unlinkSync(rlgwebd_options.control_socket);
1236 } 1256 }
1237 1257
1238 var tls_options = {}; 1258 var tls_options = {};
1239 if (rlgwebd_options.use_https) { 1259 if (rlgwebd_options.use_https) {
1240 tls_options.key = fs.readFileSync(rlgwebd_options.keyfile), 1260 /* If the cert can't be found, don't fall back to insecure HTTP. */
1241 tls_options.cert = fs.readFileSync(rlgwebd_options.certfile), 1261 tls_options.key = read_or_die(rlgwebd_options.keyfile, "Keyfile");
1242 tls_options.ca = fs.readFileSync(rlgwebd_options.cafile) 1262 tls_options.cert = read_or_die(rlgwebd_options.certfile, "Certfile");
1263 if ("cafile" in rlgwebd_options)
1264 tls_options.ca = read_or_die(rlgwebd_options.cafile, "CA file");
1243 }; 1265 };
1244 1266
1245 /* Open the control socket before chrooting where it can't be found */ 1267 /* Open the control socket before chrooting where it can't be found */
1246 var ctlServer = net.createServer(function (sock) { 1268 var ctlServer = net.createServer(function (sock) {
1247 sock.on('data', consoleHandler); 1269 sock.on('data', consoleHandler);
1271 tslog('rlgwebd running on port %d', rlgwebd_options.http_port); 1293 tslog('rlgwebd running on port %d', rlgwebd_options.http_port);
1272 wsServer = new WebSocketServer({"httpServer": httpServer}); 1294 wsServer = new WebSocketServer({"httpServer": httpServer});
1273 wsServer.on("request", wsHandler); 1295 wsServer.on("request", wsHandler);
1274 tslog('WebSockets are online'); 1296 tslog('WebSockets are online');
1275 if (rlgwebd_options.use_https) { 1297 if (rlgwebd_options.use_https) {
1276 var httpsServer = https.createServer(tls_options, webHandler); 1298 var httpsServer = https.createServer(tls_options, webHandler);
1277 httpsServer.listen(rlgwebd_options.https_port); 1299 httpsServer.listen(rlgwebd_options.https_port);
1278 tslog('TLS running on port %d', rlgwebd_options.https_port); 1300 tslog('TLS running on port %d', rlgwebd_options.https_port);
1279 wssServer = new WebSocketServer({"httpServer": httpsServer}); 1301 var wssServer = new WebSocketServer({"httpServer": httpsServer});
1280 wssServer.on("request", wsHandler); 1302 wssServer.on("request", wsHandler);
1281 tslog('Secure WebSockets are online'); 1303 tslog('Secure WebSockets are online');
1282 } 1304 }
1283 progressWatcher = startProgressWatcher(); 1305 progressWatcher = startProgressWatcher();
1284 setInterval(pushStatus, 40000); 1306 setInterval(pushStatus, 40000);