feature: Add support for disabling echo.

This commit is contained in:
greysoh 2024-05-06 15:07:15 -04:00
parent 41d37048d2
commit e447c0f9e1
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37

View file

@ -13,12 +13,13 @@ const serverBaseURL: string =
const axios = baseAxios.create({ const axios = baseAxios.create({
baseURL: serverBaseURL, baseURL: serverBaseURL,
validateStatus(status) { validateStatus: () => true
return true;
},
}); });
async function readFromKeyboard(stream: ssh2.ServerChannel): Promise<any> { async function readFromKeyboard(stream: ssh2.ServerChannel, disableEcho: boolean = false): Promise<string> {
const leftEscape = "\x1B[D";
const rightEscape = "\x1B[C";
let line = ""; let line = "";
let lineIndex = 0; let lineIndex = 0;
let isReady = false; let isReady = false;
@ -34,16 +35,10 @@ async function readFromKeyboard(stream: ssh2.ServerChannel): Promise<any> {
return; return;
} else if (readStreamData.includes("\x7F")) { } else if (readStreamData.includes("\x7F")) {
// \x7F = Ascii escape code for backspace (client side) // \x7F = Ascii escape code for backspace (client side)
if (line == "" || line == "\r") return setTimeout(eventLoop, 5); // \u0008 = Ascii escape code for backspace (server side)
line = line.substring(0, line.length - 1); line = line.substring(0, line.length - 1);
if (!disableEcho) stream.write("\u0008 \u0008");
// Ascii escape code for backspace (server side)
stream.write("\u0008 \u0008");
} else if (readStreamData.includes("\x1B")) { } else if (readStreamData.includes("\x1B")) {
const leftEscape = "\x1B[D";
const rightEscape = "\x1B[C";
if (readStreamData.includes(rightEscape)) { if (readStreamData.includes(rightEscape)) {
if (lineIndex + 1 > line.length) return setTimeout(eventLoop, 5); if (lineIndex + 1 > line.length) return setTimeout(eventLoop, 5);
lineIndex += 1; lineIndex += 1;
@ -54,13 +49,24 @@ async function readFromKeyboard(stream: ssh2.ServerChannel): Promise<any> {
return setTimeout(eventLoop, 5); return setTimeout(eventLoop, 5);
} }
stream.write(readStreamData); if (!disableEcho) stream.write(readStreamData);
} else { } else {
lineIndex += readStreamData.length; lineIndex += readStreamData.length;
// There isn't a splice method for String prototypes. So, ugh: // There isn't a splice method for String prototypes. So, ugh:
line = line.substring(0, lineIndex - 1) + readStreamData + line.substring(lineIndex + readStreamData.length, line.length); line = line.substring(0, lineIndex - 1) + readStreamData + line.substring(lineIndex - 1);
stream.write(readStreamData);
if (!disableEcho) {
let deltaCursor = line.length - lineIndex;
// wtf?
if (deltaCursor < 0) {
console.log("FIXME: somehow, our deltaCursor value is negative! please investigate me");
deltaCursor = 0;
}
stream.write(line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor));
}
} }
setTimeout(eventLoop, 5); setTimeout(eventLoop, 5);