GoodBuddy Agents / Validate Agent source (push) Canceled after 0s
GoodBuddy Agents / Build Linux arm64 Agent (push) Canceled after 0s
GoodBuddy Agents / Build Linux x64 Agent (push) Canceled after 0s
Cross-platform packages / Validate source (push) Canceled after 0s
Cross-platform packages / Authorize desktop packaging (push) Canceled after 0s
Cross-platform packages / linux arm64 (push) Canceled after 0s
Cross-platform packages / macos arm64 (push) Canceled after 0s
Cross-platform packages / windows arm64 (push) Canceled after 0s
Cross-platform packages / linux x64 (push) Canceled after 0s
Cross-platform packages / macos x64 (push) Canceled after 0s
Cross-platform packages / windows x64 (push) Canceled after 0s
Cross-platform packages / Publish GitHub and OSS release (push) Canceled after 0s
Managed SSH project selection repeatedly activated the remote Host and could leave idle ACP bindings rejected as stale-controller after a controller generation change. Host environment updates also coupled project state to component installation evidence and presented a crowded control surface. Project selection is now local-only, one Host connection serves compatible project and conversation callers, idle bindings are safely replaced only after proven controller takeover, and active prompts keep exact resume/replay. Remote environment setup now uses explicit signed compound package preparation with bounded transfer, rollback, recovery, and clearer settings. Managed Hosts must be updated to separately distributed Agent 0.11.6 before the daemon-side controller recovery fix takes effect. Desktop packages do not embed the Agent or remote Runtime, and existing project data is preserved. Release note: 托管 SSH 项目切换不再重复连接,同一 Host 可顺畅承载多个项目和对话;同时修复重连后的 stale-controller,并优化远程环境安装、更新与状态布局。
358 lines
11 KiB
TypeScript
358 lines
11 KiB
TypeScript
import { randomUUID } from 'node:crypto'
|
|
import type {
|
|
SshHostDraftInspectionRequest,
|
|
SshHostKeyInspection,
|
|
SshHostsSnapshot,
|
|
SshHostValidationRequest,
|
|
SshHostValidationResult
|
|
} from '../../shared/ssh-host-contracts'
|
|
import {
|
|
sshHostCreateInputSchema,
|
|
sshHostDraftInspectionRequestSchema,
|
|
sshHostUpdateInputSchema,
|
|
sshHostValidationRequestSchema
|
|
} from '../../shared/ssh-host-contracts'
|
|
import type { SshHostStore } from './ssh-host-store'
|
|
import type {
|
|
SshHostKeyCandidate,
|
|
SshTransport
|
|
} from './ssh-transport'
|
|
|
|
const CANDIDATE_TTL_MS = 5 * 60_000
|
|
const MAX_PENDING_CANDIDATES = 100
|
|
|
|
type PendingHostKey = {
|
|
candidate: SshHostKeyCandidate
|
|
hostId?: string
|
|
expectedRevision?: number
|
|
sourceTargetKey?: string
|
|
targetKey: string
|
|
expiresAt: number
|
|
}
|
|
|
|
function targetKey(host: {
|
|
hostname: string
|
|
port: number
|
|
username: string
|
|
}): string {
|
|
return `${host.hostname}\0${host.port}\0${host.username}`
|
|
}
|
|
|
|
export type SshHostLifecycleHooks = {
|
|
onHostEdited?: (hostId: string) => void
|
|
onHostRemoved?: (hostId: string) => void
|
|
}
|
|
|
|
export class SshHostService {
|
|
private readonly pendingHostKeys = new Map<
|
|
string,
|
|
PendingHostKey
|
|
>()
|
|
private activeInspections = 0
|
|
private readonly activeInspectionKeys = new Set<string>()
|
|
private readonly activeValidations = new Set<string>()
|
|
private readonly activeValidationKeys = new Set<string>()
|
|
private readonly now: () => number
|
|
private readonly lifecycleHooks: SshHostLifecycleHooks
|
|
|
|
constructor(
|
|
private readonly store: SshHostStore,
|
|
private readonly transport: SshTransport,
|
|
nowOrHooks: (() => number) | SshHostLifecycleHooks = Date.now,
|
|
lifecycleHooks: SshHostLifecycleHooks = {}
|
|
) {
|
|
this.now =
|
|
typeof nowOrHooks === 'function' ? nowOrHooks : Date.now
|
|
this.lifecycleHooks =
|
|
typeof nowOrHooks === 'function'
|
|
? lifecycleHooks
|
|
: nowOrHooks
|
|
}
|
|
|
|
getSnapshot(): Promise<SshHostsSnapshot> {
|
|
return this.store.getSnapshot()
|
|
}
|
|
|
|
remove(hostId: string): Promise<void>
|
|
remove<T>(
|
|
hostId: string,
|
|
commitLocalDeletion: () => T
|
|
): Promise<T>
|
|
async remove<T>(
|
|
hostId: string,
|
|
commitLocalDeletion?: () => T
|
|
): Promise<T | void> {
|
|
const result = commitLocalDeletion
|
|
? await this.store.remove(hostId, commitLocalDeletion)
|
|
: await this.store.remove(hostId)
|
|
for (const [candidateId, pending] of this.pendingHostKeys) {
|
|
if (pending.hostId === hostId) {
|
|
this.pendingHostKeys.delete(candidateId)
|
|
}
|
|
}
|
|
this.lifecycleHooks.onHostRemoved?.(hostId)
|
|
return result
|
|
}
|
|
|
|
async inspectDraftHostKey(
|
|
request: SshHostDraftInspectionRequest
|
|
): Promise<SshHostKeyInspection> {
|
|
const parsed = sshHostDraftInspectionRequestSchema.parse(request)
|
|
const requestedTargetKey = targetKey(parsed)
|
|
const inspectionKeys = [
|
|
`target:${requestedTargetKey}`,
|
|
...(parsed.hostId ? [`host:${parsed.hostId}`] : [])
|
|
]
|
|
if (
|
|
inspectionKeys.some((key) =>
|
|
this.activeValidationKeys.has(key)
|
|
)
|
|
) {
|
|
throw new Error('SSH 主机正在验证,请等待当前操作完成')
|
|
}
|
|
if (
|
|
inspectionKeys.some((key) =>
|
|
this.activeInspectionKeys.has(key)
|
|
)
|
|
) {
|
|
throw new Error('SSH 主机密钥正在检查,请等待当前操作完成')
|
|
}
|
|
for (const [candidateId, pending] of this.pendingHostKeys) {
|
|
if (pending.expiresAt <= this.now()) {
|
|
this.pendingHostKeys.delete(candidateId)
|
|
}
|
|
}
|
|
if (
|
|
this.pendingHostKeys.size + this.activeInspections >=
|
|
MAX_PENDING_CANDIDATES
|
|
) {
|
|
throw new Error('SSH 主机密钥检查过多,请稍后重试')
|
|
}
|
|
this.activeInspections += 1
|
|
for (const key of inspectionKeys) {
|
|
this.activeInspectionKeys.add(key)
|
|
}
|
|
let existing:
|
|
| Awaited<ReturnType<SshHostStore['getHostIdentity']>>
|
|
| undefined
|
|
let candidate: SshHostKeyCandidate
|
|
try {
|
|
existing = parsed.hostId
|
|
? await this.store.getHostIdentity(parsed.hostId)
|
|
: undefined
|
|
candidate = await this.transport.inspectHostKey({
|
|
hostname: parsed.hostname,
|
|
port: parsed.port,
|
|
username: parsed.username
|
|
})
|
|
} finally {
|
|
this.activeInspections -= 1
|
|
for (const key of inspectionKeys) {
|
|
this.activeInspectionKeys.delete(key)
|
|
}
|
|
}
|
|
const sameEndpoint =
|
|
existing !== undefined &&
|
|
existing.hostname === parsed.hostname &&
|
|
existing.port === parsed.port
|
|
const existingHostKey = sameEndpoint
|
|
? existing?.hostKey
|
|
: undefined
|
|
const state =
|
|
!existingHostKey
|
|
? 'unverified'
|
|
: existingHostKey.fingerprintSha256 ===
|
|
candidate.fingerprintSha256
|
|
? 'verified'
|
|
: 'changed'
|
|
const candidateId = randomUUID()
|
|
for (const [existingId, pending] of this.pendingHostKeys) {
|
|
if (
|
|
!this.activeValidations.has(existingId) &&
|
|
(pending.targetKey === requestedTargetKey ||
|
|
(existing && pending.hostId === existing.id))
|
|
) {
|
|
this.pendingHostKeys.delete(existingId)
|
|
}
|
|
}
|
|
this.pendingHostKeys.set(candidateId, {
|
|
candidate,
|
|
...(existing
|
|
? {
|
|
hostId: existing.id,
|
|
expectedRevision: existing.revision,
|
|
sourceTargetKey: targetKey(existing)
|
|
}
|
|
: {}),
|
|
targetKey: requestedTargetKey,
|
|
expiresAt: this.now() + CANDIDATE_TTL_MS
|
|
})
|
|
return {
|
|
candidateId,
|
|
...(existing ? { hostId: existing.id } : {}),
|
|
state,
|
|
algorithm: candidate.algorithm,
|
|
fingerprintSha256: candidate.fingerprintSha256,
|
|
...(state === 'changed' && existing?.hostKey
|
|
? {
|
|
previousHostKey: {
|
|
algorithm: existing.hostKey.algorithm,
|
|
fingerprintSha256:
|
|
existing.hostKey.fingerprintSha256
|
|
}
|
|
}
|
|
: {})
|
|
}
|
|
}
|
|
|
|
discardCandidate(candidateId: string): void {
|
|
if (!this.activeValidations.has(candidateId)) {
|
|
this.pendingHostKeys.delete(candidateId)
|
|
}
|
|
}
|
|
|
|
async validateAndSave(
|
|
request: SshHostValidationRequest
|
|
): Promise<SshHostValidationResult> {
|
|
const parsed = sshHostValidationRequestSchema.parse(request)
|
|
const pending = this.pendingHostKeys.get(parsed.candidateId)
|
|
if (!pending || pending.expiresAt <= this.now()) {
|
|
this.pendingHostKeys.delete(parsed.candidateId)
|
|
throw new Error('SSH 主机密钥检查已过期,请重新检查')
|
|
}
|
|
if (pending.targetKey !== targetKey(parsed.input)) {
|
|
throw new Error('SSH 主机配置已变化,请重新检查主机密钥')
|
|
}
|
|
if (
|
|
pending.candidate.fingerprintSha256 !==
|
|
parsed.fingerprintSha256
|
|
) {
|
|
throw new Error('SSH 主机密钥指纹不匹配')
|
|
}
|
|
if (this.activeValidations.has(parsed.candidateId)) {
|
|
throw new Error('SSH 主机正在验证,请等待当前操作完成')
|
|
}
|
|
const validationKeys = [
|
|
`target:${pending.targetKey}`,
|
|
...(pending.hostId ? [`host:${pending.hostId}`] : [])
|
|
]
|
|
if (
|
|
validationKeys.some((key) =>
|
|
this.activeValidationKeys.has(key)
|
|
)
|
|
) {
|
|
throw new Error('SSH 主机正在验证,请等待当前操作完成')
|
|
}
|
|
this.activeValidations.add(parsed.candidateId)
|
|
for (const key of validationKeys) {
|
|
this.activeValidationKeys.add(key)
|
|
}
|
|
|
|
try {
|
|
let resolvedExisting:
|
|
| Awaited<
|
|
ReturnType<SshHostStore['resolveConnectionTarget']>
|
|
>
|
|
| undefined
|
|
if (pending.hostId) {
|
|
resolvedExisting =
|
|
await this.store.resolveConnectionTarget(
|
|
pending.hostId
|
|
)
|
|
const existing = {
|
|
id: resolvedExisting.host.id,
|
|
hostname: resolvedExisting.host.hostname,
|
|
port: resolvedExisting.host.port,
|
|
username: resolvedExisting.host.username,
|
|
revision: resolvedExisting.hostRevision
|
|
}
|
|
if (
|
|
existing.revision !== pending.expectedRevision ||
|
|
targetKey(existing) !== pending.sourceTargetKey
|
|
) {
|
|
this.pendingHostKeys.delete(parsed.candidateId)
|
|
throw new Error(
|
|
'SSH 主机配置已变化,请重新检查主机密钥'
|
|
)
|
|
}
|
|
sshHostUpdateInputSchema.parse(parsed.input)
|
|
if (
|
|
parsed.input.password.action === 'keep' &&
|
|
(existing.hostname !== parsed.input.hostname ||
|
|
existing.port !== parsed.input.port ||
|
|
existing.username !== parsed.input.username ||
|
|
parsed.input.authentication !== 'password')
|
|
) {
|
|
throw new Error(
|
|
'主机地址、端口、用户名或认证方式已变化,请重新输入密码'
|
|
)
|
|
}
|
|
} else {
|
|
sshHostCreateInputSchema.parse(parsed.input)
|
|
}
|
|
|
|
let password: string | undefined
|
|
if (parsed.input.authentication === 'password') {
|
|
if (parsed.input.password.action === 'replace') {
|
|
if (!this.store.isSecureStorageAvailable()) {
|
|
throw new Error(
|
|
'系统安全存储不可用,无法保存 SSH 密码'
|
|
)
|
|
}
|
|
password = parsed.input.password.value
|
|
} else if (
|
|
parsed.input.password.action === 'keep' &&
|
|
pending.hostId
|
|
) {
|
|
password = resolvedExisting?.host.password
|
|
} else {
|
|
throw new Error('SSH 密码不能为空')
|
|
}
|
|
}
|
|
|
|
const connection = await this.transport.testConnection({
|
|
id: pending.hostId ?? parsed.candidateId,
|
|
name: parsed.input.name,
|
|
hostname: parsed.input.hostname,
|
|
port: parsed.input.port,
|
|
username: parsed.input.username,
|
|
authentication: parsed.input.authentication,
|
|
...(password ? { password } : {}),
|
|
hostKey: pending.candidate
|
|
})
|
|
const host = await this.store.commitValidated({
|
|
...(pending.hostId ? { hostId: pending.hostId } : {}),
|
|
...(pending.expectedRevision !== undefined
|
|
? { expectedRevision: pending.expectedRevision }
|
|
: {}),
|
|
input: parsed.input,
|
|
hostKey: pending.candidate
|
|
})
|
|
if (pending.hostId) {
|
|
this.lifecycleHooks.onHostEdited?.(host.id)
|
|
}
|
|
for (const [candidateId, candidate] of this.pendingHostKeys) {
|
|
if (
|
|
candidateId === parsed.candidateId ||
|
|
candidate.targetKey === pending.targetKey ||
|
|
(pending.hostId && candidate.hostId === pending.hostId)
|
|
) {
|
|
this.pendingHostKeys.delete(candidateId)
|
|
}
|
|
}
|
|
return {
|
|
host,
|
|
connection: {
|
|
hostId: host.id,
|
|
...connection
|
|
}
|
|
}
|
|
} finally {
|
|
this.activeValidations.delete(parsed.candidateId)
|
|
for (const key of validationKeys) {
|
|
this.activeValidationKeys.delete(key)
|
|
}
|
|
}
|
|
}
|
|
}
|