I stumbled Across a library called ssh2 that is used to both connect to SSH Servers and also host SSH Servers themselves. Now the problem is that I have copied and pasted the code they offered in their NPM page but the shell gets closed as soon as it is opened on the client-side, the error PTY allocation request failed on channel 0 shell request failed on channel 0 keeps showing up again and again. (P.S: Keep in mind that I host the NodeJS Server on a windows machine , not on a Linux distribution)
const { timingSafeEqual } = require('crypto');
const { readFileSync } = require('fs');
const { inspect } = require('util');
const { utils: { parseKey }, Server } = require('ssh2');
const allowedUser = Buffer.from('foo');
const allowedPassword = Buffer.from('bar');
const allowedPubKey = parseKey(readFileSync('foo.pub'));
function checkValue(input, allowed) {
const autoReject = (input.length !== allowed.length);
if (autoReject) {
// Prevent leaking length information by always making a comparison with the
// same input when lengths don't match what we expect ...
allowed = input;
}
const isMatch = timingSafeEqual(input, allowed);
return (!autoReject && isMatch);
}
new Server({
hostKeys: [readFileSync('host.key')]
}, (client) => {
console.log('Client connected!');
client.on('authentication', (ctx) => {
let allowed = true;
if (!checkValue(Buffer.from(ctx.username), allowedUser))
allowed = false;
switch (ctx.method) {
case 'password':
if (!checkValue(Buffer.from(ctx.password), allowedPassword))
return ctx.reject();
break;
case 'publickey':
if (ctx.key.algo !== allowedPubKey.type
|| !checkValue(ctx.key.data, allowedPubKey.getPublicSSH())
|| (ctx.signature && allowedPubKey.verify(ctx.blob, ctx.signature) !== true)) {
return ctx.reject();
}
break;
default:
return ctx.reject();
}
if (allowed)
ctx.accept();
else
ctx.reject();
}).on('ready', () => {
console.log('Client authenticated!');
client.on('session', (accept, reject) => {
const session = accept();
session.once('exec', (accept, reject, info) => {
console.log('Client wants to execute: ' + inspect(info.command));
const stream = accept();
stream.stderr.write('Oh no, the dreaded errors!\n');
stream.write('Just kidding about the errors!\n');
stream.exit(0);
stream.end();
});
});
}).on('close', () => {
console.log('Client disconnected');
});
}).listen(0, '127.0.0.1', function() {
console.log('Listening on port ' + this.address().port);
});
I tried multiple solutions but none are working. I keep reading that I should implement code in order to "spawn a shell" but most of the libraries involved in this situation are not maintained (or so I believe) to find a conclusive solution.
i found this which may help: https://bobcares.com/blog/pty-allocation-request-failed-on-channel-0/
it seems that this is a common ssh error about virtual terminals. you may have to use WSL in some manner. I'm not really sure how this would work on windows since it doesn't have devices like linux, ie /dev/pts /dev/tty etc....