I am trying to code a remote shell with Node.js. Here's what I got for the moment :
Client
var net = require('net');
var client = net.connect({port: 1234}, function(){
process.stdin.pipe(client);
client.pipe(process.stdout);
});
Server
var net = require('net'),
spawn = require('child_process').spawn;
var server = net.createServer(function(socket){
var sh = spawn('/bin/sh');
sh.stdout.pipe(socket);
sh.stderr.pipe(socket);
socket.pipe(sh.stdin);
});
server.listen(1234);
It works pretty well with simple commands but when I launch emacs or nano it doesn't because control sequences aren't sent. I would like to make it behave kinda like ssh. Is such a thing possible ? Maybe using process.stdin.setRawMode(true) ?
Thanks
I found what I was looking for. It's called pty.js.
Server-side:
var net = require('net');
var pty = require('pty.js');
var server = net.createServer();
server.on('connection', function(socket){
var term = pty.spawn('bash', [], {
name: 'xterm-color',
cols: 80,
rows: 40,
cwd: process.env.HOME,
env: process.env
});
socket.pipe(term);
term.pipe(socket);
});
server.listen(1234);
Client-side:
var net = require('net');
var client = net.connect(1234);
client.on('connect', function(){
process.stdin.setRawMode(true);
process.stdin.pipe(client);
client.pipe(process.stdout);
});
client.on('end', function(){
console.log('[Connection closed by peer]');
process.stdin.setRawMode(false);
process.exit();
});
Only works with node v0.10.x for the moment.