fix: Fixes character reading in readFromKeyboard not displaying correctly.

This commit is contained in:
greysoh 2024-05-09 19:38:30 -04:00
parent 47b707e8cd
commit 422ec568e2
Signed by: imterah
GPG key ID: 8FA7DD57BA6CEA37

View file

@ -18,75 +18,80 @@ export async function readFromKeyboard(
let lineIndex = 0; let lineIndex = 0;
async function eventLoop(): Promise<any> { async function eventLoop(): Promise<any> {
const readStreamData = stream.read(); const readStreamDataBuf = stream.read();
if (readStreamData == null) return setTimeout(eventLoop, pullRate); if (readStreamDataBuf == null) return setTimeout(eventLoop, pullRate);
if (readStreamData.includes("\x03")) { const readStreamData = readStreamDataBuf.toString();
stream.write("^C");
return promise("");
} else if (readStreamData.includes("\r") || readStreamData.includes("\n")) {
return promise(line.replace("\r", ""));
} else if (readStreamData.includes(clientBackspace)) {
if (line.length == 0) return setTimeout(eventLoop, pullRate); // Here because if we do it in the parent if statement, shit breaks
line = line.substring(0, lineIndex - 1) + line.substring(lineIndex);
if (!disableEcho) { // Fixes several bugs (incl. potential social eng. exploits, ssh-copy-id being broken, etc)
const deltaCursor = line.length - lineIndex; for (const character of readStreamData.split("")) {
if (character == "\x03") {
stream.write("^C");
return promise("");
} else if (character == "\r" || character == "\n") {
return promise(line.replace("\r", ""));
} else if (character == clientBackspace) {
if (line.length == 0) return setTimeout(eventLoop, pullRate); // Here because if we do it in the parent if statement, shit breaks
line = line.substring(0, lineIndex - 1) + line.substring(lineIndex);
if (deltaCursor == line.length) return setTimeout(eventLoop, pullRate); if (!disableEcho) {
const deltaCursor = line.length - lineIndex;
if (deltaCursor < 0) { if (deltaCursor == line.length) return setTimeout(eventLoop, pullRate);
// Use old technique if the delta is < 0, as the new one is tailored to the start + 1 to end - 1
stream.write(ourBackspace + " " + ourBackspace); if (deltaCursor < 0) {
// Use old technique if the delta is < 0, as the new one is tailored to the start + 1 to end - 1
stream.write(ourBackspace + " " + ourBackspace);
} else {
// Jump forward to the front, and remove the last character
stream.write(rightEscape.repeat(deltaCursor) + " " + ourBackspace);
// Go backwards & rerender text & go backwards again (wtf?)
stream.write(
leftEscape.repeat(deltaCursor + 1) +
line.substring(lineIndex - 1) +
leftEscape.repeat(deltaCursor + 1),
);
}
lineIndex -= 1;
}
} else if (character == "\x1B") {
if (character == rightEscape) {
if (lineIndex + 1 > line.length) return setTimeout(eventLoop, pullRate);
lineIndex += 1;
} else if (character == leftEscape) {
if (lineIndex - 1 < 0) return setTimeout(eventLoop, pullRate);
lineIndex -= 1;
} else { } else {
// Jump forward to the front, and remove the last character return setTimeout(eventLoop, pullRate);
stream.write(rightEscape.repeat(deltaCursor) + " " + ourBackspace);
// Go backwards & rerender text & go backwards again (wtf?)
stream.write(
leftEscape.repeat(deltaCursor + 1) +
line.substring(lineIndex - 1) +
leftEscape.repeat(deltaCursor + 1),
);
} }
lineIndex -= 1; if (!disableEcho) stream.write(character);
}
} else if (readStreamData.includes("\x1B")) {
if (readStreamData.includes(rightEscape)) {
if (lineIndex + 1 > line.length) return setTimeout(eventLoop, pullRate);
lineIndex += 1;
} else if (readStreamData.includes(leftEscape)) {
if (lineIndex - 1 < 0) return setTimeout(eventLoop, pullRate);
lineIndex -= 1;
} else { } else {
return setTimeout(eventLoop, pullRate); lineIndex += 1;
}
if (!disableEcho) stream.write(readStreamData); // There isn't a splice method for String prototypes. So, ugh:
} else { line =
lineIndex += readStreamData.length; line.substring(0, lineIndex - 1) +
character +
line.substring(lineIndex - 1);
// There isn't a splice method for String prototypes. So, ugh: if (!disableEcho) {
line = let deltaCursor = line.length - lineIndex;
line.substring(0, lineIndex - 1) +
readStreamData +
line.substring(lineIndex - 1);
if (!disableEcho) { // wtf?
let deltaCursor = line.length - lineIndex; if (deltaCursor < 0) {
console.log(
"FIXME: somehow, our deltaCursor value is negative! please investigate me",
);
deltaCursor = 0;
}
// wtf? stream.write(
if (deltaCursor < 0) { line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor),
console.log(
"FIXME: somehow, our deltaCursor value is negative! please investigate me",
); );
deltaCursor = 0;
} }
stream.write(
line.substring(lineIndex - 1) + leftEscape.repeat(deltaCursor),
);
} }
} }