Skip to content

Instantly share code, notes, and snippets.

@remy
Last active September 28, 2017 17:02
Show Gist options
  • Save remy/a41c4b58c879a98674a77868bb0b2a3f to your computer and use it in GitHub Desktop.
Save remy/a41c4b58c879a98674a77868bb0b2a3f to your computer and use it in GitHub Desktop.
Usage in glitch once placed in the root: npm.start = ./wetty.js -c 'node server.js'
#!/usr/bin/env node
var express = require('express');
var http = require('http');
var path = require('path');
var server = require('socket.io');
var pty = require('pty.js');
var fs = require('fs');
var opts = require('optimist')
.options({
port: {
demand: false,
alias: 'p',
default: 3000,
description: 'wetty listen port'
},
cmd: {
demand: true,
alias: 'c',
description: 'the command to run'
}
}).boolean('allow_discovery').argv;
process.on('uncaughtException', function(e) {
console.error('Error: ' + e);
});
var app = express();
app.use('/', express.static(path.join(__dirname, 'public')));
var httpserv = http.createServer(app).listen(opts.port, function() {
console.log('http on port ' + opts.port);
});
var io = server(httpserv, {path: '/wetty/socket.io'});
io.on('connection', function(socket){
var term;
term = pty.spawn('/bin/bash', ["-c", `${opts.cmd}; sleep 0.5; echo "<END>"`], {
name: 'xterm-256color'
});
console.log((new Date()) + " PID=" + term.pid + " STARTED");
term.on('data', function(data) {
socket.emit('output', data);
});
term.on('exit', function(code) {
console.log((new Date()) + " PID=" + term.pid + " ENDED")
});
socket.on('resize', function(data) {
term.resize(data.col, data.row);
});
socket.on('input', function(data) {
term.write(data);
});
socket.on('disconnect', function() {
term.end();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment