Skip to content

fix(server-monitor): support Base64 SSH private keys and passphrase - #2

Open
KuGouGo wants to merge 1 commit into
egerndaddy:mainfrom
KuGouGo:fix/server-monitor-private-key
Open

fix(server-monitor): support Base64 SSH private keys and passphrase#2
KuGouGo wants to merge 1 commit into
egerndaddy:mainfrom
KuGouGo:fix/server-monitor-private-key

Conversation

@KuGouGo

@KuGouGo KuGouGo commented Jul 26, 2026

Copy link
Copy Markdown

问题背景

server-monitor 当前直接把模块环境变量中的 privateKey 原样传给 ctx.ssh.connect()。在 Egern 模块编辑页粘贴多行 PEM 私钥时,私钥可能因换行保存/粘贴方式发生变化,底层 libssh2 随后在认证前报错:

ssh2 error -16: Unable to extract public key from private key file:
Unsupported private key file format

服务器端日志可见客户端已经连接到 22 端口,但在尝试认证前断开,因此问题发生在客户端私钥解析阶段,而非服务器公钥授权、文件权限或网络连接。

此外,Egern 官方 ctx.ssh.connect(config) API 已提供可选的 passphrase 字段,但原模块尚未透传,带口令的私钥无法使用。

修改内容

  1. 新增 privateKeyBase64 环境变量,允许用户将完整 PEM 私钥编码为单行 Base64 后填写,避免多行输入带来的换行损坏。
  2. 脚本内解码 Base64,并在连接前统一规范私钥:
    • 去除首尾空白;
    • 将 CRLF/CR 统一为 LF;
    • 兼容单行编辑器产生的字面量 \\n
    • 保证结尾存在换行。
  3. 保留原 privateKey 字段,兼容已有配置;当两个字段同时存在时,优先使用 privateKeyBase64
  4. 新增可选 passphrase 并透传给 ctx.ssh.connect()
  5. 保留密码认证逻辑不变,并补充主机、用户名和认证信息缺失时的明确提示。

兼容性

  • 现有密码登录配置:行为不变。
  • 现有原始私钥配置:继续可用,并增加换行规范化。
  • 新配置:推荐使用单行 Base64。
  • 此修改只解决私钥内容在输入/存储过程中的完整性问题;底层 libssh2 本身不支持的密钥容器格式仍需转换为其支持的 PEM 格式。

验证

  • JavaScript 通过 node --check
  • Base64 解码结果与原 PEM 内容逐字节一致(RSA PEM 与 ECDSA PEM 均验证)。
  • 使用 ECDSA P-256 传统 PEM,经 Base64 字段解码后在 Egern 小组件中连接成功。
  • 同一密钥通过 OpenSSH 命令行实际登录服务器验证成功。
  • git diff --check 通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant