diff options
-rwxr-xr-x | cholerab/knut/index.js | 63 | ||||
-rwxr-xr-x | cholerab/knut/plugs/ttycnser/bin/login | 4 | ||||
-rwxr-xr-x | cholerab/knut/plugs/ttycnser/bin/write | 4 | ||||
l--------- | cholerab/knut/plugs/ttycnser/index | 1 | ||||
-rw-r--r-- | cholerab/knut/src/io/slurp.js | 38 |
5 files changed, 110 insertions, 0 deletions
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[2;1H[2K[33;1m>>>> ${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; + }); + }); + }; +})(); |