mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
Classify SSH exit-signal as signaled so auto-reconnect does not fight admin idle kicks. Bare transport closed and error still reconnect. Related to #3617.
58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
/**
|
|
* Classify how a remote SSH shell channel ended.
|
|
*
|
|
* An exit-signal (ssh2: code null, signal set) is an administrative
|
|
* termination — idle kick, SIGHUP, SIGTERM — not a VPN/transport flap.
|
|
* Those stay "signaled" so auto-reconnect can leave them alone.
|
|
* A channel that closes with neither exit-status nor exit-signal stays
|
|
* "closed" and is still eligible to reconnect.
|
|
*/
|
|
|
|
function normalizeRemoteExitSignal(signal) {
|
|
return typeof signal === "string" && signal.length > 0 ? signal : null;
|
|
}
|
|
|
|
function classifyRemoteShellClose({ idleTimedOut, exitSignal, shellExited }) {
|
|
if (idleTimedOut) return "timeout";
|
|
const signal = normalizeRemoteExitSignal(exitSignal);
|
|
if (signal) return "signaled";
|
|
if (shellExited) return "exited";
|
|
return "closed";
|
|
}
|
|
|
|
function buildRemoteShellExitPayload({
|
|
sessionId,
|
|
generation,
|
|
exitCode,
|
|
exitSignal,
|
|
shellExited,
|
|
idleTimedOut,
|
|
}) {
|
|
const signal = normalizeRemoteExitSignal(exitSignal);
|
|
const reason = classifyRemoteShellClose({
|
|
idleTimedOut: idleTimedOut === true,
|
|
exitSignal: signal,
|
|
shellExited: shellExited === true,
|
|
});
|
|
const payload = {
|
|
sessionId,
|
|
reason,
|
|
_terminalSessionGeneration: generation,
|
|
};
|
|
if (typeof exitCode === "number") {
|
|
payload.exitCode = exitCode;
|
|
} else if (!signal) {
|
|
payload.remoteClosedWithoutExitStatus = true;
|
|
}
|
|
if (signal) {
|
|
payload.exitSignal = signal;
|
|
}
|
|
return payload;
|
|
}
|
|
|
|
module.exports = {
|
|
normalizeRemoteExitSignal,
|
|
classifyRemoteShellClose,
|
|
buildRemoteShellExitPayload,
|
|
};
|