Mercurial > hg > rlgwebd
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 |
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 | 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 }, |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
87 "arogue7": { |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
88 "name": "Advanced Rogue 7", |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
89 "uname": "arogue7", |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
90 "suffix": ".ar7sav", |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
91 "path": "/usr/bin/arogue7" |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
92 }, |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
93 "xrogue": { |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
94 "name": "XRogue", |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
95 "uname": "xrogue", |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
96 "suffix": ".xrsav", |
9758ca68e7f1
Add support for Advanced Rogue 7 and XRogue.
John "Elwin" Edwards
parents:
190
diff
changeset
|
97 "path": "/usr/bin/xrogue" |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
98 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
99 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
100 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
101 /* Global state */ |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
102 var logins = {}; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
103 var sessions = {}; |
156
127f9e256d02
Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents:
155
diff
changeset
|
104 var dglgames = {}; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
105 var allowlogin = true; |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
106 var gamemux = new events.EventEmitter(); |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
107 |
184
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
108 /* A base class. TermSession and DglSession inherit from it. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
109 function BaseGame() { |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
110 /* Games subclass EventEmitter, though there are few listeners. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
111 events.EventEmitter.call(this); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
112 /* Array of watching WebSockets. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
113 this.watchers = []; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
114 /* replaybuf holds the output since the last screen clear, so watchers can |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
115 * begin with a complete screen. replaylen is the number of bytes stored. */ |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
116 this.replaybuf = Buffer.alloc(1024); |
184
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
117 this.replaylen = 0; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
118 /* Time of last activity. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
119 this.lasttime = new Date(); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
120 } |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
121 BaseGame.prototype = new events.EventEmitter(); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
122 |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
123 BaseGame.prototype.tag = function () { |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
124 if (this.pname === undefined || this.gname === undefined) |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
125 return ""; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
126 return this.gname + "/" + this.pname; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
127 }; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
128 |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
129 BaseGame.prototype.framepush = function(chunk) { |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
130 /* If this chunk resets the screen, discard what preceded it. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
131 if (isclear(chunk)) { |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
132 this.replaybuf = Buffer.alloc(1024); |
184
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
133 this.replaylen = 0; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
134 } |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
135 /* Make sure there's space. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
136 while (this.replaybuf.length < chunk.length + this.replaylen) { |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
137 var nbuf = Buffer.alloc(this.replaybuf.length * 2); |
184
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
138 this.replaybuf.copy(nbuf, 0, 0, this.replaylen); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
139 this.replaybuf = nbuf; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
140 if (this.replaybuf.length > 65536) { |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
141 tslog("Warning: %s frame buffer at %d bytes", this.tag(), |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
142 this.replaybuf.length); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
143 } |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
144 } |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
145 chunk.copy(this.replaybuf, this.replaylen); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
146 this.replaylen += chunk.length; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
147 }; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
148 |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
149 /* Adds a watcher. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
150 BaseGame.prototype.attach = function (wsReq) { |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
151 var conn = wsReq.accept(null, wsReq.origin); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
152 conn.sendUTF(JSON.stringify({ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
153 "t": "w", "w": this.w, "h": this.h, "p": this.pname, "g": this.gname |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
154 })); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
155 conn.sendUTF(JSON.stringify({"t": "d", |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
156 "d": this.replaybuf.toString("hex", 0, this.replaylen)})); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
157 conn.on('close', this.detach.bind(this, conn)); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
158 this.watchers.push(conn); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
159 }; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
160 |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
161 BaseGame.prototype.detach = function (socket) { |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
162 var n = this.watchers.indexOf(socket); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
163 if (n >= 0) { |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
164 this.watchers.splice(n, 1); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
165 tslog("A WebSocket watcher has left game %s", this.tag()); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
166 } |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
167 }; |
181
926b0780bc44
Print metadata at the beginning of ttyrec files.
John "Elwin" Edwards
parents:
180
diff
changeset
|
168 |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
169 /* Constructor. A TermSession handles a pty and the game running on it. |
169
6f4b7e1b32e8
rlgwebd.js: clean up TermSession parameters.
John "Elwin" Edwards
parents:
168
diff
changeset
|
170 * gname: (String) Name of the game to launch. |
6f4b7e1b32e8
rlgwebd.js: clean up TermSession parameters.
John "Elwin" Edwards
parents:
168
diff
changeset
|
171 * pname: (String) The player's name. |
170 | 172 * wsReq: (WebSocketRequest) The request from the client. |
173 * | |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
174 * Events: |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
175 * "data": Data generated by child. Parameters: buf (Buffer) |
87
bd2cf6dda28d
RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
85
diff
changeset
|
176 * "exit": Child terminated. Parameters: none |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
177 */ |
170 | 178 function TermSession(gname, pname, wsReq) { |
184
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
179 BaseGame.call(this); |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
180 /* Don't launch anything that's not a real game. */ |
169
6f4b7e1b32e8
rlgwebd.js: clean up TermSession parameters.
John "Elwin" Edwards
parents:
168
diff
changeset
|
181 if (gname in games) { |
6f4b7e1b32e8
rlgwebd.js: clean up TermSession parameters.
John "Elwin" Edwards
parents:
168
diff
changeset
|
182 this.game = games[gname]; |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
183 this.gname = gname; |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
184 } |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
185 else { |
168 | 186 this.failed = true; |
170 | 187 wsReq.reject(404, errorcodes[2], "No such game"); |
188 tslog("Game %s is not available", game); | |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
189 return; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
190 } |
169
6f4b7e1b32e8
rlgwebd.js: clean up TermSession parameters.
John "Elwin" Edwards
parents:
168
diff
changeset
|
191 this.pname = pname; |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
192 /* Set up the sizes. */ |
170 | 193 this.w = Math.floor(Number(wsReq.resourceURL.query.w)); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
194 if (!(this.w > 0 && this.w < 256)) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
195 this.w = 80; |
170 | 196 this.h = Math.floor(Number(wsReq.resourceURL.query.h)); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
197 if (!(this.h > 0 && this.h < 256)) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
198 this.h = 24; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
199 /* Environment. */ |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
200 var childenv = {}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
201 for (var key in process.env) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
202 childenv[key] = process.env[key]; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
203 } |
87
bd2cf6dda28d
RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
85
diff
changeset
|
204 var args = ["-n", this.pname]; |
bd2cf6dda28d
RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
85
diff
changeset
|
205 var spawnopts = {"env": childenv, "cwd": "/", "rows": this.h, "cols": this.w, |
bd2cf6dda28d
RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
85
diff
changeset
|
206 "name": "xterm-256color"}; |
bd2cf6dda28d
RLG-Web: use the pty module.
John "Elwin" Edwards <elwin@sdf.org>
parents:
85
diff
changeset
|
207 this.term = pty.spawn(this.game.path, args, spawnopts); |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
208 tslog("%s playing %s (pid %d)", this.pname, this.gname, this.term.pid); |
168 | 209 this.failed = false; |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
210 sessions[this.gname + "/" + this.pname] = this; |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
211 gamemux.emit('begin', this.gname, this.pname, 'rlg'); |
40
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
212 /* Set up the lockfile and ttyrec */ |
160
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
213 var ts = timestamp(this.lasttime); |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
214 var progressdir = path.join("/dgldir/inprogress", this.gname); |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
215 this.lock = path.join(progressdir, this.pname + ":node:" + ts + ".ttyrec"); |
110
18a81cc0084b
RLG-Web: fix lockfile mixup.
John "Elwin" Edwards <elwin@sdf.org>
parents:
109
diff
changeset
|
216 var lmsg = this.term.pid.toString() + '\n' + this.h + '\n' + this.w + '\n'; |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
217 fs.writeFile(this.lock, lmsg, "utf8", function (err) { |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
218 if (err) tslog("Locking failed: %s", err); |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
219 }); |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
220 var ttyrec = path.join("/dgldir/ttyrec", this.pname, this.gname, |
40
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
221 ts + ".ttyrec"); |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
222 this.record = fs.createWriteStream(ttyrec, { mode: 0664 }); |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
223 /* The player's WebSocket and its handlers. */ |
170 | 224 this.playerconn = wsReq.accept(null, wsReq.origin); |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
225 this.playerconn.on('message', this.input_msg.bind(this)); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
226 this.playerconn.on('close', this.close.bind(this)); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
227 /* Send initial data. */ |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
228 this.playerconn.sendUTF(JSON.stringify({"t": "s", "w": this.w, "h": this.h, |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
229 "p": this.pname, "g": this.gname})); |
181
926b0780bc44
Print metadata at the beginning of ttyrec files.
John "Elwin" Edwards
parents:
180
diff
changeset
|
230 /* Begin the ttyrec with some metadata, like dgamelaunch does. */ |
926b0780bc44
Print metadata at the beginning of ttyrec files.
John "Elwin" Edwards
parents:
180
diff
changeset
|
231 var descstr = "\x1b[2J\x1b[1;1H\r\n"; |
926b0780bc44
Print metadata at the beginning of ttyrec files.
John "Elwin" Edwards
parents:
180
diff
changeset
|
232 descstr += "Player: " + this.pname + "\r\nGame: " + this.game.name + "\r\n"; |
926b0780bc44
Print metadata at the beginning of ttyrec files.
John "Elwin" Edwards
parents:
180
diff
changeset
|
233 descstr += "Server: Roguelike Gallery - rlgallery.org\r\n"; |
926b0780bc44
Print metadata at the beginning of ttyrec files.
John "Elwin" Edwards
parents:
180
diff
changeset
|
234 descstr += "Filename: " + ts + ".ttyrec\r\n"; |
926b0780bc44
Print metadata at the beginning of ttyrec files.
John "Elwin" Edwards
parents:
180
diff
changeset
|
235 descstr += "Time: (" + Math.floor(this.lasttime.getTime() / 1000) + ") "; |
926b0780bc44
Print metadata at the beginning of ttyrec files.
John "Elwin" Edwards
parents:
180
diff
changeset
|
236 descstr += this.lasttime.toUTCString().slice(0, -4) + "\r\n"; |
926b0780bc44
Print metadata at the beginning of ttyrec files.
John "Elwin" Edwards
parents:
180
diff
changeset
|
237 descstr += "Size: " + this.w + "x" + this.h + "\r\n\x1b[2J"; |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
238 this.write_ttyrec(descstr); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
239 this.term.on("data", this.write_ttyrec.bind(this)); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
240 this.term.on("exit", this.destroy.bind(this)); |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
241 } |
184
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
242 TermSession.prototype = new BaseGame(); |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
243 |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
244 /* Currently this also sends to the player and any watchers. */ |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
245 TermSession.prototype.write_ttyrec = function (datastr) { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
246 this.lasttime = new Date(); |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
247 var buf = Buffer.from(datastr); |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
248 var chunk = Buffer.alloc(buf.length + 12); |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
249 /* TTYREC headers */ |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
250 chunk.writeUInt32LE(Math.floor(this.lasttime.getTime() / 1000), 0); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
251 chunk.writeUInt32LE(1000 * (this.lasttime.getTime() % 1000), 4); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
252 chunk.writeUInt32LE(buf.length, 8); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
253 buf.copy(chunk, 12); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
254 this.record.write(chunk); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
255 this.framepush(buf); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
256 /* Send to the player. */ |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
257 var msg = JSON.stringify({"t": "d", "d": buf.toString("hex")}); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
258 this.playerconn.sendUTF(msg); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
259 /* Send to any watchers. */ |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
260 for (var i = 0; i < this.watchers.length; i++) { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
261 if (this.watchers[i].connected) |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
262 this.watchers[i].sendUTF(msg); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
263 } |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
264 this.emit('data', buf); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
265 }; |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
266 |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
267 /* For writing to the subprocess's stdin. */ |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
268 TermSession.prototype.write = function (data) { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
269 this.term.write(data); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
270 }; |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
271 |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
272 TermSession.prototype.input_msg = function (message) { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
273 var parsedMsg = getMsgWS(message); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
274 if (parsedMsg.t == 'q') { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
275 this.close(); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
276 } |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
277 else if (parsedMsg.t == 'd') { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
278 var hexstr = parsedMsg.d.replace(/[^0-9a-f]/gi, ""); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
279 if (hexstr.length % 2 != 0) { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
280 hexstr = hexstr.slice(0, -1); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
281 } |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
282 var keybuf = Buffer.from(hexstr, "hex"); |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
283 this.write(keybuf); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
284 } |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
285 }; |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
286 |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
287 /* Teardown. */ |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
288 TermSession.prototype.close = function () { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
289 if (this.tag() in sessions) |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
290 this.term.kill('SIGHUP'); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
291 }; |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
292 |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
293 TermSession.prototype.destroy = function () { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
294 var tag = this.tag(); |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
295 fs.unlink(this.lock, function (err) { |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
296 if (err) tslog("Lock removal failed: %s", err); |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
297 }); |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
298 this.record.end(); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
299 var watchsocks = this.watchers; |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
300 this.watchers = []; |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
301 for (var i = 0; i < watchsocks.length; i++) { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
302 if (watchsocks[i].connected) |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
303 watchsocks[i].close(); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
304 } |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
305 if (this.playerconn.connected) { |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
306 this.playerconn.sendUTF(JSON.stringify({"t": "q"})); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
307 this.playerconn.close(); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
308 } |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
309 this.emit('exit'); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
310 gamemux.emit('end', this.gname, this.pname); |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
311 delete sessions[tag]; |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
312 tslog("Game %s ended.", tag); |
198
ea28353d620a
Make sure games have saved and exited before stopping the server.
John "Elwin" Edwards
parents:
195
diff
changeset
|
313 /* Was that the last game? */ |
ea28353d620a
Make sure games have saved and exited before stopping the server.
John "Elwin" Edwards
parents:
195
diff
changeset
|
314 if (!allowlogin && Object.keys(sessions).length == 0) { |
ea28353d620a
Make sure games have saved and exited before stopping the server.
John "Elwin" Edwards
parents:
195
diff
changeset
|
315 shutdown(); |
ea28353d620a
Make sure games have saved and exited before stopping the server.
John "Elwin" Edwards
parents:
195
diff
changeset
|
316 } |
183
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
317 }; |
db2f5ab112e9
Move all TermSession methods into the prototype.
John "Elwin" Edwards
parents:
182
diff
changeset
|
318 |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
319 function DglSession(filename) { |
184
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
320 BaseGame.call(this); |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
321 var pathcoms = filename.split('/'); |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
322 this.gname = pathcoms[pathcoms.length - 2]; |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
323 if (!(this.gname in games)) { |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
324 this.emit('open', false); |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
325 return; |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
326 } |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
327 var basename = pathcoms[pathcoms.length - 1]; |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
328 var firstsep = basename.indexOf(':'); |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
329 this.pname = basename.slice(0, firstsep); |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
330 var fname = basename.slice(firstsep + 1); |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
331 this.ttyrec = path.join("/dgldir/ttyrec", this.pname, this.gname, fname); |
165
59e62710cbb5
rlgwebd.js: prevent races when reading ttyrecs.
John "Elwin" Edwards
parents:
164
diff
changeset
|
332 /* Flag to prevent multiple handlers from reading simultaneously and |
59e62710cbb5
rlgwebd.js: prevent races when reading ttyrecs.
John "Elwin" Edwards
parents:
164
diff
changeset
|
333 * getting into a race. */ |
59e62710cbb5
rlgwebd.js: prevent races when reading ttyrecs.
John "Elwin" Edwards
parents:
164
diff
changeset
|
334 this.reading = false; |
174
dc12ba30d559
Fix further crashes when following dgamelaunch games.
John "Elwin" Edwards
parents:
171
diff
changeset
|
335 this.rpos = 0; |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
336 fs.readFile(filename, {encoding: "utf8"}, (function (err, data) { |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
337 if (err) { |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
338 this.emit('open', false); |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
339 return; |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
340 } |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
341 var lines = data.split('\n'); |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
342 this.h = Number(lines[1]); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
343 this.w = Number(lines[2]); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
344 fs.open(this.ttyrec, "r", (function (err, fd) { |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
345 if (err) { |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
346 this.emit('open', false); |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
347 } |
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
348 else { |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
349 this.fd = fd; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
350 this.emit('open', true); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
351 tslog("DGL %s: open", this.tag()); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
352 gamemux.emit('begin', this.gname, this.pname, 'dgl'); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
353 this.startchunk(); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
354 this.recwatcher = fs.watch(this.ttyrec, this.notifier.bind(this)); |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
355 } |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
356 }).bind(this)); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
357 }).bind(this)); |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
358 } |
184
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
359 DglSession.prototype = new BaseGame(); |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
360 |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
361 /* 3 functions to get data from the ttyrec file. */ |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
362 DglSession.prototype.startchunk = function () { |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
363 if (this.reading) |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
364 return; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
365 this.reading = true; |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
366 var header = Buffer.alloc(12); |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
367 fs.read(this.fd, header, 0, 12, this.rpos, this.datachunk.bind(this)); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
368 }; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
369 |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
370 DglSession.prototype.datachunk = function (err, n, buf) { |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
371 /* Stop recursion if end of file has been reached. */ |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
372 if (err || n < 12) { |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
373 if (!err && n > 0) { |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
374 tslog("DGL %s: expected 12-byte header, got %d", this.tag(), n); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
375 } |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
376 this.reading = false; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
377 return; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
378 } |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
379 this.rpos += 12; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
380 /* Update timestamp, to within 1 second. */ |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
381 this.lasttime = new Date(1000 * buf.readUInt32LE(0)); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
382 var datalen = buf.readUInt32LE(8); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
383 if (datalen > 16384) { |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
384 // Something is probably wrong... |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
385 tslog("DGL %s: looking for %d bytes", this.tag(), datalen); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
386 } |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
387 var databuf = Buffer.alloc(datalen); |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
388 fs.read(this.fd, databuf, 0, datalen, this.rpos, this.handledata.bind(this)); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
389 }; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
390 |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
391 DglSession.prototype.handledata = function (err, n, buf) { |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
392 if (err || n < buf.length) { |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
393 /* Next time, read the header again. */ |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
394 this.rpos -= 12; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
395 this.reading = false; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
396 tslog("DGL %s: expected %d bytes, got %d", this.tag(), buf.length, n); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
397 return; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
398 } |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
399 this.rpos += n; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
400 this.reading = false; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
401 /* Process the data */ |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
402 this.framepush(buf); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
403 var wmsg = JSON.stringify({"t": "d", "d": buf.toString("hex")}); |
212
e6af951def94
rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents:
210
diff
changeset
|
404 for (let watcher of this.watchers) { |
e6af951def94
rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents:
210
diff
changeset
|
405 if (watcher.connected) |
e6af951def94
rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents:
210
diff
changeset
|
406 watcher.sendUTF(wmsg); |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
407 } |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
408 this.emit("data", buf); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
409 /* Recurse. */ |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
410 this.startchunk(); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
411 }; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
412 |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
413 /* Handles events from the ttyrec file watcher. */ |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
414 DglSession.prototype.notifier = function (ev, finame) { |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
415 if (ev == "change") |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
416 this.startchunk(); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
417 /* If another kind of event appears, something strange happened. */ |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
418 }; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
419 |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
420 DglSession.prototype.close = function () { |
213
08665c56c6a0
Fix race condition related to watching DGL games.
John "Elwin" Edwards
parents:
212
diff
changeset
|
421 /* The watcher might not be open yet. */ |
08665c56c6a0
Fix race condition related to watching DGL games.
John "Elwin" Edwards
parents:
212
diff
changeset
|
422 if ("recwatcher" in this) |
08665c56c6a0
Fix race condition related to watching DGL games.
John "Elwin" Edwards
parents:
212
diff
changeset
|
423 this.recwatcher.close(); |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
424 /* Ensure all data is handled before quitting. */ |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
425 this.startchunk(); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
426 var connlist = this.watchers; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
427 this.watchers = []; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
428 for (var i = 0; i < connlist.length; i++) { |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
429 if (connlist[i].connected) |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
430 connlist[i].close(); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
431 } |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
432 fs.close(this.fd, function (err) { |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
433 if (err) tslog("PTY close failed: %s", err); |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
434 }); |
185
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
435 this.emit("close"); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
436 gamemux.emit('end', this.gname, this.pname); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
437 tslog("DGL %s: closed", this.tag()); |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
438 }; |
bbfda4a4eb7f
Finish moving DglSession methods into the prototype.
John "Elwin" Edwards
parents:
184
diff
changeset
|
439 |
170 | 440 function wsStartGame(wsReq) { |
107
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
441 var playmatch = wsReq.resourceURL.pathname.match(/^\/play\/([^\/]*)$/); |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
442 if (!playmatch[1] || !(playmatch[1] in games)) { |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
443 wsReq.reject(404, errorcodes[2]); |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
444 return; |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
445 } |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
446 var gname = playmatch[1]; |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
447 if (!allowlogin) { |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
448 wsReq.reject(404, errorcodes[6]); |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
449 return; |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
450 } |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
451 if (!("key" in wsReq.resourceURL.query)) { |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
452 wsReq.reject(404, "No key given."); |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
453 return; |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
454 } |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
455 var lkey = wsReq.resourceURL.query["key"]; |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
456 if (!(lkey in logins)) { |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
457 wsReq.reject(404, errorcodes[1]); |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
458 return; |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
459 } |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
460 var pname = logins[lkey].name; |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
461 function progcallback(err, fname) { |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
462 if (fname) { |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
463 wsReq.reject(404, errorcodes[4]); |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
464 tslog("%s is already playing %s", pname, gname); |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
465 } |
170 | 466 else { |
467 new TermSession(gname, pname, wsReq); | |
468 } | |
107
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
469 }; |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
470 checkprogress(pname, games[gname], progcallback, []); |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
471 } |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
472 |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
473 /* Some functions which check whether a player is currently playing or |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
474 * has a saved game. Maybe someday they will provide information on |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
475 * the game. */ |
40
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
476 function checkprogress(user, game, callback, args) { |
142 | 477 var progressdir = path.join("/dgldir/inprogress", game.uname); |
40
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
478 fs.readdir(progressdir, function(err, files) { |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
479 if (err) { |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
480 args.unshift(err, null); |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
481 callback.apply(null, args); |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
482 return; |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
483 } |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
484 var fre = RegExp("^" + user + ":"); |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
485 for (var i = 0; i < files.length; i++) { |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
486 if (files[i].match(fre)) { |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
487 args.unshift(null, files[i]); |
40
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
488 callback.apply(null, args); |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
489 return; |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
490 } |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
491 } |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
492 args.unshift(null, false); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
493 callback.apply(null, args); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
494 }); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
495 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
496 |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
497 function checksaved(user, game, callback, args) { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
498 var savedirc = game.uname + "save"; |
157
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
499 var basename = String(pwent.uid) + "-" + user + game.suffix; |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
500 var savefile = path.join("/var/games/roguelike", savedirc, basename); |
206
04c2a895b679
RLGWebD: replace deprecated fs.exists() with fs.access().
John "Elwin" Edwards
parents:
204
diff
changeset
|
501 fs.access(savefile, function (err) { |
04c2a895b679
RLGWebD: replace deprecated fs.exists() with fs.access().
John "Elwin" Edwards
parents:
204
diff
changeset
|
502 if (err) |
04c2a895b679
RLGWebD: replace deprecated fs.exists() with fs.access().
John "Elwin" Edwards
parents:
204
diff
changeset
|
503 args.unshift(false); |
04c2a895b679
RLGWebD: replace deprecated fs.exists() with fs.access().
John "Elwin" Edwards
parents:
204
diff
changeset
|
504 else |
04c2a895b679
RLGWebD: replace deprecated fs.exists() with fs.access().
John "Elwin" Edwards
parents:
204
diff
changeset
|
505 args.unshift(true); |
40
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
506 callback.apply(null, args); |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
507 }); |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
508 } |
f7116eb3f791
rlgwebd.js: refactor some game-starting code.
John "Elwin" Edwards <elwin@sdf.org>
parents:
39
diff
changeset
|
509 |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
510 function playerstatus(user, callback) { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
511 var sdata = {}; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
512 function finishp() { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
513 for (var gname in games) { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
514 if (!(gname in sdata)) |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
515 return; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
516 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
517 callback(sdata); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
518 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
519 function regsaved(exists, game) { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
520 if (exists) |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
521 sdata[game.uname] = "s"; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
522 else |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
523 sdata[game.uname] = "0"; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
524 finishp(); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
525 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
526 function regactive(err, filename, game) { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
527 if (!err && filename) { |
88
d644e7d46852
RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents:
87
diff
changeset
|
528 if (filename.match(/^[^:]*:node:/)) |
d644e7d46852
RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents:
87
diff
changeset
|
529 sdata[game.uname] = "p"; |
d644e7d46852
RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents:
87
diff
changeset
|
530 else |
d644e7d46852
RLG-Web: make /pstatus/* differentiate between dgl and RLG-Web games.
John "Elwin" Edwards <elwin@sdf.org>
parents:
87
diff
changeset
|
531 sdata[game.uname] = "d"; |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
532 finishp(); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
533 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
534 else |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
535 checksaved(user, game, regsaved, [game]); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
536 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
537 for (var gname in games) { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
538 checkprogress(user, games[gname], regactive, [games[gname]]); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
539 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
540 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
541 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
542 /* A few utility functions */ |
160
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
543 function timestamp(dd) { |
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
544 if (!(dd instanceof Date)) { |
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
545 dd = new Date(); |
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
546 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
547 sd = dd.toISOString(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
548 sd = sd.slice(0, sd.indexOf(".")); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
549 return sd.replace("T", "."); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
550 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
551 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
552 function randkey(words) { |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
553 if (!words || !(words > 0)) |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
554 words = 1; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
555 function rand32() { |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
556 rnum = Math.floor(Math.random() * 65536 * 65536); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
557 hexstr = rnum.toString(16); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
558 while (hexstr.length < 8) |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
559 hexstr = "0" + hexstr; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
560 return hexstr; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
561 } |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
562 var key = ""; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
563 for (var i = 0; i < words; i++) |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
564 key += rand32(); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
565 return key; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
566 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
567 |
60
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
568 /* Compares two buffers, returns true for equality up to index n */ |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
569 function bufncmp(buf1, buf2, n) { |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
570 if (!Buffer.isBuffer(buf1) || !Buffer.isBuffer(buf2)) |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
571 return false; |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
572 for (var i = 0; i < n; i++) { |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
573 if (i == buf1.length && i == buf2.length) |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
574 return true; |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
575 if (i == buf1.length || i == buf2.length) |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
576 return false; |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
577 if (buf1[i] != buf2[i]) |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
578 return false; |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
579 } |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
580 return true; |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
581 } |
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
582 |
180
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
583 function isclear(buf) { |
212
e6af951def94
rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents:
210
diff
changeset
|
584 for (let clearer of clearbufs) { |
e6af951def94
rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents:
210
diff
changeset
|
585 if (bufncmp(buf, clearer, clearer.length)) |
180
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
586 return true; |
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
587 } |
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
588 return false; |
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
589 } |
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
590 |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
591 function tslog() { |
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
592 arguments[0] = new Date().toISOString() + ": " + String(arguments[0]); |
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
593 console.log.apply(console, arguments); |
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
594 } |
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
595 |
203
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
596 // Read a file synchronously, exiting if anything goes wrong. |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
597 // Intended only for files required at startup. |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
598 function read_or_die(filename, description) { |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
599 var contents; |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
600 try { |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
601 contents = fs.readFileSync(filename); |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
602 } |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
603 catch (err) { |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
604 if (err.code == "ENOENT") { |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
605 tslog("%s %s does not exist", description, filename); |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
606 } |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
607 else { |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
608 console.log(err.stack); |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
609 } |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
610 process.exit(1); |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
611 } |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
612 return contents; |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
613 } |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
614 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
615 /* Returns a list of the cookies in the request, obviously. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
616 function getCookies(req) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
617 cookies = []; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
618 if ("cookie" in req.headers) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
619 cookstrs = req.headers["cookie"].split("; "); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
620 for (var i = 0; i < cookstrs.length; i++) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
621 eqsign = cookstrs[i].indexOf("="); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
622 if (eqsign > 0) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
623 name = cookstrs[i].slice(0, eqsign).toLowerCase(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
624 val = cookstrs[i].slice(eqsign + 1); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
625 cookies[name] = val; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
626 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
627 else if (eqsign < 0) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
628 cookies[cookstrs[i]] = null; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
629 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
630 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
631 return cookies; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
632 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
633 |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
634 function getMsg(posttext) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
635 var jsonobj; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
636 if (!posttext) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
637 return {}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
638 try { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
639 jsonobj = JSON.parse(posttext); |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
640 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
641 catch (e) { |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
642 if (e instanceof SyntaxError) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
643 return {}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
644 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
645 if (typeof(jsonobj) != "object") |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
646 return {}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
647 return jsonobj; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
648 } |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
649 |
107
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
650 function getMsgWS(msgObj) { |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
651 if (msgObj.type != "utf8") |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
652 return {}; |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
653 return getMsg(msgObj.utf8Data); |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
654 } |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
655 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
656 function login(req, res, formdata) { |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
657 if (!allowlogin) { |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
658 sendError(res, 6, null, false); |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
659 return; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
660 } |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
661 if (!("name" in formdata)) { |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
662 sendError(res, 2, "Username not given.", false); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
663 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
664 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
665 else if (!("pw" in formdata)) { |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
666 sendError(res, 2, "Password not given.", false); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
667 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
668 } |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
669 var username = String(formdata["name"]); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
670 var password = String(formdata["pw"]); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
671 function checkit(code, signal) { |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
672 /* Checks the exit status, see sqlickrypt.c for details. */ |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
673 if (code != 0) { |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
674 sendError(res, 3); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
675 if (code == 1) |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
676 tslog("Password check failed for user %s", username); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
677 else if (code == 2) |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
678 tslog("Attempted login by nonexistent user %s", username); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
679 else |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
680 tslog("Login failed: sqlickrypt error %d", code); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
681 return; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
682 } |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
683 var lkey = randkey(2); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
684 while (lkey in logins) |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
685 lkey = randkey(2); |
160
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
686 logins[lkey] = {"name": username, "ts": new Date()}; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
687 res.writeHead(200, {'Content-Type': 'application/json'}); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
688 var reply = {"t": "l", "k": lkey, "u": username}; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
689 res.write(JSON.stringify(reply)); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
690 res.end(); |
190
11b7baed2e21
Stop printing credentials in the logfile.
John "Elwin" Edwards
parents:
185
diff
changeset
|
691 tslog("%s has logged in", username); |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
692 return; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
693 } |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
694 /* Launch the sqlickrypt utility to check the password. */ |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
695 var pwchecker = child_process.spawn("/bin/sqlickrypt", ["check"]); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
696 pwchecker.on("exit", checkit); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
697 pwchecker.stdin.end(username + '\n' + password + '\n', "utf8"); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
698 return; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
699 } |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
700 |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
701 /* Sets things up for a new user, like dgamelaunch's commands[register] */ |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
702 function regsetup(username) { |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
703 function regsetup_l2(err) { |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
704 for (var g in games) { |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
705 fs.mkdir(path.join("/dgldir/ttyrec", username, games[g].uname), 0755, |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
706 function (err) { |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
707 if (err) tslog("ttyrec mkdir failed: %s", err); |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
708 }); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
709 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
710 } |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
711 fs.mkdir(path.join("/dgldir/userdata", username), 0755, function (err) { |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
712 if (err) tslog("Userdata mkdir failed: %s", err); |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
713 }); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
714 fs.mkdir(path.join("/dgldir/ttyrec/", username), 0755, regsetup_l2); |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
715 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
716 |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
717 function register(req, res, formdata) { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
718 var uname, passwd, email; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
719 if (typeof (formdata.name) != "string" || formdata.name === "") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
720 sendError(res, 2, "No name given."); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
721 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
722 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
723 else |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
724 uname = formdata["name"]; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
725 if (typeof (formdata.pw) != "string" || formdata.pw === "") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
726 sendError(res, 2, "No password given."); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
727 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
728 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
729 else |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
730 passwd = formdata["pw"]; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
731 if (typeof (formdata.email) != "string" || formdata.email === "") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
732 /* E-mail is optional */ |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
733 email = "nobody@nowhere.not"; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
734 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
735 else |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
736 email = formdata["email"]; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
737 function checkreg(code, signal) { |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
738 if (code === 0) { |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
739 var lkey = randkey(2); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
740 while (lkey in logins) |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
741 lkey = randkey(2); |
160
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
742 logins[lkey] = {"name": uname, "ts": new Date()}; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
743 var reply = {"t": "r", "k": lkey, "u": uname}; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
744 res.writeHead(200, {'Content-Type': 'application/json'}); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
745 res.write(JSON.stringify(reply)); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
746 res.end(); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
747 tslog("Added new user: %s", uname); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
748 regsetup(uname); |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
749 } |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
750 else if (code == 4) { |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
751 sendError(res, 2, "Invalid characters in name or email."); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
752 tslog("Attempted registration: %s %s", uname, email); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
753 } |
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
754 else if (code == 1) { |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
755 sendError(res, 2, "Username " + uname + " is already being used."); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
756 tslog("Attempted duplicate registration: %s", uname); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
757 } |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
758 else { |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
759 sendError(res, 0, null); |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
760 tslog("sqlickrypt register failed with code %d", code); |
20
5f785e1d5cca
RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
19
diff
changeset
|
761 } |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
762 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
763 var child_adder = child_process.spawn("/bin/sqlickrypt", ["register"]); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
764 child_adder.on("exit", checkreg); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
765 child_adder.stdin.end(uname + '\n' + passwd + '\n' + email + '\n', "utf8"); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
766 return; |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
767 } |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
768 |
111
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
769 /* Stops a running game if the request has the proper key. */ |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
770 function stopgame(res, formdata) { |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
771 if (!("key" in formdata) || !(formdata["key"] in logins)) { |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
772 sendError(res, 1); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
773 return; |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
774 } |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
775 var pname = logins[formdata["key"]].name; |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
776 if (!("g" in formdata) || !(formdata["g"] in games)) { |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
777 sendError(res, 2, "No such game."); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
778 return; |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
779 } |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
780 var gname = formdata["g"]; |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
781 function checkback(err, fname) { |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
782 if (!fname) { |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
783 sendError(res, 7); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
784 return; |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
785 } |
142 | 786 var fullfile = path.join("/dgldir/inprogress", gname, fname); |
111
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
787 fs.readFile(fullfile, "utf8", function(err, fdata) { |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
788 if (err) { |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
789 sendError(res, 7); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
790 return; |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
791 } |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
792 var pid = parseInt(fdata.split('\n')[0], 10); |
139 | 793 try { |
794 process.kill(pid, 'SIGHUP'); | |
795 } | |
796 catch (err) { | |
797 /* If the PID is invalid, the lockfile is stale. */ | |
798 if (err.code == "ESRCH") { | |
799 var nodere = RegExp("^" + pname + ":node:"); | |
800 if (fname.match(nodere)) { | |
210
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
801 fs.unlink(fullfile, function (err) { |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
802 if (err) tslog("Stale lock removal failed: %s", err); |
b04313038a0b
Changes for compatibility with recent versions of NodeJS.
John "Elwin" Edwards
parents:
208
diff
changeset
|
803 }); |
139 | 804 } |
805 } | |
806 } | |
111
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
807 /* The response doesn't mean that the game is gone. The only way |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
808 * to make sure a dgamelaunch-supervised game is over would be to |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
809 * poll fname until it disappears. */ |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
810 res.writeHead(200, {'Content-Type': 'application/json'}); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
811 res.write(JSON.stringify({"t": "q"})); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
812 res.end(); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
813 }); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
814 } |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
815 checkprogress(pname, games[gname], checkback, []); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
816 } |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
817 |
155
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
818 function startProgressWatcher() { |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
819 var watchdirs = []; |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
820 for (var gname in games) { |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
821 watchdirs.push(path.join("/dgldir/inprogress", gname)); |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
822 } |
167 | 823 var subproc = child_process.spawn("/bin/dglwatcher", watchdirs); |
155
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
824 subproc.stdout.setEncoding('utf8'); |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
825 subproc.stdout.on('data', function (chunk) { |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
826 var fname = chunk.slice(2, -1); |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
827 var filere = /.*\/([^\/]*)\/([^\/:]*):(node:)?(.*)/; |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
828 var matchresult = fname.match(filere); |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
829 if (!matchresult || matchresult[3]) |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
830 return; |
156
127f9e256d02
Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents:
155
diff
changeset
|
831 var gname = matchresult[1]; |
127f9e256d02
Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents:
155
diff
changeset
|
832 var pname = matchresult[2]; |
127f9e256d02
Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents:
155
diff
changeset
|
833 var tag = gname + "/" + pname; |
155
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
834 if (chunk[0] == "E") { |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
835 tslog("DGL: %s is playing %s: %s", pname, gname, fname) |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
836 dglgames[tag] = new DglSession(fname); |
155
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
837 } |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
838 else if (chunk[0] == "C") { |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
839 tslog("DGL: %s started playing %s: %s", pname, gname, fname) |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
840 dglgames[tag] = new DglSession(fname); |
155
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
841 } |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
842 else if (chunk[0] == "D") { |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
843 tslog("DGL: %s finished playing %s: %s", pname, gname, fname) |
164
3a97e4ee50f0
rlgwebd.js: read ttyrecs created by dgamelaunch.
John "Elwin" Edwards
parents:
163
diff
changeset
|
844 dglgames[tag].close(); |
156
127f9e256d02
Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents:
155
diff
changeset
|
845 delete dglgames[tag]; |
155
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
846 } |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
847 else { |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
848 tslog("Watcher says: %s", chunk) |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
849 } |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
850 }); |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
851 subproc.stdout.resume(); |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
852 return subproc; |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
853 } |
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
854 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
855 function serveStatic(req, res, fname) { |
212
e6af951def94
rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents:
210
diff
changeset
|
856 if (fname[0] !== "/") |
e6af951def94
rlgwebd: use some newer Javascript features.
John "Elwin" Edwards
parents:
210
diff
changeset
|
857 fname = "/" + fname; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
858 var nname = path.normalize(fname); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
859 if (nname == "" || nname == "/") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
860 nname = "index.html"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
861 if (nname.match(/\/$/)) |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
862 path.join(nname, "index.html"); /* it was a directory */ |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
863 var realname = path.join(rlgwebd_options.static_root, nname); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
864 var extension = path.extname(realname); |
206
04c2a895b679
RLGWebD: replace deprecated fs.exists() with fs.access().
John "Elwin" Edwards
parents:
204
diff
changeset
|
865 fs.access(realname, function (access_err) { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
866 var resheaders = {}; |
206
04c2a895b679
RLGWebD: replace deprecated fs.exists() with fs.access().
John "Elwin" Edwards
parents:
204
diff
changeset
|
867 if (access_err || !extension || extension == ".html") |
31
7dd6becf9ce9
rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents:
30
diff
changeset
|
868 resheaders["Content-Type"] = "text/html; charset=utf-8"; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
869 else if (extension == ".png") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
870 resheaders["Content-Type"] = "image/png"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
871 else if (extension == ".css") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
872 resheaders["Content-Type"] = "text/css"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
873 else if (extension == ".js") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
874 resheaders["Content-Type"] = "text/javascript"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
875 else if (extension == ".svg") |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
876 resheaders["Content-Type"] = "image/svg+xml"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
877 else |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
878 resheaders["Content-Type"] = "application/octet-stream"; |
206
04c2a895b679
RLGWebD: replace deprecated fs.exists() with fs.access().
John "Elwin" Edwards
parents:
204
diff
changeset
|
879 if (!access_err) { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
880 fs.readFile(realname, function (error, data) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
881 if (error) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
882 res.writeHead(500, {}); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
883 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
884 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
885 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
886 res.writeHead(200, resheaders); |
34
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
887 if (req.method != 'HEAD') |
57f4b36ef06b
rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents:
32
diff
changeset
|
888 res.write(data); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
889 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
890 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
891 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
892 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
893 else { |
125
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
894 send404(res, nname, req.method == 'HEAD'); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
895 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
896 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
897 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
898 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
899 |
160
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
900 /* Currently, this doesn't do anything blocking, but keep the callback */ |
103
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
901 function getStatus(callback) { |
66
57bf0dcd080e
Display idle time of games in progress.
John "Elwin" Edwards <elwin@sdf.org>
parents:
64
diff
changeset
|
902 var now = new Date(); |
103
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
903 var statusinfo = {"s": allowlogin, "g": []}; |
158
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
904 for (var tag in sessions) { |
176
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
905 var gamedesc = {"c": "rlg"}; |
158
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
906 gamedesc["p"] = sessions[tag].pname; |
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
907 gamedesc["g"] = sessions[tag].game.uname; |
160
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
908 gamedesc["i"] = now - sessions[tag].lasttime; |
175
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
909 gamedesc["w"] = sessions[tag].watchers.length; |
103
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
910 statusinfo["g"].push(gamedesc); |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
911 } |
156
127f9e256d02
Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents:
155
diff
changeset
|
912 for (var tag in dglgames) { |
176
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
913 var dglinfo = {"c": "dgl"}; |
163
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
914 var slash = tag.search('/'); |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
915 dglinfo["g"] = tag.slice(0, slash); |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
916 dglinfo["p"] = tag.slice(slash + 1); |
175
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
917 dglinfo["i"] = now - dglgames[tag].lasttime; |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
918 dglinfo["w"] = dglgames[tag].watchers.length; |
176
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
919 statusinfo["g"].push(dglinfo); |
156
127f9e256d02
Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents:
155
diff
changeset
|
920 } |
160
ed837da65e5f
RLGWebD: Clean up code related to session timestamps.
John "Elwin" Edwards
parents:
159
diff
changeset
|
921 callback(statusinfo); |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
922 } |
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
923 |
103
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
924 function statusmsg(req, res) { |
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
925 function respond(info) { |
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
926 res.writeHead(200, { "Content-Type": "application/json" }); |
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
927 if (req.method != 'HEAD') |
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
928 res.write(JSON.stringify(info)); |
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
929 res.end(); |
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
930 } |
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
931 getStatus(respond); |
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
932 } |
f30495f7ede8
RLG-Web server: refactor statusmsg() to work with WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
102
diff
changeset
|
933 |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
934 function pstatusmsg(req, res) { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
935 if (req.method == 'HEAD') { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
936 res.writeHead(200, { "Content-Type": "application/json" }); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
937 res.end(); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
938 return; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
939 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
940 var target = url.parse(req.url).pathname; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
941 var pmatch = target.match(/^\/pstatus\/(.*)/); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
942 if (pmatch && pmatch[1]) |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
943 var pname = pmatch[1]; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
944 else { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
945 sendError(res, 2, "No name given."); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
946 return; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
947 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
948 var reply = {"name": pname}; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
949 playerstatus(pname, function (pdata) { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
950 reply["stat"] = pdata; |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
951 res.writeHead(200, { "Content-Type": "application/json" }); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
952 res.write(JSON.stringify(reply)); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
953 res.end(); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
954 }); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
955 } |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
956 |
124
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
957 function getuinfo(req, res) { |
125
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
958 var urlobj = url.parse(req.url, true); |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
959 var query = urlobj.query; |
124
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
960 if (!("key" in query) || !(query["key"] in logins)) { |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
961 sendError(res, 1); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
962 return; |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
963 } |
125
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
964 var match = urlobj.pathname.match(/^\/[^\/]*\/(.*)/); |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
965 if (!match || !match[1]) { |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
966 send404(res, urlobj.pathname, req.method == 'HEAD'); |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
967 return; |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
968 } |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
969 var which = match[1]; |
124
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
970 var name = logins[query["key"]].name; |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
971 var reply = { "u": name }; |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
972 function send() { |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
973 res.writeHead(200, { "Content-Type": "application/json" }); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
974 res.write(JSON.stringify(reply)); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
975 res.end(); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
976 } |
125
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
977 if (which == "pw") { |
124
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
978 /* Don't actually divulge passwords. */ |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
979 reply["pw"] = ""; |
125
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
980 send(); |
124
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
981 } |
125
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
982 else if (which == "email") { |
124
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
983 var address; |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
984 function finish(code, signal) { |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
985 if (code != 0) { |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
986 tslog("sqlickrypt: %d with name %s", code, name); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
987 sendError(res, 2); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
988 } |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
989 else { |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
990 reply["email"] = address; |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
991 send(); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
992 } |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
993 } |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
994 var subproc = child_process.spawn("/bin/sqlickrypt", ["getmail"]); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
995 subproc.stdout.on("data", function (data) { |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
996 address = data.toString().replace(/\n/g, ""); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
997 }); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
998 subproc.on("exit", finish); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
999 subproc.stdin.end(name + '\n', "utf8"); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
1000 } |
125
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1001 else { |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1002 send404(res, urlobj.pathname, req.method == 'HEAD'); |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1003 return; |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1004 } |
124
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
1005 } |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
1006 |
126
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1007 function setuinfo(req, res, postdata) { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1008 var urlobj = url.parse(req.url, true); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1009 var query = urlobj.query; |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1010 if (!("key" in query) || !(query["key"] in logins)) { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1011 sendError(res, 1); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1012 return; |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1013 } |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1014 var name = logins[query["key"]].name; |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1015 var match = urlobj.pathname.match(/^\/[^\/]*\/(.*)/); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1016 if (!match || !match[1]) { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1017 send404(res, urlobj.pathname, true); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1018 return; |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1019 } |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1020 var which = match[1]; |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1021 if (!("v" in postdata)) { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1022 sendError(res, 2, "No value provided"); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1023 return; |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1024 } |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1025 if (which == "email" || which == "pw") { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1026 var args; |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1027 if (which == "email") |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1028 args = ["setmail"]; |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1029 else |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1030 args = ["setpw"]; |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1031 var child = child_process.execFile("/bin/sqlickrypt", args, |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1032 function (err, stdout, stderr) { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1033 if (err) { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1034 tslog("Could not set %s: sqlickrypt error %d", which, err.code); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1035 sendError(res, 2); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1036 } |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1037 else { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1038 tslog("User %s has changed %s", name, which); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1039 res.writeHead(200, { "Content-Type": "application/json" }); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1040 res.end(JSON.stringify({"t": "t"})); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1041 } |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1042 }); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1043 child.stdin.end(name + "\n" + postdata.v + "\n", "utf8"); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1044 } |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1045 else { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1046 send404(res, urlobj.pathname, true); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1047 } |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1048 } |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1049 |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
1050 function sendError(res, ecode, msg, box) { |
31
7dd6becf9ce9
rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents:
30
diff
changeset
|
1051 res.writeHead(200, { "Content-Type": "application/json" }); |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
1052 var edict = {"t": "E"}; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
1053 if (!(ecode < errorcodes.length && ecode > 0)) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
1054 ecode = 0; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
1055 edict["c"] = ecode; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
1056 edict["s"] = errorcodes[ecode]; |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
1057 if (msg) |
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
1058 edict["s"] += ": " + msg; |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
1059 if (box) |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
1060 res.write(JSON.stringify([edict])); |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
1061 else |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
1062 res.write(JSON.stringify(edict)); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1063 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1064 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1065 |
125
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1066 function send404(res, path, nopage) { |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1067 res.writeHead(404, {"Content-Type": "text/html; charset=utf-8"}); |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1068 if (!nopage) { |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1069 res.write("<html><head><title>" + path + "</title></head>\n<body><h1>" |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1070 + path + " Not Found</h1></body></html>\n"); |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1071 } |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1072 res.end(); |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1073 } |
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1074 |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1075 function webHandler(req, res) { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1076 /* default headers for the response */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1077 var resheaders = {'Content-Type': 'text/html'}; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1078 /* The request body will be added to this as it arrives. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1079 var reqbody = ""; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1080 var formdata; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1081 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1082 /* Register a listener to get the body. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1083 function moredata(chunk) { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1084 reqbody += chunk; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1085 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1086 req.on('data', moredata); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1087 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1088 /* This will send the response once the whole request is here. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1089 function respond() { |
16
ef6127ed6da3
RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents:
8
diff
changeset
|
1090 formdata = getMsg(reqbody); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1091 var target = url.parse(req.url).pathname; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1092 /* First figure out if the client is POSTing to a command interface. */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1093 if (req.method == 'POST') { |
159 | 1094 if (target == "/login") { |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1095 login(req, res, formdata); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1096 } |
19
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
1097 else if (target == "/addacct") { |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
1098 register(req, res, formdata); |
188bbd857124
RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
1099 } |
111
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
1100 else if (target == "/quit") { |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
1101 stopgame(res, formdata); |
f56fdfeed01a
Replace taking over games with forced saves.
John "Elwin" Edwards <elwin@sdf.org>
parents:
110
diff
changeset
|
1102 } |
126
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1103 else if (target.match(/^\/uinfo\//)) { |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1104 setuinfo(req, res, formdata); |
3e3824711791
RLG-Web server: allow changing e-mail and password.
John "Elwin" Edwards <elwin@sdf.org>
parents:
125
diff
changeset
|
1105 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1106 else { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1107 res.writeHead(405, resheaders); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1108 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1109 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1110 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1111 else if (req.method == 'GET' || req.method == 'HEAD') { |
159 | 1112 if (target == '/status') { |
32
c75fc4b1d13d
rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
31
diff
changeset
|
1113 statusmsg(req, res); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1114 } |
125
5ad15380f851
Improve the /uinfo interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
124
diff
changeset
|
1115 else if (target.match(/^\/uinfo\//)) { |
124
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
1116 getuinfo(req, res); |
fbeb0bf2b51d
Add a user information interface at /uinfo.
John "Elwin" Edwards <elwin@sdf.org>
parents:
121
diff
changeset
|
1117 } |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
1118 else if (target.match(/^\/pstatus\//)) { |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
1119 pstatusmsg(req, res); |
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
1120 } |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1121 else /* Go look for it in the filesystem */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1122 serveStatic(req, res, target); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1123 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1124 else { /* Some other method */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1125 res.writeHead(501, resheaders); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1126 res.write("<html><head><title>501</title></head>\n<body><h1>501 Not Implemented</h1></body></html>\n"); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1127 res.end(); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1128 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1129 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1130 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1131 req.on('end', respond); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1132 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1133 |
100
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
94
diff
changeset
|
1134 function wsHandler(wsRequest) { |
158
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1135 var watchmatch = wsRequest.resource.match(/^\/watch\/(.*)$/); |
107
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
1136 var playmatch = wsRequest.resource.match(/^\/play\//); |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1137 if (watchmatch !== null) { |
175
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1138 if (watchmatch[1] in sessions) { |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1139 var tsession = sessions[watchmatch[1]]; |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1140 tsession.attach(wsRequest); |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1141 tslog("Game %s is being watched via WebSockets", tsession.tag()); |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1142 } |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1143 else if (watchmatch[1] in dglgames) { |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1144 var dsession = dglgames[watchmatch[1]]; |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1145 dsession.attach(wsRequest); |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1146 tslog("DGL game %s is being watched via WebSockets", dsession.tag()); |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1147 } |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1148 else { |
158
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1149 wsRequest.reject(404, errorcodes[7]); |
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1150 return; |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1151 } |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1152 } |
107
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
1153 else if (playmatch !== null) { |
170 | 1154 wsStartGame(wsRequest); |
107
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
1155 } |
b64e31c5ec31
RLG-Web server: add playing through WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
104
diff
changeset
|
1156 else if (wsRequest.resourceURL.pathname == "/status") { |
100
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
94
diff
changeset
|
1157 var conn = wsRequest.accept(null, wsRequest.origin); |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1158 var tell = function () { |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1159 getStatus(function (info) { |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1160 info["t"] = "t"; |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1161 conn.sendUTF(JSON.stringify(info)); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1162 }); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1163 } |
176
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
1164 var beginH = function (gname, pname, client) { |
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
1165 conn.sendUTF(JSON.stringify({"t": "b", "c": client, "p": pname, |
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
1166 "g": gname})); |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1167 }; |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1168 var listH = function (list) { |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1169 conn.sendUTF(JSON.stringify(list)); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1170 }; |
158
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1171 var endH = function (gname, pname) { |
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1172 conn.sendUTF(JSON.stringify({"t": "e", "p": pname, "g": gname})); |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1173 }; |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1174 gamemux.on('begin', beginH); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1175 gamemux.on('list', listH); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1176 gamemux.on('end', endH); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1177 conn.on('message', tell); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1178 conn.on('close', function () { |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1179 gamemux.removeListener('begin', beginH); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1180 gamemux.removeListener('list', listH); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1181 gamemux.removeListener('end', endH); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1182 }); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1183 tell(); |
100
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
94
diff
changeset
|
1184 } |
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
94
diff
changeset
|
1185 else |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1186 wsRequest.reject(404, "No such resource."); |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1187 } |
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1188 |
163
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1189 /* Only games with low idle time are included. Use getStatus() for the |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1190 * complete list. */ |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
1191 function pushStatus() { |
163
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1192 var now = new Date(); |
176
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
1193 var statusinfo = {"t": "p", "s": allowlogin, "g": []}; |
163
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1194 for (var tag in sessions) { |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1195 var delta = now - sessions[tag].lasttime; |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1196 if (delta < 60000) { |
176
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
1197 var gamedesc = {"c": "rlg"}; |
163
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1198 gamedesc["p"] = sessions[tag].pname; |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1199 gamedesc["g"] = sessions[tag].game.uname; |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1200 gamedesc["i"] = delta; |
175
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1201 gamedesc["w"] = sessions[tag].watchers.length; |
163
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1202 statusinfo["g"].push(gamedesc); |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1203 } |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1204 } |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1205 for (var tag in dglgames) { |
175
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1206 var delta = now - dglgames[tag].lasttime; |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1207 if (delta < 60000) { |
176
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
1208 var dglinfo = {"c": "dgl"}; |
175
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1209 var slash = tag.search('/'); |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1210 dglinfo["g"] = tag.slice(0, slash); |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1211 dglinfo["p"] = tag.slice(slash + 1); |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1212 dglinfo["i"] = delta; |
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1213 dglinfo["w"] = dglgames[tag].watchers.length; |
176
bf518a00190b
Add client-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
175
diff
changeset
|
1214 statusinfo["g"].push(dglinfo); |
175
4dd87508fc96
Add server-side support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
174
diff
changeset
|
1215 } |
163
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1216 } |
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1217 gamemux.emit('list', statusinfo); |
100
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
94
diff
changeset
|
1218 } |
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
94
diff
changeset
|
1219 |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1220 function shutdown () { |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1221 httpServer.close(); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1222 httpServer.removeAllListeners('request'); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1223 ctlServer.close(); |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1224 tslog("Shutting down..."); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1225 process.exit(); |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1226 } |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1227 |
158
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1228 function consoleHandler(chunk) { |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1229 var msg = chunk.toString().split('\n')[0]; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1230 if (msg == "quit") { |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1231 allowlogin = false; |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1232 tslog("Disconnecting..."); |
158
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1233 for (var tag in sessions) { |
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1234 sessions[tag].close(); |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1235 } |
155
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
1236 progressWatcher.stdin.end("\n"); |
198
ea28353d620a
Make sure games have saved and exited before stopping the server.
John "Elwin" Edwards
parents:
195
diff
changeset
|
1237 if (Object.keys(sessions).length == 0) |
ea28353d620a
Make sure games have saved and exited before stopping the server.
John "Elwin" Edwards
parents:
195
diff
changeset
|
1238 setTimeout(shutdown, 500); |
ea28353d620a
Make sure games have saved and exited before stopping the server.
John "Elwin" Edwards
parents:
195
diff
changeset
|
1239 /* Otherwise wait for the games to end */ |
27
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1240 } |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1241 } |
83f9a799a374
rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents:
26
diff
changeset
|
1242 |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1243 process.on("exit", function () { |
158
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1244 for (var tag in sessions) { |
198
ea28353d620a
Make sure games have saved and exited before stopping the server.
John "Elwin" Edwards
parents:
195
diff
changeset
|
1245 sessions[tag].term.kill('SIGTERM'); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1246 } |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
1247 tslog("Quitting..."); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1248 return; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1249 }); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1250 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1251 /* Initialization STARTS HERE */ |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1252 process.env["TERM"] = "xterm-256color"; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1253 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1254 if (process.getuid() != 0) { |
26
9b58f8d3ea70
rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents:
23
diff
changeset
|
1255 tslog("Not running as root, cannot chroot."); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1256 process.exit(1); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1257 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1258 |
29
cf9d294bc52f
rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents:
28
diff
changeset
|
1259 var httpServer; // declare here so shutdown() can find it |
100
3dbfdaf62623
RLG-Web: begin converting to WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
94
diff
changeset
|
1260 var wsServer; |
155
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
1261 var progressWatcher; |
29
cf9d294bc52f
rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents:
28
diff
changeset
|
1262 |
157
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
1263 var pwent; |
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
1264 try { |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1265 pwent = posix.getpwnam(rlgwebd_options.username); |
157
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
1266 } |
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
1267 catch (err) { |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1268 tslog("Could not drop to user %s: user does not exist", rlgwebd_options.username); |
157
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
1269 process.exit(1); |
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
1270 } |
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
1271 |
85
4303d94d87a2
rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents:
84
diff
changeset
|
1272 /* This could be nonblocking, but nothing else can start yet anyway. */ |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1273 if (fs.existsSync(rlgwebd_options.control_socket)) { |
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1274 fs.unlinkSync(rlgwebd_options.control_socket); |
85
4303d94d87a2
rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents:
84
diff
changeset
|
1275 } |
4303d94d87a2
rlgwebd.js: Unlink control socket at startup.
John "Elwin" Edwards <elwin@sdf.org>
parents:
84
diff
changeset
|
1276 |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1277 var tls_options = {}; |
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1278 if (rlgwebd_options.use_https) { |
203
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
1279 /* If the cert can't be found, don't fall back to insecure HTTP. */ |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
1280 tls_options.key = read_or_die(rlgwebd_options.keyfile, "Keyfile"); |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
1281 tls_options.cert = read_or_die(rlgwebd_options.certfile, "Certfile"); |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
1282 if ("cafile" in rlgwebd_options) |
5491ca3a335b
Fail cleanly if necessary files can't be opened.
John "Elwin" Edwards
parents:
202
diff
changeset
|
1283 tls_options.ca = read_or_die(rlgwebd_options.cafile, "CA file"); |
201 | 1284 }; |
1285 | |
208
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1286 /* Make sure the socket directory is secure. */ |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1287 var socket_dir = path.dirname(rlgwebd_options.control_socket); |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1288 try { |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1289 fs.mkdirSync(socket_dir, 0o700); |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1290 } |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1291 catch (err) { |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1292 if (err.code == "EEXIST") { |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1293 fs.chownSync(socket_dir, 0, 0); |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1294 fs.chmodSync(socket_dir, 0o700); |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1295 } |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1296 else { |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1297 throw err; |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1298 } |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1299 } |
f06f2d1a5035
Fix possibly insecure permissions on the control socket.
John "Elwin" Edwards
parents:
206
diff
changeset
|
1300 |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1301 /* Open the control socket before chrooting where it can't be found */ |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1302 var ctlServer = net.createServer(function (sock) { |
158
9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
John "Elwin" Edwards
parents:
157
diff
changeset
|
1303 sock.on('data', consoleHandler); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1304 }); |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1305 ctlServer.listen(rlgwebd_options.control_socket, function () { |
139 | 1306 /* rlgwebd.js now assumes that it has been started by the rlgwebd shell |
1307 * script, or some other method that detaches it and sets up stdio. */ | |
1308 /* chroot and drop permissions. posix.chroot() does chdir() itself. */ | |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1309 try { |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1310 posix.chroot(rlgwebd_options.chrootDir); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1311 } |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1312 catch (err) { |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1313 tslog("chroot to %s failed: %s", rlgwebd_options.chrootDir, err); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1314 process.exit(1); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1315 } |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1316 try { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1317 // drop gid first, that requires UID=0 |
157
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
1318 process.setgid(pwent.gid); |
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
1319 process.setuid(pwent.uid); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1320 } |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1321 catch (err) { |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1322 tslog("Could not drop permissions: %s", err); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1323 process.exit(1); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1324 } |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1325 if (rlgwebd_options.use_https) { |
204 | 1326 httpServer = https.createServer(tls_options, webHandler); |
1327 httpServer.listen(rlgwebd_options.port); | |
1328 tslog('rlgwebd running on port %d (TLS)', rlgwebd_options.port); | |
1329 wsServer = new WebSocketServer({"httpServer": httpServer}); | |
1330 wsServer.on("request", wsHandler); | |
202
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1331 tslog('Secure WebSockets are online'); |
7f25bb89b59c
Move RLGWebD configuration options into a configuration file.
John "Elwin" Edwards
parents:
201
diff
changeset
|
1332 } |
204 | 1333 else { |
1334 httpServer = http.createServer(webHandler); | |
1335 httpServer.listen(rlgwebd_options.port); | |
1336 tslog('rlgwebd running on port %d', rlgwebd_options.port); | |
1337 wsServer = new WebSocketServer({"httpServer": httpServer}); | |
1338 wsServer.on("request", wsHandler); | |
1339 tslog('WebSockets are online'); | |
1340 } | |
155
245a2959f504
Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
148
diff
changeset
|
1341 progressWatcher = startProgressWatcher(); |
163
0f6da35b27a0
RLGWebD: overhaul the list of current games.
John "Elwin" Edwards
parents:
162
diff
changeset
|
1342 setInterval(pushStatus, 40000); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1343 }); |
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
1344 |