mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-12-06 19:03:43 +00:00
54 lines
2.1 KiB
JavaScript
54 lines
2.1 KiB
JavaScript
"use strict";
|
|
|
|
const { randomUUID } = require("node:crypto");
|
|
|
|
// Keep the authenticated dial profile in a closure, never in discovery metadata
|
|
// or persisted host data. The terminal still owns this profile's lifetime.
|
|
function attachSessionSftpDial(session, options, { sessions, sessionId, sender, openSftp, closeSftp }) {
|
|
const dialOptions = { ...options };
|
|
for (const key of ["sessionId", "sourceSessionId", "bootEpoch", "_connRef", "_sourceReuseState", "sftpReuseOptions", "sock"]) {
|
|
delete dialOptions[key];
|
|
}
|
|
const assertActive = (signal) => {
|
|
if (signal?.aborted) throw new DOMException("SFTP open cancelled", "AbortError");
|
|
if (sessions.get(sessionId) !== session || sender?.isDestroyed?.()) {
|
|
throw new Error("Source SSH session is no longer active");
|
|
}
|
|
};
|
|
Object.defineProperty(session, "_openDedicatedSftp", {
|
|
value: async (payload = {}) => {
|
|
assertActive(payload.abortSignal);
|
|
const controller = new AbortController();
|
|
const abort = () => controller.abort();
|
|
payload.abortSignal?.addEventListener("abort", abort, { once: true });
|
|
session.stream?.once?.("close", abort);
|
|
session.conn?.once?.("close", abort);
|
|
try {
|
|
const opened = await openSftp({ sender }, {
|
|
...dialOptions,
|
|
sessionId: randomUUID(),
|
|
sourceSessionId: sessionId,
|
|
reuseTransport: false,
|
|
singleChannelSsh: true,
|
|
fileProtocol: payload.fileProtocol ?? session.sftpFileProtocol ?? "auto",
|
|
sudo: Boolean(payload.sudo),
|
|
abortSignal: controller.signal,
|
|
});
|
|
try {
|
|
assertActive(controller.signal);
|
|
} catch (err) {
|
|
try { await closeSftp(null, { sftpId: opened.sftpId }); } catch { /* Preserve cancellation. */ }
|
|
throw err;
|
|
}
|
|
return { ...opened, sourceSessionId: sessionId };
|
|
} finally {
|
|
payload.abortSignal?.removeEventListener("abort", abort);
|
|
session.stream?.removeListener?.("close", abort);
|
|
session.conn?.removeListener?.("close", abort);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
module.exports = { attachSessionSftpDial };
|