var fs = require('fs');
var http = require('http');
var net = require('net');
var socketIo = require('socket.io');
var nodeStatic = require('node-static');

var ALLOWED_HOSTS = {
    'primary-key.mit.edu': true,
    'xvm-remote.mit.edu': true,
    'zygorthian-space-raiders.mit.edu': true
};
var REMCTL_PORT = 4373;

function isAllowedEndpoint(host, port) {
    return ALLOWED_HOSTS[host] && port === REMCTL_PORT;
}

var file = new nodeStatic.Server(__dirname + '/static/');
var app = http.createServer(function(req, res) {
    req.addListener('end', function() {
        file.serve(req, res);
    });
});

var io = socketIo.listen(app);

io.sockets.on('connection', function(socket) {
    var tcp = null;

    socket.on('init', function(host, port) {
        if (tcp !== null)
            return;  // Ignore.
        if (!isAllowedEndpoint(host, port)) {
            socket.emit('init-error', 'Bad host/port');
            socket.disconnect();
            return;
        }

        // Bah.
        if (/^v0\.6\./.exec(process.version)) {
            tcp = net.createConnection(port, host, function() {
                socket.emit('ready');
            });
        } else {
            tcp = net.createConnection({host: host, port: port}, function() {
                socket.emit('ready');
            });
        }

        tcp.on('data', function(buffer) {
            socket.emit('data', buffer.toString('base64'));
        });
        tcp.on('end', function() {
            socket.emit('end');
        });
        tcp.on('error', function(e) {
            // TODO: Send a more structured error along?
            socket.emit('error', e.toString());
        });
        tcp.on('close', function() {
            socket.emit('close');
            socket.disconnect();
        });
    });

    socket.on('write', function(data) {
        if (tcp === null)
            return;  // Ignore.
        tcp.write(data, 'base64');
    });
    socket.on('end', function() {
        if (tcp === null)
            return;  // Ignore.
        tcp.end();
    });
});

if (process.env.OPENSHIFT_INTERNAL_PORT) {
    // Meh.
    io.set('log level', 1);
}
app.listen(process.env.OPENSHIFT_INTERNAL_PORT || 8080, process.env.OPENSHIFT_INTERNAL_IP || '127.0.0.1');

@@ -589,24 +589,33 @@
 res);%0A    %7D)
+.resume()
 ;%0A%7D);%0A%0Avar i

Fix ctlfish under node 0.10