|
| 1 | +import type * as CommandExecutor from "@effect/platform/CommandExecutor" |
| 2 | +import type { PlatformError } from "@effect/platform/Error" |
| 3 | +import * as FileSystem from "@effect/platform/FileSystem" |
| 4 | +import * as Path from "@effect/platform/Path" |
| 5 | +import { Effect } from "effect" |
| 6 | + |
| 7 | +import type { TemplateConfig } from "../core/domain.js" |
| 8 | +import { runDockerInspectContainerIp } from "../shell/docker.js" |
| 9 | +import { findSshPrivateKey, resolveAuthorizedKeysPath } from "./path-helpers.js" |
| 10 | + |
| 11 | +const sshOptions = "-tt -Y -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" |
| 12 | + |
| 13 | +const sanitizeSshHostAlias = (value: string): string => { |
| 14 | + const normalized = value |
| 15 | + .trim() |
| 16 | + .replace(/[^A-Za-z0-9._-]+/g, "-") |
| 17 | + .replace(/-+/g, "-") |
| 18 | + .replace(/^[.-]+|[.-]+$/g, "") |
| 19 | + return normalized.length === 0 ? "docker-git" : normalized |
| 20 | +} |
| 21 | + |
| 22 | +export type EditorSshAccess = { |
| 23 | + readonly alias: string |
| 24 | + readonly host: string |
| 25 | + readonly port: number |
| 26 | + readonly workspacePath: string |
| 27 | + readonly configSnippet: string |
| 28 | + readonly terminalShortcut: string |
| 29 | +} |
| 30 | + |
| 31 | +export type ResolvedProjectSshAccess = { |
| 32 | + readonly sshCommand: string |
| 33 | + readonly editor: EditorSshAccess |
| 34 | + readonly authorizedKeysPath: string |
| 35 | + readonly authorizedKeysExists: boolean |
| 36 | + readonly ipAddress?: string | undefined |
| 37 | +} |
| 38 | + |
| 39 | +// CHANGE: centralize ssh command rendering for terminal access |
| 40 | +// WHY: keep terminal ssh output and editor ssh output derived from the same topology |
| 41 | +// QUOTE(ТЗ): "подключиться по SSH" |
| 42 | +// REF: issue-196 |
| 43 | +// SOURCE: n/a |
| 44 | +// FORMAT THEOREM: forall c: config(c) -> command(c) is deterministic |
| 45 | +// PURITY: CORE |
| 46 | +// EFFECT: n/a |
| 47 | +// INVARIANT: localhost uses configured sshPort; container IP uses port 22 |
| 48 | +// COMPLEXITY: O(1) |
| 49 | +export const buildSshCommand = ( |
| 50 | + config: TemplateConfig, |
| 51 | + sshKey: string | null, |
| 52 | + ipAddress?: string |
| 53 | +): string => { |
| 54 | + const host = ipAddress ?? "localhost" |
| 55 | + const port = ipAddress ? 22 : config.sshPort |
| 56 | + return sshKey === null |
| 57 | + ? `ssh ${sshOptions} -p ${port} ${config.sshUser}@${host}` |
| 58 | + : `ssh -i ${sshKey} ${sshOptions} -p ${port} ${config.sshUser}@${host}` |
| 59 | +} |
| 60 | + |
| 61 | +// CHANGE: derive a stable Remote-SSH host alias and config snippet |
| 62 | +// WHY: Cursor/VS Code Remote-SSH are more reliable with ~/.ssh/config aliases than inline nested ssh commands |
| 63 | +// QUOTE(ТЗ): "Что бы можно было подключиться к SSH одной командой?" |
| 64 | +// REF: issue-196 |
| 65 | +// SOURCE: n/a |
| 66 | +// FORMAT THEOREM: forall c: config(c) -> alias(c) ∧ snippet(c) |
| 67 | +// PURITY: CORE |
| 68 | +// EFFECT: n/a |
| 69 | +// INVARIANT: alias is shell-safe and derived from container identity |
| 70 | +// COMPLEXITY: O(1) |
| 71 | +export const buildEditorSshAccess = ( |
| 72 | + config: TemplateConfig, |
| 73 | + sshKeyPath: string | null, |
| 74 | + ipAddress?: string |
| 75 | +): EditorSshAccess => { |
| 76 | + const alias = sanitizeSshHostAlias(config.containerName) |
| 77 | + const host = ipAddress ?? "localhost" |
| 78 | + const port = ipAddress ? 22 : config.sshPort |
| 79 | + const configLines = [ |
| 80 | + `Host ${alias}`, |
| 81 | + ` HostName ${host}`, |
| 82 | + ` User ${config.sshUser}`, |
| 83 | + ` Port ${port}`, |
| 84 | + " LogLevel ERROR", |
| 85 | + " StrictHostKeyChecking no", |
| 86 | + " UserKnownHostsFile /dev/null" |
| 87 | + ] |
| 88 | + if (sshKeyPath !== null) { |
| 89 | + configLines.push(` IdentityFile ${sshKeyPath}`, " IdentitiesOnly yes") |
| 90 | + } |
| 91 | + return { |
| 92 | + alias, |
| 93 | + host, |
| 94 | + port, |
| 95 | + workspacePath: config.targetDir, |
| 96 | + configSnippet: configLines.join("\n"), |
| 97 | + terminalShortcut: `ssh ${alias}` |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export const formatEditorSshAccessSummary = ( |
| 102 | + access: EditorSshAccess, |
| 103 | + clonedOnHostname?: string |
| 104 | +): string => { |
| 105 | + const firstHopLine = clonedOnHostname === undefined |
| 106 | + ? "" |
| 107 | + : `\nFirst hop host: ${clonedOnHostname} (add ProxyJump/ProxyCommand when connecting from another machine)` |
| 108 | + return `Remote-SSH host: ${access.alias} |
| 109 | +Terminal shortcut: ${access.terminalShortcut} |
| 110 | +Remote workspace: ${access.workspacePath}${firstHopLine}` |
| 111 | +} |
| 112 | + |
| 113 | +export const formatEditorSshAccessDetails = ( |
| 114 | + access: EditorSshAccess, |
| 115 | + clonedOnHostname?: string |
| 116 | +): string => { |
| 117 | + const firstHopNote = clonedOnHostname === undefined |
| 118 | + ? "" |
| 119 | + : `\nIf your editor runs on another machine, keep this host block as the inner container target and add your own ProxyJump/ProxyCommand for the first hop to ${clonedOnHostname}.` |
| 120 | + return `Remote-SSH host: ${access.alias} |
| 121 | +Terminal shortcut: ${access.terminalShortcut} |
| 122 | +Remote workspace: ${access.workspacePath} |
| 123 | +VS Code/Cursor: Connect to Host... -> ${access.alias} |
| 124 | +
|
| 125 | +Add to ~/.ssh/config: |
| 126 | +${access.configSnippet}${firstHopNote}` |
| 127 | +} |
| 128 | + |
| 129 | +// CHANGE: resolve terminal/editor SSH access from the current runtime context |
| 130 | +// WHY: create/clone and list flows need consistent access info without duplicating fs/docker probing |
| 131 | +// QUOTE(ТЗ): "как подключиться к SSH к Cursor, VS code" |
| 132 | +// REF: issue-196 |
| 133 | +// SOURCE: n/a |
| 134 | +// FORMAT THEOREM: forall c: runtime(c) -> ssh(c) ∧ editor(c) |
| 135 | +// PURITY: SHELL |
| 136 | +// EFFECT: Effect<ResolvedProjectSshAccess, PlatformError, FileSystem | Path | CommandExecutor> |
| 137 | +// INVARIANT: authorized_keys path and ssh key are resolved against the same baseDir |
| 138 | +// COMPLEXITY: O(1) + docker inspect |
| 139 | +export const resolveProjectSshAccess = ( |
| 140 | + baseDir: string, |
| 141 | + config: TemplateConfig |
| 142 | +): Effect.Effect< |
| 143 | + ResolvedProjectSshAccess, |
| 144 | + PlatformError, |
| 145 | + FileSystem.FileSystem | Path.Path | CommandExecutor.CommandExecutor |
| 146 | +> => |
| 147 | + Effect.gen(function*(_) { |
| 148 | + const fs = yield* _(FileSystem.FileSystem) |
| 149 | + const path = yield* _(Path.Path) |
| 150 | + |
| 151 | + const isInsideContainer = yield* _(fs.exists("/.dockerenv")) |
| 152 | + const ipAddress = isInsideContainer |
| 153 | + ? yield* _( |
| 154 | + runDockerInspectContainerIp(baseDir, config.containerName).pipe( |
| 155 | + Effect.orElse(() => Effect.succeed("")), |
| 156 | + Effect.map((value) => (value.length > 0 ? value : undefined)) |
| 157 | + ) |
| 158 | + ) |
| 159 | + : undefined |
| 160 | + |
| 161 | + const authorizedKeysPath = resolveAuthorizedKeysPath(path, baseDir, config.authorizedKeysPath) |
| 162 | + const authorizedKeysExists = yield* _(fs.exists(authorizedKeysPath)) |
| 163 | + const sshKeyPath = yield* _(findSshPrivateKey(fs, path, process.cwd())) |
| 164 | + const editor = buildEditorSshAccess(config, sshKeyPath, ipAddress) |
| 165 | + |
| 166 | + return { |
| 167 | + sshCommand: buildSshCommand(config, sshKeyPath, ipAddress), |
| 168 | + editor, |
| 169 | + authorizedKeysPath, |
| 170 | + authorizedKeysExists, |
| 171 | + ipAddress |
| 172 | + } |
| 173 | + }) |
0 commit comments