* feat(auth): authenticate daemon clients through hello Give local CLI and desktop clients a per-run credential while remote clients send the host password inside the protocol. Keep relay passwordless admission for this mobile release so existing saved hosts keep working until the follow-up enforcement release. Move password recovery for saved hosts into host settings and keep pairing credentials in the add flow. Include isolated web QA evidence for both flows. * fix(auth): preserve Hub bootstrap and local credential expectations Hub-owned sockets already carry admission; keep their hello handling synchronous through session bootstrap while resolving credentials asynchronously for ordinary sockets. Keep the resolver out of lightweight configuration imports. Update CLI expectations for same-home credentials, let a rejected connection yield to another usable connection, and clear pairing secrets when add-flow targets change. * fix(pairing): retain passwords across edits to the same target Track the last valid pairing host while a URI is edited. This preserves an entered password for whitespace and partial edits but clears it when a different host becomes the target. * test(auth): align local credential checks across platforms Expect the CLI to authenticate through the credential in its selected home, and verify POSIX file modes only where Node exposes them. * feat(app): move host password into a settings row with a modal The host page rendered an always-visible password form above Name on every host. Replace it with a Password row under a Security section below Appearance; tapping it opens a modal with Save and, when a password is saved, Clear. The connection-error callout keeps its guidance and gains a Set password button that opens the same modal. Drop the unused pairing.hostPassword body/reopen/saveError strings. * chore: remove committed QA screenshots QA evidence stays out of the repository; the PR body describes it in words. * feat(app): keep the host password out of the host page The saved password is connection data, entered only when a host is added. A Password setting on the host page reads as the daemon's own password, so the row, modal, and callout button are gone. A rejected host shows the daemon's reason with guidance to remove the host and add it again with the password. useHostMutations no longer exposes setHostPassword; relay imports still set it inside the runtime. * feat(app): show host connection errors in an error alert HostConnectionError renders the existing Alert with the connection error as its title. Auth rejections add the re-add guidance as the description; other errors show the title only. The Overview and Connections pages share it. * style(app): soften alert borders to half-opacity accent * feat(app): size alerts on the button scale Alert takes a size prop (xs/sm/md/lg, default md) whose geometry lives next to the button's in control-geometry: one font size for title and description, the button icon size centered on the first text line, button padding and radius, and a one-line alert as tall as the button of the same size. Consumers pick sm among settings rows, cards, panels, and modals, and md when the alert is the whole page or sheet state. * style(app): roomier, rounder alerts with a tight title block Alert sm padding returns to the original 12/16 and every size is at least borderRadius.xl. Title and description sit a small gap apart with no line height override. The icon slot carries a zero-width text strut in the alert's font so the icon centers on the first line for titled, title-only, and description-only alerts. * refactor(app): align the alert icon with a centered lead row Drop the zero-width strut. The icon and the alert's first text (title, else description) share one row centered on each other; the description and actions sit below, indented by the icon slot width plus gap so they start at the lead text's left edge. * refactor(app): drop the unused Alert icon override
439 lines
14 KiB
TypeScript
439 lines
14 KiB
TypeScript
import {
|
|
waitForDaemonReady,
|
|
resolvePaseoHome,
|
|
type DaemonInstance,
|
|
readLocalCredentialForTarget,
|
|
} from "@getpaseo/server/daemon-control";
|
|
import { describeDaemonTarget, type DaemonTarget } from "./daemon-target.js";
|
|
export type { DaemonTarget } from "./daemon-target.js";
|
|
import {
|
|
buildDaemonWebSocketUrl,
|
|
buildRelayWebSocketUrl,
|
|
normalizeHostPort,
|
|
parseConnectionUri,
|
|
shouldUseTlsForDefaultHostedRelay,
|
|
} from "@getpaseo/protocol/daemon-endpoints";
|
|
import {
|
|
parseConnectionOfferFromUrl,
|
|
type ConnectionOffer,
|
|
} from "@getpaseo/protocol/connection-offer";
|
|
import { parseSshTransportUri } from "@getpaseo/protocol/ssh-transport";
|
|
import { DaemonClient, type WebSocketLike } from "@getpaseo/client/internal/daemon-client";
|
|
import { WebSocket } from "ws";
|
|
import { getOrCreateCliClientId } from "./client-id.js";
|
|
import { resolveCliVersion } from "../version.js";
|
|
import { createSshTunnel } from "../ssh/ssh-tunnel.js";
|
|
|
|
export interface ConnectOptions {
|
|
target: DaemonTarget;
|
|
timeout?: number;
|
|
instance?: DaemonInstance;
|
|
}
|
|
export function resolveClientPaseoHome(
|
|
target: DaemonTarget,
|
|
env: Readonly<Record<string, string | undefined>> = process.env,
|
|
): string {
|
|
return target.kind === "instance" ? target.home : resolvePaseoHome(env);
|
|
}
|
|
const DEFAULT_TIMEOUT = 15000;
|
|
type TransportTarget =
|
|
| { type: "tcp"; url: string }
|
|
| { type: "ipc"; url: string; socketPath: string };
|
|
|
|
export function getDaemonHost(options: ConnectOptions): string {
|
|
return describeDaemonTarget(options.target);
|
|
}
|
|
|
|
export function buildDaemonConnectionCommandError(options: ConnectOptions & { error: unknown }) {
|
|
const error = options.error;
|
|
let message = error instanceof Error ? error.message : String(error);
|
|
if (error && typeof error === "object" && "message" in error) message = String(error.message);
|
|
if (options.target.kind === "endpoint")
|
|
message = message.replaceAll(options.target.host, describeDaemonTarget(options.target));
|
|
if (message.startsWith("Cannot connect to daemon at "))
|
|
return error as { code: string; message: string; details: string };
|
|
let code = "DAEMON_UNREACHABLE";
|
|
if (typeof error === "object" && error !== null && "code" in error) code = String(error.code);
|
|
else if (message === "Password required") code = "AUTH_REQUIRED";
|
|
else if (message === "Incorrect password") code = "AUTH_FAILED";
|
|
return {
|
|
code,
|
|
message: `Cannot connect to daemon at ${describeDaemonTarget(options.target)}: ${message}`,
|
|
details: describeConnectionRemedy(code, options.target),
|
|
};
|
|
}
|
|
|
|
function describeConnectionRemedy(code: string, target: DaemonTarget): string {
|
|
if (code === "AUTH_REQUIRED")
|
|
return "The daemon requires a password. Set PASEO_PASSWORD and retry.";
|
|
if (code === "AUTH_FAILED")
|
|
return "The daemon rejected the password. Check PASEO_PASSWORD and retry.";
|
|
if (target.kind === "instance")
|
|
return `Start with: paseo daemon start --home ${JSON.stringify(target.home)}`;
|
|
return "Check the selected endpoint and credentials. SSH transport does not install or start the daemon.";
|
|
}
|
|
|
|
export function normalizeDaemonHost(raw: string): string | null {
|
|
const trimmed = raw.trim();
|
|
if (!trimmed) {
|
|
return null;
|
|
}
|
|
|
|
if (trimmed.startsWith("tcp://")) {
|
|
try {
|
|
const parsed = parseConnectionUri(trimmed);
|
|
const endpoint = normalizeHostPort(
|
|
parsed.isIpv6 ? `[${parsed.host}]:${parsed.port}` : `${parsed.host}:${parsed.port}`,
|
|
);
|
|
const query = new URLSearchParams();
|
|
if (parsed.useTls) {
|
|
query.set("ssl", "true");
|
|
}
|
|
if (parsed.password) {
|
|
query.set("password", parsed.password);
|
|
}
|
|
const queryString = query.toString();
|
|
const suffix = queryString ? `?${queryString}` : "";
|
|
return `tcp://${endpoint}${suffix}`;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
if (
|
|
trimmed.startsWith("unix://") ||
|
|
trimmed.startsWith("pipe://") ||
|
|
trimmed.startsWith("\\\\.\\pipe\\")
|
|
) {
|
|
return trimmed.startsWith("\\\\.\\pipe\\") ? `pipe://${trimmed}` : trimmed;
|
|
}
|
|
|
|
if (trimmed.startsWith("/") || trimmed.startsWith("~")) {
|
|
return `unix://${trimmed}`;
|
|
}
|
|
|
|
// Windows absolute paths (e.g. C:\Users\foo) are filesystem paths, not TCP or IPC targets.
|
|
if (/^[A-Za-z]:[/\\]/.test(trimmed)) {
|
|
return null;
|
|
}
|
|
|
|
if (/^\d+$/.test(trimmed)) {
|
|
return `127.0.0.1:${trimmed}`;
|
|
}
|
|
|
|
return trimmed.includes(":") ? trimmed : null;
|
|
}
|
|
|
|
function stripIpcPrefix(trimmed: string): string {
|
|
if (trimmed.startsWith("unix://")) return trimmed.slice("unix://".length).trim();
|
|
if (trimmed.startsWith("pipe://")) return trimmed.slice("pipe://".length).trim();
|
|
return trimmed;
|
|
}
|
|
|
|
export function resolveDaemonTarget(host: string): TransportTarget {
|
|
const trimmed = normalizeDaemonHost(host);
|
|
if (!trimmed) {
|
|
throw new Error(`Invalid daemon target: ${host}`);
|
|
}
|
|
if (
|
|
trimmed.startsWith("unix://") ||
|
|
trimmed.startsWith("pipe://") ||
|
|
trimmed.startsWith("\\\\.\\pipe\\")
|
|
) {
|
|
const socketPath = stripIpcPrefix(trimmed);
|
|
if (!socketPath) {
|
|
throw new Error("Invalid IPC daemon target: missing socket path");
|
|
}
|
|
const isUnixSocket = trimmed.startsWith("unix://");
|
|
return {
|
|
type: "ipc",
|
|
url: isUnixSocket ? `ws+unix://${socketPath}:/ws` : "ws://localhost/ws",
|
|
socketPath,
|
|
};
|
|
}
|
|
|
|
if (trimmed.startsWith("tcp://")) {
|
|
const parsed = parseConnectionUri(trimmed);
|
|
const endpoint = normalizeHostPort(
|
|
parsed.isIpv6 ? `[${parsed.host}]:${parsed.port}` : `${parsed.host}:${parsed.port}`,
|
|
);
|
|
return {
|
|
type: "tcp",
|
|
url: buildDaemonWebSocketUrl(endpoint, { useTls: parsed.useTls }),
|
|
};
|
|
}
|
|
|
|
return {
|
|
type: "tcp",
|
|
url: `ws://${trimmed}/ws`,
|
|
};
|
|
}
|
|
|
|
export function resolveDaemonPassword(host: string): string | undefined {
|
|
const trimmed = host.trim();
|
|
if (trimmed.startsWith("tcp://")) {
|
|
const fromUri = parseConnectionUri(trimmed).password;
|
|
if (fromUri) return fromUri;
|
|
}
|
|
const fromEnv = process.env.PASEO_PASSWORD;
|
|
return fromEnv && fromEnv.length > 0 ? fromEnv : undefined;
|
|
}
|
|
|
|
export function resolveDaemonCredential(
|
|
host: string,
|
|
home: string,
|
|
): { kind: "password"; password: string } | { kind: "localCredential"; token: string } | null {
|
|
const password = resolveDaemonPassword(host);
|
|
if (password) return { kind: "password", password };
|
|
const token = readLocalCredentialForTarget(home, host);
|
|
return token ? { kind: "localCredential", token } : null;
|
|
}
|
|
|
|
/**
|
|
* Create a WebSocket factory that works in Node.js
|
|
*/
|
|
function createNodeWebSocketFactory() {
|
|
return (
|
|
url: string,
|
|
options?: { headers?: Record<string, string>; protocols?: string[]; socketPath?: string },
|
|
): WebSocketLike => {
|
|
return new WebSocket(url, options?.protocols, {
|
|
headers: options?.headers,
|
|
...(options?.socketPath ? { socketPath: options.socketPath } : {}),
|
|
}) as unknown as WebSocketLike;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create and connect a daemon client
|
|
* Returns the connected client or throws if connection fails
|
|
*/
|
|
async function tryConnectHost(
|
|
host: string,
|
|
credential: ReturnType<typeof resolveDaemonCredential>,
|
|
home: string,
|
|
clientId: string,
|
|
timeout: number,
|
|
nodeWebSocketFactory: ReturnType<typeof createNodeWebSocketFactory>,
|
|
): Promise<{ client: DaemonClient } | { error: unknown }> {
|
|
const target = resolveDaemonTarget(host);
|
|
const client = new DaemonClient({
|
|
url: target.url,
|
|
clientId,
|
|
clientType: "cli",
|
|
appVersion: resolveCliVersion(),
|
|
...(credential?.kind === "password" ? { password: credential.password } : {}),
|
|
...(credential?.kind === "localCredential"
|
|
? { localCredential: () => readLocalCredentialForTarget(home, host) ?? undefined }
|
|
: {}),
|
|
connectTimeoutMs: timeout,
|
|
webSocketFactory: (
|
|
url: string,
|
|
config?: { headers?: Record<string, string>; protocols?: string[] },
|
|
) =>
|
|
nodeWebSocketFactory(url, {
|
|
headers: config?.headers,
|
|
protocols: config?.protocols,
|
|
...(target.type === "ipc" ? { socketPath: target.socketPath } : {}),
|
|
}),
|
|
reconnect: { enabled: false },
|
|
});
|
|
|
|
try {
|
|
await client.connect();
|
|
return { client };
|
|
} catch (error) {
|
|
await client.close().catch(() => {});
|
|
return { error };
|
|
}
|
|
}
|
|
|
|
async function connectViaRelayOffer(
|
|
offer: ConnectionOffer,
|
|
clientId: string,
|
|
timeout: number,
|
|
nodeWebSocketFactory: ReturnType<typeof createNodeWebSocketFactory>,
|
|
): Promise<DaemonClient> {
|
|
const url = buildRelayWebSocketUrl({
|
|
endpoint: offer.relay.endpoint,
|
|
serverId: offer.serverId,
|
|
role: "client",
|
|
useTls: offer.relay.useTls ?? shouldUseTlsForDefaultHostedRelay(offer.relay.endpoint),
|
|
});
|
|
|
|
const client = new DaemonClient({
|
|
url,
|
|
clientId,
|
|
clientType: "cli",
|
|
appVersion: resolveCliVersion(),
|
|
connectTimeoutMs: timeout,
|
|
webSocketFactory: (
|
|
target: string,
|
|
config?: { headers?: Record<string, string>; protocols?: string[] },
|
|
) => nodeWebSocketFactory(target, { headers: config?.headers, protocols: config?.protocols }),
|
|
e2ee: { enabled: true, daemonPublicKeyB64: offer.daemonPublicKeyB64 },
|
|
reconnect: { enabled: false },
|
|
});
|
|
|
|
try {
|
|
await client.connect();
|
|
return client;
|
|
} catch (error) {
|
|
await client.close().catch(() => {});
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
const lastError = client.lastError ? ` (${client.lastError})` : "";
|
|
throw new Error(`Failed to connect via relay offer: ${message}${lastError}`, { cause: error });
|
|
}
|
|
}
|
|
|
|
function parseHostOfferOrNull(host: string | undefined): ConnectionOffer | null {
|
|
if (!host) return null;
|
|
try {
|
|
return parseConnectionOfferFromUrl(host);
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
throw new Error(`Invalid pairing offer URL: ${message}`, { cause: error });
|
|
}
|
|
}
|
|
|
|
async function connectSelectedDaemon(options: ConnectOptions): Promise<DaemonClient> {
|
|
const timeout = options.timeout ?? DEFAULT_TIMEOUT;
|
|
const deadline = Date.now() + timeout;
|
|
const explicitHost =
|
|
options.target.kind === "endpoint"
|
|
? options.target.host
|
|
: (
|
|
await waitForDaemonReady(options.target.home, {
|
|
timeoutMs: timeout,
|
|
instance: options.instance,
|
|
})
|
|
).listen;
|
|
const home = resolveClientPaseoHome(options.target);
|
|
const clientId = await getOrCreateCliClientId(home);
|
|
const nodeWebSocketFactory = createNodeWebSocketFactory();
|
|
|
|
if (explicitHost?.trim().startsWith("ssh://")) {
|
|
const target = parseSshTransportUri(explicitHost.trim());
|
|
const tunnel = await createSshTunnel(target);
|
|
const password = resolveDaemonPassword(explicitHost);
|
|
const result = await tryConnectHost(
|
|
tunnel.endpoint,
|
|
password ? { kind: "password", password } : null,
|
|
home,
|
|
clientId,
|
|
Math.max(1, deadline - Date.now()),
|
|
nodeWebSocketFactory,
|
|
);
|
|
if ("client" in result) {
|
|
const close = result.client.close.bind(result.client);
|
|
result.client.close = async () => {
|
|
try {
|
|
await close();
|
|
} finally {
|
|
tunnel.close();
|
|
}
|
|
};
|
|
return result.client;
|
|
}
|
|
|
|
const failure = tunnel.failureDetail();
|
|
tunnel.close();
|
|
if (failure) throw new Error(`SSH connection failed: ${failure}`, { cause: result.error });
|
|
throw result.error;
|
|
}
|
|
const offer = parseHostOfferOrNull(explicitHost);
|
|
if (offer) {
|
|
return connectViaRelayOffer(
|
|
offer,
|
|
clientId,
|
|
Math.max(1, deadline - Date.now()),
|
|
nodeWebSocketFactory,
|
|
);
|
|
}
|
|
|
|
const result = await tryConnectHost(
|
|
explicitHost,
|
|
resolveDaemonCredential(explicitHost, home),
|
|
home,
|
|
clientId,
|
|
Math.max(1, deadline - Date.now()),
|
|
nodeWebSocketFactory,
|
|
);
|
|
if ("client" in result) return result.client;
|
|
throw result.error;
|
|
}
|
|
|
|
export async function connectToDaemon(options: ConnectOptions): Promise<DaemonClient> {
|
|
try {
|
|
return await connectSelectedDaemon(options);
|
|
} catch (error) {
|
|
throw buildDaemonConnectionCommandError({ ...options, error });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Try to connect to the daemon, returns null if connection fails
|
|
*/
|
|
export async function tryConnectToDaemon(options: ConnectOptions): Promise<DaemonClient | null> {
|
|
try {
|
|
return await connectToDaemon(options);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** Minimal agent type for ID resolution */
|
|
interface AgentLike {
|
|
id: string;
|
|
title?: string | null;
|
|
}
|
|
|
|
/**
|
|
* Resolve an agent ID from a partial ID or name.
|
|
* Supports:
|
|
* - Full ID match
|
|
* - Prefix match (first N characters)
|
|
* - Title/name match (case-insensitive)
|
|
*
|
|
* Returns the full agent ID if found, null otherwise.
|
|
*/
|
|
export function resolveAgentId(idOrName: string, agents: AgentLike[]): string | null {
|
|
if (!idOrName || agents.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
const query = idOrName.toLowerCase();
|
|
|
|
// Try exact ID match first
|
|
const exactMatch = agents.find((a) => a.id === idOrName);
|
|
if (exactMatch) {
|
|
return exactMatch.id;
|
|
}
|
|
|
|
// Try ID prefix match
|
|
const prefixMatches = agents.filter((a) => a.id.toLowerCase().startsWith(query));
|
|
if (prefixMatches.length === 1 && prefixMatches[0]) {
|
|
return prefixMatches[0].id;
|
|
}
|
|
|
|
// Try title/name match (case-insensitive)
|
|
const titleMatches = agents.filter((a) => a.title?.toLowerCase() === query);
|
|
if (titleMatches.length === 1 && titleMatches[0]) {
|
|
return titleMatches[0].id;
|
|
}
|
|
|
|
// Try partial title match
|
|
const partialTitleMatches = agents.filter((a) => a.title?.toLowerCase().includes(query));
|
|
if (partialTitleMatches.length === 1 && partialTitleMatches[0]) {
|
|
return partialTitleMatches[0].id;
|
|
}
|
|
|
|
// If we have multiple prefix matches and no unique title match, return first prefix match
|
|
const firstPrefixMatch = prefixMatches[0];
|
|
if (firstPrefixMatch) {
|
|
return firstPrefixMatch.id;
|
|
}
|
|
|
|
return null;
|
|
}
|