annotate rlgwebd @ 204:a200b313870d

Use either HTTP or HTTPS. If HTTPS is enabled, RLGWebD will not use insecure HTTP.
author John "Elwin" Edwards
date Sun, 08 Jan 2017 16:17:11 -0500
parents 5491ca3a335b
children 04c2a895b679
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
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3 var http = require('http');
201
f3843245a35e Initial support for TLS.
John "Elwin" Edwards
parents: 198
diff changeset
4 var https = require('https');
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
5 var net = require('net');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
6 var url = require('url');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 var path = require('path');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
8 var fs = require('fs');
55
96815eae4ebe RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents: 49
diff changeset
9 var events = require('events');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
10 var child_process = require('child_process');
141
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
11 // Dependencies
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
12 var posix = require("posix");
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
13 var pty = require("pty.js");
1a156a7746a7 RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents: 139
diff changeset
14 var WebSocketServer = require("websocket").server;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15
202
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
16 /* Default options */
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
17 var rlgwebd_options = {
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
18 control_socket: "/var/run/rlgwebd.sock",
204
a200b313870d Use either HTTP or HTTPS.
John "Elwin" Edwards
parents: 203
diff changeset
19 port: 8080,
202
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
20 chrootDir: "/var/dgl/",
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
21 username: "rodney",
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
22 static_root: "/var/www/"
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
23 };
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
24
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
25 /* Read configuration from a file */
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
26 var config_file = "/etc/rlgwebd.conf";
203
5491ca3a335b Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents: 202
diff changeset
27 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
28
202
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
29 for (var i = 0; i < config_lines.length; i++) {
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
30 if (config_lines[i].length > 0 && config_lines[i][0] != '#') {
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
31 var config_fields = config_lines[i].split('=');
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
32 if (config_fields.length < 2)
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
33 continue;
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
34 var option_name = config_fields[0].trim();
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
35 // 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
36 var option_value = config_fields[1].trim();
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
37 rlgwebd_options[option_name] = option_value;
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
38 }
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
39 }
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
40
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
41 /* Should HTTPS be enabled? */
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
42 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
43 "certfile" in rlgwebd_options)
7f25bb89b59c Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents: 201
diff changeset
44 rlgwebd_options["use_https"] = true;
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
45
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
46 var clearbufs = [
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
47 new Buffer([27, 91, 72, 27, 91, 50, 74]), // xterm: CSI H CSI 2J
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
48 new Buffer([27, 91, 72, 27, 91, 74]) // screen: CSI H CSI J
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
49 ];
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
50
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
51 /* Data on the games available. */
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
52 var games = {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
53 "rogue3": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
54 "name": "Rogue V3",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
55 "uname": "rogue3",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
56 "suffix": ".r3sav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
57 "path": "/usr/bin/rogue3"
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
58 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
59 "rogue4": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
60 "name": "Rogue V4",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
61 "uname": "rogue4",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
62 "suffix": ".r4sav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
63 "path": "/usr/bin/rogue4"
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
64 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
65 "rogue5": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
66 "name": "Rogue V5",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
67 "uname": "rogue5",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
68 "suffix": ".r5sav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
69 "path": "/usr/bin/rogue5"
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
70 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
71 "srogue": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
72 "name": "Super-Rogue",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
73 "uname": "srogue",
42
8f6bc0df58fa rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 40
diff changeset
74 "suffix": ".srsav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
75 "path": "/usr/bin/srogue"
120
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
76 },
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
77 "arogue5": {
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
78 "name": "Advanced Rogue 5",
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
79 "uname": "arogue5",
54979d35611a Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents: 113
diff changeset
80 "suffix": ".ar5sav",
180
85fde763c7ed Improve the clear-command recognition.
John "Elwin" Edwards
parents: 176
diff changeset
81 "path": "/usr/bin/arogue5"
191
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 190
diff changeset
82 },
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 190
diff changeset
83 "arogue7": {
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 190
diff changeset
84 "name": "Advanced Rogue 7",
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 190
diff changeset
85 "uname": "arogue7",
9758ca68e7f1 Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents: 190
diff changeset