Put this project under version control, finally.

This commit is contained in:
John "Elwin" Edwards 2012-05-06 08:45:40 -07:00
commit 8dec6dff87
11 changed files with 3461 additions and 0 deletions

72
bell.svg Normal file
View file

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="16"
height="16"
id="svg2"
version="1.1"
inkscape:version="0.48.1 r9760"
sodipodi:docname="New document 1">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="11.2"
inkscape:cx="17.844259"
inkscape:cy="5.9285714"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="873"
inkscape:window-height="546"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="0" />
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-1036.3622)">
<path
style="fill:none;stroke:#000000;stroke-linejoin:round;stroke-opacity:1"
d="m 4.2931485,1040.1868 c 0.8310158,-1.1611 2.3601954,-2 3.7880723,-2 1.4278768,0 2.9570562,0.8389 3.7880722,2 1.238121,1.73 0.404997,4.2608 1,6.3033 0.234101,0.8036 1,2.3033 1,2.3033 l -11.5761445,0 c 0,0 0.7658995,-1.4997 1,-2.3033 0.5950026,-2.0425 -0.2381208,-4.5733 1,-6.3033 z"
id="rect2985"
inkscape:connector-curvature="0"
sodipodi:nodetypes="ssssccss" />
<path
sodipodi:type="arc"
style="fill:none;stroke:#000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none"
id="path3762"
sodipodi:cx="11.696428"
sodipodi:cy="14.660714"
sodipodi:rx="1.25"
sodipodi:ry="1.25"
d="m 12.946428,14.660714 a 1.25,1.25 0 1 1 -2.5,0 1.25,1.25 0 1 1 2.5,0 z"
transform="translate(-1.25,1035.4693)" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.5 KiB

103
index-rlg.html Normal file
View file

@ -0,0 +1,103 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>WebTTY</title>
<script type="text/javascript" src="termemu.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>
<div id ="top">
<span id="ttitle"></span>
<img src="/bell.png" alt="bell" id="bell">
</div>
<div id="termwrap">TERM</div>
<div class="keyrow">
<div class="key" onclick="vkey('`')">`</div>
<div class="key" onclick="vkey('1')">1</div>
<div class="key" onclick="vkey('2')">2</div>
<div class="key" onclick="vkey('3')">3</div>
<div class="key" onclick="vkey('4')">4</div>
<div class="key" onclick="vkey('5')">5</div>
<div class="key" onclick="vkey('6')">6</div>
<div class="key" onclick="vkey('7')">7</div>
<div class="key" onclick="vkey('8')">8</div>
<div class="key" onclick="vkey('9')">9</div>
<div class="key" onclick="vkey('0')">0</div>
<div class="key" onclick="vkey('-')">-</div>
<div class="key" onclick="vkey('=')">=</div>
<div class="key" onclick="vkey('\b')" style="width: 2.5em">Bksp</div>
</div>
<div class="keyrow">
<div class="key" onclick="vkey('\t')" style="width: 2.5em">Tab</div>
<div class="key" onclick="vkey('q')">Q</div>
<div class="key" onclick="vkey('w')">W</div>
<div class="key" onclick="vkey('e')">E</div>
<div class="key" onclick="vkey('r')">R</div>
<div class="key" onclick="vkey('t')">T</div>
<div class="key" onclick="vkey('y')">Y</div>
<div class="key" onclick="vkey('u')">U</div>
<div class="key" onclick="vkey('i')">I</div>
<div class="key" onclick="vkey('o')">O</div>
<div class="key" onclick="vkey('p')">P</div>
<div class="key" onclick="vkey('[')">[</div>
<div class="key" onclick="vkey(']')">]</div>
<div class="key" onclick="vkey('\\')">\</div>
</div>
<div class="keyrow">
<div class="key" onclick="togglectrl()" id="ctrlkey">Ctrl</div>
<div class="key" onclick="vkey('a')">A</div>
<div class="key" onclick="vkey('s')">S</div>
<div class="key" onclick="vkey('d')">D</div>
<div class="key" onclick="vkey('f')">F</div>
<div class="key" onclick="vkey('g')">G</div>
<div class="key" onclick="vkey('h')">H</div>
<div class="key" onclick="vkey('j')">J</div>
<div class="key" onclick="vkey('k')">K</div>
<div class="key" onclick="vkey('l')">L</div>
<div class="key" onclick="vkey(';')">;</div>
<div class="key" onclick="vkey('\'')">'</div>
<div class="key" onclick="vkey('\n')" style="width: 4em">Ret</div>
</div>
<div class="keyrow">
<div class="key" onclick="toggleshift()" id="shiftkey">Shift</div>
<div class="key" onclick="vkey('z')">Z</div>
<div class="key" onclick="vkey('x')">X</div>
<div class="key" onclick="vkey('c')">C</div>
<div class="key" onclick="vkey('v')">V</div>
<div class="key" onclick="vkey('b')">B</div>
<div class="key" onclick="vkey('n')">N</div>
<div class="key" onclick="vkey('m')">M</div>
<div class="key" onclick="vkey(',')">,</div>
<div class="key" onclick="vkey('.')">.</div>
<div class="key" onclick="vkey('/')">/</div>
</div>
<div class="keyrow">
<div class="key" onclick="vkey(' ')" id="spacebar"></div>
</div>
<div class="rbutton" onclick="stop()">Stop</div>
<div class="rbutton">Font:
<span onclick="textsize(false)">Smaller</span>
<span onclick="textsize(true)">Larger</span>
</div>
<div>
<form id="loginform" action="/login" method="post">
<div>
Name: <input type="text" name="name" id="input_name">
Password: <input type="password" name="pw" id="input_pw">
Choose game: <select name="game" id="input_game">
<option label="Rogue V3" value="rogue3">Rogue V3</option>
<option label="Rogue V4" value="rogue4">Rogue V4</option>
<option label="Rogue V5" value="rogue5">Rogue V5</option>
<option label="Super-Rogue" value="srogue">Super-Rogue</option>
</select>
<input type="submit" value="Play" onclick="formlogin(event)">
</div>
</form>
</div>
<div id="debug">
<p>Debugging Output</p>
</div>
</body>
</html>

91
index-sh.html Normal file
View file

@ -0,0 +1,91 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>WebTTY</title>
<script type="text/javascript" src="termemu.js"></script>
<script type="text/javascript" src="shterm.js"></script>
<link rel="stylesheet" type="text/css" href="tty.css">
</head>
<body onload="setup()" onkeydown="sendkey(event)">
<h1>WebTTY</h1>
<div id ="top">
<span id="ttitle"></span>
<img src="/bell.png" alt="bell" id="bell">
</div>
<div id="termwrap">
Browsing with Javascript turned off? I sympathize. I didn't want Javascript to be necessary for WebTerm. Unfortunately, the only other way to make it work was Java applets.
</div>
<div class="keyrow">
<div class="key" onclick="vkey('`')">`</div>
<div class="key" onclick="vkey('1')">1</div>
<div class="key" onclick="vkey('2')">2</div>
<div class="key" onclick="vkey('3')">3</div>
<div class="key" onclick="vkey('4')">4</div>
<div class="key" onclick="vkey('5')">5</div>
<div class="key" onclick="vkey('6')">6</div>
<div class="key" onclick="vkey('7')">7</div>
<div class="key" onclick="vkey('8')">8</div>
<div class="key" onclick="vkey('9')">9</div>
<div class="key" onclick="vkey('0')">0</div>
<div class="key" onclick="vkey('-')">-</div>
<div class="key" onclick="vkey('=')">=</div>
<div class="key" onclick="vkey('\b')" style="width: 2.5em">Bksp</div>
</div>
<div class="keyrow">
<div class="key" onclick="vkey('\t')" style="width: 2.5em">Tab</div>
<div class="key" onclick="vkey('q')">Q</div>
<div class="key" onclick="vkey('w')">W</div>
<div class="key" onclick="vkey('e')">E</div>
<div class="key" onclick="vkey('r')">R</div>
<div class="key" onclick="vkey('t')">T</div>
<div class="key" onclick="vkey('y')">Y</div>
<div class="key" onclick="vkey('u')">U</div>
<div class="key" onclick="vkey('i')">I</div>
<div class="key" onclick="vkey('o')">O</div>
<div class="key" onclick="vkey('p')">P</div>
<div class="key" onclick="vkey('[')">[</div>
<div class="key" onclick="vkey(']')">]</div>
<div class="key" onclick="vkey('\\')">\</div>
</div>
<div class="keyrow">
<div class="key" onclick="togglectrl()" id="ctrlkey">Ctrl</div>
<div class="key" onclick="vkey('a')">A</div>
<div class="key" onclick="vkey('s')">S</div>
<div class="key" onclick="vkey('d')">D</div>
<div class="key" onclick="vkey('f')">F</div>
<div class="key" onclick="vkey('g')">G</div>
<div class="key" onclick="vkey('h')">H</div>
<div class="key" onclick="vkey('j')">J</div>
<div class="key" onclick="vkey('k')">K</div>
<div class="key" onclick="vkey('l')">L</div>
<div class="key" onclick="vkey(';')">;</div>
<div class="key" onclick="vkey('\'')">'</div>
<div class="key" onclick="vkey('\n')" style="width: 4em">Ret</div>
</div>
<div class="keyrow">
<div class="key" onclick="toggleshift()" id="shiftkey">Shift</div>
<div class="key" onclick="vkey('z')">Z</div>
<div class="key" onclick="vkey('x')">X</div>
<div class="key" onclick="vkey('c')">C</div>
<div class="key" onclick="vkey('v')">V</div>
<div class="key" onclick="vkey('b')">B</div>
<div class="key" onclick="vkey('n')">N</div>
<div class="key" onclick="vkey('m')">M</div>
<div class="key" onclick="vkey(',')">,</div>
<div class="key" onclick="vkey('.')">.</div>
<div class="key" onclick="vkey('/')">/</div>
</div>
<div class="keyrow">
<div class="key" onclick="vkey(' ')" id="spacebar"></div>
</div>
<div class="rbutton" onclick="login()">Log in</div>
<div class="rbutton" onclick="stop()">Stop</div>
<div class="rbutton">Font:
<span onclick="textsize(false)">Smaller</span>
<span onclick="textsize(true)">Larger</span>
</div>
<div id="debug">
<p>Debugging Output</p>
</div>
</body>
</html>

149
ptyhelper.c Normal file
View file

@ -0,0 +1,149 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <pty.h>
#include <utmp.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <termios.h>
int got_sighup = 0;
void handle_HUP(int signum) {
if (signum == SIGHUP)
got_sighup = 1;
return;
}
int main(int argc, char *argv[]) {
int ptymaster, ptyslave; /* File descriptors */
int child;
int status, selstatus;
int w = 80, h = 24, t;
struct sigaction sighup_act;
fd_set readset;
struct timeval select_time;
char buf[4096];
int nread;
char *penv, *ptmp;
#if 0
struct termios ptysettings;
#endif
struct winsize ptysize;
if (argc == 1) {
fprintf(stderr, "No command given.\n");
exit(1);
}
/* Set up the signal handler. */
sighup_act.sa_handler = &handle_HUP;
sighup_act.sa_flags = SA_RESTART;
sigaction(SIGHUP, &sighup_act, NULL);
/* Check the environment for configuration options. */
penv = getenv("PTYHELPER");
if (penv != NULL) {
t = strtol(penv, &ptmp, 10);
if (t > 0 && t < 256)
h = t;
if (*ptmp != '\0') {
penv = ptmp + 1;
t = strtol(penv, &ptmp, 10);
if (t > 0 && t < 256)
w = t;
}
}
/* Set up the size. */
ptysize.ws_row = h;
ptysize.ws_col = w;
/* Open a pty */
if (openpty(&ptymaster, &ptyslave, NULL, NULL, &ptysize)) {
return 1;
}
#if 0
/* Put it into raw mode. */
tcgetattr(ptyslave, &ptysettings);
cfmakeraw(&ptysettings);
tcsetattr(ptyslave, TCSANOW, &ptysettings);
#endif
/* Start the child */
/* forkpty() might be more convenient. */
if (!(child = fork())) {
/* Child process */
login_tty(ptyslave);
close(ptymaster);
execvp(argv[1], argv + 1);
perror("execvp() failed");
return 1;
}
close(ptyslave);
while (1) {
/* Now do a select() over stdin and ptymaster, and write anything that
* appears to ptymaster and stdout respectively. */
FD_ZERO(&readset);
FD_SET(0, &readset);
FD_SET(ptymaster, &readset);
select_time.tv_sec = 1;
select_time.tv_usec = 0;
selstatus = select(ptymaster + 1, &readset, NULL, NULL, &select_time);
if (selstatus > 0) {
/* TODO make sure it all gets written if a signal interrupts write(). */
if (FD_ISSET(0, &readset)) {
nread = read(0, buf, 4096);
if (nread > 0) {
write(ptymaster, buf, nread);
}
}
if (FD_ISSET(ptymaster, &readset)) {
nread = read(ptymaster, buf, 4096);
if (nread > 0) {
write(1, buf, nread);
}
}
}
/* Periodically check to see if we're done. */
/* TODO: catch SIGCHLD and only wait() if it is delivered. */
if (waitpid(child, &status, WNOHANG)) {
break;
}
/* If node sighup's us, pass it along. */
if (got_sighup) {
kill(child, SIGHUP);
}
}
/* Get any leftover output and clean up. */
/* FIXME looping over select() is pointless if there's only one fd that
* nothing's writing to. Just loop over read() until it's empty. */
while (1) {
FD_ZERO(&readset);
FD_SET(ptymaster, &readset);
select_time.tv_sec = 0;
select_time.tv_usec = 0;
if (select(ptymaster + 1, &readset, NULL, NULL, &select_time) > 0) {
nread = read(ptymaster, buf, 4096);
if (nread > 0) {
write(1, buf, nread);
}
else
break;
}
else
break;
}
close(ptymaster);
/* Return the child's exit status. */
if (WIFEXITED(status))
return WEXITSTATUS(status);
return 0;
}

28
quickrypt.c Normal file
View file

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crypt.h>
int main(int argc, char *argv[]) {
char clear[32], enc[120], *ptr;
fgets(&clear, 32, stdin);
if (!(ptr = strchr(&clear, '\n')))
return 1;
else
*ptr = '\0';
fgets(&enc, 120, stdin);
if (!(ptr = strchr(&enc, '\n')))
return 1;
else
*ptr = '\0';
ptr = crypt(clear, enc);
if (!strcmp(argv[argc - 1], "-s")) {
/* Option -s for "show": output the encrypted version. */
printf("%s\n", ptr);
return 0;
}
/* Otherwise this is a check. */
else if (!strcmp(ptr, enc))
return 0;
return 1;
}

485
rlgterm.js Normal file
View file

@ -0,0 +1,485 @@
/* rlgterm.js: Roguelike Gallery's driver for termemu.js */
// A state machine that keeps track of polling the server.
var ajaxstate = {
state: 0,
timerID: null,
clear: function () {
if (this.timerID != null) {
window.clearTimeout(this.timerID);
this.timerID = null;
}
},
set: function (ms) {
this.clear();
this.timerID = window.setTimeout(getData, ms);
},
gotdata: function () {
this.set(100);
this.state = 0;
},
gotnothing: function () {
if (this.state == 0) {
this.set(100);
this.state = 1;
}
else if (this.state == 1) {
this.set(300);
this.state = 2;
}
else if (this.state == 2) {
this.set(1000);
this.state = 3;
}
else {
this.set(5000);
this.state = 3;
}
},
posted: function () {
this.set(100);
this.state = 0;
}
};
function writeData(hexstr) {
var codenum;
var codes = [];
var nc;
var u8wait = 0; /* Stores bits from previous bytes of multibyte sequences. */
var expect = 0; /* The number of 10------ bytes expected. */
/* UTF-8 translation. */
for (var i = 0; i < hexstr.length; i += 2) {
nc = Number("0x" + hexstr.substr(i, 2));
if (nc < 0x7F) {
/* 0------- */
codes.push(nc);
/* Any incomplete sequence will be discarded. */
u8wait = 0;
expect = 0;
}
else if (nc < 0xC0) {
/* 10------ : part of a multibyte sequence */
if (expect > 0) {
u8wait <<= 6;
u8wait += (nc & 0x3F);
expect--;
if (expect == 0) {
codes.push(u8wait);
u8wait = 0;
}
}
else {
/* Assume an initial byte was missed. */
u8wait = 0;
}
}
/* These will all discard any incomplete sequence. */
else if (nc < 0xE0) {
/* 110----- : introduces 2-byte sequence */
u8wait = (nc & 0x1F);
expect = 1;
}
else if (nc < 0xF0) {
/* 1110---- : introduces 3-byte sequence */
u8wait = (nc & 0x0F);
expect = 2;
}
else if (nc < 0xF8) {
/* 11110--- : introduces 4-byte sequence */
u8wait = (nc & 0x07);
expect = 3;
}
else if (nc < 0xFC) {
/* 111110-- : introduces 5-byte sequence */
u8wait = (nc & 0x03);
expect = 4;
}
else if (nc < 0xFE) {
/* 1111110- : introduces 6-byte sequence */
u8wait = (nc & 0x01);
expect = 5;
}
else {
/* 1111111- : should never appear */
u8wait = 0;
expect = 0;
}
/* Supporting all 31 bits is probably overkill... */
}
termemu.write(codes);
return;
}
/* Processes a message from the server, returning true or false if it was a
* data message with or without data, null if not data. */
function processMsg(msg) {
var msglines = msg.split("\n");
var havedata = null;
if (!msglines[0])
return null;
if (msglines[0].charAt(0) == 'd') {
if (msglines[1]){
writeData(msglines[1]);
havedata = true;
}
else {
havedata = false;
}
}
else if (msglines[0] == "E1") {
logout();
}
else if (msglines[0].charAt(0) == "T") {
setTitle(msglines[1]);
}
else if (msglines[0] == "q1") {
logout();
}
else {
debug(1, "Unrecognized server message " + msglines[0]);
}
return havedata;
}
function getData() {
if (termemu.sessid == null)
return;
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = function () {
if (datareq.readyState == 4 && datareq.status == 200) {
var wasdata = processMsg(datareq.responseText);
if (wasdata != null) {
if (wasdata)
ajaxstate.gotdata();
else
ajaxstate.gotnothing();
}
return;
}
};
datareq.open('POST', '/feed', true);
datareq.send("id=" + termemu.sessid);
return;
}
function postResponseHandler() {
if (this.readyState == 4 && this.status == 200) {
// We might want to do something with wasdata someday.
var wasdata = processMsg(this.responseText);
ajaxstate.posted();
return;
}
}
function sendback(str) {
/* For responding to terminal queries. */
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = postResponseHandler;
datareq.open('POST', '/feed', true);
datareq.send("id=" + termemu.sessid + "&keys=" + str);
return;
}
/* ASCII values of keys 0-9. */
var numShifts = [41, 33, 64, 35, 36, 37, 94, 38, 42, 40];
var keyHexCodes = {
init: function () {
this[KeyboardEvent.DOM_VK_RETURN] = ["0d", "0d"];
this[KeyboardEvent.DOM_VK_SPACE] = ["20", "20"];
this[KeyboardEvent.DOM_VK_TAB] = ["09", "09"];
this[KeyboardEvent.DOM_VK_BACK_QUOTE] = ["60", "7e"];
this[KeyboardEvent.DOM_VK_OPEN_BRACKET] = ["5b", "7b"];
this[KeyboardEvent.DOM_VK_CLOSE_BRACKET] = ["5d", "7d"];
this[KeyboardEvent.DOM_VK_BACK_SLASH] = ["5c", "7c"];
this[KeyboardEvent.DOM_VK_SEMICOLON] = ["3b", "3a"];
this[KeyboardEvent.DOM_VK_QUOTE] = ["27", "22"];
this[KeyboardEvent.DOM_VK_COMMA] = ["2c", "3c"];
this[KeyboardEvent.DOM_VK_PERIOD] = ["2e", "3e"];
this[KeyboardEvent.DOM_VK_SLASH] = ["2f", "3f"];
this[KeyboardEvent.DOM_VK_EQUALS] = ["3d", "2b"];
this[KeyboardEvent.DOM_VK_SUBTRACT] = ["2d", "5f"];
this[KeyboardEvent.DOM_VK_BACK_SPACE] = ["08", "08"];
this[KeyboardEvent.DOM_VK_ESCAPE] = ["1b", "1b"];
/* Multi-char control sequences! Neat! */
this[KeyboardEvent.DOM_VK_PAGE_UP] = ["1b5b357e", "1b5b357e"];
this[KeyboardEvent.DOM_VK_PAGE_DOWN] = ["1b5b367e", "1b5b367e"];
this.appCursor(false);
this.appKeypad(false);
},
appCursor: function (on) {
if (on) {
this[KeyboardEvent.DOM_VK_LEFT] = ["1b4f44", "1b4f44"];
this[KeyboardEvent.DOM_VK_RIGHT] = ["1b4f43", "1b4f43"];
this[KeyboardEvent.DOM_VK_UP] = ["1b4f41", "1b4f41"];
this[KeyboardEvent.DOM_VK_DOWN] = ["1b4f42", "1b4f42"];
this[KeyboardEvent.DOM_VK_END] = ["1b4f46", "1b4f46"];
this[KeyboardEvent.DOM_VK_HOME] = ["1b4f48", "1b4f48"];
}
else {
this[KeyboardEvent.DOM_VK_LEFT] = ["1b5b44", "1b5b44"];
this[KeyboardEvent.DOM_VK_RIGHT] = ["1b5b43", "1b5b43"];
this[KeyboardEvent.DOM_VK_UP] = ["1b5b41", "1b5b41"];
this[KeyboardEvent.DOM_VK_DOWN] = ["1b5b42", "1b5b42"];
this[KeyboardEvent.DOM_VK_END] = ["1b5b46", "1b5b46"];
this[KeyboardEvent.DOM_VK_HOME] = ["1b5b48", "1b5b48"];
}
},
appKeypad: function (on) {
/* In theory, these should produce either numerals or the k[a-c][1-3]
* sequences. Since we can't count on the terminfo description actually
* containing those sequences, pretend they're just arrow keys etc.
*/
this[KeyboardEvent.DOM_VK_NUMPAD1] = ["1b4f46", "1b4f46"];
this[KeyboardEvent.DOM_VK_NUMPAD2] = ["1b4f42", "1b4f42"];
this[KeyboardEvent.DOM_VK_NUMPAD3] = ["1b5b367e", "1b5b367e"];
this[KeyboardEvent.DOM_VK_NUMPAD4] = ["1b4f44", "1b4f44"];
this[KeyboardEvent.DOM_VK_NUMPAD5] = ["1b5b45", "1b5b45"];
this[KeyboardEvent.DOM_VK_NUMPAD6] = ["1b4f43", "1b4f43"];
this[KeyboardEvent.DOM_VK_NUMPAD7] = ["1b4f48", "1b4f48"];
this[KeyboardEvent.DOM_VK_NUMPAD8] = ["1b4f41", "1b4f41"];
this[KeyboardEvent.DOM_VK_NUMPAD9] = ["1b5b357e", "1b5b357e"];
return;
}
};
function sendkey(ev) {
if (termemu.sessid == null)
return;
var keynum = ev.keyCode;
var code;
if (keynum >= ev.DOM_VK_A && keynum <= ev.DOM_VK_Z) {
/* Letters. This assumes the codes are 65-90. */
if (ev.ctrlKey)
keynum -= 64;
else if (!ev.shiftKey)
keynum += 32;
code = keynum.toString(16);
if (code.length < 2)
code = "0" + code;
}
else if (keynum >= ev.DOM_VK_0 && keynum <= ev.DOM_VK_9) {
/* The number row, NOT the numpad. */
if (ev.shiftKey) {
code = numShifts[keynum - 48].toString(16);
}
else {
code = keynum.toString(16);
}
}
else if (keynum in keyHexCodes) {
if (ev.shiftKey)
code = keyHexCodes[keynum][1];
else
code = keyHexCodes[keynum][0];
}
else if (keynum == ev.DOM_VK_SHIFT || keynum == ev.DOM_VK_CONTROL ||
keynum == ev.DOM_VK_ALT || keynum == ev.DOM_VK_CAPS_LOCK) {
return;
}
else {
debug(1, "Ignoring keycode " + keynum);
return;
}
if (termemu.sessid != null)
ev.preventDefault();
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = postResponseHandler;
datareq.open('POST', '/feed', true);
datareq.send("id=" + termemu.sessid + "&keys=" + code);
return;
}
var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c",
';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e"
}
function vkey(c) {
if (termemu.sessid == null)
return;
var keystr;
if (c.match(/^[a-z]$/)) {
if (termemu.ctrlp()) {
var n = c.charCodeAt(0) - 96;
keystr = n.toString(16);
if (keystr.length < 2)
keystr = "0" + keystr;
}
else if (termemu.shiftp())
keystr = c.toUpperCase().charCodeAt(0).toString(16);
else
keystr = c.charCodeAt(0).toString(16);
}
else if (c.match(/^[0-9]$/)) {
if (termemu.shiftp())
keystr = numShifts[c.charCodeAt(0) - 48].toString(16);
else
keystr = c.charCodeAt(0).toString(16);
}
else if (c == '\n')
keystr = "0a";
else if (c == '\t')
keystr = "09";
else if (c == '\b')
keystr = "08";
else if (c == ' ')
keystr = "20";
else if (c in charshifts) {
if (termemu.shiftp())
keystr = charshifts[c];
else
keystr = c.charCodeAt(0).toString(16);
}
else
return;
//writeData("Sending " + keystr);
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = postResponseHandler;
datareq.open('POST', '/feed', true);
datareq.send("id=" + termemu.sessid + "&keys=" + keystr);
return;
}
function setup() {
keyHexCodes.init();
termemu.init("termwrap");
setTitle("Not connected.");
return;
}
function toggleshift() {
termemu.toggleshift();
keydiv = document.getElementById("shiftkey");
if (termemu.shiftp())
keydiv.className = "keysel";
else
keydiv.className = "key";
return;
}
function togglectrl() {
termemu.togglectrl();
keydiv = document.getElementById("ctrlkey");
if (termemu.ctrlp())
keydiv.className = "keysel";
else
keydiv.className = "key";
return;
}
function formlogin(ev) {
ev.preventDefault();
if (termemu.sessid != null)
return;
var formname = document.getElementById("input_name").value;
var formpass = document.getElementById("input_pw").value;
var formgame = document.getElementById("input_game").value;
var formdata = "game=" + encodeURIComponent(formgame) + "&name=" + encodeURIComponent(formname) + "&pw=" + encodeURIComponent(formpass);
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) {
var datalines = req.responseText.split("\n");
if (datalines[0] == 'l1') {
/* Success */
termemu.sessid = datalines[1];
setTitle("Playing as " + formname);
debug(1, "Logged in with id " + termemu.sessid);
document.getElementById("loginform").style.display = "none";
getData();
}
else {
debug(1, "Could not start game: " + datalines[1]);
document.getElementById("input_name").value = "";
document.getElementById("input_pw").value = "";
}
}
};
req.open('POST', '/login', true);
req.send(formdata);
return;
}
function logout() {
if (termemu.sessid == null)
return;
termemu.sessid = null;
setTitle("Game over.");
document.getElementById("loginform").style.display = "block";
return;
}
function stop() {
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) {
processMsg(req.responseText);
return;
}
};
req.open('POST', '/feed', true);
req.send("id=" + termemu.sessid + "&quit=quit");
return;
}
function debug(level, msg) {
if (level < debugSuppress)
return;
var msgdiv = document.createElement("div");
var msgtext = document.createTextNode(msg);
msgdiv.appendChild(msgtext);
document.getElementById("debug").appendChild(msgdiv);
return;
}
function textsize(larger) {
var cssSize = termemu.view.style.fontSize;
if (!cssSize) {
return;
}
var match = cssSize.match(/\d*/);
if (!match) {
return;
}
var csize = Number(match[0]);
var nsize;
if (larger) {
if (csize >= 48)
nsize = 48;
else if (csize >= 20)
nsize = csize + 4;
else if (csize >= 12)
nsize = csize + 2;
else if (csize >= 8)
nsize = csize + 1;
else
nsize = 8;
}
else {
if (csize <= 8)
nsize = 8;
else if (csize <= 12)
nsize = csize - 1;
else if (csize <= 20)
nsize = csize - 2;
else if (csize <= 48)
nsize = csize - 4;
else
nsize = 48;
}
document.getElementById("term").style.fontSize = nsize.toString() + "px";
termemu.resize();
debug(1, "Changing font size to " + nsize.toString());
return;
}
function bell(on) {
var imgnode = document.getElementById("bell");
if (on) {
imgnode.style.visibility = "visible";
window.setTimeout(bell, 1500, false);
}
else
imgnode.style.visibility = "hidden";
return;
}

478
shterm.js Normal file
View file

@ -0,0 +1,478 @@
// A state machine that keeps track of polling the server.
var ajaxstate = {
state: 0,
timerID: null,
clear: function () {
if (this.timerID != null) {
window.clearTimeout(this.timerID);
this.timerID = null;
}
},
set: function (ms) {
this.clear();
this.timerID = window.setTimeout(getData, ms);
},
gotdata: function () {
this.set(100);
this.state = 0;
},
gotnothing: function () {
if (this.state == 0) {
this.set(100);
this.state = 1;
}
else if (this.state == 1) {
this.set(300);
this.state = 2;
}
else if (this.state == 2) {
this.set(1000);
this.state = 3;
}
else {
this.set(5000);
this.state = 3;
}
},
posted: function () {
this.set(100);
this.state = 0;
}
};
function writeData(hexstr) {
var codenum;
var codes = [];
var nc;
var u8wait = 0; /* Stores bits from previous bytes of multibyte sequences. */
var expect = 0; /* The number of 10------ bytes expected. */
/* UTF-8 translation. */
for (var i = 0; i < hexstr.length; i += 2) {
nc = Number("0x" + hexstr.substr(i, 2));
if (nc < 0x7F) {
/* 0------- */
codes.push(nc);
/* Any incomplete sequence will be discarded. */
u8wait = 0;
expect = 0;
}
else if (nc < 0xC0) {
/* 10------ : part of a multibyte sequence */
if (expect > 0) {
u8wait <<= 6;
u8wait += (nc & 0x3F);
expect--;
if (expect == 0) {
codes.push(u8wait);
u8wait = 0;
}
}
else {
/* Assume an initial byte was missed. */
u8wait = 0;
}
}
/* These will all discard any incomplete sequence. */
else if (nc < 0xE0) {
/* 110----- : introduces 2-byte sequence */
u8wait = (nc & 0x1F);
expect = 1;
}
else if (nc < 0xF0) {
/* 1110---- : introduces 3-byte sequence */
u8wait = (nc & 0x0F);
expect = 2;
}
else if (nc < 0xF8) {
/* 11110--- : introduces 4-byte sequence */
u8wait = (nc & 0x07);
expect = 3;
}
else if (nc < 0xFC) {
/* 111110-- : introduces 5-byte sequence */
u8wait = (nc & 0x03);
expect = 4;
}
else if (nc < 0xFE) {
/* 1111110- : introduces 6-byte sequence */
u8wait = (nc & 0x01);
expect = 5;
}
else {
/* 1111111- : should never appear */
u8wait = 0;
expect = 0;
}
/* Supporting all 31 bits is probably overkill... */
}
termemu.write(codes);
return;
}
function getData() {
if (!termemu.alive)
return;
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = function () {
if (datareq.readyState == 4 && datareq.status == 200) {
var datalines = datareq.responseText.split("\n");
if (!datalines[0]) {
return;
}
else if (datalines[0] == "E1") {
termemu.alive = false;
return;
}
else if (datalines[0].charAt(0) != 'd') {
return;
}
if (datalines[1]) {
writeData(datalines[1]);
ajaxstate.gotdata();
}
else {
ajaxstate.gotnothing();
}
return;
}
};
datareq.open('GET', '/feed', true);
datareq.send(null);
return;
}
function postResponseHandler() {
if (this.readyState == 4 && this.status == 200) {
var datalines = this.responseText.split("\n");
if (!datalines[0])
return;
else if (datalines[0] == "E1") {
termemu.alive = false;
return;
}
else if (datalines[0].charAt(0) != "d")
return;
/* It is a data message */
if (datalines[1]) {
writeData(datalines[1]);
}
ajaxstate.posted();
return;
}
}
function sendback(str) {
/* For responding to terminal queries. */
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = postResponseHandler;
datareq.open('POST', '/feed', true);
datareq.send("keys=" + str);
return;
}
/* ASCII values of keys 0-9. */
var numShifts = [41, 33, 64, 35, 36, 37, 94, 38, 42, 40];
var keyHexCodes = {
init: function () {
this[KeyboardEvent.DOM_VK_RETURN] = ["0d", "0d"];
this[KeyboardEvent.DOM_VK_SPACE] = ["20", "20"];
this[KeyboardEvent.DOM_VK_TAB] = ["09", "09"];
this[KeyboardEvent.DOM_VK_BACK_QUOTE] = ["60", "7e"];
this[KeyboardEvent.DOM_VK_OPEN_BRACKET] = ["5b", "7b"];
this[KeyboardEvent.DOM_VK_CLOSE_BRACKET] = ["5d", "7d"];
this[KeyboardEvent.DOM_VK_BACK_SLASH] = ["5c", "7c"];
this[KeyboardEvent.DOM_VK_SEMICOLON] = ["3b", "3a"];
this[KeyboardEvent.DOM_VK_QUOTE] = ["27", "22"];
this[KeyboardEvent.DOM_VK_COMMA] = ["2c", "3c"];
this[KeyboardEvent.DOM_VK_PERIOD] = ["2e", "3e"];
this[KeyboardEvent.DOM_VK_SLASH] = ["2f", "3f"];
this[KeyboardEvent.DOM_VK_EQUALS] = ["3d", "2b"];
this[KeyboardEvent.DOM_VK_SUBTRACT] = ["2d", "5f"];
this[KeyboardEvent.DOM_VK_BACK_SPACE] = ["08", "08"];
this[KeyboardEvent.DOM_VK_ESCAPE] = ["1b", "1b"];
this[KeyboardEvent.DOM_VK_PAGE_UP] = ["1b5b357e", "1b5b357e"];
this[KeyboardEvent.DOM_VK_PAGE_DOWN] = ["1b5b367e", "1b5b367e"];
this.appCursor(false);
this.appKeypad(false);
},
/* Multi-char control sequences! Neat! */
appCursor: function (on) {
/* Aren't special keys vile? */
if (on) {
this[KeyboardEvent.DOM_VK_LEFT] = ["1b4f44", "1b4f44"];
this[KeyboardEvent.DOM_VK_RIGHT] = ["1b4f43", "1b4f43"];
this[KeyboardEvent.DOM_VK_UP] = ["1b4f41", "1b4f41"];
this[KeyboardEvent.DOM_VK_DOWN] = ["1b4f42", "1b4f42"];
this[KeyboardEvent.DOM_VK_END] = ["1b4f46", "1b4f46"];
this[KeyboardEvent.DOM_VK_HOME] = ["1b4f48", "1b4f48"];
}
else {
this[KeyboardEvent.DOM_VK_LEFT] = ["1b5b44", "1b5b44"];
this[KeyboardEvent.DOM_VK_RIGHT] = ["1b5b43", "1b5b43"];
this[KeyboardEvent.DOM_VK_UP] = ["1b5b41", "1b5b41"];
this[KeyboardEvent.DOM_VK_DOWN] = ["1b5b42", "1b5b42"];
this[KeyboardEvent.DOM_VK_END] = ["1b5b46", "1b5b46"];
this[KeyboardEvent.DOM_VK_HOME] = ["1b5b48", "1b5b48"];
}
},
appKeypad: function (on) {
/* In theory, these should produce either numerals or the k[a-c][1-3]
* sequences. Since we can't count on the terminfo description actually
* containing those sequences, pretend they're just arrow keys etc.
*/
this[KeyboardEvent.DOM_VK_NUMPAD1] = ["1b4f46", "1b4f46"];
this[KeyboardEvent.DOM_VK_NUMPAD2] = ["1b4f42", "1b4f42"];
this[KeyboardEvent.DOM_VK_NUMPAD3] = ["1b5b367e", "1b5b367e"];
this[KeyboardEvent.DOM_VK_NUMPAD4] = ["1b4f44", "1b4f44"];
this[KeyboardEvent.DOM_VK_NUMPAD5] = ["1b5b45", "1b5b45"];
this[KeyboardEvent.DOM_VK_NUMPAD6] = ["1b4f43", "1b4f43"];
this[KeyboardEvent.DOM_VK_NUMPAD7] = ["1b4f48", "1b4f48"];
this[KeyboardEvent.DOM_VK_NUMPAD8] = ["1b4f41", "1b4f41"];
this[KeyboardEvent.DOM_VK_NUMPAD9] = ["1b5b357e", "1b5b357e"];
return;
}
};
function sendkey(ev) {
var keynum = ev.keyCode;
var code;
if (keynum >= ev.DOM_VK_A && keynum <= ev.DOM_VK_Z) {
/* Letters. This assumes the codes are 65-90. */
if (ev.ctrlKey)
keynum -= 64;
else if (!ev.shiftKey)
keynum += 32;
code = keynum.toString(16);
if (code.length < 2)
code = "0" + code;
}
else if (keynum >= ev.DOM_VK_0 && keynum <= ev.DOM_VK_9) {
/* The number row. */
if (ev.shiftKey) {
code = numShifts[keynum - 48].toString(16);
}
else {
code = keynum.toString(16);
}
}
else if (keynum in keyHexCodes) {
if (ev.shiftKey)
code = keyHexCodes[keynum][1];
else
code = keyHexCodes[keynum][0];
}
else if (keynum == ev.DOM_VK_SHIFT || keynum == ev.DOM_VK_CONTROL ||
keynum == ev.DOM_VK_ALT || keynum == ev.DOM_VK_CAPS_LOCK) {
return;
}
else {
debug(1, "Ignoring keycode " + keynum);
return;
}
if (termemu.alive)
ev.preventDefault();
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = postResponseHandler;
datareq.open('POST', '/feed', true);
datareq.send("keys=" + code);
//dkey(code);
return;
}
var charshifts = { '-': "5f", '=': "2b", '[': "7b", ']': "7d", '\\': "7c",
';': "3a", '\'': "22", ',': "3c", '.': "3e", '/': "3f", '`': "7e"
}
function vkey(c) {
var keystr;
if (c.match(/^[a-z]$/)) {
if (termemu.ctrlp()) {
var n = c.charCodeAt(0) - 96;
keystr = n.toString(16);
if (keystr.length < 2)
keystr = "0" + keystr;
}
else if (termemu.shiftp())
keystr = c.toUpperCase().charCodeAt(0).toString(16);
else
keystr = c.charCodeAt(0).toString(16);
}
else if (c.match(/^[0-9]$/)) {
if (termemu.shiftp())
keystr = numShifts[c.charCodeAt(0) - 48].toString(16);
else
keystr = c.charCodeAt(0).toString(16);
}
else if (c == '\n')
keystr = "0a";
else if (c == '\t')
keystr = "09";
else if (c == '\b')
keystr = "08";
else if (c == ' ')
keystr = "20";
else if (c in charshifts) {
if (termemu.shiftp())
keystr = charshifts[c];
else
keystr = c.charCodeAt(0).toString(16);
}
else
return;
//writeData("Sending " + keystr);
var datareq = new XMLHttpRequest();
datareq.onreadystatechange = postResponseHandler;
datareq.open('POST', '/feed', true);
datareq.send("keys=" + keystr);
return;
}
function setup() {
keyHexCodes.init();
termemu.init("termwrap");
setTitle("Not connected.");
return;
}
function toggleshift() {
termemu.toggleshift();
keydiv = document.getElementById("shiftkey");
if (termemu.shiftp())
keydiv.className = "keysel";
else
keydiv.className = "key";
return;
}
function togglectrl() {
termemu.togglectrl();
keydiv = document.getElementById("ctrlkey");
if (termemu.ctrlp())
keydiv.className = "keysel";
else
keydiv.className = "key";
return;
}
function login() {
if (termemu.alive)
return;
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) {
var datalines = req.responseText.split("\n");
if (datalines[0] == 'l1') {
/* Success */
termemu.alive = true;
setTitle("Logged in");
debug(1, "Logged in with id " + datalines[1]);
getData();
return;
}
return;
}
};
req.open('POST', '/login', true);
req.send("login=login");
return;
}
function stop() {
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) {
var datalines = req.responseText.split("\n");
/* Figure out whether or not it worked. */
termemu.alive = false;
return;
}
};
req.open('POST', '/feed', true);
req.send("quit=quit");
return;
}
function setTitle(tstr) {
var titlespan = document.getElementById("ttitle");
var tnode = document.createTextNode(tstr);
if (titlespan.childNodes.length == 0)
titlespan.appendChild(tnode);
else
titlespan.replaceChild(tnode, titlespan.childNodes[0]);
return;
}
function debug(level, msg) {
if (level < debugSuppress)
return;
var msgdiv = document.createElement("div");
var msgtext = document.createTextNode(msg);
msgdiv.appendChild(msgtext);
document.getElementById("debug").appendChild(msgdiv);
return;
}
/* This should be a termemu method. */
function textsize(larger) {
var cssSize = document.getElementById("term").style.fontSize;
if (!cssSize)
return;
var match = cssSize.match(/\d*/);
if (!match)
return;
var csize = Number(match[0]);
var nsize;
if (larger) {
if (csize >= 48)
nsize = 48;
else if (csize >= 20)
nsize = csize + 4;
else if (csize >= 12)
nsize = csize + 2;
else if (csize >= 8)
nsize = csize + 1;
else
nsize = 8;
}
else {
if (csize <= 8)
nsize = 8;
else if (csize <= 12)
nsize = csize - 1;
else if (csize <= 20)
nsize = csize - 2;
else if (csize <= 48)
nsize = csize - 4;
else
nsize = 48;
}
document.getElementById("term").style.fontSize = nsize.toString() + "px";
termemu.resize();
debug(1, "Changing font size to " + nsize.toString());
return;
}
function bell(on) {
var imgnode = document.getElementById("bell");
if (on) {
imgnode.style.visibility = "visible";
window.setTimeout(bell, 1500, false);
}
else
imgnode.style.visibility = "hidden";
return;
}
function dkey(codestr) {
var dstr = "Keystring: ";
for (var i = 0; i < codestr.length; i += 2) {
code = Number("0x" + codestr.substr(i, 2));
if (code < 32 || (code >= 127 && code < 160))
dstr += "\\x" + code.toString(16);
else
dstr += String.fromCharCode(code);
}
debug(1, dstr);
return;
}

1105
termemu.js Normal file

File diff suppressed because it is too large Load diff

90
tty.css Normal file
View file

@ -0,0 +1,90 @@
div#top {
font-size: 1.2em;
text-align: center;
margin: 0.2em;
}
img#bell {
visibility: hidden;
margin-left: 2em;
}
div.keyrow {
font-size: 1.2em;
}
div.keyrow > div:first-child {
clear: left;
}
div.key {
width: 2em;
height: 2em;
float: left;
border: 2px solid black;
margin: 0.2em;
text-align: center;
}
div.keysel {
width: 2em;
height: 2em;
float: left;
border: 2px solid black;
margin: 0.2em;
text-align: center;
background-color: #C0FFC0;
}
div#shiftkey {
width: 4em;
}
div#ctrlkey {
width: 3em;
}
div#spacebar {
width: 8em;
margin-left: 12em;
}
div#termwrap {
margin: 0.5em auto;
padding: 1em;
background-color: #808080;
border: 0.25em solid #202020;
border-radius: 1em;
}
div#inwrap {
overflow-y: scroll;
}
div#term {
display: table;
font-size: 12px;
font-family: monospace;
white-space: pre;
}
div#term > div {
display: table-row-group;
}
div.termrow {
display: table-row;
}
span.termcell {
display: table-cell;
}
div.rbutton {
float: right;
clear: right;
border: 2px solid black;
text-align: center;
margin: 0.2em;
font-size: 1.2em;
padding: 0.1em;
}
div.rbutton > span {
border: 1px solid black;
padding: 0 0.1em;
}
div#debug {
width: 100%;
height: 10em;
overflow: scroll;
white-space: pre;
clear: both;
}
div#debug > div {
font-family: monospace;
}

329
webtty.js Executable file
View file

@ -0,0 +1,329 @@
#!/usr/bin/env node
var http = require('http');
var url = require('url');
var path = require('path');
var fs = require('fs');
//var tty = require("tty");
var child_process = require("child_process");
var serveStaticRoot = "/home/elwin/hk/nodejs/rlg/s/";
var sessions = {};
/* Constructor for TermSessions. Note that it opens the terminal and
* adds itself to the sessions dict.
*/
function TermSession(sessid) {
//var pterm = tty.open("/bin/bash");
this.child = child_process.spawn("./ptywreck/ptyhelperC", ["bash"]);
var ss = this;
/* Eventually we'll need to make sure the sessid isn't in use yet. */
this.sessid = sessid;
//this.ptmx = pterm[0];
//this.child = pterm[1];
this.alive = true;
this.data = [];
this.child.stdout.on("data", function (buf) {
ss.data.push(buf);
});
this.child.stderr.on("data", function (buf) {
ss.data.push(buf);
});
this.child.on("exit", function (code, signal) {
ss.exitcode = (code != null ? code : 255);
ss.alive = false;
/* 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;
var nbuf = new Buffer(pos);
var tptr;
pos = 0;
while (this.data.length > 0) {
tptr = this.data.shift();
tptr.copy(nbuf, pos);
pos += tptr.length;
}
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.ptmx.destroy();
/* 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.");
};
sessions[sessid] = this;
}
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 login(req, res, formdata) {
var resheaders = {'Content-Type': 'text/plain'};
var sessid = randkey();
var nsession = new TermSession(sessid);
resheaders["Set-Cookie"] = "ID=" + sessid;
res.writeHead(200, resheaders);
res.write("l1\n" + sessid + "\n");
res.end();
console.log("Started new session with key " + sessid + ", pid " + nsession.child.pid);
return;
}
function findTermSession(req) {
var cookies = getCookies(req);
if ("id" in cookies) {
var sessid = cookies["id"];
if (sessid in sessions) {
return sessions[sessid];
}
}
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);
path.exists(realname, function (exists) {
var resheaders = {};
if (!exists || !extension || extension == ".html")
resheaders["Content-Type"] = "text/html";
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) {
/* Not nice, not sensible. First see if it's readable, then respond
* 200 or 500. Don't throw nasty errors. */
res.writeHead(200, resheaders);
fs.readFile(realname, function (error, data) {
if (error) throw error;
res.write(data);
res.end();
});
}
else {
res.writeHead(404, resheaders);
res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>" + nname + " Not Found</h1></body></html>\n");
res.end();
}
});
return;
}
function readFeed(res, term) {
res.writeHead(200, { "Content-Type": "text/plain" });
if (term) {
var result = term.read();
if (result == null)
resultstr = "";
else
resultstr = result.toString("hex");
res.write("d" + resultstr.length.toString() + "\n" + resultstr + "\n");
}
else {
//console.log("Where's the term?");
res.write("d0\n\n");
}
}
var errorcodes = [ "Generic Error", "Not logged in", "Invalid data" ];
function sendError(res, ecode) {
res.writeHead(200, { "Content-Type": "text/plain" });
if (ecode < errorcodes.length && ecode > 0)
res.write("E" + ecode + '\n' + errorcodes[ecode] + '\n');
else
res.write("E0\nGeneric Error\n");
res.end();
}
function handler(req, res) {
/* 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() {
var target = url.parse(req.url).pathname;
var cterm = findTermSession(req);
/* First figure out if the client is POSTing to a command interface. */
if (req.method == 'POST') {
formdata = getFormValues(reqbody);
if (target == '/feed') {
if (!cterm) {
sendError(res, 1);
return;
}
if (formdata["quit"] == "quit") {
/* The client wants to quit. */
// FIXME need to send a message back to the client
cterm.close();
}
else if (formdata["keys"]) {
/* process the keys */
hexstr = formdata["keys"][0].replace(/[^0-9a-f]/gi, "");
if (hexstr.length % 2 != 0) {
sendError(res, 2);
return;
}
keybuf = new Buffer(hexstr, "hex");
cterm.write(keybuf);
}
readFeed(res, cterm);
res.end();
}
else if (target == "/login") {
login(req, res, formdata);
}
else {
res.writeHead(405, resheaders);
res.end();
}
}
else if (req.method == 'GET' || req.method == 'HEAD') {
if (target == '/feed') {
if (!cterm) {
sendError(res, 1);
return;
}
readFeed(res, cterm);
res.end();
}
/* Default page, create a new term */
/* FIXME New term not created anymore, is a special case still needed? */
else if (target == '/') {
serveStatic(req, res, "/");
}
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);
}
process.on("exit", function () {
for (var sessid in sessions) {
if (sessions[sessid].alive)
sessions[sessid].child.kill('SIGHUP');
}
console.log("Quitting...");
return;
});
process.env["TERM"] = "xterm-256color";
http.createServer(handler).listen(8080, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8080/');

531
webttyd.js Executable file
View file

@ -0,0 +1,531 @@
#!/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 tty = require("tty");
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 */
//var pterm = tty.open(games[game].path, ["-n", user.toString()]);
/* 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.ptmx = pterm[0];
//this.child = pterm[1];
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.ptmx.destroy();
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);
return;
}
// check for an existing game
fs.readdir(progressdir, function(err, files) {
if (!err) {
var fre = RegExp("^" + username + ":");
for (var i = 0; i < files.length; i++) {
if (files[i].match(fre)) {
sendError(res, 4, null);
return;
}
}
}
// If progressdir isn't readable, start a new game anyway.
startgame();
});
}
/* Look for the user in the password file */
fs.readFile(passwdfile, "utf8", function(err, data) {
if (err) {
sendError(res, 3);
console.log("Can't authenticate: " + err.toString());
return;
}
var dlines = data.split('\n');
for (var n = 0; n < dlines.length; n++) {
var fields = dlines[n].split(':');
if (fields[0] == username) {
// check the password with the quickrypt utility
checker = require('child_process').spawn("/bin/quickrypt")
checker.on("exit", checkit);
checker.stdin.end(password + '\n' + fields[2] + '\n', "utf8");
return;
}
}
sendError(res, 3);
console.log("Attempted login by nonexistent user " + username);
});
return;
}
function logout(term, res) {
if (!term.alive) {
sendError(res, 1, null);
return;
}
cterm.close();
var resheaders = {'Content-Type': 'text/plain'};
res.writeHead(200, resheaders);
res.write("q1\n\n");
res.end();
return;
}
function findTermSession(formdata) {
if ("id" in formdata) {
var sessid = formdata["id"][0];
if (sessid in sessions) {
return sessions[sessid];
}
}
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);
path.exists(realname, function (exists) {
var resheaders = {};
if (!exists || !extension || extension == ".html")
resheaders["Content-Type"] = "text/html";
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);
res.write(data);
res.end();
}
});
}
else {
res.writeHead(404, resheaders);
res.write("<html><head><title>" + nname + "</title></head>\n<body><h1>" + nname + " Not Found</h1></body></html>\n");
res.end();
}
});
return;
}
function readFeed(res, term) {
if (term) {
var result = term.read();
res.writeHead(200, { "Content-Type": "text/plain" });
if (result == null)
resultstr = "";
else
resultstr = result.toString("hex");
if (result == null && !term.alive) {
/* Child has terminated and data is flushed. */
res.write("q1\n\n");
}
else
res.write("d" + resultstr.length.toString() + "\n" + resultstr + "\n");
res.end();
}
else {
//console.log("Where's the term?");
sendError(res, 1, null);
}
}
var errorcodes = [ "Generic Error", "Not logged in", "Invalid data",
"Login failed", "Already playing", "Game launch failed" ];
function sendError(res, ecode, msg) {
res.writeHead(200, { "Content-Type": "text/plain" });
if (ecode < errorcodes.length && ecode > 0) {
var emsg = errorcodes[ecode];
if (msg)
emsg += ": " + msg;
res.write("E" + ecode + '\n' + emsg + '\n');
}
else
res.write("E0\nGeneric Error\n");
res.end();
}
function handler(req, res) {
/* 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() {
formdata = getFormValues(reqbody);
var target = url.parse(req.url).pathname;
var cterm = findTermSession(formdata);
/* First figure out if the client is POSTing to a command interface. */
if (req.method == 'POST') {
if (target == '/feed') {
if (!cterm) {
sendError(res, 1, null);
return;
}
if ("quit" in formdata) {
/* The client wants to terminate the process. */
logout(cterm, res);
}
else if (formdata["keys"]) {
/* process the keys */
hexstr = formdata["keys"][0].replace(/[^0-9a-f]/gi, "");
if (hexstr.length % 2 != 0) {
sendError(res, 2, "incomplete byte");
return;
}
keybuf = new Buffer(hexstr, "hex");
cterm.write(keybuf);
}
readFeed(res, cterm);
}
else if (target == "/login") {
login(req, res, formdata);
}
else {
res.writeHead(405, resheaders);
res.end();
}
}
else if (req.method == 'GET' || req.method == 'HEAD') {
if (target == '/feed') {
if (!cterm) {
sendError(res, 1, null);
return;
}
readFeed(res, cterm);
}
/* Default page, create a new term */
/* FIXME New term not created anymore, is a special case still needed? */
else if (target == '/') {
serveStatic(req, res, "/");
}
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);
}
process.on("exit", function () {
for (var sessid in sessions) {
if (sessions[sessid].alive)
sessions[sessid].child.kill('SIGHUP');
}
console.log("Quitting...");
return;
});
/* Initialization STARTS HERE */
process.env["TERM"] = "xterm-256color";
if (process.getuid() != 0) {
console.log("Not running as root, cannot chroot.");
process.exit(1);
}
try {
process.chdir(chrootDir);
}
catch (err) {
console.log("Cannot enter " + chrootDir + " : " + err);
process.exit(1);
}
try {
daemon.chroot(chrootDir);
}
catch (err) {
console.log("chroot to " + chrootDir + " failed: " + err);
process.exit(1);
}
try {
// drop gid first, that requires UID=0
process.setgid(dropToGID);
process.setuid(dropToUID);
}
catch (err) {
console.log("Could not drop permissions: " + err);
process.exit(1);
}
http.createServer(handler).listen(8080, "127.0.0.1");
console.log('webttyd running at http://127.0.0.1:8080/');