fix: support windows ssh hosts (#5004)

* feat: add windows ssh relay base support

* feat: support windows ssh relay runtime services

* fix: default windows ssh pty cwd to user profile

* fix: support windows hosts over system ssh

* fix: preserve degraded windows relay native deps

* fix: gate windows shell args by relay platform

* fix: preserve windows relay fallback pipes

* test: align windows native deps relay fixture

* fix: build valid windows install lock command

* fix: address windows SSH relay review findings

Resolve correctness, efficiency, and reuse issues found reviewing the
Windows SSH native-host support:

- GC liveness on Windows now probes the actual named pipe (via node
  net.connect against markers + deterministic candidates) instead of
  substring-matching Win32_Process command lines, which could remove a
  live relay dir. Reports ALIVE conservatively only when there is no
  liveness signal at all (no markers and no seed pipes).
- Resolve the remote node path once per deploy and thread it through
  install/repair/launch instead of re-resolving 3-7x.
- Replace the 200ms node -e poll loop with a single long-lived remote
  wait process during Windows relay startup.
- Skip the no-op executable command on Windows in uploadRelay.
- Make the Windows fallback pipe name deterministic and recoverable
  (drop the global counter), with an extra reconnect attempt.
- Normalize the prepended node bin dir to backslashes on Windows PATH.
- Batch the system-SSH Windows directory upload into a single streamed
  JSON package instead of one ssh process per file.
- Extract relay endpoint/marker helpers into ssh-relay-endpoints.ts and
  consolidate the PowerShell EncodedCommand encoding into the shared
  powershell-command-encoding module.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Support cancellation and timeouts in Windows port scanning

- Propagate the request AbortSignal and a 5-second timeout to both
  PowerShell and netstat child processes during Windows port scanning.
- Avoid spawning the netstat fallback process if the port scan has
  already been aborted.
- Wrap the .NET OSArchitecture check in a try/catch block during SSH
  Windows platform detection to robustly fall back to environment
  variables if needed.

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
This commit is contained in:
Leynier Gutiérrez González
2026-06-09 01:17:34 -07:00
committed by GitHub
co-authored by Claude Opus 4.8 Jinjing
parent ce66cee37b
commit 98d02bca47
49 changed files with 3055 additions and 314 deletions
+185 -2
View File
@@ -1,11 +1,17 @@
/* eslint-disable max-lines -- Why: system-ssh process wrapping and fallback file operations share cleanup contracts. */
import { spawn, type ChildProcess } from 'child_process'
import { constants } from 'node:fs'
import { existsSync } from 'fs'
import { lstat, open, readdir } from 'fs/promises'
import { join as pathJoin } from 'path'
import { Duplex } from 'stream'
import { pipeline } from 'stream/promises'
import type { ClientChannel } from 'ssh2'
import type { SshTarget } from '../../shared/ssh-types'
import { wrapRemoteCommandForPosixShell, shellEscape } from './ssh-connection-utils'
import type { SshExecOptions } from './ssh-connection-utils'
import { isWindowsRemoteHost, joinRemotePath, type RemoteHostPlatform } from './ssh-remote-platform'
import { powerShellCommand, powerShellLiteral } from './ssh-remote-powershell'
const SYSTEM_SSH_PATHS =
process.platform === 'win32'
@@ -27,6 +33,7 @@ type SystemSshCommandChannel = ClientChannel & {
type SystemSshOperationOptions = {
signal?: AbortSignal
hostPlatform?: RemoteHostPlatform
}
/**
@@ -68,7 +75,11 @@ export function spawnSystemSsh(target: SshTarget): SystemSshProcess {
return wrapChildProcess(proc)
}
export function spawnSystemSshCommand(target: SshTarget, command: string): ClientChannel {
export function spawnSystemSshCommand(
target: SshTarget,
command: string,
options?: SshExecOptions
): ClientChannel {
const sshPath = findSystemSsh()
if (!sshPath) {
throw new Error(
@@ -76,7 +87,9 @@ export function spawnSystemSshCommand(target: SshTarget, command: string): Clien
)
}
const proc = spawn(sshPath, [...buildSshArgs(target), wrapRemoteCommandForPosixShell(command)], {
const remoteCommand =
options?.wrapCommand === false ? command : wrapRemoteCommandForPosixShell(command)
const proc = spawn(sshPath, [...buildSshArgs(target), remoteCommand], {
stdio: ['pipe', 'pipe', 'pipe'],
windowsHide: true
})
@@ -90,6 +103,11 @@ export async function uploadDirectoryViaSystemSsh(
options?: SystemSshOperationOptions
): Promise<void> {
throwIfAborted(options?.signal)
if (options?.hostPlatform && isWindowsRemoteHost(options.hostPlatform)) {
await uploadDirectoryViaSystemSshWindows(target, localDir, remoteDir, options)
return
}
const sshPath = findSystemSsh()
if (!sshPath) {
throw new Error('No system ssh binary found. Install OpenSSH to use system SSH transport.')
@@ -145,6 +163,16 @@ export async function writeFileViaSystemSsh(
options?: SystemSshOperationOptions
): Promise<void> {
throwIfAborted(options?.signal)
if (options?.hostPlatform && isWindowsRemoteHost(options.hostPlatform)) {
await writeBufferViaSystemSshWindows(
target,
remotePath,
Buffer.from(contents, 'utf-8'),
options
)
return
}
const channel = spawnSystemSshCommand(target, `cat > ${shellEscape(remotePath)}`)
const closePromise = awaitWithSystemSshAbort(
options?.signal,
@@ -157,6 +185,161 @@ export async function writeFileViaSystemSsh(
await closePromise
}
async function uploadDirectoryViaSystemSshWindows(
target: SshTarget,
localDir: string,
remoteDir: string,
options: SystemSshOperationOptions
): Promise<void> {
const hostPlatform = options.hostPlatform
if (!hostPlatform) {
throw new Error('Windows system SSH upload requires a remote host platform')
}
const entries = await collectWindowsUploadEntries(
localDir,
remoteDir,
hostPlatform,
options.signal
)
await writeWindowsUploadPackageViaSystemSsh(target, entries, options)
}
type WindowsUploadEntry =
| {
kind: 'directory'
path: string
}
| {
kind: 'file'
path: string
contentsBase64: string
}
async function collectWindowsUploadEntries(
localDir: string,
remoteDir: string,
hostPlatform: RemoteHostPlatform,
signal: AbortSignal | undefined
): Promise<WindowsUploadEntry[]> {
const entries: WindowsUploadEntry[] = [{ kind: 'directory', path: remoteDir }]
const dirEntries = await readdir(localDir, { withFileTypes: true })
for (const entry of dirEntries) {
throwIfAborted(signal)
const localPath = pathJoin(localDir, entry.name)
const remotePath = joinRemotePath(hostPlatform, remoteDir, entry.name)
const statResult = await lstat(localPath)
if (statResult.isSymbolicLink() || (!statResult.isFile() && !statResult.isDirectory())) {
continue
}
if (statResult.isDirectory()) {
entries.push(
...(await collectWindowsUploadEntries(localPath, remotePath, hostPlatform, signal))
)
continue
}
const buffer = await readLocalUploadFile(localPath, statResult)
entries.push({ kind: 'file', path: remotePath, contentsBase64: buffer.toString('base64') })
}
return entries
}
async function writeWindowsUploadPackageViaSystemSsh(
target: SshTarget,
entries: WindowsUploadEntry[],
options: SystemSshOperationOptions
): Promise<void> {
throwIfAborted(options.signal)
const channel = spawnSystemSshCommand(target, makeWindowsUploadPackageCommand(), {
wrapCommand: false
})
const closePromise = awaitWithSystemSshAbort(
options.signal,
() => channel.close(),
waitForChannelClose(channel, 'windows relay upload')
)
if (!options.signal?.aborted) {
channel.stdin.end(JSON.stringify(entries))
}
await closePromise
}
async function readLocalUploadFile(
localPath: string,
statResult: Awaited<ReturnType<typeof lstat>>
): Promise<Buffer> {
const handle = await open(localPath, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0))
try {
const openedStat = await handle.stat()
if (
!openedStat.isFile() ||
openedStat.size !== statResult.size ||
(statResult.ino !== 0 && openedStat.ino !== 0 && openedStat.ino !== statResult.ino) ||
(statResult.dev !== 0 && openedStat.dev !== 0 && openedStat.dev !== statResult.dev)
) {
throw new Error(`File changed during upload: ${localPath}`)
}
return await handle.readFile()
} finally {
await handle.close()
}
}
async function writeBufferViaSystemSshWindows(
target: SshTarget,
remotePath: string,
contents: Buffer,
options: SystemSshOperationOptions
): Promise<void> {
throwIfAborted(options.signal)
const channel = spawnSystemSshCommand(target, makeWindowsWriteFileCommand(remotePath), {
wrapCommand: false
})
const closePromise = awaitWithSystemSshAbort(
options.signal,
() => channel.close(),
waitForChannelClose(channel, `write ${remotePath}`)
)
if (!options.signal?.aborted) {
channel.stdin.end(contents)
}
await closePromise
}
function makeWindowsWriteFileCommand(remotePath: string): string {
return powerShellCommand(
[
'$ErrorActionPreference = "Stop"',
`$path = ${powerShellLiteral(remotePath)}`,
'$parent = [System.IO.Path]::GetDirectoryName($path)',
'if ($parent) { $null = [System.IO.Directory]::CreateDirectory($parent) }',
'$inputStream = [Console]::OpenStandardInput()',
'$outputStream = [System.IO.File]::Open($path, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write, [System.IO.FileShare]::None)',
'try { $inputStream.CopyTo($outputStream) } finally { $outputStream.Dispose() }'
].join('; ')
)
}
function makeWindowsUploadPackageCommand(): string {
return powerShellCommand(
[
'$ErrorActionPreference = "Stop"',
'$json = [Console]::In.ReadToEnd()',
'if ([string]::IsNullOrWhiteSpace($json)) { return }',
'$items = $json | ConvertFrom-Json',
'foreach ($item in @($items)) {',
' $path = [string]$item.path',
' if ($item.kind -eq "directory") {',
' $null = [System.IO.Directory]::CreateDirectory($path)',
' continue',
' }',
' $parent = [System.IO.Path]::GetDirectoryName($path)',
' if ($parent) { $null = [System.IO.Directory]::CreateDirectory($parent) }',
' [System.IO.File]::WriteAllBytes($path, [Convert]::FromBase64String([string]$item.contentsBase64))',
'}'
].join('; ')
)
}
export function buildSshArgs(target: SshTarget): string[] {
const args: string[] = []