Mercurial > hg > rlgwebd
annotate rlgwebd.js @ 184:ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
author | John "Elwin" Edwards |
---|---|
date | Mon, 19 Jan 2015 14:44:27 -0500 |
parents | db2f5ab112e9 |
children | bbfda4a4eb7f |
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 |
30
b5570a594266
rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents:
29
diff
changeset
|
22 |
180
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
23 var clearbufs = [ |
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
24 new Buffer([27, 91, 72, 27, 91, 50, 74]), // xterm: CSI H CSI 2J |
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
25 new Buffer([27, 91, 72, 27, 91, 74]) // screen: CSI H CSI J |
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
26 ]; |
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
27 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
28 /* Data on the games available. */ |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
29 var games = { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
30 "rogue3": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
31 "name": "Rogue V3", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
32 "uname": "rogue3", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
33 "suffix": ".r3sav", |
180
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
34 "path": "/usr/bin/rogue3" |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
35 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
36 "rogue4": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
37 "name": "Rogue V4", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
38 "uname": "rogue4", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
39 "suffix": ".r4sav", |
180
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
40 "path": "/usr/bin/rogue4" |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
41 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
42 "rogue5": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
43 "name": "Rogue V5", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
44 "uname": "rogue5", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
45 "suffix": ".r5sav", |
180
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
46 "path": "/usr/bin/rogue5" |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
47 }, |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
48 "srogue": { |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
49 "name": "Super-Rogue", |
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
50 "uname": "srogue", |
42
8f6bc0df58fa
rlgwebd.js: add a player status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents:
40
diff
changeset
|
51 "suffix": ".srsav", |
180
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
52 "path": "/usr/bin/srogue" |
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", |
180
85fde763c7ed
Improve the clear-command recognition.
John "Elwin" Edwards
parents:
176
diff
changeset
|
58 "path": "/usr/bin/arogue5" |
0
bd412f63ce0d
Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
59 } |
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 |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
62 /* Global state */ |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
63 var logins = {}; |
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
64 var sessions = {}; |
156
127f9e256d02
Keep a list of dgamelaunch games and put it in the /status message.
John "Elwin" Edwards
parents:
155
diff
changeset
|
65 var dglgames = {}; |
39
e8ac0e3d2614
RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents:
37
diff
changeset
|
66 var allowlogin = true; |
104
7d444ba4739e
RLG-Web server: send status events over WebSockets.
John "Elwin" Edwards <elwin@sdf.org>
parents:
103
diff
changeset
|
67 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
|
68 |
184
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
69 /* 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
|
70 function BaseGame() { |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
71 /* 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
|
72 events.EventEmitter.call(this); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
73 /* Array of watching WebSockets. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
74 this.watchers = []; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
75 /* 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
|
76 * begin with a complete screen. replaylen is the number of bytes stored. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
77 this.replaybuf = new Buffer(1024); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
78 this.replaylen = 0; |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
79 /* Time of last activity. */ |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
80 this.lasttime = new Date(); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
81 } |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
82 BaseGame.prototype = new events.EventEmitter(); |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
parents:
183
diff
changeset
|
83 |
ecedc6f7e4ac
Move TermSession and DglSession common code into a base class.
John "Elwin" Edwards
|