Add encoding fallback for legacy SSH servers

- Detect UTF-8 decode failures (replacement characters)
- Auto-fallback to latin1 for non-UTF-8 locales
- Fixes garbled characters on servers without UTF-8 support

Fixes #282
This commit is contained in:
ZacharyZcR
2025-10-02 14:22:25 +08:00
parent 1363e42c29
commit a097b1e300
+12 -1
View File
@@ -427,7 +427,18 @@ wss.on("connection", async (ws: WebSocket, req) => {
sshStream = stream; sshStream = stream;
stream.on("data", (data: Buffer) => { stream.on("data", (data: Buffer) => {
ws.send(JSON.stringify({ type: "data", data: data.toString() })); let decoded = data.toString("utf-8");
// Defensive fallback: if UTF-8 decode produces replacement characters
// and buffer contains high bytes, try latin1 (for legacy servers without UTF-8 locale)
if (
decoded.includes("\uFFFD") &&
data.some((byte) => byte >= 0x80 && byte <= 0xff)
) {
decoded = data.toString("latin1");
}
ws.send(JSON.stringify({ type: "data", data: decoded }));
}); });
stream.on("close", () => { stream.on("close", () => {