From 6a517bb3c9102628eb20f1f470af97c3502fe935 Mon Sep 17 00:00:00 2001 From: EDDYMENS Date: Sat, 13 Jan 2024 22:55:29 +0100 Subject: [PATCH] add support for interactive sessions --- backend.js | 24 +++++++++++++++++++----- frontend.js | 45 +++++---------------------------------------- 2 files changed, 24 insertions(+), 45 deletions(-) diff --git a/backend.js b/backend.js index a3c0ec6..1a47aed 100644 --- a/backend.js +++ b/backend.js @@ -14,13 +14,27 @@ var ptyProcess = pty.spawn(shell, [], { }); wss.on('connection', ws => { console.log("new session") + + // Catch incoming request ws.on('message', command => { - ptyProcess.write(command); + var processedCommand = commandProcessor(command) + // console.log(processedCommand, "incoming command"); + ptyProcess.write(processedCommand); }) - ptyProcess.on('data', function (data) { - ws.send(data) - console.log(data); + // Output: Sent to the frontend + ptyProcess.on('data', function (rawOutput) { + var processedOutput = outputProcessor(rawOutput); + ws.send(processedOutput); + console.log(processedOutput); }); -}) \ No newline at end of file +}) + +const commandProcessor = function(command) { + return command; +} + +const outputProcessor = function(output) { + return output; +} \ No newline at end of file diff --git a/frontend.js b/frontend.js index fc8e23b..ba97274 100644 --- a/frontend.js +++ b/frontend.js @@ -18,57 +18,22 @@ function init() { }; prompt(term); - term.onData(e => { - switch (e) { - case '\u0003': // Ctrl+C - term.write('^C'); - prompt(term); - break; - case '\r': // Enter - runCommand(term, command); - command = ''; - break; - case '\u007F': // Backspace (DEL) - // Do not delete the prompt - if (term._core.buffer.x > 2) { - term.write('\b \b'); - if (command.length > 0) { - command = command.substr(0, command.length - 1); - } - } - break; - case '\u0009': - console.log('tabbed', output, ["dd", "ls"]); - break; - default: - if (e >= String.fromCharCode(0x20) && e <= String.fromCharCode(0x7E) || e >= '\u00a0') { - command += e; - term.write(e); - } - } + term.onKey(key => { + runCommand(term, key.key); }); } -function clearInput(command) { - var inputLengh = command.length; - for (var i = 0; i < inputLengh; i++) { - term.write('\b \b'); - } -} function prompt(term) { - command = ''; term.write('\r\n$ '); } socket.onmessage = (event) => { term.write(event.data); + } function runCommand(term, command) { - if (command.length > 0) { - clearInput(command); - socket.send(command + '\n'); - return; - } + socket.send(command); + } init();