Mercurial > hg > rlgwebd
annotate rlgwebd.js @ 158:9961a538c00e
rlgwebd.js: get rid of numerical game identifiers.
Games will be indentified by gamename/username pairs. This will allow
better interoperability with dgamelaunch.
Polling clients are no longer supported; the code remnants need to be
removed. The reaper() function will likely crash. Unexpectedly, the
WebSocket client still works well enough to play. Watching and listing
current games are probably broken.
author | John "Elwin" Edwards |
---|---|
date | Thu, 01 Jan 2015 15:56:22 -0500 |
parents | e7f809f06c5c |
children | a613380ffdc2 |
rev | line source |
---|---|
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1 #!/usr/bin/env node |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
2 |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
3 var http = require('http'); |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
4 var net = require('net'); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
5 var url = require('url'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
6 var path = require('path'); |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
7 var fs = require('fs'); |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
8 var events = require('events'); |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
9 var child_process = require('child_process'); |
141
1a156a7746a7
RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents:
139
diff
changeset
|
10 // Dependencies |
1a156a7746a7
RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents:
139
diff
changeset
|
11 var posix = require("posix"); |
1a156a7746a7
RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents:
139
diff
changeset
|
12 var pty = require("pty.js"); |
1a156a7746a7
RLGWebD: use NODE_PATH to find modules.
John "Elwin" Edwards
parents:
139
diff
changeset
|
13 var WebSocketServer = require("websocket").server; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
14 |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
15 /* Configuration variables */ |
139 | 16 // The first file is NOT in the chroot. |
28
2ad2b6491aa9
rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents:
27
diff
changeset
|
17 var ctlsocket = "/var/local/rlgwebd/ctl"; |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
18 var httpPort = 8080; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
19 var chrootDir = "/var/dgl/"; |
157
e7f809f06c5c
Use posix.getpwnam() to look up UID/GID to drop to.
John "Elwin" Edwards
parents:
156
diff
changeset
|
20 var dropToUser = "rodney"; |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
21 var serveStaticRoot = "/var/www/"; // inside the chroot |
36
a0387f112bcf
RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents:
34
diff
changeset
|
22 var playtimeout = 3600000; // Idle time before games are autosaved, in ms |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
23 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
24 /* Data on the games available. */ |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
25 var games = { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
26 "rogue3": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
27 "name": "Rogue V3", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
28 "uname": "rogue3", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
29 "suffix": ".r3sav", |
144
81a8e7aa4687
RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents:
142
diff
changeset
|
30 "path": "/usr/bin/rogue3", |
60
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
31 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
32 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
33 "rogue4": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
34 "name": "Rogue V4", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
35 "uname": "rogue4", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
36 "suffix": ".r4sav", |
144
81a8e7aa4687
RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents:
142
diff
changeset
|
37 "path": "/usr/bin/rogue4", |
60
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
38 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
39 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
40 "rogue5": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
41 "name": "Rogue V5", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
42 "uname": "rogue5", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
43 "suffix": ".r5sav", |
144
81a8e7aa4687
RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents:
142
diff
changeset
|
44 "path": "/usr/bin/rogue5", |
60
31bb3cf4f25f
Make sure watchers start with completely drawn screens.
John "Elwin" Edwards <elwin@sdf.org>
parents:
55
diff
changeset
|
45 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
46 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
47 "srogue": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
48 "name": "Super-Rogue", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
49 "uname": "srogue", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
50 "suffix": ".srsav", |
144
81a8e7aa4687
RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents:
142
diff
changeset
|
51 "path": "/usr/bin/srogue", |
121
077adfeea038
Correct the clear sequence for srogue.
John "Elwin" Edwards <elwin@sdf.org>
parents:
120
diff
changeset
|
52 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J |
120
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
113
diff
changeset
|
53 }, |
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
113
diff
changeset
|
54 "arogue5": { |
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
113
diff
changeset
|
55 "name": "Advanced Rogue 5", |
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
113
diff
changeset
|
56 "uname": "arogue5", |
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
113
diff
changeset
|
57 "suffix": ".ar5sav", |
144
81a8e7aa4687
RLGWebD: game binaries have moved to /usr/bin.
John "Elwin" Edwards
parents:
142
diff
changeset
|
58 "path": "/usr/bin/arogue5", |
120
54979d35611a
Add support for Advanced Rogue 5.
John "Elwin" Edwards <elwin@sdf.org>
parents:
113
diff
changeset
|
59 "clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
60 } |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
61 }; |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
62 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
63 /* Global state */ |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
64 var logins = {}; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
65 var sessions = {}; |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
66 var clients = {}; |
156
127f9e256d02
Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents:
155
diff
changeset
|
67 var dglgames = {}; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
68 var allowlogin = true; |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
69 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
|
70 |
55
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
71 /* Constructor. A TermSession handles a pty and the game running on it. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
72 * game: (String) Name of the game to launch. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
73 * lkey: (String, key) The user's id, a key into logins. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
74 * dims: (Array [Number, Number]) Height and width of the pty. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
75 * handlers: (Object) Key-value pairs, event names and functions to |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
76 * install to handle them. |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
77 * Events: |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
78 * "open": Emitted on startup. Parameters: success (Boolean) |
96815eae4ebe
RLG-Web: make multiple watchers possible.
John "Elwin" Edwards <elwin@sdf.org>
parents:
49
diff
changeset
|
79 * "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
|
80 * "exit": Child terminated. Parameters: none |