annotate rlgwebd @ 213:08665c56c6a0 default tip

Fix race condition related to watching DGL games. It's possible for a dgamelaunch game to end and cause rlgwebd to stop watching it before rlgwebd has started watching it.
author John "Elwin" Edwards
date Sun, 16 Aug 2020 20:56:18 -0400
parents e6af951def94
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1 #!/usr/bin/env node
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
2
212
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
3 const http = require('http');
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
4 const https = require('https');
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
5 const net = require('net');
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
6 const url = require('url');
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
7 const path = require('path');
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
8 const fs = require('fs');
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
9 const events = require('events');
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
10 const child_process = require('child_process');
141
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
11 // Dependencies
212
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
12 const posix = require("posix");
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
13 const pty = require("node-pty");
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
14 const WebSocketServer = require("websocket").server;
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
15
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
16 const errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
17 "Login failed", "Already playing", "Game launch failed",
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
18 "Server shutting down", "Game not in progress" ];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19
202
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
20 /* Default options */
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
21 var rlgwebd_options = {
208
f06f2d1a5035 Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents: 206
diff changeset
22 control_socket: "/var/run/rlgwebd/rlgwebd.sock",
204
a200b313870d Use either HTTP or HTTPS.
John "Elwin" Edwards
parents: 203
diff changeset
23 port: 8080,
202
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
24 chrootDir: "/var/dgl/",
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
25 username: "rodney",
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
26 static_root: "/var/www/"
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
27 };
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
28
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
29 /* Read configuration from a file */
212
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
30 const config_file = "/etc/rlgwebd.conf";
203
5491ca3a335b Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents: 202
diff changeset
31 var config_lines = read_or_die(config_file, "Configuration file").toString().split('\n');
5491ca3a335b Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents: 202
diff changeset
32
212
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
33 for (let conf_line of config_lines) {
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
34 if (conf_line.length > 0 && conf_line[0] != '#') {
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
35 var config_fields = conf_line.split('=');
202
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
36 if (config_fields.length < 2)
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
37 continue;
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
38 var option_name = config_fields[0].trim();
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
39 // This can't handle values containing '=' or whitespace at the end
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
40 var option_value = config_fields[1].trim();
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
41 rlgwebd_options[option_name] = option_value;
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
42 }
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
43 }
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
44
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
45 /* Should HTTPS be enabled? */
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
46 if ("domain_name" in rlgwebd_options && "keyfile" in rlgwebd_options &&
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
47 "certfile" in rlgwebd_options)
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
48 rlgwebd_options["use_https"] = true;
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
49
212
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
50 const clearbufs = [
210
b04313038a0b Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents: 208
diff changeset
51 Buffer.from([27, 91, 72, 27, 91, 50, 74]), // xterm: CSI H CSI 2J
b04313038a0b Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents: 208
diff changeset
52 Buffer.from([27, 91, 72, 27, 91, 74]) // screen: CSI H CSI J
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
53 ];
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
54
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
55 /* Data on the games available. */
212
e6af951def94 rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents: 210
diff changeset
56 const games = {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
57 "rogue3": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
58 "name": "Rogue V3",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
59 "uname": "rogue3",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
60 "suffix": ".r3sav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
61 "path": "/usr/bin/rogue3"
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
62 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
63 "rogue4": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
64 "name": "Rogue V4",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
65 "uname": "rogue4",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
66 "suffix": ".r4sav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
67 "path": "/usr/bin/rogue4"
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
68 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
69 "rogue5": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
70 "name": "Rogue V5",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
71 "uname": "rogue5",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
72 "suffix": ".r5sav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
73 "path": "/usr/bin/rogue5"
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
74 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
75 "srogue": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
76 "name": "Super-Rogue",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
77 "uname": "srogue",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
78 "suffix": ".srsav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
79 "path": "/usr/bin/srogue"
120
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
80 },
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
81 "arogue5": {
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
82 "name": "Advanced Rogue 5",
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
83 "uname": "arogue5",
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
84 "suffix": ".ar5sav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
85 "path": "/usr/bin/arogue5"
191
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 190
diff changeset
86 },