I'm trying to craete an interactive ssh server in nodejs which can handle a keypress on the client side like in a normal linux ssh session when you start an mc and afte you can use the arrow keys. I found the ssh2 package, but it can parse the data in the server side only after the 'enter' keypresses. How can I detect the y/n keypresses in this demo code without pressing the enter key?
var fs = require('fs');
var username = null;
var ssh2 = require('ssh2');
new ssh2.Server({
hostKeys: [fs.readFileSync('ssh.key')]
}, function(client) {
console.log('Client connected!');
client.on('authentication', function(ctx) {
// auth
ctx.accept();
}).on('ready', function() {
console.log('Client authenticated!');
client.on('session', function(accept, reject) {
var session = accept();
session.once('shell', function(accept, reject, info) {
var stream = accept();
stream.write("Do you want to continue? Y/N ");
stream.on('data', function(data) {
var args = data.toString();
switch(args[0])
{
case "y":
stream.write("Your choice: y\n");
break;
case "n":
stream.write("Your choice: n\n");
break;
default:
stream.stderr.write("Error!\n");
break;
}
if(typeof stream != 'undefined')
{
stream.write("$ ");
}
});
});
});
}).on('end', function() {
console.log('Client disconnected');
});
}).listen(2222, '127.0.0.1', function() {
console.log('Listening on port ' + this.address().port);
});