Mercurial > hg > rlgwebd
changeset 8:ad0a31e52007
Call the dgl replacement rlgwebd instead of webttyd.
| author | John "Elwin" Edwards <elwin@sdf.org> |
|---|---|
| date | Mon, 07 May 2012 16:08:59 -0700 |
| parents | d1b3c3af34d6 |
| children | 826a7ced69f8 |
| files | index-rlg.html rlgwebd.js webttyd.js |
| diffstat | 3 files changed, 528 insertions(+), 533 deletions(-) [+] |
line wrap: on
line diff
--- a/index-rlg.html Mon May 07 16:03:10 2012 -0700 +++ b/index-rlg.html Mon May 07 16:08:59 2012 -0700 @@ -1,14 +1,14 @@ <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> -<title>WebTTY</title> +<title>RLG-Web</title> <script type="text/javascript" src="termemu.js"></script> <script type="text/javascript" src="termemu-keys.js"></script> <script type="text/javascript" src="rlgterm.js"></script> <link rel="stylesheet" type="text/css" href="tty.css"> </head> <body onload="setup()" onkeydown="sendkey(event)"> -<h1>WebTTY</h1> +<h1>RLG-Web</h1> <div id ="top"> <span id="ttitle"></span> <img src="/bell.png" alt="bell" id="bell">
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rlgwebd.js Mon May 07 16:08:59 2012 -0700 @@ -0,0 +1,526 @@ +#!/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'); +var url = require('url'); +var path = require('path'); +var fs = require('fs'); +var child_process = require('child_process'); +var daemon = require(path.join(localModules, "daemon")); + +var chrootDir = "/var/dgl/"; +var dropToUID = 501; +var dropToGID = 501; +var serveStaticRoot = "/var/www/"; // inside the chroot +var passwdfile = "/dgldir/dgl-login"; +var sessions = {}; + +var games = { + "rogue3": { + "name": "Rogue V3", + "uname": "rogue3", + "path": "/bin/rogue3" + }, + "rogue4": { + "name": "Rogue V4", + "uname": "rogue4", + "path": "/bin/rogue4" + }, + "rogue5": { + "name": "Rogue V5", + "uname": "rogue5", + "path": "/bin/rogue5" + }, + "srogue": { + "name": "Super-Rogue", + "uname": "srogue", + "path": "/bin/srogue" + } +}; + +/* Constructor for TermSessions. Note that it opens the terminal and + * adds itself to the sessions dict. It currently assumes the user has + * been authenticated. + */ +function TermSession(game, user, files) { + /* First make sure starting the game will work. */ + if (!(game in games)) { + // TODO: throw an exception instead + return null; + } + /* This order seems to best avoid race conditions... */ + this.alive = false; + this.sessid = randkey(); + while (this.sessid in sessions) { + this.sessid = randkey(); + } + /* Grab a spot in the sessions table. */ + sessions[this.sessid] = this; + /* TODO handle tty-opening errors */ + /* TODO make argument-finding into a method */ + args = [games[game].path, "-n", user.toString()]; + this.child = child_process.spawn("/bin/ptyhelper", args); + var ss = this; + this.alive = true; + this.data = []; + this.lock = files[0]; + fs.writeFile(this.lock, this.child.pid.toString() + '\n80\n24\n', "utf8"); + this.record = fs.createWriteStream(files[1], { mode: 0664 }); + /* END setup */ + function ttyrec_chunk(buf) { + var ts = new Date(); + 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.data.push(chunk); + ss.record.write(chunk); + } + this.child.stdout.on("data", ttyrec_chunk); + this.child.stderr.on("data", ttyrec_chunk); + this.child.on("exit", function (code, signal) { + ss.exitcode = (code != null ? code : 255); + ss.alive = false; + fs.unlink(ss.lock); + /* Wait for all the data to get collected */ + setTimeout(ss.cleanup, 1000); + }); + this.write = function (data) { + if (this.alive) + this.child.stdin.write(data); + /* Otherwise, throw some kind of exception? */ + }; + this.read = function () { + if (this.data.length == 0) + return null; + var pos = 0; + var i = 0; + for (i = 0; i < this.data.length; i++) + pos += this.data[i].length - 12; + var nbuf = new Buffer(pos); + var tptr; + pos = 0; + while (this.data.length > 0) { + tptr = this.data.shift(); + tptr.copy(nbuf, pos, 12); + pos += tptr.length - 12; + } + return nbuf; + }; + this.close = function () { + if (this.alive) + this.child.kill('SIGHUP'); + }; + this.cleanup = function () { + /* Call this when the child is dead. */ + if (this.alive) + return; + ss.record.end(); + /* Give the client a chance to read any leftover data. */ + if (ss.data.length > 0) + setTimeout(ss.remove, 8000); + else + ss.remove(); + }; + this.remove = function () { + delete sessions[ss.sessid]; + console.log("Session " + this.sessid + " removed."); + }; +} + +/* A few utility functions */ +function timestamp() { + dd = new Date(); + sd = dd.toISOString(); + sd = sd.slice(0, sd.indexOf(".")); + return sd.replace("T", "."); +} + +function randkey() { + rnum = Math.floor(Math.random() * 65536 * 65536); + hexstr = rnum.toString(16); + while (hexstr.length < 8) + hexstr = "0" + hexstr; + return hexstr; +} + +/* 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; +} + +function urlDec(encstr) { + var decstr = ""; + var tnum; + for (var i = 0; i < encstr.length; i++) + { + if (encstr.charAt(i) == "+") + decstr += " "; + else if (encstr.charAt(i) == "%") + { + tnum = Number("0x" + encstr.slice(i + 1, 2)); + if (!isNaN(tnum) && tnum >= 0) + decstr += String.fromCharCode(tnum); + i += 2; + } + else + decstr += encstr.charAt(i); + } + return decstr; +} + +/* Returns the contents of a form */ +function getFormValues(formtext) { + var pairstrs = formtext.split("&"); + var data = {}; + for (var i = 0; i < pairstrs.length; i++) + { + var eqsign = pairstrs[i].indexOf("="); + if (eqsign > 0) { + rawname = pairstrs[i].slice(0, eqsign); + rawval = pairstrs[i].slice(eqsign + 1); + name = urlDec(rawname); + val = urlDec(rawval); + if (!(name in data)) + data[name] = []; + data[name].push(val); + } + } + return data; +} + +function auth(username, password) { + // Real authentication not implemented + return true; +} + +function login(req, res, formdata) { + if (!("game" in formdata)) { + sendError(res, 2, "No game specified."); + return; + } + else if (!("name" in formdata)) { + sendError(res, 2, "Username not given."); + return; + } + else if (!("pw" in formdata)) { + sendError(res, 2, "Password not given."); + return; + } + var username = formdata["name"][0]; + var password = formdata["pw"][0]; + var gname = formdata["game"][0]; + if (!(gname in games)) { + sendError(res, 2, "No such game: " + gname); + console.log("Request for nonexistant game \"" + gname + "\""); + return; + } + var progressdir = "/dgldir/inprogress-" + games[gname].uname; + + // This sets up the game once starting is approved. + function startgame() { + var ts = timestamp(); + var lockfile = path.join(progressdir, username + ":node:" + ts + ".ttyrec"); + var ttyrec = path.join("/dgldir/ttyrec", username, gname, ts + ".ttyrec"); + var nsession = new TermSession(gname, username, [lockfile, ttyrec]); + if (nsession) { + /* Technically there's a race condition for the "lock"file, but since + * it requires the user deliberately starting two games at similar times, + * it's not too serious. We can't get O_EXCL in Node anyway. */ + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.write("l1\n" + nsession.sessid + "\n"); + res.end(); + console.log("%s playing %s (key %s, pid %d)", username, gname, + nsession.sessid, nsession.child.pid); + } + else { + sendError(res, 5, "Failed to open TTY"); + console.log("Unable to allocate TTY for " + gname); + } + } + function checkit(code, signal) { + // check the password + if (code != 0) { + sendError(res, 3); + console.log("Password check failed for user " + username);
