2012-05-06 08:45:40 -07:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
// If you can't quite trust node to find it on its own
|
|
|
|
|
var localModules = '/usr/local/lib/node_modules/';
|
|
|
|
|
var http = require('http');
|
2012-06-04 20:45:27 -07:00
|
|
|
var net = require('net');
|
2012-05-06 08:45:40 -07:00
|
|
|
var url = require('url');
|
|
|
|
|
var path = require('path');
|
|
|
|
|
var fs = require('fs');
|
2012-06-18 13:43:51 -07:00
|
|
|
var events = require('events');
|
2012-05-06 08:45:40 -07:00
|
|
|
var child_process = require('child_process');
|
|
|
|
|
var daemon = require(path.join(localModules, "daemon"));
|
2012-07-09 12:24:03 -07:00
|
|
|
var pty = require(path.join(localModules, "pty.js"));
|
2012-07-12 22:16:15 -07:00
|
|
|
var WebSocketServer = require(path.join(localModules, "websocket")).server;
|
2012-05-06 08:45:40 -07:00
|
|
|
|
2012-06-05 09:22:17 -07:00
|
|
|
/* Configuration variables */
|
2012-06-04 20:45:27 -07:00
|
|
|
// These first two files are NOT in the chroot.
|
|
|
|
|
var ctlsocket = "/var/local/rlgwebd/ctl";
|
|
|
|
|
var logfile = "/var/local/rlgwebd/log";
|
2012-06-05 09:22:17 -07:00
|
|
|
var httpPort = 8080;
|
2012-05-06 08:45:40 -07:00
|
|
|
var chrootDir = "/var/dgl/";
|
|
|
|
|
var dropToUID = 501;
|
|
|
|
|
var dropToGID = 501;
|
|
|
|
|
var serveStaticRoot = "/var/www/"; // inside the chroot
|
2012-06-06 10:01:18 -07:00
|
|
|
var playtimeout = 3600000; // Idle time before games are autosaved, in ms
|
2012-06-05 09:22:17 -07:00
|
|
|
|
2012-06-07 15:43:06 -07:00
|
|
|
/* Data on the games available. */
|
2012-05-06 08:45:40 -07:00
|
|
|
var games = {
|
|
|
|
|
"rogue3": {
|
|
|
|
|
"name": "Rogue V3",
|
|
|
|
|
"uname": "rogue3",
|
2012-06-09 11:46:58 -07:00
|
|
|
"suffix": ".r3sav",
|
2012-06-19 19:11:59 -07:00
|
|
|
"path": "/bin/rogue3",
|
|
|
|
|
"clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
|
2012-05-06 08:45:40 -07:00
|
|
|
},
|
|
|
|
|
"rogue4": {
|
|
|
|
|
"name": "Rogue V4",
|
|
|
|
|
"uname": "rogue4",
|
2012-06-09 11:46:58 -07:00
|
|
|
"suffix": ".r4sav",
|
2012-06-19 19:11:59 -07:00
|
|
|
"path": "/bin/rogue4",
|
|
|
|
|
"clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
|
2012-05-06 08:45:40 -07:00
|
|
|
},
|
|
|
|
|
"rogue5": {
|
|
|
|
|
"name": "Rogue V5",
|
|
|
|
|
"uname": "rogue5",
|
2012-06-09 11:46:58 -07:00
|
|
|
"suffix": ".r5sav",
|
2012-06-19 19:11:59 -07:00
|
|
|
"path": "/bin/rogue5",
|
|
|
|
|
"clear": new Buffer([27, 91, 72, 27, 91, 50, 74]) // CSI H CSI 2J
|
2012-05-06 08:45:40 -07:00
|
|
|
},
|
|
|
|
|
"srogue": {
|
|
|
|
|
"name": "Super-Rogue",
|
|
|
|
|
"uname": "srogue",
|
2012-06-09 11:46:58 -07:00
|
|
|
"suffix": ".srsav",
|
2012-06-19 19:11:59 -07:00
|
|
|
"path": "/bin/srogue",
|
|
|
|
|
"clear": new Buffer([27, 91, 72, 27, 91, 74]) // CSI H CSI J
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-06-07 15:43:06 -07:00
|
|
|
/* Global state */
|
|
|
|
|
var logins = {};
|
|
|
|
|
var sessions = {};
|
2012-06-18 13:43:51 -07:00
|
|
|
var clients = {};
|
2012-06-07 15:43:06 -07:00
|
|
|
var allowlogin = true;
|
2012-06-18 13:43:51 -07:00
|
|
|
var nextsession = 0;
|
2012-07-13 22:26:20 -07:00
|
|
|
var gamemux = new events.EventEmitter();
|
2012-06-18 13:43:51 -07:00
|
|
|
|
|
|
|
|
/* Constructor. A TermSession handles a pty and the game running on it.
|
|
|
|
|
* game: (String) Name of the game to launch.
|
|
|
|
|
* lkey: (String, key) The user's id, a key into logins.
|
|
|
|
|
* dims: (Array [Number, Number]) Height and width of the pty.
|
|
|
|
|
* handlers: (Object) Key-value pairs, event names and functions to
|
|
|
|
|
* install to handle them.
|
|
|
|
|
* Events:
|
|
|
|
|
* "open": Emitted on startup. Parameters: success (Boolean)
|
|
|
|
|
* "data": Data generated by child. Parameters: buf (Buffer)
|
2012-07-09 12:24:03 -07:00
|
|
|
* "exit": Child terminated. Parameters: none
|
2012-05-06 08:45:40 -07:00
|
|
|
*/
|
2012-06-18 13:43:51 -07:00
|
|
|
function TermSession(game, lkey, dims, handlers) {
|
|
|
|
|
var ss = this;
|
|
|
|
|
/* Subclass EventEmitter to do the hard work. */
|
|
|
|
|
events.EventEmitter.call(this);
|
|
|
|
|
for (var evname in handlers)
|
|
|
|
|
this.on(evname, handlers[evname]);
|
|
|
|
|
/* Don't launch anything that's not a real game. */
|
2012-06-05 14:51:42 -07:00
|
|
|
if (game in games) {
|
|
|
|
|
this.game = games[game];
|
|
|
|
|
}
|
|
|
|
|
else {
|
2012-06-18 13:43:51 -07:00
|
|
|
this.emit('open', false);
|
|
|
|
|
return;
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
if (lkey in logins) {
|
|
|
|
|
this.key = lkey;
|
|
|
|
|
this.pname = logins[lkey].name;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
this.emit('open', false);
|
|
|
|
|
return;
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
|
|
|
|
/* Grab a spot in the sessions table. */
|
2012-06-18 13:43:51 -07:00
|
|
|
this.sessid = nextsession++;
|
2012-05-06 08:45:40 -07:00
|
|
|
sessions[this.sessid] = this;
|
2012-05-17 09:32:19 -07:00
|
|
|
/* Set up the sizes. */
|
|
|
|
|
this.w = Math.floor(Number(dims[1]));
|
|
|
|
|
if (!(this.w > 0 && this.w < 256))
|
|
|
|
|
this.w = 80;
|
|
|
|
|
this.h = Math.floor(Number(dims[0]));
|
|
|
|
|
if (!(this.h > 0 && this.h < 256))
|
|
|
|
|
this.h = 24;
|
|
|
|
|
/* Environment. */
|
|
|
|
|
var childenv = {};
|
|
|
|
|
for (var key in process.env) {
|
|
|
|
|
childenv[key] = process.env[key];
|
|
|
|
|
}
|
2012-07-09 12:24:03 -07:00
|
|
|
var args = ["-n", this.pname];
|
|
|
|
|
var spawnopts = {"env": childenv, "cwd": "/", "rows": this.h, "cols": this.w,
|
|
|
|
|
"name": "xterm-256color"};
|
|
|
|
|
this.term = pty.spawn(this.game.path, args, spawnopts);
|
|
|
|
|
tslog("%s playing %s (index %d, pid %d)", this.pname, this.game.uname,
|
|
|
|
|
this.sessid, this.term.pid);
|
2012-07-08 08:40:08 -07:00
|
|
|
this.emit('open', true, this.sessid);
|
2012-07-13 22:26:20 -07:00
|
|
|
gamemux.emit('begin', this.sessid, this.pname, this.game.uname);
|
2012-06-08 18:11:47 -07:00
|
|
|
/* Set up the lockfile and ttyrec */
|
|
|
|
|
var ts = timestamp();
|
|
|
|
|
var progressdir = "/dgldir/inprogress-" + this.game.uname;
|
2012-06-18 13:43:51 -07:00
|
|
|
this.lock = path.join(progressdir, this.pname + ":node:" + ts + ".ttyrec");
|
2012-07-15 21:03:36 -07:00
|
|
|
var lmsg = this.term.pid.toString() + '\n' + this.h + '\n' + this.w + '\n';
|
2012-06-08 18:11:47 -07:00
|
|
|
fs.writeFile(this.lock, lmsg, "utf8");
|
2012-06-18 13:43:51 -07:00
|
|
|
var ttyrec = path.join("/dgldir/ttyrec", this.pname, this.game.uname,
|
2012-06-08 18:11:47 -07:00
|
|
|
ts + ".ttyrec");
|
|
|
|
|
this.record = fs.createWriteStream(ttyrec, { mode: 0664 });
|
2012-06-19 19:11:59 -07:00
|
|
|
/* Holds the output since the last screen clear, so watchers can begin
|
|
|
|
|
* with a complete screen. */
|
|
|
|
|
this.framebuf = new Buffer(1024);
|
|
|
|
|
this.frameoff = 0;
|
2012-06-18 13:43:51 -07:00
|
|
|
logins[lkey].sessions.push(this.sessid);
|
2012-05-06 08:45:40 -07:00
|
|
|
/* END setup */
|
2012-07-09 12:24:03 -07:00
|
|
|
function ttyrec_chunk(datastr) {
|
2012-05-06 08:45:40 -07:00
|
|
|
var ts = new Date();
|
2012-07-09 12:24:03 -07:00
|
|
|
var buf = new Buffer(datastr);
|
2012-05-06 08:45:40 -07:00
|
|
|
var chunk = new Buffer(buf.length + 12);
|
|
|
|
|
/* TTYREC headers */
|
|
|
|
|
chunk.writeUInt32LE(Math.floor(ts.getTime() / 1000), 0);
|
|
|
|
|
chunk.writeUInt32LE(1000 * (ts.getTime() % 1000), 4);
|
|
|
|
|
chunk.writeUInt32LE(buf.length, 8);
|
|
|
|
|
buf.copy(chunk, 12);
|
|
|
|
|
ss.record.write(chunk);
|
2012-06-19 19:11:59 -07:00
|
|
|
ss.framepush(buf);
|
2012-06-18 13:43:51 -07:00
|
|
|
ss.emit('data', buf);
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
2012-07-09 12:24:03 -07:00
|
|
|
this.term.on("data", ttyrec_chunk);
|
2012-06-19 19:11:59 -07:00
|
|
|
this.framepush = function(chunk) {
|
|
|
|
|
/* If this chunk resets the screen, discard what preceded it. */
|
|
|
|
|
if (bufncmp(chunk, this.game.clear, this.game.clear.length)) {
|
|
|
|
|
this.framebuf = new Buffer(1024);
|
|
|
|
|
this.frameoff = 0;
|
|
|
|
|
}
|
|
|
|
|
/* Make sure there's space. */
|
|
|
|
|
while (this.framebuf.length < chunk.length + this.frameoff) {
|
|
|
|
|
var nbuf = new Buffer(this.framebuf.length * 2);
|
|
|
|
|
this.framebuf.copy(nbuf, 0, 0, this.frameoff);
|
|
|
|
|
this.framebuf = nbuf;
|
|
|
|
|
if (this.framebuf.length > 65536) {
|
|
|
|
|
tslog("Warning: Game %d frame buffer at %d bytes", this.sessid,
|
|
|
|
|
this.framebuf.length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
chunk.copy(this.framebuf, this.frameoff);
|
|
|
|
|
this.frameoff += chunk.length;
|
|
|
|
|
};
|
2012-06-18 13:43:51 -07:00
|
|
|
this.write = function(data) {
|
2012-07-09 12:24:03 -07:00
|
|
|
this.term.write(data);
|
2012-06-18 13:43:51 -07:00
|
|
|
};
|
2012-07-12 22:16:15 -07:00
|
|
|
// Teardown.
|
2012-07-09 12:24:03 -07:00
|
|
|
this.term.on("exit", function () {
|
2012-05-06 08:45:40 -07:00
|
|
|
fs.unlink(ss.lock);
|
2012-06-18 13:43:51 -07:00
|
|
|
ss.record.end();
|
2012-07-09 12:24:03 -07:00
|
|
|
ss.emit('exit');
|
2012-06-18 13:43:51 -07:00
|
|
|
var id = ss.sessid;
|
|
|
|
|
delete sessions[id];
|
2012-06-19 19:11:59 -07:00
|
|
|
tslog("Game %s ended.", id);
|
2012-07-13 22:26:20 -07:00
|
|
|
gamemux.emit('end', id);
|
2012-05-06 08:45:40 -07:00
|
|
|
});
|
2012-06-18 13:43:51 -07:00
|
|
|
this.close = function () {
|
2012-07-15 09:04:39 -07:00
|
|
|
if (this.sessid in sessions)
|
|
|
|
|
this.term.kill('SIGHUP');
|
2012-06-18 13:43:51 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
TermSession.prototype = new events.EventEmitter();
|
|
|
|
|
|
|
|
|
|
function Watcher(session) {
|
|
|
|
|
var ss = this; // that
|
|
|
|
|
this.session = session;
|
|
|
|
|
this.alive = true;
|
|
|
|
|
/* State for messaging. */
|
|
|
|
|
this.nsend = 0;
|
|
|
|
|
this.sendQ = [];
|
|
|
|
|
/* Get a place in the table. */
|
|
|
|
|
this.id = randkey(2);
|
|
|
|
|
while (this.id in clients) {
|
|
|
|
|
this.id = randkey(2);
|
|
|
|
|
}
|
|
|
|
|
clients[this.id] = this;
|
2012-06-19 19:11:59 -07:00
|
|
|
/* Recreate the current screen state from the session's buffer. */
|
|
|
|
|
this.sendQ.push({"t": "d", "n": this.nsend++,
|
|
|
|
|
"d": session.framebuf.toString("hex", 0, session.frameoff)});
|
2012-06-18 13:43:51 -07:00
|
|
|
function dataH(buf) {
|
|
|
|
|
var reply = {};
|
|
|
|
|
reply.t = "d";
|
|
|
|
|
reply.n = ss.nsend++;
|
|
|
|
|
reply.d = buf.toString("hex");
|
|
|
|
|
ss.sendQ.push(reply);
|
|
|
|
|
}
|
2012-07-09 12:24:03 -07:00
|
|
|
function exitH() {
|
2012-06-18 13:43:51 -07:00
|
|
|
ss.alive = false;
|
|
|
|
|
ss.sendQ.push({"t": "q"});
|
|
|
|
|
}
|
|
|
|
|
session.on('data', dataH);
|
|
|
|
|
session.on('exit', exitH);
|
|
|
|
|
this.read = function() {
|
|
|
|
|
/* Returns an array of all outstanding messages, empty if none. */
|
|
|
|
|
var temp = this.sendQ;
|
|
|
|
|
this.sendQ = [];
|
|
|
|
|
/* Clean up if finished. */
|
|
|
|
|
if (!this.alive) {
|
|
|
|
|
delete clients[this.id];
|
|
|
|
|
}
|
|
|
|
|
return temp;
|
|
|
|
|
};
|
|
|
|
|
this.quit = function() {
|
|
|
|
|
this.session.removeListener('data', dataH);
|
|
|
|
|
this.session.removeListener('exit', exitH);
|
|
|
|
|
delete clients[this.id];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Player(gamename, lkey, dims, callback) {
|
|
|
|
|
var ss = this;
|
|
|
|
|
this.alive = false;
|
|
|
|
|
/* State for messaging. */
|
|
|
|
|
this.nsend = 0;
|
|
|
|
|
this.nrecv = 0;
|
|
|
|
|
this.sendQ = [];
|
|
|
|
|
this.recvQ = []
|
|
|
|
|
this.Qtimeout = null;
|
|
|
|
|
/* Get a place in the table. */
|
|
|
|
|
this.id = randkey(2);
|
|
|
|
|
while (this.id in clients) {
|
|
|
|
|
this.id = randkey(2);
|
|
|
|
|
}
|
|
|
|
|
clients[this.id] = this;
|
|
|
|
|
|
|
|
|
|
this.read = function() {
|
|
|
|
|
var temp = this.sendQ;
|
|
|
|
|
this.sendQ = [];
|
|
|
|
|
/* Clean up if finished. */
|
|
|
|
|
if (!this.alive) {
|
|
|
|
|
clearTimeout(this.Qtimeout);
|
|
|
|
|
delete clients[this.id];
|
|
|
|
|
}
|
|
|
|
|
return temp;
|
|
|
|
|
};
|
2012-05-24 11:36:57 -07:00
|
|
|
this.write = function (data, n) {
|
|
|
|
|
if (!this.alive || typeof (n) != "number") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var oindex = n - this.nrecv;
|
|
|
|
|
if (oindex === 0) {
|
2012-06-18 13:43:51 -07:00
|
|
|
this.session.write(data);
|
2012-05-24 11:36:57 -07:00
|
|
|
this.nrecv++;
|
|
|
|
|
var next;
|
2012-06-18 13:43:51 -07:00
|
|
|
while ((next = this.recvQ.shift()) !== undefined) {
|
|
|
|
|
this.session.write(next);
|
2012-05-24 11:36:57 -07:00
|
|
|
this.nrecv++;
|
|
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
if (this.recvQ.length == 0 && this.Qtimeout) {
|
2012-05-24 11:36:57 -07:00
|
|
|
clearTimeout(this.Qtimeout);
|
|
|
|
|
this.Qtimeout = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (oindex > 0 && oindex <= 1024) {
|
2012-06-18 13:43:51 -07:00
|
|
|
tslog("Client %s: Stashing message %d at %d", this.id, n, oindex - 1);
|
|
|
|
|
this.recvQ[oindex - 1] = data;
|
2012-05-24 11:36:57 -07:00
|
|
|
if (!this.Qtimeout) {
|
2012-06-18 13:43:51 -07:00
|
|
|
var nextn = this.nrecv + this.recvQ.length + 1;
|
2012-05-24 11:36:57 -07:00
|
|
|
this.Qtimeout = setTimeout(this.flushQ, 30000, this, nextn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Otherwise, discard it */
|
|
|
|
|
return;
|
|
|
|
|
};
|
2012-06-18 13:43:51 -07:00
|
|
|
this.flushQ = function (client, n) {
|
2012-05-24 11:36:57 -07:00
|
|
|
/* Callback for when an unreceived message times out.
|
|
|
|
|
* n is the first empty space that will not be given up on. */
|
2012-06-18 13:43:51 -07:00
|
|
|
if (!client.alive || client.nrecv >= n)
|
2012-05-24 11:36:57 -07:00
|
|
|
return;
|
2012-06-18 13:43:51 -07:00
|
|
|
client.nrecv++;
|
2012-05-24 11:36:57 -07:00
|
|
|
var next;
|
|
|
|
|
/* Clear the queue up to n */
|
2012-06-18 13:43:51 -07:00
|
|
|
while (client.nrecv < n) {
|
|
|
|
|
next = client.recvQ.shift();
|
2012-05-24 11:36:57 -07:00
|
|
|
if (next !== undefined)
|
2012-06-18 13:43:51 -07:00
|
|
|
client.session.write(next);
|
|
|
|
|
client.nrecv++;
|
2012-05-24 11:36:57 -07:00
|
|
|
}
|
|
|
|
|
/* Clear out anything that's ready. */
|
2012-06-18 13:43:51 -07:00
|
|
|
while ((next = client.recvQ.shift()) !== undefined) {
|
|
|
|
|
client.session.write(next);
|
|
|
|
|
client.nrecv++;
|
2012-05-24 11:36:57 -07:00
|
|
|
}
|
|
|
|
|
/* Now set another timeout if necessary. */
|
2012-06-18 13:43:51 -07:00
|
|
|
if (client.recvQ.length != 0) {
|
|
|
|
|
var nextn = client.nrecv + client.recvQ.length + 1;
|
|
|
|
|
client.Qtimeout = setTimeout(client.flushQ, 30000, client, nextn);
|
2012-05-24 11:36:57 -07:00
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
tslog("Flushing queue for player %s", player.id);
|
2012-05-06 08:45:40 -07:00
|
|
|
};
|
2012-07-11 10:30:33 -07:00
|
|
|
this.reset = function () {
|
|
|
|
|
/* To be called when the game is taken over. */
|
|
|
|
|
if (this.Qtimeout) {
|
|
|
|
|
clearTimeout(this.Qtimeout);
|
|
|
|
|
this.Qtimeout = null;
|
|
|
|
|
}
|
|
|
|
|
for (var i = 0; i < this.recvQ.length; i++) {
|
|
|
|
|
if (this.recvQ[i] !== undefined) {
|
|
|
|
|
this.session.write(this.recvQ[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.recvQ = [];
|
|
|
|
|
this.nrecv = 0;
|
|
|
|
|
this.nsend = 0;
|
|
|
|
|
this.sendQ = [{"t": "d", "n": this.nsend++,
|
|
|
|
|
"d": this.session.framebuf.toString("hex", 0, this.session.frameoff)}];
|
|
|
|
|
};
|
2012-06-18 13:43:51 -07:00
|
|
|
this.quit = function() {
|
2012-05-06 08:45:40 -07:00
|
|
|
if (this.alive)
|
2012-06-18 13:43:51 -07:00
|
|
|
this.session.close();
|
2012-05-06 08:45:40 -07:00
|
|
|
};
|
2012-07-08 08:40:08 -07:00
|
|
|
function openH(success, id) {
|
2012-06-18 13:43:51 -07:00
|
|
|
if (success) {
|
|
|
|
|
ss.alive = true;
|
2012-07-08 08:40:08 -07:00
|
|
|
ss.session = sessions[id];
|
|
|
|
|
ss.h = sessions[id].h;
|
|
|
|
|
ss.w = sessions[id].w;
|
2012-06-18 13:43:51 -07:00
|
|
|
}
|
|
|
|
|
callback(ss, success);
|
|
|
|
|
}
|
|
|
|
|
function dataH(chunk) {
|
|
|
|
|
var reply = {};
|
|
|
|
|
reply.t = "d";
|
|
|
|
|
reply.n = ss.nsend++;
|
|
|
|
|
reply.d = chunk.toString("hex");
|
|
|
|
|
ss.sendQ.push(reply);
|
|
|
|
|
}
|
2012-07-09 12:24:03 -07:00
|
|
|
function exitH() {
|
2012-06-18 13:43:51 -07:00
|
|
|
ss.alive = false;
|
|
|
|
|
ss.sendQ.push({"t": "q"});
|
|
|
|
|
}
|
|
|
|
|
var handlers = {'open': openH, 'data': dataH, 'exit': exitH};
|
|
|
|
|
this.session = new TermSession(gamename, lkey, dims, handlers);
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
|
|
|
|
|
2012-07-12 22:16:15 -07:00
|
|
|
// Also known as WebSocketAndTermSessionClosureGlueFactory
|
|
|
|
|
function wsWatcher(conn, session) {
|
|
|
|
|
var ss = this; // is this even needed?
|
|
|
|
|
var dataH = function(buf) {
|
|
|
|
|
conn.sendUTF(JSON.stringify({"t": "d", "d": buf.toString("hex")}));
|
|
|
|
|
};
|
|
|
|
|
var exitH = function() {
|
|
|
|
|
if (conn.connected)
|
|
|
|
|
conn.close();
|
|
|
|
|
}
|
|
|
|
|
session.on('data', dataH);
|
|
|
|
|
session.on('exit', exitH);
|
|
|
|
|
conn.on('close', function(code, desc) {
|
|
|
|
|
session.removeListener('data', dataH);
|
|
|
|
|
session.removeListener('exit', exitH);
|
2012-07-13 08:52:17 -07:00
|
|
|
if (session.sessid in sessions)
|
|
|
|
|
tslog("A WebSocket watcher has left game %d", session.sessid);
|
2012-07-12 22:16:15 -07:00
|
|
|
});
|
2012-07-13 08:39:39 -07:00
|
|
|
conn.sendUTF(JSON.stringify({
|
|
|
|
|
"t": "w", "w": session.w, "h": session.h,
|
|
|
|
|
"p": session.pname, "g": session.game.uname
|
|
|
|
|
}));
|
2012-07-12 22:16:15 -07:00
|
|
|
conn.sendUTF(JSON.stringify({"t": "d",
|
|
|
|
|
"d": session.framebuf.toString("hex", 0, session.frameoff)}));
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-15 09:04:39 -07:00
|
|
|
function wsPlay(wsReq, game, lkey, dims) {
|
|
|
|
|
var conn;
|
|
|
|
|
var session;
|
|
|
|
|
/* Listeners on the WebSocket */
|
|
|
|
|
function messageH(message) {
|
|
|
|
|
var parsedMsg = getMsgWS(message);
|
|
|
|
|
if (parsedMsg.t == 'q') {
|
|
|
|
|
session.close();
|
|
|
|
|
}
|
|
|
|
|
else if (parsedMsg.t == 'd') {
|
|
|
|
|
var hexstr = parsedMsg.d.replace(/[^0-9a-f]/gi, "");
|
|
|
|
|
if (hexstr.length % 2 != 0) {
|
|
|
|
|
hexstr = hexstr.slice(0, -1);
|
|
|
|
|
}
|
|
|
|
|
var keybuf = new Buffer(hexstr, "hex");
|
|
|
|
|
session.write(keybuf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function closeH() {
|
|
|
|
|
session.close();
|
|
|
|
|
}
|
|
|
|
|
/* These listen on the TermSession. */
|
|
|
|
|
function openH(success, id) {
|
|
|
|
|
if (success) {
|
|
|
|
|
var reply = {"t": "s", "id": id, "w": sessions[id].w, "h":
|
|
|
|
|
sessions[id].h, "p": sessions[id].pname, "g": game};
|
|
|
|
|
conn = wsReq.accept(null, wsReq.origin);
|
|
|
|
|
conn.sendUTF(JSON.stringify(reply));
|
|
|
|
|
conn.on('message', messageH);
|
|
|
|
|
conn.on('close', closeH);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
wsReq.reject(500, errorcodes[5]);
|
|
|
|
|
tslog("Unable to allocate TTY for %s", game);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function dataH(chunk) {
|
|
|
|
|
var msg = {};
|
|
|
|
|
msg.t = "d";
|
|
|
|
|
msg.d = chunk.toString("hex");
|
|
|
|
|
conn.sendUTF(JSON.stringify(msg));
|
|
|
|
|
}
|
|
|
|
|
function exitH() {
|
|
|
|
|
if (conn.connected)
|
|
|
|
|
conn.sendUTF(JSON.stringify({"t": "q"}));
|
|
|
|
|
conn.close();
|
|
|
|
|
session.removeListener('open', openH);
|
|
|
|
|
session.removeListener('data', dataH);
|
|
|
|
|
session.removeListener('exit', exitH);
|
|
|
|
|
}
|
|
|
|
|
var handlers = {'open': openH, 'data': dataH, 'exit': exitH};
|
|
|
|
|
session = new TermSession(game, lkey, dims, handlers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function wsStart(wsReq) {
|
|
|
|
|
var playmatch = wsReq.resourceURL.pathname.match(/^\/play\/([^\/]*)$/);
|
|
|
|
|
if (!playmatch[1] || !(playmatch[1] in games)) {
|
|
|
|
|
wsReq.reject(404, errorcodes[2]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var gname = playmatch[1];
|
|
|
|
|
if (!allowlogin) {
|
|
|
|
|
wsReq.reject(404, errorcodes[6]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!("key" in wsReq.resourceURL.query)) {
|
|
|
|
|
wsReq.reject(404, "No key given.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var lkey = wsReq.resourceURL.query["key"];
|
|
|
|
|
if (!(lkey in logins)) {
|
|
|
|
|
wsReq.reject(404, errorcodes[1]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var pname = logins[lkey].name;
|
|
|
|
|
var dims = [wsReq.resourceURL.query.h, wsReq.resourceURL.query.w];
|
|
|
|
|
function progcallback(err, fname) {
|
|
|
|
|
if (fname) {
|
|
|
|
|
wsReq.reject(404, errorcodes[4]);
|
|
|
|
|
tslog("%s is already playing %s", pname, gname);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
wsPlay(wsReq, gname, lkey, dims);
|
|
|
|
|
};
|
|
|
|
|
checkprogress(pname, games[gname], progcallback, []);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-18 13:43:51 -07:00
|
|
|
/* Some functions which check whether a player is currently playing or
|
|
|
|
|
* has a saved game. Maybe someday they will provide information on
|
|
|
|
|
* the game. */
|
2012-06-08 18:11:47 -07:00
|
|
|
function checkprogress(user, game, callback, args) {
|
|
|
|
|
var progressdir = "/dgldir/inprogress-" + game.uname;
|
|
|
|
|
fs.readdir(progressdir, function(err, files) {
|
|
|
|
|
if (err) {
|
|
|
|
|
args.unshift(err, null);
|
|
|
|
|
callback.apply(null, args);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var fre = RegExp("^" + user + ":");
|
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
|
|
|
|
if (files[i].match(fre)) {
|
2012-06-09 11:46:58 -07:00
|
|
|
args.unshift(null, files[i]);
|
2012-06-08 18:11:47 -07:00
|
|
|
callback.apply(null, args);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-09 11:46:58 -07:00
|
|
|
args.unshift(null, false);
|
2012-06-08 18:11:47 -07:00
|
|
|
callback.apply(null, args);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-09 11:46:58 -07:00
|
|
|
function checksaved(user, game, callback, args) {
|
|
|
|
|
var savedirc = game.uname + "save";
|
|
|
|
|
var basename = String(dropToUID) + "-" + user + game.suffix;
|
|
|
|
|
var savefile = path.join("/var/games/roguelike", savedirc, basename);
|
2012-07-08 08:02:17 -07:00
|
|
|
fs.exists(savefile, function (exist) {
|
2012-06-09 11:46:58 -07:00
|
|
|
args.unshift(exist);
|
|
|
|
|
callback.apply(null, args);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function playerstatus(user, callback) {
|
|
|
|
|
var sdata = {};
|
|
|
|
|
function finishp() {
|
|
|
|
|
for (var gname in games) {
|
|
|
|
|
if (!(gname in sdata))
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
callback(sdata);
|
|
|
|
|
}
|
|
|
|
|
function regsaved(exists, game) {
|
|
|
|
|
if (exists)
|
|
|
|
|
sdata[game.uname] = "s";
|
|
|
|
|
else
|
|
|
|
|
sdata[game.uname] = "0";
|
|
|
|
|
finishp();
|
|
|
|
|
}
|
|
|
|
|
function regactive(err, filename, game) {
|
|
|
|
|
if (!err && filename) {
|
2012-07-09 12:51:12 -07:00
|
|
|
if (filename.match(/^[^:]*:node:/))
|
|
|
|
|
sdata[game.uname] = "p";
|
|
|
|
|
else
|
|
|
|
|
sdata[game.uname] = "d";
|
2012-06-09 11:46:58 -07:00
|
|
|
finishp();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
checksaved(user, game, regsaved, [game]);
|
|
|
|
|
}
|
|
|
|
|
for (var gname in games) {
|
|
|
|
|
checkprogress(user, games[gname], regactive, [games[gname]]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-06 08:45:40 -07:00
|
|
|
/* A few utility functions */
|
|
|
|
|
function timestamp() {
|
|
|
|
|
dd = new Date();
|
|
|
|
|
sd = dd.toISOString();
|
|
|
|
|
sd = sd.slice(0, sd.indexOf("."));
|
|
|
|
|
return sd.replace("T", ".");
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-07 15:43:06 -07:00
|
|
|
function randkey(words) {
|
|
|
|
|
if (!words || !(words > 0))
|
|
|
|
|
words = 1;
|
|
|
|
|
function rand32() {
|
|
|
|
|
rnum = Math.floor(Math.random() * 65536 * 65536);
|
|
|
|
|
hexstr = rnum.toString(16);
|
|
|
|
|
while (hexstr.length < 8)
|
|
|
|
|
hexstr = "0" + hexstr;
|
|
|
|
|
return hexstr;
|
|
|
|
|
}
|
|
|
|
|
var key = "";
|
|
|
|
|
for (var i = 0; i < words; i++)
|
|
|
|
|
key += rand32();
|
|
|
|
|
return key;
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
|
|
|
|
|
2012-06-19 19:11:59 -07:00
|
|
|
/* Compares two buffers, returns true for equality up to index n */
|
|
|
|
|
function bufncmp(buf1, buf2, n) {
|
|
|
|
|
if (!Buffer.isBuffer(buf1) || !Buffer.isBuffer(buf2))
|
|
|
|
|
return false;
|
|
|
|
|
for (var i = 0; i < n; i++) {
|
|
|
|
|
if (i == buf1.length && i == buf2.length)
|
|
|
|
|
return true;
|
|
|
|
|
if (i == buf1.length || i == buf2.length)
|
|
|
|
|
return false;
|
|
|
|
|
if (buf1[i] != buf2[i])
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-04 10:19:36 -07:00
|
|
|
function tslog() {
|
|
|
|
|
arguments[0] = new Date().toISOString() + ": " + String(arguments[0]);
|
|
|
|
|
console.log.apply(console, arguments);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-06 08:45:40 -07:00
|
|
|
/* Returns a list of the cookies in the request, obviously. */
|
|
|
|
|
function getCookies(req) {
|
|
|
|
|
cookies = [];
|
|
|
|
|
if ("cookie" in req.headers) {
|
|
|
|
|
cookstrs = req.headers["cookie"].split("; ");
|
|
|
|
|
for (var i = 0; i < cookstrs.length; i++) {
|
|
|
|
|
eqsign = cookstrs[i].indexOf("=");
|
|
|
|
|
if (eqsign > 0) {
|
|
|
|
|
name = cookstrs[i].slice(0, eqsign).toLowerCase();
|
|
|
|
|
val = cookstrs[i].slice(eqsign + 1);
|
|
|
|
|
cookies[name] = val;
|
|
|
|
|
}
|
|
|
|
|
else if (eqsign < 0)
|
|
|
|
|
cookies[cookstrs[i]] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cookies;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-17 09:32:19 -07:00
|
|
|
function getMsg(posttext) {
|
|
|
|
|
var jsonobj;
|
|
|
|
|
if (!posttext)
|
|
|
|
|
return {};
|
|
|
|
|
try {
|
|
|
|
|
jsonobj = JSON.parse(posttext);
|
|
|
|
|
}
|
|
|
|
|
catch (e) {
|
|
|
|
|
if (e instanceof SyntaxError)
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
if (typeof(jsonobj) != "object")
|
|
|
|
|
return {};
|
|
|
|
|
return jsonobj;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-15 09:04:39 -07:00
|
|
|
function getMsgWS(msgObj) {
|
|
|
|
|
if (msgObj.type != "utf8")
|
|
|
|
|
return {};
|
|
|
|
|
return getMsg(msgObj.utf8Data);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-06 10:01:18 -07:00
|
|
|
function reaper() {
|
|
|
|
|
var now = new Date();
|
|
|
|
|
function reapcheck(session) {
|
|
|
|
|
fs.fstat(session.record.fd, function (err, stats) {
|
|
|
|
|
if (!err && now - stats.mtime > playtimeout) {
|
2012-06-18 13:43:51 -07:00
|
|
|
tslog("Reaping session %s", session.sessid);
|
2012-06-09 21:20:14 -07:00
|
|
|
/* Dissociate it with its login name. */
|
|
|
|
|
var sn = logins[session.key].sessions.indexOf(session.sessid);
|
|
|
|
|
if (sn >= 0) {
|
|
|
|
|
logins[session.key].sessions.splice(sn, 1);
|
|
|
|
|
if (now - logins[session.key].ts > playtimeout)
|
|
|
|
|
logins[session.key].ts = new Date(now - playtimeout);
|
|
|
|
|
}
|
|
|
|
|
/* Shut it down. */
|
2012-06-06 10:01:18 -07:00
|
|
|
session.close();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
for (var sessid in sessions) {
|
|
|
|
|
reapcheck(sessions[sessid]);
|
|
|
|
|
}
|
2012-06-09 21:20:14 -07:00
|
|
|
/* HELPME this is about as clever as I can code, so I can't tell whether
|
|
|
|
|
* there are any bugs. */
|
|
|
|
|
for (var lkey in logins) {
|
|
|
|
|
if (logins[lkey].sessions.length == 0) {
|
|
|
|
|
/* A login with no current games can be killed for inactivity. */
|
|
|
|
|
if (now - logins[lkey].ts > playtimeout * 4) {
|
|
|
|
|
tslog("Login for %s (key %s) timed out", logins[lkey].name, lkey);
|
|
|
|
|
delete logins[lkey];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* Check for games that have terminated normally, and update
|
|
|
|
|
* the timestamp. */
|
|
|
|
|
var expired = [];
|
|
|
|
|
var targarray = logins[lkey].sessions;
|
|
|
|
|
/* Let's not find out what happens if you modify an array
|
|
|
|
|
* you're iterating through. */
|
|
|
|
|
for (var i = 0; i < targarray.length; i++) {
|
|
|
|
|
if (!(targarray[i] in sessions))
|
|
|
|
|
expired.push(targarray[i]);
|
|
|
|
|
}
|
|
|
|
|
if (expired.length > 0) {
|
|
|
|
|
logins[lkey].ts = new Date(now);
|
|
|
|
|
for (var j = 0; j < expired.length; j++) {
|
2012-06-18 13:43:51 -07:00
|
|
|
targarray.splice(targarray.indexOf(expired[j]), 1);
|
2012-06-09 21:20:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-06 10:01:18 -07:00
|
|
|
}
|
|
|
|
|
|
2012-05-06 08:45:40 -07:00
|
|
|
function login(req, res, formdata) {
|
2012-06-04 14:21:41 -07:00
|
|
|
if (!allowlogin) {
|
2012-06-18 13:43:51 -07:00
|
|
|
sendError(res, 6, null, false);
|
2012-06-04 14:21:41 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2012-06-07 15:43:06 -07:00
|
|
|
if (!("name" in formdata)) {
|
2012-06-18 13:43:51 -07:00
|
|
|
sendError(res, 2, "Username not given.", false);
|
2012-05-06 08:45:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (!("pw" in formdata)) {
|
2012-06-18 13:43:51 -07:00
|
|
|
sendError(res, 2, "Password not given.", false);
|
2012-05-06 08:45:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2012-06-07 15:43:06 -07:00
|
|
|
var username = String(formdata["name"]);
|
|
|
|
|
var password = String(formdata["pw"]);
|
|
|
|
|
function checkit(code, signal) {
|
|
|
|
|
/* Checks the exit status, see sqlickrypt.c for details. */
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
sendError(res, 3);
|
|
|
|
|
if (code == 1)
|
|
|
|
|
tslog("Password check failed for user %s", username);
|
|
|
|
|
else if (code == 2)
|
|
|
|
|
tslog("Attempted login by nonexistent user %s", username);
|
|
|
|
|
else
|
|
|
|
|
tslog("Login failed: sqlickrypt error %d", code);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var lkey = randkey(2);
|
|
|
|
|
while (lkey in logins)
|
|
|
|
|
lkey = randkey(2);
|
2012-06-09 21:20:14 -07:00
|
|
|
logins[lkey] = {"name": username, "ts": new Date(), "sessions": []};
|
2012-06-07 15:43:06 -07:00
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'});
|
|
|
|
|
var reply = {"t": "l", "k": lkey, "u": username};
|
|
|
|
|
res.write(JSON.stringify(reply));
|
|
|
|
|
res.end();
|
|
|
|
|
tslog("%s has logged in (key %s)", username, lkey);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/* Launch the sqlickrypt utility to check the password. */
|
|
|
|
|
var pwchecker = child_process.spawn("/bin/sqlickrypt", ["check"]);
|
|
|
|
|
pwchecker.on("exit", checkit);
|
|
|
|
|
pwchecker.stdin.end(username + '\n' + password + '\n', "utf8");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function startgame(req, res, formdata) {
|
|
|
|
|
if (!allowlogin) {
|
|
|
|
|
sendError(res, 6, null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!("key" in formdata)) {
|
|
|
|
|
sendError(res, 2, "No key given.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (!("game" in formdata)) {
|
|
|
|
|
sendError(res, 2, "No game specified.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var lkey = String(formdata["key"]);
|
|
|
|
|
if (!(lkey in logins)) {
|
|
|
|
|
sendError(res, 1, null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
logins[lkey].ts = new Date();
|
|
|
|
|
}
|
|
|
|
|
var username = logins[lkey].name;
|
2012-05-17 09:32:19 -07:00
|
|
|
var gname = formdata["game"];
|
2012-06-18 13:43:51 -07:00
|
|
|
// If dims are not given or invalid, the constructor will handle it.
|
2012-05-17 09:32:19 -07:00
|
|
|
var dims = [formdata["h"], formdata["w"]];
|
2012-05-06 08:45:40 -07:00
|
|
|
if (!(gname in games)) {
|
|
|
|
|
sendError(res, 2, "No such game: " + gname);
|
2012-06-04 10:19:36 -07:00
|
|
|
tslog("Request for nonexistant game \"%s\"", gname);
|
2012-05-06 08:45:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2012-06-08 18:11:47 -07:00
|
|
|
// A callback to pass to the game-in-progress checker.
|
|
|
|
|
var launch = function(err, fname) {
|
2012-07-11 10:30:33 -07:00
|
|
|
var nodematch = new RegExp("^" + username + ":node:");
|
|
|
|
|
if (fname && (fname.match(nodematch) === null)) {
|
|
|
|
|
/* It's being played in dgamelaunch. */
|
2012-07-15 20:30:33 -07:00
|
|
|
sendError(res, 4, "dgamelaunch");
|
2012-06-08 18:11:47 -07:00
|
|
|
tslog("%s is already playing %s", username, gname);
|
|
|
|
|
return;
|
2012-06-07 15:43:06 -07:00
|
|
|
}
|
|
|
|
|
// Game starting has been approved.
|
2012-06-18 13:43:51 -07:00
|
|
|
var respondlaunch = function(nclient, success) {
|
|
|
|
|
if (success) {
|
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'});
|
|
|
|
|
var reply = {"t": "s", "id": nclient.id, "w": nclient.w, "h":
|
2012-07-10 16:32:49 -07:00
|
|
|
nclient.h, "p": username, "g": gname};
|
2012-06-18 13:43:51 -07:00
|
|
|
res.write(JSON.stringify(reply));
|
|
|
|
|
res.end();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
sendError(res, 5, "Failed to open TTY");
|
|
|
|
|
tslog("Unable to allocate TTY for %s", gname);
|
|
|
|
|
}
|
|
|
|
|
};
|
2012-07-11 10:30:33 -07:00
|
|
|
if (fname) {
|
|
|
|
|
for (var cid in clients) {
|
|
|
|
|
cli = clients[cid];
|
|
|
|
|
if ((cli instanceof Player) &&
|
|
|
|
|
cli.session.pname == username &&
|
|
|
|
|
cli.session.game.uname == gname) {
|
|
|
|
|
cli.reset();
|
|
|
|
|
respondlaunch(cli, true);
|
|
|
|
|
tslog("Game %d has been taken over.", cli.session.sessid);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-15 20:30:33 -07:00
|
|
|
/* If there's no player, it's a WebSocket game, and shouldn't be
|
|
|
|
|
* seized. */
|
|
|
|
|
sendError(res, 4, "WebSocket");
|
2012-07-11 10:30:33 -07:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
new Player(gname, lkey, dims, respondlaunch);
|
|
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
};
|
2012-06-08 18:11:47 -07:00
|
|
|
checkprogress(username, games[gname], launch, []);
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
|
|
|
|
|
2012-06-18 13:43:51 -07:00
|
|
|
function watch(req, res, formdata) {
|
|
|
|
|
if (!("n" in formdata)) {
|
|
|
|
|
sendError(res, 2, "Game number not given");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var gamenumber = Number(formdata["n"]);
|
|
|
|
|
if (!(gamenumber in sessions)) {
|
|
|
|
|
sendError(res, 7);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var session = sessions[gamenumber];
|
|
|
|
|
var watch = new Watcher(session);
|
2012-07-10 16:32:49 -07:00
|
|
|
var reply = {"t": "w", "id": watch.id, "w": session.w, "h": session.h,
|
|
|
|
|
"p": session.pname, "g": session.game.uname};
|
2012-06-18 13:43:51 -07:00
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'});
|
|
|
|
|
res.write(JSON.stringify(reply));
|
|
|
|
|
res.end();
|
|
|
|
|
tslog("Game %d is being watched (key %s)", gamenumber, watch.id);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-22 21:44:38 -07:00
|
|
|
/* Sets things up for a new user, like dgamelaunch's commands[register] */
|
|
|
|
|
function regsetup(username) {
|
|
|
|
|
function regsetup_l2(err) {
|
|
|
|
|
for (var g in games) {
|
|
|
|
|
fs.mkdir(path.join("/dgldir/ttyrec", username, games[g].uname), 0755);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fs.mkdir(path.join("/dgldir/userdata", username), 0755);
|
|
|
|
|
fs.mkdir(path.join("/dgldir/ttyrec/", username), 0755, regsetup_l2);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-22 20:54:33 -07:00
|
|
|
function register(req, res, formdata) {
|
|
|
|
|
var uname, passwd, email;
|
|
|
|
|
if (typeof (formdata.name) != "string" || formdata.name === "") {
|
|
|
|
|
sendError(res, 2, "No name given.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
uname = formdata["name"];
|
|
|
|
|
if (typeof (formdata.pw) != "string" || formdata.pw === "") {
|
|
|
|
|
sendError(res, 2, "No password given.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
passwd = formdata["pw"];
|
|
|
|
|
if (typeof (formdata.email) != "string" || formdata.email === "") {
|
|
|
|
|
/* E-mail is optional */
|
|
|
|
|
email = "nobody@nowhere.not";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
email = formdata["email"];
|
|
|
|
|
function checkreg(code, signal) {
|
2012-06-07 15:43:06 -07:00
|
|
|
if (code === 0) {
|
|
|
|
|
var lkey = randkey(2);
|
|
|
|
|
while (lkey in logins)
|
|
|
|
|
lkey = randkey(2);
|
2012-06-09 21:20:14 -07:00
|
|
|
logins[lkey] = {"name": uname, "ts": new Date(), "sessions": []};
|
2012-06-07 15:43:06 -07:00
|
|
|
var reply = {"t": "r", "k": lkey, "u": uname};
|
|
|
|
|
res.writeHead(200, {'Content-Type': 'application/json'});
|
|
|
|
|
res.write(JSON.stringify(reply));
|
|
|
|
|
res.end();
|
|
|
|
|
tslog("Added new user: %s", uname);
|
|
|
|
|
regsetup(uname);
|
|
|
|
|
}
|
|
|
|
|
else if (code == 4) {
|
2012-05-22 20:54:33 -07:00
|
|
|
sendError(res, 2, "Invalid characters in name or email.");
|
2012-06-04 10:19:36 -07:00
|
|
|
tslog("Attempted registration: %s %s", uname, email);
|
2012-05-22 21:44:38 -07:00
|
|
|
}
|
|
|
|
|
else if (code == 1) {
|
2012-05-22 20:54:33 -07:00
|
|
|
sendError(res, 2, "Username " + uname + " is already being used.");
|
2012-06-04 10:19:36 -07:00
|
|
|
tslog("Attempted duplicate registration: %s", uname);
|
2012-05-22 21:44:38 -07:00
|
|
|
}
|
2012-06-07 15:43:06 -07:00
|
|
|
else {
|
2012-05-22 20:54:33 -07:00
|
|
|
sendError(res, 0, null);
|
2012-06-04 10:19:36 -07:00
|
|
|
tslog("sqlickrypt register failed with code %d", code);
|
2012-05-22 21:44:38 -07:00
|
|
|
}
|
2012-05-22 20:54:33 -07:00
|
|
|
}
|
|
|
|
|
var child_adder = child_process.spawn("/bin/sqlickrypt", ["register"]);
|
|
|
|
|
child_adder.on("exit", checkreg);
|
|
|
|
|
child_adder.stdin.end(uname + '\n' + passwd + '\n' + email + '\n', "utf8");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-18 13:43:51 -07:00
|
|
|
/* Ends the game, obviously. Less obviously, stops watching the game if
|
|
|
|
|
* the client is a Watcher instead of a Player. */
|
|
|
|
|
function endgame(client, res) {
|
|
|
|
|
if (!client.alive) {
|
|
|
|
|
sendError(res, 7, null, true);
|
2012-05-06 08:45:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
client.quit();
|
|
|
|
|
// Give things some time to happen.
|
|
|
|
|
if (client instanceof Player)
|
|
|
|
|
setTimeout(readFeed, 200, client, res);
|
2012-06-20 07:37:15 -07:00
|
|
|
else
|
|
|
|
|
readFeed(client, res);
|
2012-05-06 08:45:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-18 13:43:51 -07:00
|
|
|
function findClient(formdata, playersOnly) {
|
2012-05-17 09:32:19 -07:00
|
|
|
if (typeof(formdata) != "object")
|
|
|
|
|
return null;
|
2012-05-06 08:45:40 -07:00
|
|
|
if ("id" in formdata) {
|
2012-06-18 13:43:51 -07:00
|
|
|
var id = formdata["id"];
|
|
|
|
|
if (id in clients && (!playersOnly || clients[id] instanceof Player)) {
|
|
|
|
|
return clients[id];
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function serveStatic(req, res, fname) {
|
|
|
|
|
var nname = path.normalize(fname);
|
|
|
|
|
if (nname == "" || nname == "/")
|
|
|
|
|
nname = "index.html";
|
|
|
|
|
if (nname.match(/\/$/))
|
|
|
|
|
path.join(nname, "index.html"); /* it was a directory */
|
|
|
|
|
var realname = path.join(serveStaticRoot, nname);
|
|
|
|
|
var extension = path.extname(realname);
|
2012-07-08 08:02:17 -07:00
|
|
|
fs.exists(realname, function (exists) {
|
2012-05-06 08:45:40 -07:00
|
|
|
var resheaders = {};
|
|
|
|
|
if (!exists || !extension || extension == ".html")
|
2012-06-05 09:31:49 -07:00
|
|
|
resheaders["Content-Type"] = "text/html; charset=utf-8";
|
2012-05-06 08:45:40 -07:00
|
|
|
else if (extension == ".png")
|
|
|
|
|
resheaders["Content-Type"] = "image/png";
|
|
|
|
|
else if (extension == ".css")
|
|
|
|
|
resheaders["Content-Type"] = "text/css";
|
|
|
|
|
else if (extension == ".js")
|
|
|
|
|
resheaders["Content-Type"] = "text/javascript";
|
|
|
|
|
else if (extension == ".svg")
|
|
|
|
|
resheaders["Content-Type"] = "image/svg+xml";
|
|
|
|
|
else
|
|
|
|
|
resheaders["Content-Type"] = "application/octet-stream";
|
|
|
|
|
if (exists) {
|
|
|
|
|
fs.readFile(realname, function (error, data) {
|
|
|
|
|
if (error) {
|
|
|
|
|
res.writeHead(500, {});
|
|
|
|
|
res.end();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
res.writeHead(200, resheaders);
|
2012-06-05 20:13:47 -07:00
|
|
|
if (req.method != 'HEAD')
|
|
|
|
|
res.write(data);
|
2012-05-06 08:45:40 -07:00
|
|
|
res.end();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
res.writeHead(404, resheaders);
|
2012-06-05 20:13:47 -07:00
|
|
|
if (req.method != 'HEAD') {
|
|
|
|
|
res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>"
|
|
|
|
|
+ nname + " Not Found</h1></body></html>\n");
|
|
|
|
|
}
|
2012-05-06 08:45:40 -07:00
|
|
|
res.end();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-18 13:43:51 -07:00
|
|
|
function readFeed(client, res) {
|
|
|
|
|
if (!client) {
|
|
|
|
|
sendError(res, 7, null, true);
|
|
|
|
|
return;
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
var msgs = client.read();
|
|
|
|
|
if (!allowlogin && !msgs.length) {
|
|
|
|
|
sendError(res, 6, null, true);
|
|
|
|
|
return;
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
|
|
|
res.write(JSON.stringify(msgs));
|
|
|
|
|
res.end();
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
|
|
|
|
|
2012-07-13 09:17:05 -07:00
|
|
|
function getStatus(callback) {
|
2012-06-20 11:37:05 -07:00
|
|
|
var now = new Date();
|
2012-07-13 09:17:05 -07:00
|
|
|
var statusinfo = {"s": allowlogin, "g": []};
|
2012-06-20 11:37:05 -07:00
|
|
|
function idleset(i, idletime) {
|
2012-07-13 09:17:05 -07:00
|
|
|
if (i >= 0 && i < statusinfo.g.length) {
|
|
|
|
|
statusinfo.g[i].i = idletime;
|
2012-06-20 11:37:05 -07:00
|
|
|
}
|
2012-07-13 09:17:05 -07:00
|
|
|
for (var j = 0; j < statusinfo.g.length; j++) {
|
|
|
|
|
if (!("i" in statusinfo.g[j]))
|
2012-06-20 11:37:05 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2012-07-13 09:17:05 -07:00
|
|
|
callback(statusinfo);
|
2012-06-20 11:37:05 -07:00
|
|
|
}
|
2012-06-05 14:51:42 -07:00
|
|
|
for (var sessid in sessions) {
|
2012-06-18 13:43:51 -07:00
|
|
|
var gamedesc = {};
|
|
|
|
|
gamedesc["n"] = sessid;
|
|
|
|
|
gamedesc["p"] = sessions[sessid].pname;
|
2012-06-20 08:49:10 -07:00
|
|
|
gamedesc["g"] = sessions[sessid].game.uname;
|
2012-07-13 09:17:05 -07:00
|
|
|
statusinfo["g"].push(gamedesc);
|
2012-06-05 14:51:42 -07:00
|
|
|
}
|
2012-07-13 09:17:05 -07:00
|
|
|
if (statusinfo.g.length == 0) {
|
|
|
|
|
callback(statusinfo);
|
2012-06-20 11:37:05 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
function makecallback(i) {
|
|
|
|
|
return function (err, stats) {
|
|
|
|
|
if (err)
|
|
|
|
|
idleset(i, null);
|
|
|
|
|
else
|
|
|
|
|
idleset(i, now - stats.mtime);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-13 09:17:05 -07:00
|
|
|
for (var i = 0; i < statusinfo.g.length; i++) {
|
|
|
|
|
if (statusinfo.g[i].n in sessions) {
|
|
|
|
|
fs.fstat(sessions[statusinfo.g[i].n].record.fd, makecallback(i));
|
2012-06-20 11:37:05 -07:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
idleset(i, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-05 14:51:42 -07:00
|
|
|
}
|
|
|
|
|
|
2012-07-13 09:17:05 -07:00
|
|
|
function statusmsg(req, res) {
|
|
|
|
|
function respond(info) {
|
|
|
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
|
|
|
if (req.method != 'HEAD')
|
|
|
|
|
res.write(JSON.stringify(info));
|
|
|
|
|
res.end();
|
|
|
|
|
}
|
|
|
|
|
getStatus(respond);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-09 11:46:58 -07:00
|
|
|
function pstatusmsg(req, res) {
|
|
|
|
|
if (req.method == 'HEAD') {
|
|
|
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
|
|
|
res.end();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var target = url.parse(req.url).pathname;
|
|
|
|
|
var pmatch = target.match(/^\/pstatus\/(.*)/);
|
|
|
|
|
if (pmatch && pmatch[1])
|
|
|
|
|
var pname = pmatch[1];
|
|
|
|
|
else {
|
|
|
|
|
sendError(res, 2, "No name given.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var reply = {"name": pname};
|
|
|
|
|
playerstatus(pname, function (pdata) {
|
|
|
|
|
reply["stat"] = pdata;
|
|
|
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
|
|
|
res.write(JSON.stringify(reply));
|
|
|
|
|
res.end();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-06 08:45:40 -07:00
|
|
|
var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
|
2012-06-04 14:21:41 -07:00
|
|
|
"Login failed", "Already playing", "Game launch failed",
|
2012-06-07 15:43:06 -07:00
|
|
|
"Server shutting down", "Game not in progress" ];
|
2012-05-06 08:45:40 -07:00
|
|
|
|
2012-06-18 13:43:51 -07:00
|
|
|
function sendError(res, ecode, msg, box) {
|
2012-06-05 09:31:49 -07:00
|
|
|
res.writeHead(200, { "Content-Type": "application/json" });
|
2012-05-17 09:32:19 -07:00
|
|
|
var edict = {"t": "E"};
|
|
|
|
|
if (!(ecode < errorcodes.length && ecode > 0))
|
|
|
|
|
ecode = 0;
|
|
|
|
|
edict["c"] = ecode;
|
|
|
|
|
edict["s"] = errorcodes[ecode];
|
|
|
|
|
if (msg)
|
|
|
|
|
edict["s"] += ": " + msg;
|
2012-06-18 13:43:51 -07:00
|
|
|
if (box)
|
|
|
|
|
res.write(JSON.stringify([edict]));
|
|
|
|
|
else
|
|
|
|
|
res.write(JSON.stringify(edict));
|
2012-05-06 08:45:40 -07:00
|
|
|
res.end();
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-18 13:43:51 -07:00
|
|
|
// TODO new-objects done to here
|
2012-06-04 14:21:41 -07:00
|
|
|
function webHandler(req, res) {
|
2012-05-06 08:45:40 -07:00
|
|
|
/* default headers for the response */
|
|
|
|
|
var resheaders = {'Content-Type': 'text/html'};
|
|
|
|
|
/* The request body will be added to this as it arrives. */
|
|
|
|
|
var reqbody = "";
|
|
|
|
|
var formdata;
|
|
|
|
|
|
|
|
|
|
/* Register a listener to get the body. */
|
|
|
|
|
function moredata(chunk) {
|
|
|
|
|
reqbody += chunk;
|
|
|
|
|
}
|
|
|
|
|
req.on('data', moredata);
|
|
|
|
|
|
|
|
|
|
/* This will send the response once the whole request is here. */
|
|
|
|
|
function respond() {
|
2012-05-17 09:32:19 -07:00
|
|
|
formdata = getMsg(reqbody);
|
2012-05-06 08:45:40 -07:00
|
|
|
var target = url.parse(req.url).pathname;
|
|
|
|
|
/* First figure out if the client is POSTing to a command interface. */
|
|
|
|
|
if (req.method == 'POST') {
|
|
|
|
|
if (target == '/feed') {
|
2012-06-18 13:43:51 -07:00
|
|
|
var client = findClient(formdata, false);
|
|
|
|
|
if (!client) {
|
|
|
|
|
sendError(res, 7, null, true);
|
2012-05-06 08:45:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2012-05-17 09:32:19 -07:00
|
|
|
if (formdata.t == "q") {
|
2012-05-06 08:45:40 -07:00
|
|
|
/* The client wants to terminate the process. */
|
2012-06-18 13:43:51 -07:00
|
|
|
endgame(client, res);
|
2012-06-19 19:11:59 -07:00
|
|
|
return; // endgame() calls readFeed() itself.
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
2012-05-17 09:32:19 -07:00
|
|
|
else if (formdata.t == "d" && typeof(formdata.d) == "string") {
|
2012-06-18 13:43:51 -07:00
|
|
|
if (!(client instanceof Player)) {
|
|
|
|
|
sendError(res, 7, "Watching", true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-05-06 08:45:40 -07:00
|
|
|
/* process the keys */
|
2012-07-15 09:04:39 -07:00
|
|
|
var hexstr = formdata.d.replace(/[^0-9a-f]/gi, "");
|
2012-05-06 08:45:40 -07:00
|
|
|
if (hexstr.length % 2 != 0) {
|
2012-06-18 13:43:51 -07:00
|
|
|
sendError(res, 2, "incomplete byte", true);
|
2012-05-06 08:45:40 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2012-07-15 09:04:39 -07:00
|
|
|
var keybuf = new Buffer(hexstr, "hex");
|
2012-06-18 13:43:51 -07:00
|
|
|
client.write(keybuf, formdata.n);
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
readFeed(client, res);
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
|
|
|
|
else if (target == "/login") {
|
|
|
|
|
login(req, res, formdata);
|
|
|
|
|
}
|
2012-05-22 20:54:33 -07:00
|
|
|
else if (target == "/addacct") {
|
|
|
|
|
register(req, res, formdata);
|
|
|
|
|
}
|
2012-06-07 15:43:06 -07:00
|
|
|
else if (target == "/play") {
|
|
|
|
|
startgame(req, res, formdata);
|
|
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
else if (target == "/watch") {
|
|
|
|
|
watch(req, res, formdata);
|
|
|
|
|
}
|
2012-05-06 08:45:40 -07:00
|
|
|
else {
|
|
|
|
|
res.writeHead(405, resheaders);
|
|
|
|
|
res.end();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (req.method == 'GET' || req.method == 'HEAD') {
|
|
|
|
|
if (target == '/feed') {
|
2012-06-05 20:13:47 -07:00
|
|
|
if (req.method == 'HEAD') {
|
|
|
|
|
res.writeHead(200, {"Content-Type": "application/json"});
|
|
|
|
|
res.end();
|
|
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
else
|
|
|
|
|
sendError(res, 7, null, true);
|
|
|
|
|
return;
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
2012-06-05 14:51:42 -07:00
|
|
|
else if (target == '/status') {
|
|
|
|
|
statusmsg(req, res);
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
2012-06-09 11:46:58 -07:00
|
|
|
else if (target.match(/^\/pstatus\//)) {
|
|
|
|
|
pstatusmsg(req, res);
|
|
|
|
|
}
|
2012-05-06 08:45:40 -07:00
|
|
|
else /* Go look for it in the filesystem */
|
|
|
|
|
serveStatic(req, res, target);
|
|
|
|
|
}
|
|
|
|
|
else { /* Some other method */
|
|
|
|
|
res.writeHead(501, resheaders);
|
|
|
|
|
res.write("<html><head><title>501</title></head>\n<body><h1>501 Not Implemented</h1></body></html>\n");
|
|
|
|
|
res.end();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
req.on('end', respond);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 22:16:15 -07:00
|
|
|
function wsHandler(wsRequest) {
|
2012-07-13 22:26:20 -07:00
|
|
|
var watchmatch = wsRequest.resource.match(/^\/watch\/([0-9]*)$/);
|
2012-07-15 09:04:39 -07:00
|
|
|
var playmatch = wsRequest.resource.match(/^\/play\//);
|
2012-07-13 22:26:20 -07:00
|
|
|
if (watchmatch !== null) {
|
|
|
|
|
if (watchmatch[1] && Number(watchmatch[1]) in sessions) {
|
|
|
|
|
var tsession = sessions[Number(watchmatch[1])];
|
|
|
|
|
var conn = wsRequest.accept(null, wsRequest.origin);
|
|
|
|
|
new wsWatcher(conn, tsession);
|
|
|
|
|
tslog("Game %d is being watched via WebSockets", tsession.sessid);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
wsRequest.reject(404, errorcodes[7]);
|
|
|
|
|
}
|
2012-07-15 09:04:39 -07:00
|
|
|
else if (playmatch !== null) {
|
|
|
|
|
wsStart(wsRequest);
|
|
|
|
|
}
|
|
|
|
|
else if (wsRequest.resourceURL.pathname == "/status") {
|
2012-07-12 22:16:15 -07:00
|
|
|
var conn = wsRequest.accept(null, wsRequest.origin);
|
2012-07-13 22:26:20 -07:00
|
|
|
var tell = function () {
|
|
|
|
|
getStatus(function (info) {
|
|
|
|
|
info["t"] = "t";
|
|
|
|
|
conn.sendUTF(JSON.stringify(info));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
var beginH = function (n, name, game) {
|
|
|
|
|
conn.sendUTF(JSON.stringify({"t": "b", "n": n, "p": name, "g": game}));
|
|
|
|
|
};
|
|
|
|
|
var listH = function (list) {
|
|
|
|
|
conn.sendUTF(JSON.stringify(list));
|
|
|
|
|
};
|
|
|
|
|
var endH = function (n) {
|
|
|
|
|
conn.sendUTF(JSON.stringify({"t": "e", "n": n}));
|
|
|
|
|
};
|
|
|
|
|
gamemux.on('begin', beginH);
|
|
|
|
|
gamemux.on('list', listH);
|
|
|
|
|
gamemux.on('end', endH);
|
|
|
|
|
conn.on('message', tell);
|
|
|
|
|
conn.on('close', function () {
|
|
|
|
|
gamemux.removeListener('begin', beginH);
|
|
|
|
|
gamemux.removeListener('list', listH);
|
|
|
|
|
gamemux.removeListener('end', endH);
|
|
|
|
|
});
|
|
|
|
|
tell();
|
2012-07-12 22:16:15 -07:00
|
|
|
}
|
|
|
|
|
else
|
2012-07-13 22:26:20 -07:00
|
|
|
wsRequest.reject(404, "No such resource.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function pushStatus() {
|
|
|
|
|
getStatus(function(info) {
|
|
|
|
|
info["t"] = "t";
|
|
|
|
|
gamemux.emit('list', info);
|
|
|
|
|
});
|
2012-07-12 22:16:15 -07:00
|
|
|
}
|
|
|
|
|
|
2012-06-04 14:21:41 -07:00
|
|
|
function shutdown () {
|
|
|
|
|
httpServer.close();
|
|
|
|
|
httpServer.removeAllListeners('request');
|
2012-06-04 20:45:27 -07:00
|
|
|
ctlServer.close();
|
2012-06-04 14:21:41 -07:00
|
|
|
tslog("Shutting down...");
|
|
|
|
|
process.exit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function conHandler(chunk) {
|
|
|
|
|
var msg = chunk.toString().split('\n')[0];
|
|
|
|
|
if (msg == "quit") {
|
|
|
|
|
allowlogin = false;
|
|
|
|
|
tslog("Disconnecting...");
|
|
|
|
|
for (var sessid in sessions) {
|
|
|
|
|
sessions[sessid].close();
|
|
|
|
|
}
|
2012-06-18 13:43:51 -07:00
|
|
|
setTimeout(shutdown, 2000);
|
2012-06-04 14:21:41 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-06 08:45:40 -07:00
|
|
|
process.on("exit", function () {
|
|
|
|
|
for (var sessid in sessions) {
|
2012-07-09 12:24:03 -07:00
|
|
|
sessions[sessid].term.kill('SIGHUP');
|
2012-05-06 08:45:40 -07:00
|
|
|
}
|
2012-06-04 10:19:36 -07:00
|
|
|
tslog("Quitting...");
|
2012-05-06 08:45:40 -07:00
|
|
|
return;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* Initialization STARTS HERE */
|
|
|
|
|
process.env["TERM"] = "xterm-256color";
|
|
|
|
|
|
|
|
|
|
if (process.getuid() != 0) {
|
2012-06-04 10:19:36 -07:00
|
|
|
tslog("Not running as root, cannot chroot.");
|
2012-05-06 08:45:40 -07:00
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-04 21:34:57 -07:00
|
|
|
var httpServer; // declare here so shutdown() can find it
|
2012-07-12 22:16:15 -07:00
|
|
|
var wsServer;
|
2012-06-04 21:34:57 -07:00
|
|
|
|
2012-07-08 17:49:43 -07:00
|
|
|
/* This could be nonblocking, but nothing else can start yet anyway. */
|
|
|
|
|
if (fs.existsSync(ctlsocket)) {
|
|
|
|
|
fs.unlinkSync(ctlsocket);
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-04 20:45:27 -07:00
|
|
|
/* Open the control socket before chrooting where it can't be found */
|
|
|
|
|
var ctlServer = net.createServer(function (sock) {
|
|
|
|
|
sock.on('data', conHandler);
|
|
|
|
|
});
|
|
|
|
|
ctlServer.listen(ctlsocket, function () {
|
|
|
|
|
/* fork off and die */
|
|
|
|
|
try {
|
|
|
|
|
daemon.start(logfile);
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
tslog("Daemonization failed: %s", err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
/* chroot and drop permissions. daemon.chroot() does chdir() itself. */
|
|
|
|
|
try {
|
|
|
|
|
daemon.chroot(chrootDir);
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
tslog("chroot to %s failed: %s", chrootDir, err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// drop gid first, that requires UID=0
|
|
|
|
|
process.setgid(dropToGID);
|
|
|
|
|
process.setuid(dropToUID);
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
tslog("Could not drop permissions: %s", err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2012-06-04 21:34:57 -07:00
|
|
|
httpServer = http.createServer(webHandler);
|
2012-06-05 09:22:17 -07:00
|
|
|
httpServer.listen(httpPort);
|
|
|
|
|
tslog('rlgwebd running on port %d', httpPort);
|
2012-06-06 10:01:18 -07:00
|
|
|
setInterval(reaper, playtimeout / 4);
|
2012-07-12 22:16:15 -07:00
|
|
|
wsServer = new WebSocketServer({"httpServer": httpServer});
|
|
|
|
|
wsServer.on("request", wsHandler);
|
|
|
|
|
tslog('WebSockets are online');
|
2012-07-13 22:26:20 -07:00
|
|
|
setInterval(pushStatus, 4000);
|
2012-06-04 20:45:27 -07:00
|
|
|
});
|
|
|
|
|
|