Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(ssh): import saved servers from ~/.ssh/config (Settings button)

enesf71934248b05
diff --git a/src/main/ssh-store.ts b/src/main/ssh-store.ts
index 57c83b46c..2f904d46b 100644
--- a/src/main/ssh-store.ts
+++ b/src/main/ssh-store.ts
@@ -1,8 +1,9 @@
import { promises as fs, readFileSync } from 'fs'
+import os from 'os'
import path from 'path'
import { app, ipcMain } from 'electron'
import { IPC } from '../shared/ipc'
-import type { SshServer } from '../shared/ssh'
+import { parseSshConfig, type ParsedSshHost, type SshServer } from '../shared/ssh'
/**
* Stores saved SSH servers in ssh-servers.json. Keeps a synchronous cache so reads are
@@ -57,9 +58,20 @@ export class SshStore {
return this.writeChain
}
+ /** Parse the user's `~/.ssh/config` into importable hosts (empty if it doesn't exist). */
+ async importCandidates(): Promise<ParsedSshHost[]> {
+ try {
+ const text = await fs.readFile(path.join(os.homedir(), '.ssh', 'config'), 'utf-8')
+ return parseSshConfig(text)
+ } catch {
+ return []
+ }
+ }
+
registerIpc(): void {
ipcMain.handle(IPC.sshList, () => this.list())
ipcMain.handle(IPC.sshSave, (_e, server: SshServer) => this.save(server))
ipcMain.handle(IPC.sshDelete, (_e, id: string) => this.remove(id))
+ ipcMain.handle(IPC.sshImport, () => this.importCandidates())
}
}
diff --git a/src/preload/index.ts b/src/preload/index.ts
index a458d8ca4..5446006af 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -75,7 +75,8 @@ const api: NodeTerminalApi = {
ssh: {
list: () => ipcRenderer.invoke(IPC.sshList),
save: (server) => ipcRenderer.invoke(IPC.sshSave, server),
- remove: (id) => ipcRenderer.invoke(IPC.sshDelete, id)
+ remove: (id) => ipcRenderer.invoke(IPC.sshDelete, id),
+ importCandidates: () => ipcRenderer.invoke(IPC.sshImport)
},
git: {
status: (cwd) => ipcRenderer.invoke(IPC.gitStatus, cwd),
diff --git a/src/renderer/components/settings/sections/SshSection.tsx b/src/renderer/components/settings/sections/SshSection.tsx
index 2be394f93..1ed326ac1 100644
--- a/src/renderer/components/settings/sections/SshSection.tsx
+++ b/src/renderer/components/settings/sections/SshSection.tsx
@@ -18,11 +18,41 @@ const ENTRIES = Object.values(ROWS)
export function SshSection({ isActive }: { isActive: boolean }): React.JSX.Element {
const sshServers = useSshServers((s) => s.servers)
const [sshDraft, setSshDraft] = useState<SshServer | null>(null)
+ const [importMsg, setImportMsg] = useState<string | null>(null)
useEffect(() => {
void useSshServers.getState().hydrate()
}, [])
+ // Import named hosts from ~/.ssh/config. Dedupe against saved servers by user@host:port and
+ // skip hosts without a User (can't form a usable connection); the user can add those by hand.
+ const importFromConfig = async () => {
+ const key = (s: { user?: string; host: string; port?: number }) =>
+ `${s.user ?? ''}@${s.host}:${s.port ?? 22}`
+ const candidates = await window.nodeTerminal.ssh.importCandidates()
+ const withUser = candidates.filter((c) => c.user)
+ const skipped = candidates.length - withUser.length
+ const seen = new Set(useSshServers.getState().servers.map(key))
+ const fresh = withUser.filter((c) => !seen.has(key(c)))
+ for (const c of fresh) {
+ await useSshServers.getState().save({
+ id: crypto.randomUUID(),
+ label: c.label,
+ host: c.host,
+ user: c.user as string,
+ port: c.port,
+ identityFile: c.identityFile
+ })
+ }
+ setImportMsg(
+ candidates.length === 0
+ ? 'No hosts found in ~/.ssh/config'
+ : `${fresh.length ? `Imported ${fresh.length} server${fresh.length === 1 ? '' : 's'}` : 'No new servers to import'}${
+ skipped ? ` · skipped ${skipped} without a user` : ''
+ }`
+ )
+ }
+
const saveDisabled =
!sshDraft ||
!sshDraft.label.trim() ||
@@ -161,19 +191,25 @@ export function SshSection({ isActive }: { isActive: boolean }): React.JSX.Eleme
</div>
</div>
) : (
- <Button
- onClick={() =>
- setSshDraft({
- id: crypto.randomUUID(),
- label: '',
- host: '',
- user: '',
- port: 22
- })
- }
- >
- Add server
- </Button>
+ <div className="flex items-center gap-2">
+ <Button
+ onClick={() =>
+ setSshDraft({
+ id: crypto.randomUUID(),
+ label: '',
+ host: '',
+ user: '',
+ port: 22
+ })
+ }
+ >
+ Add server
+ </Button>
+ <Button variant="ghost" onClick={() => void importFromConfig()}>
+ Import from ~/.ssh/config
+ </Button>
+ {importMsg && <span className="text-xs text-muted">{importMsg}</span>}
+ </div>
)}
</div>
</SearchableRow>
diff --git a/src/shared/ipc.ts b/src/shared/ipc.ts
index c91dc5c16..9f349db09 100644
--- a/src/shared/ipc.ts
+++ b/src/shared/ipc.ts
@@ -60,6 +60,7 @@ export const IPC = {
sshList: 'ssh:list',
sshSave: 'ssh:save',
sshDelete: 'ssh:delete',
+ sshImport: 'ssh:import-candidates',
gitStatus: 'git:status',
gitInit: 'git:init',
gitClone: 'git:clone',
diff --git a/src/shared/ssh.test.ts b/src/shared/ssh.test.ts
index 488484ed2..81f09880d 100644
--- a/src/shared/ssh.test.ts
+++ b/src/shared/ssh.test.ts
@@ -1,5 +1,46 @@
import { describe, expect, it } from 'vitest'
-import { buildSshArgs, parseExtraArgs } from './ssh'
+import { buildSshArgs, parseExtraArgs, parseSshConfig } from './ssh'
+
+describe('parseSshConfig', () => {
+ it('parses a named host with HostName/User/Port/IdentityFile', () => {
+ const cfg = `Host prod
+ HostName 10.0.0.5
+ User deploy
+ Port 2222
+ IdentityFile ~/.ssh/prod_ed25519`
+ expect(parseSshConfig(cfg)).toEqual([
+ { label: 'prod', host: '10.0.0.5', user: 'deploy', port: 2222, identityFile: '~/.ssh/prod_ed25519' }
+ ])
+ })
+
+ it('falls back to the alias when HostName is absent and leaves optional fields undefined', () => {
+ expect(parseSshConfig('Host box\n User me')).toEqual([
+ { label: 'box', host: 'box', user: 'me', port: undefined, identityFile: undefined }
+ ])
+ })
+
+ it('skips wildcard hosts and the catch-all', () => {
+ const cfg = `Host *
+ User everyone
+Host *.internal
+ User x
+Host real
+ HostName r.example.com`
+ expect(parseSshConfig(cfg)).toEqual([
+ { label: 'real', host: 'r.example.com', user: undefined, port: undefined, identityFile: undefined }
+ ])
+ })
+
+ it('accepts key=value form, comments, and multiple aliases on one Host line', () => {
+ const cfg = `Host a b # two aliases
+ HostName=h.example.com
+ Port=22`
+ expect(parseSshConfig(cfg)).toEqual([
+ { label: 'a', host: 'h.example.com', user: undefined, port: 22, identityFile: undefined },
+ { label: 'b', host: 'h.example.com', user: undefined, port: 22, identityFile: undefined }
+ ])
+ })
+})
describe('buildSshArgs', () => {
it('minimal host/user', () => {
diff --git a/src/shared/ssh.ts b/src/shared/ssh.ts
index e9ef6dd5a..f0358b69e 100644
--- a/src/shared/ssh.ts
+++ b/src/shared/ssh.ts
@@ -61,3 +61,71 @@ export function buildSshArgs(conn: SshConnection): string[] {
args.push(`${conn.user}@${conn.host}`)
return args
}
+
+/** A host parsed from `~/.ssh/config`, ready to seed a saved server (no id yet). */
+export interface ParsedSshHost {
+ /** The `Host` alias (display label). */
+ label: string
+ /** `HostName` if set, else the alias. */
+ host: string
+ user?: string
+ port?: number
+ identityFile?: string
+}
+
+/**
+ * Parse `~/.ssh/config` text into named hosts. Each non-wildcard `Host` alias becomes one
+ * entry, taking the block's `HostName`/`User`/`Port`/`IdentityFile`. Wildcard aliases
+ * (containing `*` or `?`) and the bare `Host *` catch-all are skipped — they aren't concrete
+ * servers. Keys are case-insensitive; `key=value` and `key value` forms are both accepted.
+ */
+export function parseSshConfig(text: string): ParsedSshHost[] {
+ const hosts: ParsedSshHost[] = []
+ // Aliases sharing one `Host` line all receive the block's settings.
+ let current: { aliases: string[]; settings: Record<string, string> } | null = null
+
+ const flush = () => {
+ if (!current) return
+ for (const alias of current.aliases) {
+ if (alias.includes('*') || alias.includes('?')) continue
+ const s = current.settings
+ const port = s.port ? Number(s.port) : undefined
+ hosts.push({
+ label: alias,
+ host: s.hostname || alias,
+ user: s.user || undefined,
+ port: Number.isFinite(port) ? port : undefined,
+ identityFile: s.identityfile || undefined
+ })
+ }
+ current = null
+ }
+
+ for (const raw of text.split('\n')) {
+ const line = raw.replace(/#.*$/, '').trim()
+ if (!line) continue
+ const eq = line.indexOf('=')
+ const sp = line.search(/\s/)
+ let key: string
+ let value: string
+ if (eq !== -1 && (sp === -1 || eq < sp)) {
+ key = line.slice(0, eq).trim()
+ value = line.slice(eq + 1).trim()
+ } else if (sp !== -1) {
+ key = line.slice(0, sp).trim()
+ value = line.slice(sp + 1).trim()
+ } else {
+ key = line
+ value = ''
+ }
+ const lkey = key.toLowerCase()
+ if (lkey === 'host') {
+ flush()
+ current = { aliases: value.split(/\s+/).filter(Boolean), settings: {} }
+ } else if (current) {
+ current.settings[lkey] = value
+ }
+ }
+ flush()
+ return hosts
+}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 512a49e6b..8204f353f 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -282,6 +282,8 @@ export interface SshApi {
list(): Promise<import('./ssh').SshServer[]>
save(server: import('./ssh').SshServer): Promise<import('./ssh').SshServer[]>
remove(id: string): Promise<import('./ssh').SshServer[]>
+ /** Parse `~/.ssh/config` into importable hosts (empty if none). */
+ importCandidates(): Promise<import('./ssh').ParsedSshHost[]>
}
export interface GitFileChange {