forked from Blendman974/faceclaw
Add an ssh client to Terminal, as an alternative to (or tunnel for) g2mirror
This commit is contained in:
1 parent
ab692741f7
commit
2b75a9d66e
33 files changed
+4694
-99
No files matched your search
@@ -1,13 +1,21 @@
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
|
||||
/**
|
||||
* Terminal-app connection registry: the g2mirror hosts the app knows about,
|
||||
* stored as JSON under one settings key so every isolate sees the same list
|
||||
* and change notifications ride the ordinary settings-store channel.
|
||||
* Terminal-app connection registry: the hosts the app knows about, stored as
|
||||
* JSON under one settings key so every isolate sees the same list and change
|
||||
* notifications ride the ordinary settings-store channel.
|
||||
*
|
||||
* A connection is entered as a g2mirror:// connection string (the same form
|
||||
* g2mirror-view accepts): g2mirror://<token>@<host>[:port] for plain
|
||||
* websockets (default port 8737), g2mirrors://... for TLS (default 443).
|
||||
* A connection is entered as a connection string:
|
||||
* - g2mirror://<token>@<host>[:port]: a g2mirror server reached directly
|
||||
* (the same form g2mirror-view accepts) over a plain websocket (default
|
||||
* port 8737); g2mirrors://... for TLS (default 443).
|
||||
* - ssh://<user>@<host>[:port]: tmux on an SSH host (default port 22).
|
||||
* - ssh://<user>@<host>[:port]?g2mirror=<token>[@<host>[:port]]: a g2mirror
|
||||
* server tunnelled over SSH; the address after the token is where the
|
||||
* server listens as seen from the SSH host (default 127.0.0.1:8737).
|
||||
* SSH connections pin the host's key (hostKeys, trusted on first connection
|
||||
* after the user checks its fingerprint); their passwords live in the
|
||||
* platform keystore (app/native/ssh), not here.
|
||||
*/
|
||||
|
||||
export const TERMINAL_CONNECTIONS_KEY = "terminal.connections";
|
||||
@@ -25,6 +33,8 @@ export type TerminalConnection = {
|
||||
serverName: string | null;
|
||||
/** Auto-connect while the Terminal app is open. Toggled by Connect/Disconnect. */
|
||||
enabled: boolean;
|
||||
/** SSH connections: trusted host keys (SSH wire format, base64). */
|
||||
hostKeys: string[];
|
||||
};
|
||||
|
||||
export type ParsedConnectionString = {
|
||||
@@ -54,8 +64,79 @@ export function parseConnectionString(url: string): ParsedConnectionString | nul
|
||||
return { secure, host, port, authToken };
|
||||
}
|
||||
|
||||
/** The host[:port] part of a connection string; "" if it doesn't parse. */
|
||||
export type ParsedSshConnection = {
|
||||
user: string;
|
||||
host: string;
|
||||
port: number;
|
||||
} & (
|
||||
| { mode: "tmux" }
|
||||
| {
|
||||
mode: "g2mirror";
|
||||
authToken: string;
|
||||
/** Where g2mirror-server listens, from the SSH host's point of view. */
|
||||
targetHost: string;
|
||||
targetPort: number;
|
||||
}
|
||||
);
|
||||
|
||||
function decodeComponent(text: string): string {
|
||||
try {
|
||||
return decodeURIComponent(text);
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
/** Parse an ssh://user@host[:port][?g2mirror=token[@host[:port]]] string; null if malformed. */
|
||||
export function parseSshConnectionString(url: string): ParsedSshConnection | null {
|
||||
const match = /^ssh:\/\/([^@\s/?]+)@(\[[^\]\s]+\]|[^:/@\s?]+)(?::(\d+))?\/?(?:\?(.*))?$/.exec(url.trim());
|
||||
if (!match) return null;
|
||||
const user = decodeComponent(match[1]!);
|
||||
const host = match[2]!.replace(/^\[(.*)\]$/, "$1");
|
||||
const port = match[3] ? parseInt(match[3], 10) : 22;
|
||||
if (!user || !(port >= 1 && port <= 65535)) return null;
|
||||
const params = new Map<string, string>();
|
||||
for (const part of (match[4] ?? "").split("&").filter(Boolean)) {
|
||||
const equals = part.indexOf("=");
|
||||
if (equals < 0) return null;
|
||||
params.set(part.slice(0, equals), decodeComponent(part.slice(equals + 1)));
|
||||
}
|
||||
for (const key of params.keys()) {
|
||||
if (key !== "g2mirror") return null;
|
||||
}
|
||||
const g2mirror = params.get("g2mirror");
|
||||
if (g2mirror === undefined) return { user, host, port, mode: "tmux" };
|
||||
// token[@host[:port]], split at the last "@" so the token may contain one.
|
||||
const at = g2mirror.lastIndexOf("@");
|
||||
const authToken = at < 0 ? g2mirror : g2mirror.slice(0, at);
|
||||
const target = at < 0 ? "" : g2mirror.slice(at + 1);
|
||||
const targetMatch = /^(\[[^\]\s]+\]|[^:\s]*)(?::(\d+))?$/.exec(target);
|
||||
if (!authToken || !targetMatch) return null;
|
||||
const targetHost = targetMatch[1] || "127.0.0.1";
|
||||
const targetPort = targetMatch[2] ? parseInt(targetMatch[2], 10) : 8737;
|
||||
if (!(targetPort >= 1 && targetPort <= 65535)) return null;
|
||||
return { user, host, port, mode: "g2mirror", authToken, targetHost, targetPort };
|
||||
}
|
||||
|
||||
export type ParsedAnyConnection =
|
||||
| ({ kind: "g2mirror" } & ParsedConnectionString)
|
||||
| ({ kind: "ssh" } & ParsedSshConnection);
|
||||
|
||||
/** Parse any supported connection string; null if it's none of them. */
|
||||
export function parseAnyConnectionString(url: string): ParsedAnyConnection | null {
|
||||
const direct = parseConnectionString(url);
|
||||
if (direct) return { kind: "g2mirror", ...direct };
|
||||
const ssh = parseSshConnectionString(url);
|
||||
return ssh ? { kind: "ssh", ...ssh } : null;
|
||||
}
|
||||
|
||||
/** The [user@]host[:port] part of a connection string (never a token); "" if it doesn't parse. */
|
||||
export function connectionStringHostLabel(url: string): string {
|
||||
const ssh = parseSshConnectionString(url);
|
||||
if (ssh) {
|
||||
const host = ssh.host.includes(":") ? `[${ssh.host}]` : ssh.host;
|
||||
return `${ssh.user}@${host}${ssh.port === 22 ? "" : `:${ssh.port}`}`;
|
||||
}
|
||||
const parsed = parseConnectionString(url);
|
||||
if (!parsed) return "";
|
||||
const defaultPort = parsed.secure ? 443 : 8737;
|
||||
@@ -79,6 +160,7 @@ export function loadConnections(): TerminalConnection[] {
|
||||
url: String(item?.url ?? ""),
|
||||
serverName: typeof item?.serverName === "string" && item.serverName ? item.serverName : null,
|
||||
enabled: item?.enabled !== false,
|
||||
hostKeys: Array.isArray(item?.hostKeys) ? item.hostKeys.filter((key: unknown) => typeof key === "string") : [],
|
||||
}))
|
||||
.filter((connection) => connection.id.length > 0 && connection.url.length > 0);
|
||||
} catch {
|
||||
|
||||
Reference in new issue
Block a user