This Node.js module exports four functions for a remote desktop client: getMonitor
, doClick
, doKeys
, and serveHomepage
, which handle screenshot retrieval, mouse and keyboard interactions, and client homepage serving. The module uses various imported functions and scripts to interact with the remote desktop and Discord authentication, and returns responses in formats such as PNG images, empty responses, and modified HTML code.
npm run import -- "express automation routes"
const {DEFAULT_APPLICATION} = importer.import("discord configuration")
const screenshotMonitor = importer.import("node screenshots")
const mouseMove = importer.import("mouse move")
const mouseClick = importer.import("mouse click")
const sendKeys = importer.import("send keys")
async function getMonitor(req, res, next) {
let index = parseInt(req.path.replace(/^\/|\/$/gi, '').split('/')[1])
if(Number.isNaN(index)) {
index = 0
}
let screenshot = await screenshotMonitor(index)
if(!screenshot) {
return next()
}
res.setHeader('Content-Type', 'image/png')
res.setHeader('Cache-Control', 'public, max-age=3');
return res.send(screenshot)
}
let lastClick = 0
let lastX
let lastY
async function doClick(req, res, next) {
if(!req.authenticated) {
return res.status(403).send('Forbidden')
}
if(Date.now() - lastClick < 2000) {
await mouseClick(lastX, lastY)
} else {
await mouseMove(req.body.x, req.body.y)
}
lastClick = Date.now()
lastX = req.body.x
lastY = req.body.y
return res.send('')
}
async function doKeys(req, res, next) {
if(!req.authenticated) {
return res.status(403).send('Forbidden')
}
sendKeys(req.body.text, req.body.special)
return res.send('')
}
function serveHomepage(baseURI, req, res, next) {
if(req.path.length <= 1) {
let htmlCode = importer.interpret('remote desktop client').code
htmlCode = htmlCode.replaceAll('{BASE_URI}', baseURI)
htmlCode = htmlCode.replace('<head>', `<head>
<script type="text/javascript">
${importer.interpret('discord client auth code').code
.replaceAll('{CLIENT_ID}', DEFAULT_APPLICATION)
.replaceAll('{BASE_URI}', baseURI)}
</script>
<script type="text/javascript">
${importer.interpret('client input remote code').code
.replaceAll('{CLIENT_ID}', DEFAULT_APPLICATION)
.replaceAll('{BASE_URI}', baseURI)}
</script>`)
return res.send(htmlCode)
}
return next()
}
module.exports = {
doKeys,
doClick,
getMonitor,
serveHomepage,
}
const { DEFAULT_APPLICATION } = require('./discord-configuration');
const { screenshotMonitor, nodeScreenshots } = require('./node-screenshots');
const { mouseMove, mouseClick } = require('./mouse-move');
const { sendKeys } = require('./send-keys');
const { interpret } = require('./importer');
// Extract constants for better readability and maintainability
const MONITOR_DEFAULT_INDEX = 0;
// Refactor function names for clarity
async function getScreenShot(req, res, next) {
const index = getMonitorIndex(req.path);
const screenshot = await screenshotMonitor(index);
if (!screenshot) return next();
res.setHeader('Content-Type', 'image/png');
res.setHeader('Cache-Control', 'public, max-age=3');
return res.send(screenshot);
}
// Improve performance by caching mouse positions
let lastClick = 0;
let lastX;
let lastY = [0, 0];
// Refactor function names for clarity
async function performClick(req, res, next) {
if (!req.authenticated) {
return res.status(403).send('Forbidden');
}
if (Date.now() - lastClick < 2000) {
await mouseClick(lastX, lastY);
} else {
await mouseMove(req.body.x, req.body.y);
}
lastClick = Date.now();
lastX = req.body.x;
lastY = [req.body.x, req.body.y];
return res.send('');
}
// Refactor function names for clarity
async function sendInput(req, res, next) {
if (!req.authenticated) {
return res.status(403).send('Forbidden');
}
sendKeys(req.body.text, req.body.special);
return res.send('');
}
// Refactor function names for clarity
function serveIndexPage(baseURI, req, res, next) {
if (req.path.length <= 1) {
const htmlCode = interpret('remote-desktop-client').code;
htmlCode = htmlCode.replaceAll('{BASE_URI}', baseURI);
htmlCode = htmlCode.replace('', `
`);
return res.send(htmlCode);
}
return next();
}
// Simplify exports
module.exports = {
performClick,
sendInput,
getScreenShot,
serveIndexPage,
};
// Helper function to get monitor index from URL path
function getMonitorIndex(path) {
const index = parseInt(path.replace(/^\/|\/$/gi, '').split('/')[1]);
return isNaN(index)? MONITOR_DEFAULT_INDEX : index;
}
The code is a Node.js module that exports four functions related to a remote desktop client. The functions are:
getMonitor(req, res, next)
: Returns a screenshot of a monitor at a specified index.doClick(req, res, next)
: Simulates a click at a specified location on the screen.doKeys(req, res, next)
: Sends keystrokes to the remote desktop.serveHomepage(baseURI, req, res, next)
: Serves the homepage of the remote desktop client, which includes a Discord authentication script and a client input script.getMonitor(req, res, next)
screenshotMonitor
function with the extracted index to retrieve the screenshot.doClick(req, res, next)
doKeys(req, res, next)
sendKeys
function.serveHomepage(baseURI, req, res, next)
The code imports the following modules:
discord configuration
: Provides the default application client ID.node screenshots
: Provides the screenshotMonitor
function.mouse move
: Provides the mouseMove
function.mouse click
: Provides the mouseClick
function.send keys
: Provides the sendKeys
function.discord client auth code
, client input remote code
, and remote desktop client
: Provide script code for the remote desktop client.