3 ms·
yah, here's the code, I'm not going to excuse it being terrible, it's mostly copy-pasta from the ssh2 readme :) // Main.js var fs = require('fs'); var crypto
by dusted 6y ago
yah, here's the code, I'm not going to excuse it being terrible, it's mostly copy-pasta from the ssh2 readme :)
// Main.js
var fs = require('fs');
var crypto = require('crypto');
var inspect = require('util').inspect;
var ssh2 = require('ssh2');
var utils = ssh2.utils;
var allowedUser = Buffer.from('foo');
var allowedPassword = Buffer.from('bar');
var allowedPubKey = utils.parseKey(fs.readFileSync('foo.pub'));
const EventEmitter = require('events');
let watchers=0;
class Framer extends EventEmitter {
constructor() {
super();
const borderline = '*----------------------------------------------------------------------*\r\n';
const fd = fs.openSync('./cam', 'r+');
const stream = fs.createReadStream(null, {fd});
let frame = '';
const len = 160*60+60;
stream.on('data', (d)=> {
if (d.toString().trim() === '[stdin end]') {
console.log('stdin end?');
} else {
const bytes = d.toString();
frame = frame+bytes;
if(frame.length > len) {
const out = frame.substr(0,len);
frame = frame.substr(len);
let lines = out.split('\n');
lines.pop();
lines = lines.map ( (l)=> '|'+l.substr(40, 70) +'|');
const fin = borderline + lines.join('\r\n') + '\r\n' + borderline + '\u001b['+(lines.length+2)+'A\u001b[32D';
this.emit('frame', fin);
}
}
});
}
}
const grabber = new Framer();
new ssh2.Server({
hostKeys: [fs.readFileSync('foo.key')]
}, function(client) {
client.on('authentication', function(ctx) {
var user = Buffer.from(ctx.username);
console.log(new Date().toISOString()+ ' username:',ctx.username,' watching:', watchers);
ctx.accept();
}).on('ready', function() {
client.on('session', function(accept, reject) {
var session = accept();
session.on('pty', (accept)=>{
accept();
});
session.on('shell', (accept)=>{
const stream = accept();
const listener = (frame)=>{stream.write(frame);};
grabber.on('frame', listener);
watchers++;
stream.on('data', (d)=>{
console.log('data:',d);
if(d[0] === 0x03 || d[0] === 0x1b || d[0] == 0x71) {
console.log('client left peacefully');
stream.write('\r\n Thanks for watching, have a nice day, there were '+watchers+' watching when you logged off. \r\n');
grabber.removeListener('data', listener);
stream.exit(0);
stream.end();
watchers--;
}
});
});
});
}).on('end', function(test) {
console.log(new Date().toISOString()+' Client disconnected', test);
});
}).listen(2222, '0.0.0.0', function() {
console.log('Listening on port ' + this.address().port);
});
// running it takes two terminals, one for node, one for the hasciicam:
mkdir lava && cd java
npm install --save
while true
do
node main.js
done
// camera:
cd lava
mkfifo cam
hasciicam -m text -o cam