From b50c069f0f824f47234d29d8784489a31c0b5d40 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 13 Aug 2011 14:56:27 +0200 Subject: cholerab knut: initial commit --- cholerab/knut/index.js | 63 ++++++++++++++++++++++++++++++++++ cholerab/knut/plugs/ttycnser/bin/login | 4 +++ cholerab/knut/plugs/ttycnser/bin/write | 4 +++ cholerab/knut/plugs/ttycnser/index | 1 + cholerab/knut/src/io/slurp.js | 38 ++++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100755 cholerab/knut/index.js create mode 100755 cholerab/knut/plugs/ttycnser/bin/login create mode 100755 cholerab/knut/plugs/ttycnser/bin/write create mode 120000 cholerab/knut/plugs/ttycnser/index create mode 100644 cholerab/knut/src/io/slurp.js (limited to 'cholerab/knut') diff --git a/cholerab/knut/index.js b/cholerab/knut/index.js new file mode 100755 index 00000000..e7278678 --- /dev/null +++ b/cholerab/knut/index.js @@ -0,0 +1,63 @@ +#! /usr/bin/env node + +var host = '0.0.0.0'; +var port = 42101; + +var pipe = '/tmp/krebscode.painload.cholerab.ttycnser.' + process.env.LOGNAME; + +var fs = require('fs'); +var http = require('http'); +var slurp = require('./src/io/slurp'); +var spawn = require('child_process').spawn; + +var plugs = process.argv.slice(2); + +http.createServer(function (req, res) { + return slurp(req, function (data) { + try { + var message = JSON.parse(data); + } catch (exn) { + console.error(exn.stack); + }; + if (message) { + plugs.forEach(function (plug) { + + var env = JSON.parse(JSON.stringify(process.env)); + Object.keys(message).forEach(function (key) { + env[key] = message[key]; + }); + + var child = spawn(__dirname + '/plugs/' + plug + '/index', [], { + env: env + }); + + child.stdout.on('data', function (data) { + console.log(plug, 'stdout:', data.toString()); + }); + + child.stderr.on('data', function (data) { + console.log(plug, 'stderr:', data.toString()); + }); + + child.on('exit', function (code) { + console.log(plug, 'exit:', code); + if (code === 0) { + res.writeHead(200, { 'Content-Length': 0 }); + res.end(); + } else { + res.writeHead(500, { 'Content-Length': 0 }); + res.end(); + }; + }); + + }); + } else { + res.writeHead(400, 'You are made of stupid!', { + 'Content-Length': 0 + }); + res.end(); + }; + }); +}).listen(port, host, function () { + console.log('Serving HTTP on', host, 'port', port); +}); diff --git a/cholerab/knut/plugs/ttycnser/bin/login b/cholerab/knut/plugs/ttycnser/bin/login new file mode 100755 index 00000000..bf88d6f9 --- /dev/null +++ b/cholerab/knut/plugs/ttycnser/bin/login @@ -0,0 +1,4 @@ +#! /bin/sh +set -euf +pipe="/tmp/krebscode.painload.cholerab.ttycnser.${LOGNAME}" +ln -snf "`tty`" "$pipe" diff --git a/cholerab/knut/plugs/ttycnser/bin/write b/cholerab/knut/plugs/ttycnser/bin/write new file mode 100755 index 00000000..f358407a --- /dev/null +++ b/cholerab/knut/plugs/ttycnser/bin/write @@ -0,0 +1,4 @@ +#! /bin/sh +set -euf +pipe="/tmp/krebscode.painload.cholerab.ttycnser.${LOGNAME}" +echo -n "7>>>> ${params}8" > "${pipe}" diff --git a/cholerab/knut/plugs/ttycnser/index b/cholerab/knut/plugs/ttycnser/index new file mode 120000 index 00000000..2d949688 --- /dev/null +++ b/cholerab/knut/plugs/ttycnser/index @@ -0,0 +1 @@ +bin/write \ No newline at end of file diff --git a/cholerab/knut/src/io/slurp.js b/cholerab/knut/src/io/slurp.js new file mode 100644 index 00000000..70319743 --- /dev/null +++ b/cholerab/knut/src/io/slurp.js @@ -0,0 +1,38 @@ +module.exports = (function () { + + function join_buffers (buffers, length) { + var buffer = new Buffer(length); + var targetStart = 0; + buffers.forEach(function (x) { + x.copy(buffer, targetStart); + targetStart += x.length; + }); + return buffer; + }; + + function finish_it (req, buffers, length, callback) { + req.content = join_buffers(buffers, length); + return callback(req.content); + }; + + function nop () {}; + + return function (req, callback) { + if (req.hasOwnProperty('content')) { + return callback(req.content); + }; + var content = []; + var length = 0; + var end_handler = finish_it; + req.on('data', function (data) { + content.push(data); + length += data.length; + }); + [ 'end', 'close' ].forEach(function (event) { + req.on(event, function () { + finish_it(req, content, length, callback); + end_handler = nop; + }); + }); + }; +})(); -- cgit v1.2.3