Files
opsxshell/electron/ipc/ssh.ipc.ts
T
Logan.Li 514d62644e v2.0.0: 主题系统+SSH私钥解析修复+会话持久化+连接池优化+UI打磨
核心修复:
- ssh-service.ts: 新增 resolvePrivateKey(),将私钥文件路径(~/.ssh/id_rsa)解析为内容后传给 ssh2,
  解决 'Cannot parse privateKey: Unsupported key format' 错误
- useTerminalConnection.ts + sessionHelper.ts: 根据 authType 过滤字段,password/agent 认证不再传 privateKey
- sessionStore.ts: addSession() 现在先调用 sessionCreate() 持久化到 SQLite,解决恢复标签'无可用连接信息'问题
- database-service.ts: updateSession() 补充 private_key_path 字段持久化
- SessionDialog.tsx: 默认 username='root'、privateKey='~/.ssh/id_rsa'

UI/UX:
- 语义化主题系统(app-theme.ts),强制深色模式,全局无边框设计
- macOS 标题栏支持双击最大化(window:toggleMaximize IPC)
- macOS 菜单栏('OpsXShell' 应用菜单 + Settings Cmd+,)
- WelcomeView 4卡片布局,移除 Settings 按钮
- TabBar/TabItem 背景色统一(bg-dark-800/900),消除白色边框线

架构优化:
- SFTP 生命周期修复(useSFTP.ts useRef + autoConnectAttempted guard)
- 连接池竞态修复,支持快速多标签连接
- IPC 接口规范化,安全错误处理
- Vite 代码分割优化

质量: lint零错误 | tsc零错误 | build成功 | 49 tests pass
2026-04-22 11:15:41 +08:00

221 lines
7.4 KiB
TypeScript

/**
* SSH IPC Handlers
* 处理 SSH 连接、Shell、会话、端口转发相关 IPC
* 安全增强:规范化错误处理,不暴露敏感信息
*/
import { ipcMain, IpcMainInvokeEvent } from 'electron'
import type { SSHService } from '../services/ssh-service'
import type { ZmodemService } from '../services/zmodem-service'
import { createLogger } from '../utils/logger'
const logger = createLogger('SSH_IPC')
// 生产环境日志:使用结构化日志,不输出敏感信息
const DEBUG = process.env.NODE_ENV === 'development'
function debug(...args: unknown[]) {
if (DEBUG) logger.debug('[SSH_IPC]', ...args)
}
function logHeader(msg: string) {
logger.info(`[${msg}]`)
}
function logInfo(label: string, value: unknown) {
logger.info(`${label}: ${value}`)
}
function logSuccess(msg: string) {
logger.info(`[OK] ${msg}`)
}
function logError(msg: string) {
logger.error(msg)
}
// IPC channel names
export const SSH_CHANNELS = {
CONNECT: 'ssh:connect',
DISCONNECT: 'ssh:disconnect',
EXEC: 'ssh:exec',
CREATE_SHELL: 'ssh:createShell',
SHELL_WRITE: 'ssh:shellWrite',
SHELL_RESIZE: 'ssh:shellResize',
FORWARD_LOCAL: 'ssh:forwardLocal',
FORWARD_REMOTE: 'ssh:forwardRemote',
FORWARD_DYNAMIC: 'ssh:forwardDynamic',
SHELL_DATA: 'ssh:shellData',
} as const
export interface SSHIPCOptions {
sshService: SSHService
zmodemService: ZmodemService
mainWindowGetter: () => Electron.BrowserWindow | null
}
export function registerSSHIPCHandlers(options: SSHIPCOptions): void {
const { sshService, zmodemService, mainWindowGetter: _mainWindowGetter } = options
// SSH connect
ipcMain.handle(SSH_CHANNELS.CONNECT, async (_event: IpcMainInvokeEvent, config) => {
try {
const connection = await sshService.connect(config)
return { success: true, connectionId: connection.id }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
return { success: false, error: message }
}
})
// SSH disconnect
ipcMain.handle(SSH_CHANNELS.DISCONNECT, async (_event: IpcMainInvokeEvent, connectionId: string) => {
try {
await sshService.disconnect(connectionId)
return { success: true }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
return { success: false, error: message }
}
})
// SSH exec command
ipcMain.handle(SSH_CHANNELS.EXEC, async (_event: IpcMainInvokeEvent, connectionId: string, command: string) => {
try {
const result = await sshService.exec(connectionId, command)
return { success: true, result }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
return { success: false, error: message }
}
})
// SSH create shell
ipcMain.handle(SSH_CHANNELS.CREATE_SHELL, async (event: IpcMainInvokeEvent, connectionId: string, options?: { cols?: number; rows?: number }, shellId?: string) => {
debug('CREATE_SHELL connectionId:', connectionId, 'preferredId:', shellId)
try {
const preferredShellId = shellId
logger.info('[SSH_IPC] Creating shell for connection:', connectionId, 'preferredId:', preferredShellId)
debug('>>> Calling sshService.createShell...')
const shell = await sshService.createShell(connectionId, options || {}, (shellId, data) => {
// 检查 sender 是否已被销毁
if (event.sender.isDestroyed()) {
debug('SHELL DATA CALLBACK - SKIPPED (sender destroyed)')
return
}
debug('SHELL DATA CALLBACK shellId:', shellId, 'data length:', data.length)
try {
debug('>>> Sending SHELL_DATA to renderer...')
event.sender.send(SSH_CHANNELS.SHELL_DATA, connectionId, shellId, data)
debug('>>> SHELL_DATA sent successfully')
} catch (err) {
logger.error('>>> FAILED to send SHELL_DATA:', err)
}
zmodemService.feedShellData(shellId, data)
}, preferredShellId)
logger.info('[SSH_IPC] Shell created:', shell.id)
debug('>>> Shell created successfully:', shell.id)
return { success: true, shellId: shell.id }
} catch (error) {
logger.error('[SSH_IPC] Shell creation failed:', error)
const message = error instanceof Error ? error.message : String(error)
debug('>>> Shell creation FAILED:', message)
return { success: false, error: message }
}
})
// SSH shell write
ipcMain.handle(SSH_CHANNELS.SHELL_WRITE, async (_event: IpcMainInvokeEvent, shellId: string, data: string) => {
try {
sshService.shellWrite(shellId, data)
return { success: true }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
return { success: false, error: message }
}
})
// SSH shell resize
ipcMain.handle(SSH_CHANNELS.SHELL_RESIZE, async (_event: IpcMainInvokeEvent, shellId: string, cols: number, rows: number) => {
try {
sshService.shellResize(shellId, cols, rows)
return { success: true }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
return { success: false, error: message }
}
})
// SSH local port forward
ipcMain.handle(SSH_CHANNELS.FORWARD_LOCAL, async (
_event: IpcMainInvokeEvent,
connectionId: string,
localPort: number,
remoteHost: string,
remotePort: number
) => {
logHeader('FORWARD_LOCAL')
logInfo('connectionId', connectionId)
logInfo('localPort', localPort)
logInfo('remoteHost', remoteHost)
logInfo('remotePort', remotePort)
try {
const result = await sshService.forwardLocal(connectionId, localPort, remoteHost, remotePort)
logSuccess(`Local forward bound to port ${result.boundPort}`)
return { success: true, boundPort: result.boundPort }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
logError(`Local forward failed: ${message}`)
return { success: false, error: message }
}
})
// SSH remote port forward
ipcMain.handle(SSH_CHANNELS.FORWARD_REMOTE, async (
_event: IpcMainInvokeEvent,
connectionId: string,
remotePort: number,
localHost: string,
localPort: number
) => {
logHeader('FORWARD_REMOTE')
logInfo('connectionId', connectionId)
logInfo('remotePort', remotePort)
logInfo('localHost', localHost)
logInfo('localPort', localPort)
try {
await sshService.forwardRemote(connectionId, remotePort, localHost, localPort)
logSuccess('Remote forward established')
return { success: true }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
logError(`Remote forward failed: ${message}`)
return { success: false, error: message }
}
})
// SSH dynamic port forward (SOCKS5)
ipcMain.handle(SSH_CHANNELS.FORWARD_DYNAMIC, async (
_event: IpcMainInvokeEvent,
connectionId: string,
localPort: number
) => {
logHeader('FORWARD_DYNAMIC (SOCKS5)')
logInfo('connectionId', connectionId)
logInfo('localPort', localPort)
try {
const result = await sshService.forwardDynamic(connectionId, localPort)
logSuccess(`SOCKS5 proxy started on port ${result.boundPort}`)
return { success: true, boundPort: result.boundPort }
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
logError(`Dynamic forward failed: ${message}`)
return { success: false, error: message }
}
})
}