mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 01:40:45 +00:00
[codex] Add telnet URL deep link support (#1988)
* Add telnet URL deep link support * Fix telnet deep link review issues * Keep ssh deep links independent from telnet registration * Address Codex telnet deep link feedback
This commit is contained in:
19 files changed
+769
-15
No files matched your search
+44
-1
@@ -2,8 +2,10 @@ const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const SSH_DEEP_LINK_CHANNEL = "netcatty:deepLink:ssh";
|
||||
const TELNET_DEEP_LINK_CHANNEL = "netcatty:deepLink:telnet";
|
||||
const JMS_DEEP_LINK_CHANNEL = "netcatty:deepLink:jms";
|
||||
const SSH_PROTOCOL = "ssh";
|
||||
const TELNET_PROTOCOL = "telnet";
|
||||
const JMS_PROTOCOL = "jms";
|
||||
const SSH_DEEP_LINK_PREFERENCES_FILE = "ssh-deep-link-preferences.json";
|
||||
const JMS_DEEP_LINK_PREFERENCES_FILE = "jms-deep-link-preferences.json";
|
||||
@@ -22,6 +24,10 @@ function isSshDeepLinkUrl(rawUrl) {
|
||||
return isDeepLinkUrl(rawUrl, SSH_PROTOCOL);
|
||||
}
|
||||
|
||||
function isTelnetDeepLinkUrl(rawUrl) {
|
||||
return isDeepLinkUrl(rawUrl, TELNET_PROTOCOL);
|
||||
}
|
||||
|
||||
function isJmsDeepLinkUrl(rawUrl) {
|
||||
return isDeepLinkUrl(rawUrl, JMS_PROTOCOL);
|
||||
}
|
||||
@@ -30,6 +36,10 @@ function collectSshDeepLinkUrls(argv) {
|
||||
return collectDeepLinkUrls(argv, SSH_PROTOCOL);
|
||||
}
|
||||
|
||||
function collectTelnetDeepLinkUrls(argv) {
|
||||
return collectDeepLinkUrls(argv, TELNET_PROTOCOL);
|
||||
}
|
||||
|
||||
function collectJmsDeepLinkUrls(argv) {
|
||||
return collectDeepLinkUrls(argv, JMS_PROTOCOL);
|
||||
}
|
||||
@@ -78,10 +88,18 @@ function registerSshProtocolClient(options = {}) {
|
||||
return registerProtocolClient({ ...options, protocol: SSH_PROTOCOL });
|
||||
}
|
||||
|
||||
function registerTelnetProtocolClient(options = {}) {
|
||||
return registerProtocolClient({ ...options, protocol: TELNET_PROTOCOL });
|
||||
}
|
||||
|
||||
function removeSshProtocolClient(options = {}) {
|
||||
return removeProtocolClient({ ...options, protocol: SSH_PROTOCOL });
|
||||
}
|
||||
|
||||
function removeTelnetProtocolClient(options = {}) {
|
||||
return removeProtocolClient({ ...options, protocol: TELNET_PROTOCOL });
|
||||
}
|
||||
|
||||
function registerJmsProtocolClient(options = {}) {
|
||||
return registerProtocolClient({ ...options, protocol: JMS_PROTOCOL });
|
||||
}
|
||||
@@ -205,12 +223,27 @@ function applyDeepLinkProtocolClientPreference({
|
||||
}
|
||||
|
||||
function applySshProtocolClientPreference(options = {}) {
|
||||
return applyDeepLinkProtocolClientPreference({
|
||||
const sshApplied = applyDeepLinkProtocolClientPreference({
|
||||
enabled: options.enabled,
|
||||
registerClient: registerSshProtocolClient,
|
||||
removeClient: removeSshProtocolClient,
|
||||
options,
|
||||
});
|
||||
const telnetApplied = applyDeepLinkProtocolClientPreference({
|
||||
enabled: options.enabled,
|
||||
registerClient: registerTelnetProtocolClient,
|
||||
removeClient: removeTelnetProtocolClient,
|
||||
options,
|
||||
});
|
||||
if (sshApplied !== true && telnetApplied === true) {
|
||||
applyDeepLinkProtocolClientPreference({
|
||||
enabled: options.enabled === false,
|
||||
registerClient: registerTelnetProtocolClient,
|
||||
removeClient: removeTelnetProtocolClient,
|
||||
options,
|
||||
});
|
||||
}
|
||||
return sshApplied === true;
|
||||
}
|
||||
|
||||
function applyJmsProtocolClientPreference(options = {}) {
|
||||
@@ -270,6 +303,10 @@ function shouldDeliverSshDeepLink(options = {}) {
|
||||
return shouldDeliverDeepLink(options);
|
||||
}
|
||||
|
||||
function shouldDeliverTelnetDeepLink(options = {}) {
|
||||
return shouldDeliverDeepLink(options);
|
||||
}
|
||||
|
||||
function shouldDeliverJmsDeepLink(options = {}) {
|
||||
return shouldDeliverDeepLink(options);
|
||||
}
|
||||
@@ -314,6 +351,7 @@ function applyInitialJmsDeepLinkPreference(options = {}) {
|
||||
|
||||
module.exports = {
|
||||
SSH_DEEP_LINK_CHANNEL,
|
||||
TELNET_DEEP_LINK_CHANNEL,
|
||||
JMS_DEEP_LINK_CHANNEL,
|
||||
applyInitialJmsDeepLinkPreference,
|
||||
applyInitialSshDeepLinkPreference,
|
||||
@@ -321,16 +359,21 @@ module.exports = {
|
||||
applySshProtocolClientPreference,
|
||||
collectJmsDeepLinkUrls,
|
||||
collectSshDeepLinkUrls,
|
||||
collectTelnetDeepLinkUrls,
|
||||
isJmsDeepLinkUrl,
|
||||
isSshDeepLinkUrl,
|
||||
isTelnetDeepLinkUrl,
|
||||
readJmsDeepLinkEnabledPreference,
|
||||
readSshDeepLinkEnabledPreference,
|
||||
registerJmsProtocolClient,
|
||||
registerSshProtocolClient,
|
||||
registerTelnetProtocolClient,
|
||||
removeJmsProtocolClient,
|
||||
removeSshProtocolClient,
|
||||
removeTelnetProtocolClient,
|
||||
shouldDeliverJmsDeepLink,
|
||||
shouldDeliverSshDeepLink,
|
||||
shouldDeliverTelnetDeepLink,
|
||||
updateJmsDeepLinkEnabledPreference,
|
||||
updateSshDeepLinkEnabledPreference,
|
||||
writeJmsDeepLinkEnabledPreference,
|
||||
|
||||
@@ -8,8 +8,10 @@ const {
|
||||
applySshProtocolClientPreference,
|
||||
collectJmsDeepLinkUrls,
|
||||
collectSshDeepLinkUrls,
|
||||
collectTelnetDeepLinkUrls,
|
||||
isJmsDeepLinkUrl,
|
||||
isSshDeepLinkUrl,
|
||||
isTelnetDeepLinkUrl,
|
||||
readJmsDeepLinkEnabledPreference,
|
||||
readSshDeepLinkEnabledPreference,
|
||||
shouldDeliverJmsDeepLink,
|
||||
@@ -41,7 +43,7 @@ test("collectSshDeepLinkUrls extracts ssh URLs from process arguments", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("applySshProtocolClientPreference registers or removes the ssh handler", () => {
|
||||
test("applySshProtocolClientPreference registers or removes ssh and telnet handlers", () => {
|
||||
const calls = [];
|
||||
const app = {
|
||||
setAsDefaultProtocolClient: (...args) => {
|
||||
@@ -58,10 +60,97 @@ test("applySshProtocolClientPreference registers or removes the ssh handler", ()
|
||||
assert.equal(applySshProtocolClientPreference({ app, enabled: false, isDev: false }), true);
|
||||
assert.deepEqual(calls, [
|
||||
["set", "ssh"],
|
||||
["set", "telnet"],
|
||||
["remove", "ssh"],
|
||||
["remove", "telnet"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("applySshProtocolClientPreference keeps ssh successful when telnet registration fails", () => {
|
||||
const calls = [];
|
||||
const app = {
|
||||
setAsDefaultProtocolClient: (protocol) => {
|
||||
calls.push(["set", protocol]);
|
||||
return protocol === "ssh";
|
||||
},
|
||||
removeAsDefaultProtocolClient: (protocol) => {
|
||||
calls.push(["remove", protocol]);
|
||||
return protocol === "ssh";
|
||||
},
|
||||
};
|
||||
|
||||
assert.equal(applySshProtocolClientPreference({ app, enabled: true, isDev: false }), true);
|
||||
assert.equal(applySshProtocolClientPreference({ app, enabled: false, isDev: false }), true);
|
||||
assert.deepEqual(calls, [
|
||||
["set", "ssh"],
|
||||
["set", "telnet"],
|
||||
["remove", "ssh"],
|
||||
["remove", "telnet"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("applySshProtocolClientPreference rolls back telnet registration when ssh enable fails", () => {
|
||||
const calls = [];
|
||||
const app = {
|
||||
setAsDefaultProtocolClient: (protocol) => {
|
||||
calls.push(["set", protocol]);
|
||||
return protocol === "telnet";
|
||||
},
|
||||
removeAsDefaultProtocolClient: (protocol) => {
|
||||
calls.push(["remove", protocol]);
|
||||
return true;
|
||||
},
|
||||
};
|
||||
|
||||
assert.equal(applySshProtocolClientPreference({ app, enabled: true, isDev: false }), false);
|
||||
assert.deepEqual(calls, [
|
||||
["set", "ssh"],
|
||||
["set", "telnet"],
|
||||
["remove", "telnet"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("applySshProtocolClientPreference restores telnet registration when ssh disable fails", () => {
|
||||
const calls = [];
|
||||
const app = {
|
||||
setAsDefaultProtocolClient: (protocol) => {
|
||||
calls.push(["set", protocol]);
|
||||
return true;
|
||||
},
|
||||
removeAsDefaultProtocolClient: (protocol) => {
|
||||
calls.push(["remove", protocol]);
|
||||
return protocol === "telnet";
|
||||
},
|
||||
};
|
||||
|
||||
assert.equal(applySshProtocolClientPreference({ app, enabled: false, isDev: false }), false);
|
||||
assert.deepEqual(calls, [
|
||||
["remove", "ssh"],
|
||||
["remove", "telnet"],
|
||||
["set", "telnet"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("isTelnetDeepLinkUrl accepts only telnet URLs", () => {
|
||||
assert.equal(isTelnetDeepLinkUrl("telnet://example.com:2001"), true);
|
||||
assert.equal(isTelnetDeepLinkUrl("TELNET://example.com:2001"), true);
|
||||
assert.equal(isTelnetDeepLinkUrl("ssh://alice@example.com"), false);
|
||||
assert.equal(isTelnetDeepLinkUrl("--flag"), false);
|
||||
});
|
||||
|
||||
test("collectTelnetDeepLinkUrls extracts telnet URLs from process arguments", () => {
|
||||
assert.deepEqual(
|
||||
collectTelnetDeepLinkUrls([
|
||||
"/Applications/Netcatty.app/Contents/MacOS/Netcatty",
|
||||
"--flag",
|
||||
"telnet://example.com:2001",
|
||||
"file:///tmp/example",
|
||||
"telnet://router.local:23",
|
||||
]),
|
||||
["telnet://example.com:2001", "telnet://router.local:23"],
|
||||
);
|
||||
});
|
||||
|
||||
test("ssh deep link enabled preference persists outside renderer localStorage", () => {
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
|
||||
@@ -65,6 +65,7 @@ const fs = require("node:fs");
|
||||
const { getCliDiscoveryFilePath } = require("./cli/discoveryPath.cjs");
|
||||
const {
|
||||
SSH_DEEP_LINK_CHANNEL,
|
||||
TELNET_DEEP_LINK_CHANNEL,
|
||||
JMS_DEEP_LINK_CHANNEL,
|
||||
applyInitialJmsDeepLinkPreference,
|
||||
applyInitialSshDeepLinkPreference,
|
||||
@@ -72,12 +73,15 @@ const {
|
||||
applySshProtocolClientPreference,
|
||||
collectJmsDeepLinkUrls,
|
||||
collectSshDeepLinkUrls,
|
||||
collectTelnetDeepLinkUrls,
|
||||
isJmsDeepLinkUrl,
|
||||
isSshDeepLinkUrl,
|
||||
isTelnetDeepLinkUrl,
|
||||
readJmsDeepLinkEnabledPreference,
|
||||
readSshDeepLinkEnabledPreference,
|
||||
shouldDeliverJmsDeepLink,
|
||||
shouldDeliverSshDeepLink,
|
||||
shouldDeliverTelnetDeepLink,
|
||||
updateJmsDeepLinkEnabledPreference,
|
||||
updateSshDeepLinkEnabledPreference,
|
||||
writeJmsDeepLinkEnabledPreference,
|
||||
@@ -539,8 +543,10 @@ async function createAndShowMainWindow() {
|
||||
|
||||
let sshDeepLinkEnabled = readSshDeepLinkEnabledPreference({ app });
|
||||
const pendingSshDeepLinkUrls = sshDeepLinkEnabled ? collectSshDeepLinkUrls(process.argv) : [];
|
||||
const pendingTelnetDeepLinkUrls = sshDeepLinkEnabled ? collectTelnetDeepLinkUrls(process.argv) : [];
|
||||
const pendingOpenTerminalPaths = resolveOpenTerminalPathsFromArgs(process.argv);
|
||||
let flushingSshDeepLinks = false;
|
||||
let flushingTelnetDeepLinks = false;
|
||||
let flushingOpenTerminalPaths = false;
|
||||
let sshDeepLinkDeliveryGeneration = 0;
|
||||
|
||||
@@ -558,6 +564,15 @@ function queueSshDeepLink(rawUrl) {
|
||||
}
|
||||
}
|
||||
|
||||
function queueTelnetDeepLink(rawUrl) {
|
||||
if (!sshDeepLinkEnabled) return;
|
||||
if (!isTelnetDeepLinkUrl(rawUrl)) return;
|
||||
pendingTelnetDeepLinkUrls.push(rawUrl);
|
||||
if (app.isReady?.()) {
|
||||
void flushPendingTelnetDeepLinks();
|
||||
}
|
||||
}
|
||||
|
||||
function queueOpenTerminalPath(rawPath, options = {}) {
|
||||
const resolvedPath = resolveOpenTerminalPath(rawPath, options);
|
||||
if (!resolvedPath) return;
|
||||
@@ -584,6 +599,7 @@ ipcMain?.handle?.("netcatty:deepLink:ssh:setEnabled", async (_event, payload) =>
|
||||
writePreference: (nextEnabled) => writeSshDeepLinkEnabledPreference({ app, enabled: nextEnabled }),
|
||||
clearPending: () => {
|
||||
pendingSshDeepLinkUrls.length = 0;
|
||||
pendingTelnetDeepLinkUrls.length = 0;
|
||||
sshDeepLinkDeliveryGeneration += 1;
|
||||
},
|
||||
});
|
||||
@@ -705,6 +721,39 @@ async function deliverSshDeepLink(rawUrl, expectedGeneration = sshDeepLinkDelive
|
||||
}
|
||||
}
|
||||
|
||||
async function deliverTelnetDeepLink(rawUrl, expectedGeneration = sshDeepLinkDeliveryGeneration) {
|
||||
if (!shouldDeliverTelnetDeepLink({
|
||||
enabled: sshDeepLinkEnabled,
|
||||
deliveryGeneration: sshDeepLinkDeliveryGeneration,
|
||||
expectedGeneration,
|
||||
})) return;
|
||||
const win = await createAndShowMainWindow();
|
||||
if (!shouldDeliverTelnetDeepLink({
|
||||
enabled: sshDeepLinkEnabled,
|
||||
deliveryGeneration: sshDeepLinkDeliveryGeneration,
|
||||
expectedGeneration,
|
||||
})) return;
|
||||
focusMainWindow();
|
||||
const windowManager = getWindowManager();
|
||||
const result = await windowManager.sendWhenRendererReady?.(
|
||||
win,
|
||||
TELNET_DEEP_LINK_CHANNEL,
|
||||
{ url: rawUrl },
|
||||
{
|
||||
timeoutMs: isDev ? 30000 : 15000,
|
||||
shouldSend: () => shouldDeliverTelnetDeepLink({
|
||||
enabled: sshDeepLinkEnabled,
|
||||
deliveryGeneration: sshDeepLinkDeliveryGeneration,
|
||||
expectedGeneration,
|
||||
}),
|
||||
cancelReason: "telnet-deep-link-disabled",
|
||||
},
|
||||
);
|
||||
if (result && result.success === false && result.reason !== "telnet-deep-link-disabled") {
|
||||
console.warn("[Main] Failed to deliver telnet:// deep link:", result.error || result.reason);
|
||||
}
|
||||
}
|
||||
|
||||
async function flushPendingSshDeepLinks() {
|
||||
if (flushingSshDeepLinks) return;
|
||||
flushingSshDeepLinks = true;
|
||||
@@ -724,6 +773,25 @@ async function flushPendingSshDeepLinks() {
|
||||
}
|
||||
}
|
||||
|
||||
async function flushPendingTelnetDeepLinks() {
|
||||
if (flushingTelnetDeepLinks) return;
|
||||
flushingTelnetDeepLinks = true;
|
||||
try {
|
||||
while (sshDeepLinkEnabled && pendingTelnetDeepLinkUrls.length > 0) {
|
||||
const rawUrl = pendingTelnetDeepLinkUrls.shift();
|
||||
if (!rawUrl) continue;
|
||||
await deliverTelnetDeepLink(rawUrl, sshDeepLinkDeliveryGeneration);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn("[Main] Failed to process telnet:// deep link:", err);
|
||||
} finally {
|
||||
flushingTelnetDeepLinks = false;
|
||||
if (sshDeepLinkEnabled && pendingTelnetDeepLinkUrls.length > 0) {
|
||||
void flushPendingTelnetDeepLinks();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function deliverOpenTerminalPath(targetPath) {
|
||||
const win = await createAndShowMainWindow();
|
||||
focusMainWindow();
|
||||
@@ -796,6 +864,10 @@ if (!gotLock) {
|
||||
queueJmsDeepLink(rawUrl);
|
||||
return;
|
||||
}
|
||||
if (isTelnetDeepLinkUrl(rawUrl)) {
|
||||
queueTelnetDeepLink(rawUrl);
|
||||
return;
|
||||
}
|
||||
queueSshDeepLink(rawUrl);
|
||||
});
|
||||
|
||||
@@ -806,6 +878,7 @@ if (!gotLock) {
|
||||
|
||||
app.on("second-instance", (_event, argv, workingDirectory) => {
|
||||
const jmsDeepLinkUrls = collectJmsDeepLinkUrls(argv);
|
||||
const telnetDeepLinkUrls = collectTelnetDeepLinkUrls(argv);
|
||||
const sshDeepLinkUrls = collectSshDeepLinkUrls(argv);
|
||||
if (jmsDeepLinkUrls.length > 0) {
|
||||
if (jmsDeepLinkEnabled) {
|
||||
@@ -813,6 +886,12 @@ if (!gotLock) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (telnetDeepLinkUrls.length > 0) {
|
||||
if (sshDeepLinkEnabled) {
|
||||
telnetDeepLinkUrls.forEach(queueTelnetDeepLink);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (sshDeepLinkUrls.length > 0) {
|
||||
if (sshDeepLinkEnabled) {
|
||||
sshDeepLinkUrls.forEach(queueSshDeepLink);
|
||||
@@ -845,6 +924,7 @@ if (!gotLock) {
|
||||
applyPreference: (enabled) => applySshProtocolClientPreference({ app, enabled, isDev }),
|
||||
clearPending: () => {
|
||||
pendingSshDeepLinkUrls.length = 0;
|
||||
pendingTelnetDeepLinkUrls.length = 0;
|
||||
sshDeepLinkDeliveryGeneration += 1;
|
||||
},
|
||||
});
|
||||
@@ -964,6 +1044,7 @@ if (!gotLock) {
|
||||
// Create the main window
|
||||
void createAndShowMainWindow().then(() => {
|
||||
void flushPendingSshDeepLinks();
|
||||
void flushPendingTelnetDeepLinks();
|
||||
void flushPendingJmsDeepLinks();
|
||||
void flushPendingOpenTerminalPaths();
|
||||
|
||||
|
||||
@@ -692,6 +692,11 @@ function createPreloadApi(ctx) {
|
||||
ipcRenderer.on("netcatty:deepLink:ssh", handler);
|
||||
return () => ipcRenderer.removeListener("netcatty:deepLink:ssh", handler);
|
||||
},
|
||||
onTelnetDeepLink: (callback) => {
|
||||
const handler = (_event, payload) => callback(payload);
|
||||
ipcRenderer.on("netcatty:deepLink:telnet", handler);
|
||||
return () => ipcRenderer.removeListener("netcatty:deepLink:telnet", handler);
|
||||
},
|
||||
onOpenTerminalPath: (callback) => {
|
||||
const handler = (_event, payload) => callback(payload);
|
||||
ipcRenderer.on("netcatty:openTerminalPath", handler);
|
||||
|
||||
Reference in new issue
Block a user