Skip to content

Instantly share code, notes, and snippets.

@EddieCanales
Last active April 30, 2019 05:11
Show Gist options
  • Save EddieCanales/8253b74ad3f26778aadfb063f19a6a46 to your computer and use it in GitHub Desktop.
Save EddieCanales/8253b74ad3f26778aadfb063f19a6a46 to your computer and use it in GitHub Desktop.
How to get screenshots when using node-rdpjs. Write the partial bitmap and render it into the right section of the "buffer" image, then save that to disk whenever you need to.
const screen = require('./screen');
const SCREEN_SIZE = { width : 1364, height : 768 };
async function saveScreen(label) {
await screen.write('screen-'+Date.now()+'-'+(label || '')+'.png');
}
async function createRemoteSession(userName, password, server, rdpScreenSaveInterval=0) {
const clientConfig = {
autoLogin: true,
decompress: true, // important for getting the screenshots properly
screen: SCREEN_SIZE,
userName: userName,
password: password
};
let client = null;
let sessionCreated = false;
screen.create(SCREEN_SIZE).then(async function() {
console.log(`connecting to ${server} using username=${userName}`);
client = rdp.createClient(clientConfig).on('bitmap', function(bitmap) {
// update the screen buffer
screen.update(bitmap, true);
// NOTE: this gets called sooooo much - don't just save to file here
}).connect(server, 3389);
// write screenshots ever rdpScreenSaveInterval ms
if(rdpScreenSaveInterval) {
setInterval(function() {
await saveScreen();
}, interval);
}
// NOTE: there's nothing special about using an interval.
// You could screenshot based on whatever you want.
}).catch(function(err) {
console.error('error', err);
});
}
const Jimp = require('jimp');
let partialImage = null;
let image = null;
/**
* create the canvas to right the screen updates to
* @param size {object} - object with height and width of the screen
*/
function create(size) {
return new Promise((resolve, reject) => {
new Jimp(32,32, function(err, pImage) {
partialImage = pImage;
if(err) {
console.error(err);
reject(err);
} else {
new Jimp(size.width, size.height, function(err2, newImage) {
image = newImage;
if(err) {
console.error(err2);
reject(err2);
} else {
resolve();
}
});
}
});
});
};
/**
* update canvas with new bitmap
* @param bitmap {object}
*/
function update (bitmap) {
var output = {
width: bitmap.width,
height: bitmap.height,
data: new Uint8ClampedArray(bitmap.data)
};
if(partialImage) {
partialImage.bitmap.width = output.width;
partialImage.bitmap.height = output.height;
partialImage.bitmap.data = output.data;
image.composite(partialImage, bitmap.destLeft, bitmap.destTop);
}
}
/**
* write the current image to disk
* @param filename - file name to write to
*/
function write (filename) {
image.write(filename);
}
module.exports = {
create,
update,
write
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment