comparison rlgwebd @ 212:e6af951def94

rlgwebd: use some newer Javascript features.
author John "Elwin" Edwards
date Fri, 03 Apr 2020 15:15:02 -0400
parents b04313038a0b
children 08665c56c6a0
comparison
equal deleted inserted replaced
211:d60063a674e1 212:e6af951def94
1 #!/usr/bin/env node 1 #!/usr/bin/env node
2 2
3 var http = require('http'); 3 const http = require('http');
4 var https = require('https'); 4 const https = require('https');
5 var net = require('net'); 5 const net = require('net');
6 var url = require('url'); 6 const url = require('url');
7 var path = require('path'); 7 const path = require('path');
8 var fs = require('fs'); 8 const fs = require('fs');
9 var events = require('events'); 9 const events = require('events');
10 var child_process = require('child_process'); 10 const child_process = require('child_process');
11 // Dependencies 11 // Dependencies
12 var posix = require("posix"); 12 const posix = require("posix");
13 var pty = require("node-pty"); 13 const pty = require("node-pty");
14 var WebSocketServer = require("websocket").server; 14 const WebSocketServer = require("websocket").server;
15
16 const errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
17 "Login failed", "Already playing", "Game launch failed",
18 "Server shutting down", "Game not in progress" ];
15 19
16 /* Default options */ 20 /* Default options */
17 var rlgwebd_options = { 21 var rlgwebd_options = {
18 control_socket: "/var/run/rlgwebd/rlgwebd.sock", 22 control_socket: "/var/run/rlgwebd/rlgwebd.sock",
19 port: 8080, 23 port: 8080,
21 username: "rodney", 25 username: "rodney",
22 static_root: "/var/www/" 26 static_root: "/var/www/"
23 }; 27 };
24 28
25 /* Read configuration from a file */ 29 /* Read configuration from a file */
26 var config_file = "/etc/rlgwebd.conf"; 30 const config_file = "/etc/rlgwebd.conf";
27 var config_lines = read_or_die(config_file, "Configuration file").toString().split('\n'); 31 var config_lines = read_or_die(config_file, "Configuration file").toString().split('\n');
28 32
29 for (var i = 0; i < config_lines.length; i++) { 33 for (let conf_line of config_lines) {
30 if (config_lines[i].length > 0 && config_lines[i][0] != '#') { 34 if (conf_line.length > 0 && conf_line[0] != '#') {
31 var config_fields = config_lines[i].split('='); 35 var config_fields = conf_line.split('=');
32 if (config_fields.length < 2) 36 if (config_fields.length < 2)
33 continue; 37 continue;
34 var option_name = config_fields[0].trim(); 38 var option_name = config_fields[0].trim();
35 // This can't handle values containing '=' or whitespace at the end 39 // This can't handle values containing '=' or whitespace at the end
36 var option_value = config_fields[1].trim(); 40 var option_value = config_fields[1].trim();
41 /* Should HTTPS be enabled? */ 45 /* Should HTTPS be enabled? */
42 if ("domain_name" in rlgwebd_options && "keyfile" in rlgwebd_options && 46 if ("domain_name" in rlgwebd_options && "keyfile" in rlgwebd_options &&
43 "certfile" in rlgwebd_options) 47 "certfile" in rlgwebd_options)
44 rlgwebd_options["use_https"] = true; 48 rlgwebd_options["use_https"] = true;
45 49
46 var clearbufs = [ 50 const clearbufs = [
47 Buffer.from([27, 91, 72, 27, 91, 50, 74]), // xterm: CSI H CSI 2J 51 Buffer.from([27, 91, 72, 27, 91, 50, 74]), // xterm: CSI H CSI 2J
48 Buffer.from([27, 91, 72, 27, 91, 74]) // screen: CSI H CSI J 52 Buffer.from([27, 91, 72, 27, 91, 74]) // screen: CSI H CSI J
49 ]; 53 ];
50 54
51 /* Data on the games available. */ 55 /* Data on the games available. */
52 var games = { 56 const games = {
53 "rogue3": { 57 "rogue3": {
54 "name": "Rogue V3", 58 "name": "Rogue V3",
55 "uname": "rogue3", 59 "uname": "rogue3",
56 "suffix": ".r3sav", 60 "suffix": ".r3sav",
57 "path": "/usr/bin/rogue3" 61 "path": "/usr/bin/rogue3"
395 this.rpos += n; 399 this.rpos += n;
396 this.reading = false; 400 this.reading = false;
397 /* Process the data */ 401 /* Process the data */
398 this.framepush(buf); 402 this.framepush(buf);
399 var wmsg = JSON.stringify({"t": "d", "d": buf.toString("hex")}); 403 var wmsg = JSON.stringify({"t": "d", "d": buf.toString("hex")});
400 for (var i = 0; i < this.watchers.length; i++) { 404 for (let watcher of this.watchers) {
401 if (this.watchers[i].connected) 405 if (watcher.connected)
402 this.watchers[i].sendUTF(wmsg); 406 watcher.sendUTF(wmsg);
403 } 407 }
404 this.emit("data", buf); 408 this.emit("data", buf);
405 /* Recurse. */ 409 /* Recurse. */
406 this.startchunk(); 410 this.startchunk();
407 }; 411 };
573 } 577 }
574 return true; 578 return true;
575 } 579 }
576 580
577 function isclear(buf) { 581 function isclear(buf) {
578 for (var i = 0; i < clearbufs.length; i++) { 582 for (let clearer of clearbufs) {
579 if (bufncmp(buf, clearbufs[i], clearbufs[i].length)) 583 if (bufncmp(buf, clearer, clearer.length))
580 return true; 584 return true;
581 } 585 }
582 return false; 586 return false;
583 } 587 }
584 588
845 subproc.stdout.resume(); 849 subproc.stdout.resume();
846 return subproc; 850 return subproc;
847 } 851 }
848 852
849 function serveStatic(req, res, fname) { 853 function serveStatic(req, res, fname) {
854 if (fname[0] !== "/")
855 fname = "/" + fname;
850 var nname = path.normalize(fname); 856 var nname = path.normalize(fname);
851 if (nname == "" || nname == "/") 857 if (nname == "" || nname == "/")
852 nname = "index.html"; 858 nname = "index.html";
853 if (nname.match(/\/$/)) 859 if (nname.match(/\/$/))
854 path.join(nname, "index.html"); /* it was a directory */ 860 path.join(nname, "index.html"); /* it was a directory */
1037 else { 1043 else {
1038 send404(res, urlobj.pathname, true); 1044 send404(res, urlobj.pathname, true);
1039 } 1045 }
1040 } 1046 }
1041 1047
1042 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
1043 "Login failed", "Already playing", "Game launch failed",
1044 "Server shutting down", "Game not in progress" ];
1045
1046 function sendError(res, ecode, msg, box) { 1048 function sendError(res, ecode, msg, box) {
1047 res.writeHead(200, { "Content-Type": "application/json" }); 1049 res.writeHead(200, { "Content-Type": "application/json" });
1048 var edict = {"t": "E"}; 1050 var edict = {"t": "E"};
1049 if (!(ecode < errorcodes.length && ecode > 0)) 1051 if (!(ecode < errorcodes.length && ecode > 0))
1050 ecode = 0; 1052 ecode = 0;