annotate rlgwebd.js @ 39:e8ac0e3d2614

RLG-Web: separate logging in and starting a game. The user now logs in with a username and password, receiving a token which is then used for any actions requiring authentication. Starting a game is one such action. Games use a different set of id keys. This allows users to supply their passwords once and then play any number of successive games. Also, newly registered users do not need to supply their passwords again.
author John "Elwin" Edwards <elwin@sdf.org>
date Thu, 07 Jun 2012 15:43:06 -0700
parents 353be34de307
children f7116eb3f791
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
1 #!/usr/bin/env node
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
2
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
3 // If you can't quite trust node to find it on its own
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
4 var localModules = '/usr/local/lib/node_modules/';
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
5 var http = require('http');
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
6 var net = require('net');
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
7 var url = require('url');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
8 var path = require('path');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
9 var fs = require('fs');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
10 var child_process = require('child_process');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
11 var daemon = require(path.join(localModules, "daemon"));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
12
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
13 /* Configuration variables */
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
14 // These first two files are NOT in the chroot.
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
15 var ctlsocket = "/var/local/rlgwebd/ctl";
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
16 var logfile = "/var/local/rlgwebd/log";
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
17 var httpPort = 8080;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
18 var chrootDir = "/var/dgl/";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19 var dropToUID = 501;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
20 var dropToGID = 501;
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",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
29 "path": "/bin/rogue3"
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
30 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
31 "rogue4": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
32 "name": "Rogue V4",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
33 "uname": "rogue4",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
34 "path": "/bin/rogue4"
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 "rogue5": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
37 "name": "Rogue V5",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
38 "uname": "rogue5",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
39 "path": "/bin/rogue5"
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
40 },
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
41 "srogue": {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
42 "name": "Super-Rogue",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
43 "uname": "srogue",
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
44 "path": "/bin/srogue"
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
45 }
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
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
48 /* Global state */
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
49 var logins = {};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
50 var sessions = {};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
51 var allowlogin = true;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
52
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
53 /* Constructor for TermSessions. Note that it opens the terminal and
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
54 * adds itself to the sessions dict. It currently assumes the user has
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
55 * been authenticated.
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
56 */
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
57 function TermSession(game, user, files, dims) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
58 /* First make sure starting the game will work. */
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
59 if (game in games) {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
60 this.game = games[game];
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
61 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
62 else {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
63 // TODO: throw an exception instead
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
64 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
65 }
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
66 this.player = user;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
67 /* This order seems to best avoid race conditions... */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
68 this.alive = false;
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
69 this.sessid = randkey(2);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
70 while (this.sessid in sessions) {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
71 this.sessid = randkey(2);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
72 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
73 /* Grab a spot in the sessions table. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
74 sessions[this.sessid] = this;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
75 /* State for messaging. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
76 this.nsend = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
77 this.nrecv = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
78 this.msgQ = []
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
79 this.Qtimeout = null;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
80 /* Set up the sizes. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
81 this.w = Math.floor(Number(dims[1]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
82 if (!(this.w > 0 && this.w < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
83 this.w = 80;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
84 this.h = Math.floor(Number(dims[0]));
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
85 if (!(this.h > 0 && this.h < 256))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
86 this.h = 24;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
87 /* Environment. */
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
88 var childenv = {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
89 for (var key in process.env) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
90 childenv[key] = process.env[key];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
91 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
92 childenv["PTYHELPER"] = String(this.h) + "x" + String(this.w);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
93 /* TODO handle tty-opening errors */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
94 /* TODO make argument-finding into a method */
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
95 args = [this.game.path, "-n", user.toString()];
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
96 this.child = child_process.spawn("/bin/ptyhelper", args, {"env": childenv});
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
97 var ss = this;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
98 this.alive = true;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
99 this.data = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
100 this.lock = files[0];
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
101 fs.writeFile(this.lock, this.child.pid.toString() + '\n' + this.w + '\n' +
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
102 this.h + '\n', "utf8");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
103 this.record = fs.createWriteStream(files[1], { mode: 0664 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
104 /* END setup */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
105 function ttyrec_chunk(buf) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
106 var ts = new Date();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
107 var chunk = new Buffer(buf.length + 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
108 /* TTYREC headers */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
109 chunk.writeUInt32LE(Math.floor(ts.getTime() / 1000), 0);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
110 chunk.writeUInt32LE(1000 * (ts.getTime() % 1000), 4);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
111 chunk.writeUInt32LE(buf.length, 8);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
112 buf.copy(chunk, 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
113 ss.data.push(chunk);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
114 ss.record.write(chunk);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
115 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
116 this.child.stdout.on("data", ttyrec_chunk);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
117 this.child.stderr.on("data", ttyrec_chunk);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
118 this.child.on("exit", function (code, signal) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
119 ss.exitcode = (code != null ? code : 255);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
120 ss.alive = false;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
121 fs.unlink(ss.lock);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
122 /* Wait for all the data to get collected */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
123 setTimeout(ss.cleanup, 1000);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
124 });
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
125 this.write = function (data, n) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
126 if (!this.alive || typeof (n) != "number") {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
127 return;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
128 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
129 //console.log("Got message " + n);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
130 var oindex = n - this.nrecv;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
131 if (oindex === 0) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
132 //console.log("Writing message " + n);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
133 this.child.stdin.write(data);
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
134 this.nrecv++;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
135 var next;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
136 while ((next = this.msgQ.shift()) !== undefined) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
137 //console.log("Writing message " + this.nrecv);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
138 this.child.stdin.write(next);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
139 this.nrecv++;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
140 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
141 if (this.msgQ.length == 0 && this.Qtimeout) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
142 clearTimeout(this.Qtimeout);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
143 this.Qtimeout = null;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
144 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
145 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
146 else if (oindex > 0 && oindex <= 1024) {
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
147 tslog("Stashing message %d at %d", n, oindex - 1);
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
148 this.msgQ[oindex - 1] = data;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
149 if (!this.Qtimeout) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
150 var nextn = this.nrecv + this.msgQ.length + 1;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
151 this.Qtimeout = setTimeout(this.flushQ, 30000, this, nextn);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
152 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
153 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
154 /* Otherwise, discard it */
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
155 return;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
156 };
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
157 this.flushQ = function (session, n) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
158 /* Callback for when an unreceived message times out.
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
159 * n is the first empty space that will not be given up on. */
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
160 if (!session.alive)
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
161 return;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
162 session.nrecv++;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
163 var next;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
164 /* Clear the queue up to n */
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
165 while (session.nrecv < n) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
166 next = session.msgQ.shift();
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
167 if (next !== undefined)
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
168 session.child.stdin.write(next);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
169 session.nrecv++;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
170 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
171 /* Clear out anything that's ready. */
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
172 while ((next = session.msgQ.shift()) !== undefined) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
173 session.child.stdin.write(next);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
174 session.nrecv++;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
175 }
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
176 /* Now set another timeout if necessary. */
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
177 if (session.msgQ.length != 0) {
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
178 var nextn = session.nrecv + session.msgQ.length + 1;
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
179 session.Qtimeout = setTimeout(session.flushQ, 30000, session, nextn);
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
180 }
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
181 tslog("Flushing queue for session %s", session.sessid);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
182 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
183 this.read = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
184 if (this.data.length == 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
185 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
186 var pos = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
187 var i = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
188 for (i = 0; i < this.data.length; i++)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
189 pos += this.data[i].length - 12;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
190 var nbuf = new Buffer(pos);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
191 var tptr;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
192 pos = 0;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
193 while (this.data.length > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
194 tptr = this.data.shift();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
195 tptr.copy(nbuf, pos, 12);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
196 pos += tptr.length - 12;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
197 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
198 return nbuf;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
199 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
200 this.close = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
201 if (this.alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
202 this.child.kill('SIGHUP');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
203 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
204 this.cleanup = function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
205 /* Call this when the child is dead. */
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
206 if (ss.alive)
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
207 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
208 ss.record.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
209 /* Give the client a chance to read any leftover data. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
210 if (ss.data.length > 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
211 setTimeout(ss.remove, 8000);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
212 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
213 ss.remove();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
214 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
215 this.remove = function () {
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
216 var id = ss.sessid;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
217 delete sessions[id];
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
218 tslog("Session %s removed.", id);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
219 };
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
220 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
221
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
222 /* A few utility functions */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
223 function timestamp() {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
224 dd = new Date();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
225 sd = dd.toISOString();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
226 sd = sd.slice(0, sd.indexOf("."));
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
227 return sd.replace("T", ".");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
228 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
229
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
230 function randkey(words) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
231 if (!words || !(words > 0))
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
232 words = 1;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
233 function rand32() {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
234 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
235 hexstr = rnum.toString(16);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
236 while (hexstr.length < 8)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
237 hexstr = "0" + hexstr;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
238 return hexstr;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
239 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
240 var key = "";
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
241 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
242 key += rand32();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
243 return key;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
244 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
245
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
246 function tslog() {
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
247 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
248 console.log.apply(console, arguments);
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
249 }
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
250
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
251 /* 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
252 function getCookies(req) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
253 cookies = [];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
254 if ("cookie" in req.headers) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
255 cookstrs = req.headers["cookie"].split("; ");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
256 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
257 eqsign = cookstrs[i].indexOf("=");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
258 if (eqsign > 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
259 name = cookstrs[i].slice(0, eqsign).toLowerCase();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
260 val = cookstrs[i].slice(eqsign + 1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
261 cookies[name] = val;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
262 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
263 else if (eqsign < 0)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
264 cookies[cookstrs[i]] = null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
265 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
266 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
267 return cookies;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
268 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
269
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
270 function getMsg(posttext) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
271 var jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
272 if (!posttext)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
273 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
274 try {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
275 jsonobj = JSON.parse(posttext);
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
276 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
277 catch (e) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
278 if (e instanceof SyntaxError)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
279 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
280 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
281 if (typeof(jsonobj) != "object")
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
282 return {};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
283 return jsonobj;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
284 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
285
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
286 function reaper() {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
287 var now = new Date();
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
288 function reapcheck(session) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
289 if (!session.alive)
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
290 return;
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
291 fs.fstat(session.record.fd, function (err, stats) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
292 if (!err && now - stats.mtime > playtimeout) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
293 tslog("Reaping %s", session.sessid);
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
294 session.close();
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
295 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
296 });
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
297 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
298 for (var sessid in sessions) {
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
299 reapcheck(sessions[sessid]);
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
300 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
301 }
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
302
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
303 function login(req, res, formdata) {
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
304 if (!allowlogin) {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
305 sendError(res, 6, null);
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
306 return;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
307 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
308 if (!("name" in formdata)) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
309 sendError(res, 2, "Username not given.");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
310 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
311 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
312 else if (!("pw" in formdata)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
313 sendError(res, 2, "Password not given.");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
314 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
315 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
316 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
317 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
318 function checkit(code, signal) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
319 /* 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
320 if (code != 0) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
321 sendError(res, 3);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
322 if (code == 1)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
323 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
324 else if (code == 2)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
325 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
326 else
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
327 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
328 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
329 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
330 var lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
331 while (lkey in logins)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
332 lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
333 logins[lkey] = {"name": username, "ts": new Date()};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
334 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
335 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
336 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
337 res.end();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
338 tslog("%s has logged in (key %s)", username, lkey);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
339 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
340 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
341 /* 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
342 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
343 pwchecker.on("exit", checkit);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
344 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
345 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
346 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
347
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
348 function startgame(req, res, formdata) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
349 if (!allowlogin) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
350 sendError(res, 6, null);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
351 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
352 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
353 if (!("key" in formdata)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
354 sendError(res, 2, "No key given.");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
355 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
356 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
357 else if (!("game" in formdata)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
358 sendError(res, 2, "No game specified.");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
359 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
360 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
361 var lkey = String(formdata["key"]);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
362 if (!(lkey in logins)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
363 sendError(res, 1, null);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
364 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
365 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
366 else {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
367 logins[lkey].ts = new Date();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
368 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
369 var username = logins[lkey].name;
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
370 var gname = formdata["game"];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
371 var dims = [formdata["h"], formdata["w"]];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
372 if (!(gname in games)) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
373 sendError(res, 2, "No such game: " + gname);
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
374 tslog("Request for nonexistant game \"%s\"", gname);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
375 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
376 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
377 // check for an existing game
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
378 var progressdir = "/dgldir/inprogress-" + games[gname].uname;
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
379 fs.readdir(progressdir, function(err, files) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
380 if (!err) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
381 var fre = RegExp("^" + username + ":");
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
382 for (var i = 0; i < files.length; i++) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
383 if (files[i].match(fre)) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
384 sendError(res, 4, null);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
385 tslog("%s is already playing %s", username, gname);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
386 return;
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
387 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
388 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
389 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
390 // Game starting has been approved.
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
391 var ts = timestamp();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
392 var lockfile = path.join(progressdir, username + ":node:" + ts + ".ttyrec");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
393 var ttyrec = path.join("/dgldir/ttyrec", username, gname, ts + ".ttyrec");
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
394 var nsession = new TermSession(gname, username, [lockfile, ttyrec], dims);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
395 if (nsession) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
396 /* Technically there's a race condition for the "lock"file, but since
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
397 * it requires the user deliberately starting two games at similar times,
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
398 * it's not too serious. We can't get O_EXCL in Node anyway. */
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
399 res.writeHead(200, {'Content-Type': 'application/json'});
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
400 var reply = {"t": "l", "id": nsession.sessid, "w": nsession.w, "h":
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
401 nsession.h};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
402 res.write(JSON.stringify(reply));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
403 res.end();
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
404 tslog("%s playing %s (key %s, pid %d)", username, gname,
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
405 nsession.sessid, nsession.child.pid);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
406 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
407 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
408 sendError(res, 5, "Failed to open TTY");
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
409 tslog("Unable to allocate TTY for %s", gname);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
410 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
411 });
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
412 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
413
20
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
414 /* 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
415 function regsetup(username) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
416 function regsetup_l2(err) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
417 for (var g in games) {
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
418 fs.mkdir(path.join("/dgldir/ttyrec", username, games[g].uname), 0755);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
419 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
420 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
421 fs.mkdir(path.join("/dgldir/userdata", username), 0755);
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
422 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
423 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
424
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
425 function register(req, res, formdata) {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
426 var uname, passwd, email;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
427 if (typeof (formdata.name) != "string" || formdata.name === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
428 sendError(res, 2, "No name given.");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
429 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
430 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
431 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
432 uname = formdata["name"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
433 if (typeof (formdata.pw) != "string" || formdata.pw === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
434 sendError(res, 2, "No password given.");
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
435 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
436 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
437 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
438 passwd = formdata["pw"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
439 if (typeof (formdata.email) != "string" || formdata.email === "") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
440 /* E-mail is optional */
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
441 email = "nobody@nowhere.not";
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
442 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
443 else
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
444 email = formdata["email"];
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
445 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
446 if (code === 0) {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
447 var lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
448 while (lkey in logins)
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
449 lkey = randkey(2);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
450 logins[lkey] = {"name": uname, "ts": new Date()};
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
451 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
452 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
453 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
454 res.end();
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
455 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
456 regsetup(uname);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
457 }
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
458 else if (code == 4) {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
459 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
460 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
461 }
5f785e1d5cca RLG-Web: set up user directories on registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 19
diff changeset
462 else if (code == 1) {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
463 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
464 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
465 }
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
466 else {
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
467 sendError(res, 0, null);
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
468 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
469 }
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
470 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
471 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
472 child_adder.on("exit", checkreg);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
473 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
474 return;
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
475 }
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
476
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
477 function endgame(term, res) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
478 if (!term.alive) {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
479 sendError(res, 7, null);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
480 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
481 }
22
51d59a0e3b20 Fix some typos.
John "Elwin" Edwards <elwin@sdf.org>
parents: 20
diff changeset
482 term.close();
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
483 var resheaders = {'Content-Type': 'application/json'};
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
484 res.writeHead(200, resheaders);
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
485 res.write(JSON.stringify({"t": "q"}));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
486 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
487 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
488 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
489
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
490 function findTermSession(formdata) {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
491 if (typeof(formdata) != "object")
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
492 return null;
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
493 if ("id" in formdata) {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
494 var sessid = formdata["id"];
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
495 if (sessid in sessions) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
496 return sessions[sessid];
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
497 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
498 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
499 return null;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
500 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
501
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
502 function serveStatic(req, res, fname) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
503 var nname = path.normalize(fname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
504 if (nname == "" || nname == "/")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
505 nname = "index.html";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
506 if (nname.match(/\/$/))
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
507 path.join(nname, "index.html"); /* it was a directory */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
508 var realname = path.join(serveStaticRoot, nname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
509 var extension = path.extname(realname);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
510 path.exists(realname, function (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
511 var resheaders = {};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
512 if (!exists || !extension || extension == ".html")
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
513 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
514 else if (extension == ".png")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
515 resheaders["Content-Type"] = "image/png";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
516 else if (extension == ".css")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
517 resheaders["Content-Type"] = "text/css";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
518 else if (extension == ".js")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
519 resheaders["Content-Type"] = "text/javascript";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
520 else if (extension == ".svg")
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
521 resheaders["Content-Type"] = "image/svg+xml";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
522 else
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
523 resheaders["Content-Type"] = "application/octet-stream";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
524 if (exists) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
525 fs.readFile(realname, function (error, data) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
526 if (error) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
527 res.writeHead(500, {});
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
528 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
529 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
530 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
531 res.writeHead(200, resheaders);
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
532 if (req.method != 'HEAD')
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
533 res.write(data);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
534 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
535 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
536 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
537 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
538 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
539 res.writeHead(404, resheaders);
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
540 if (req.method != 'HEAD') {
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
541 res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>"
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
542 + nname + " Not Found</h1></body></html>\n");
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
543 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
544 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
545 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
546 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
547 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
548 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
549
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
550 function readFeed(res, term) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
551 if (term) {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
552 var reply = {};
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
553 var result = term.read();
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
554 if (result == null) {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
555 if (term.alive)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
556 reply.t = "n";
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
557 else {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
558 if (allowlogin)
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
559 reply.t = "q";
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
560 else {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
561 sendError(res, 6, null);
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
562 return;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
563 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
564 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
565 }
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
566 else {
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
567 reply.t = "d";
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
568 reply.n = term.nsend++;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
569 reply.d = result.toString("hex");
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
570 }
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
571 res.writeHead(200, { "Content-Type": "application/json" });
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
572 res.write(JSON.stringify(reply));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
573 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
574 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
575 else {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
576 sendError(res, 7, null);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
577 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
578 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
579
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
580 function statusmsg(req, res) {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
581 var reply = {"s": allowlogin, "g": []};
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
582 for (var sessid in sessions) {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
583 if (sessions[sessid].alive) {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
584 var gamedesc = {};
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
585 gamedesc["p"] = sessions[sessid].player;
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
586 gamedesc["g"] = sessions[sessid].game.name;
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
587 reply["g"].push(gamedesc);
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
588 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
589 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
590 res.writeHead(200, { "Content-Type": "application/json" });
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
591 if (req.method != 'HEAD')
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
592 res.write(JSON.stringify(reply));
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
593 res.end();
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
594 }
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
595
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
596 var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
597 "Login failed", "Already playing", "Game launch failed",
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
598 "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
599
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
600 function sendError(res, ecode, msg) {
31
7dd6becf9ce9 rlgwebd.js: improve MIME types.
John "Elwin" Edwards <elwin@sdf.org>
parents: 30
diff changeset
601 res.writeHead(200, { "Content-Type": "application/json" });
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
602 var edict = {"t": "E"};
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
603 if (!(ecode < errorcodes.length && ecode > 0))
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
604 ecode = 0;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
605 edict["c"] = ecode;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
606 edict["s"] = errorcodes[ecode];
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
607 if (msg)
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
608 edict["s"] += ": " + msg;
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
609 res.write(JSON.stringify(edict));
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
610 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
611 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
612
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
613 function webHandler(req, res) {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
614 /* default headers for the response */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
615 var resheaders = {'Content-Type': 'text/html'};
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
616 /* 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
617 var reqbody = "";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
618 var formdata;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
619
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
620 /* Register a listener to get the body. */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
621 function moredata(chunk) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
622 reqbody += chunk;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
623 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
624 req.on('data', moredata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
625
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
626 /* 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
627 function respond() {
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
628 formdata = getMsg(reqbody);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
629 var target = url.parse(req.url).pathname;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
630 var cterm = findTermSession(formdata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
631 /* 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
632 if (req.method == 'POST') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
633 if (target == '/feed') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
634 if (!cterm) {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
635 sendError(res, 7, null);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
636 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
637 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
638 if (formdata.t == "q") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
639 /* The client wants to terminate the process. */
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
640 endgame(cterm, res);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
641 }
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
642 else if (formdata.t == "d" && typeof(formdata.d) == "string") {
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
643 /* process the keys */
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
644 hexstr = formdata.d.replace(/[^0-9a-f]/gi, "");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
645 if (hexstr.length % 2 != 0) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
646 sendError(res, 2, "incomplete byte");
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
647 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
648 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
649 keybuf = new Buffer(hexstr, "hex");
16
ef6127ed6da3 RLGWeb: switch to JSON protocol.
John "Elwin" Edwards <elwin@sdf.org>
parents: 8
diff changeset
650 /* TODO OoO correction */
23
21de24c08aed Implement message order correction on the server side.
John "Elwin" Edwards <elwin@sdf.org>
parents: 22
diff changeset
651 cterm.write(keybuf, formdata.n);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
652 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
653 readFeed(res, cterm);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
654 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
655 else if (target == "/login") {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
656 login(req, res, formdata);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
657 }
19
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
658 else if (target == "/addacct") {
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
659 register(req, res, formdata);
188bbd857124 RLG-Web: add user registration
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
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 else if (target == "/play") {
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
662 startgame(req, res, formdata);
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
663 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
664 else {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
665 res.writeHead(405, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
666 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
667 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
668 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
669 else if (req.method == 'GET' || req.method == 'HEAD') {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
670 if (target == '/feed') {
34
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
671 if (req.method == 'HEAD') {
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
672 res.writeHead(200, {"Content-Type": "application/json"});
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
673 res.end();
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
674 return;
57f4b36ef06b rlgwebd.js: handle HTTP HEAD properly.
John "Elwin" Edwards <elwin@sdf.org>
parents: 32
diff changeset
675 }
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
676 if (!cterm) {
39
e8ac0e3d2614 RLG-Web: separate logging in and starting a game.
John "Elwin" Edwards <elwin@sdf.org>
parents: 37
diff changeset
677 sendError(res, 7, null);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
678 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
679 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
680 readFeed(res, cterm);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
681 }
32
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
682 else if (target == '/status') {
c75fc4b1d13d rlgwebd.js: add a status interface.
John "Elwin" Edwards <elwin@sdf.org>
parents: 31
diff changeset
683 statusmsg(req, res);
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
684 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
685 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
686 serveStatic(req, res, target);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
687 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
688 else { /* Some other method */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
689 res.writeHead(501, resheaders);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
690 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
691 res.end();
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
692 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
693 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
694 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
695 req.on('end', respond);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
696
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
697 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
698
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
699 function shutdown () {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
700 httpServer.close();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
701 httpServer.removeAllListeners('request');
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
702 ctlServer.close();
27
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
703 tslog("Shutting down...");
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
704 process.exit();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
705 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
706
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
707 function conHandler(chunk) {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
708 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
709 if (msg == "quit") {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
710 allowlogin = false;
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
711 tslog("Disconnecting...");
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
712 for (var sessid in sessions) {
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
713 sessions[sessid].close();
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
714 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
715 setTimeout(shutdown, 10000);
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
716 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
717 }
83f9a799a374 rlgwebd.js: read commands from the console
John "Elwin" Edwards <elwin@sdf.org>
parents: 26
diff changeset
718
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
719 process.on("exit", function () {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
720 for (var sessid in sessions) {
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
721 if (sessions[sessid].alive)
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
722 sessions[sessid].child.kill('SIGHUP');
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
723 }
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
724 tslog("Quitting...");
0
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
725 return;
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
726 });
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
727
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
728 /* Initialization STARTS HERE */
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
729 process.env["TERM"] = "xterm-256color";
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
730
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
731 if (process.getuid() != 0) {
26
9b58f8d3ea70 rlgwebd.js: add timestamps to log messages.
John "Elwin" Edwards <elwin@sdf.org>
parents: 23
diff changeset
732 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
733 process.exit(1);
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
734 }
bd412f63ce0d Put this project under version control, finally.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
735
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
736 var httpServer; // declare here so shutdown() can find it
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
737
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
738 /* 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
739 var ctlServer = net.createServer(function (sock) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
740 sock.on('data', conHandler);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
741 });
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
742 ctlServer.listen(ctlsocket, function () {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
743 /* fork off and die */
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
744 try {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
745 daemon.start(logfile);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
746 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
747 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
748 tslog("Daemonization failed: %s", err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
749 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
750 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
751 /* chroot and drop permissions. daemon.chroot() does chdir() itself. */
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
752 try {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
753 daemon.chroot(chrootDir);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
754 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
755 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
756 tslog("chroot to %s failed: %s", chrootDir, err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
757 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
758 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
759 try {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
760 // drop gid first, that requires UID=0
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
761 process.setgid(dropToGID);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
762 process.setuid(dropToUID);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
763 }
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
764 catch (err) {
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
765 tslog("Could not drop permissions: %s", err);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
766 process.exit(1);
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
767 }
29
cf9d294bc52f rlgwebd.js: fix reference error
John "Elwin" Edwards <elwin@sdf.org>
parents: 28
diff changeset
768 httpServer = http.createServer(webHandler);
30
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
769 httpServer.listen(httpPort);
b5570a594266 rlgwebd.js: listen on any address
John "Elwin" Edwards <elwin@sdf.org>
parents: 29
diff changeset
770 tslog('rlgwebd running on port %d', httpPort);
36
a0387f112bcf RLG-Web: autosave idle games.
John "Elwin" Edwards <elwin@sdf.org>
parents: 34
diff changeset
771 setInterval(reaper, playtimeout / 4);
28
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
772 });
2ad2b6491aa9 rlgwebd.js: become a real daemon.
John "Elwin" Edwards <elwin@sdf.org>
parents: 27
diff changeset
773