javascript - 在 ssh2-sftp-client 中使用 ssh 指纹

标签 javascript node.js ssh node-red ssh2-sftp

好的,这可能需要预先解释一下。
我目前正在使用 node-red 进行自动化项目.
我想使用 ssh 从远程服务器上传和下载文件。对于这个任务,我使用 this名为 node-red-contrib-sftpc 的 Node 红色包.我稍微重写了库,以便我可以通过移交给 Node 的有效负载移交一些用于 sftp 连接的凭据。
要建立连接 sftp.connect ssh2-sftp-client的方法用来:

await sftp.connect({ 
    host: node.server.host,
    port: node.server.port,
    username: node.server.username,
    password: node.server.password});
您可以在该文档中找到您可以提供的文档 connect带参数hostHashhostVerifier . documentation of the ssh2 model ssh2-sftp-client 所基于的 ,指出:

hostHash - string - Any valid hash algorithm supported by node. The host's key is hashed using this algorithm and passed to the hostVerifier function as a hex string. Default: (none)

hostVerifier - function - Function with parameters (hashedKey[, callback]) where hashedKey is a string hex hash of the host's key for verification purposes. Return true to continue with the handshake or false to reject and disconnect, or call callback() with true or false if you need to perform asynchronous verification. Default: (auto-accept if hostVerifier is not set)


所以这是我的问题:我该如何写 hostVerifier功能?我要通过hashedKey还有 fingerprint ,以便我可以返回 truefalse ,当握手成功与否。
我想检查给定的服务器 key 指纹是否是“正确的”指纹并且我连接到正确的服务器。
据我了解,第二个参数将是一个回调函数,但我不知道如何使用它,以便它验证握手。
这是我的尝试,或者至少是我尝试这样做的方式。
node.server.hostVerifier = function (hashedKey, (hashedKey, msg.fingerprint)=> {
  if (hashedKey = msg.fingerprint) return true;
  else return false
  }){};

await sftp.connect({ 
    host: node.server.host,
    port: node.server.port,
    username: node.server.username,
    password: node.server.password,
    hostHash: 'someHashAlgo',
    hostVerifier: node.server.hostVerifier,});
我知道这是完全错误的,但我快要疯了,因为我不知道如何正确检查 ssh 主机 key 指纹。

最佳答案

所以我自己找到了一个解决方案,并想与您分享。
我将箭头函数定义为 hostVerifier函数,通过msg.fingerprint隐含指纹的值多变的。我只这样做,如果 node.server.fingerprint有一个值(value)。所以如果我手头没有指纹,连接仍然会建立。

node.server.fingerprint = msg.fingerprint;
if(!!node.server.fingerprint){
    node.server.hostHash = 'md5';
    node.server.hostVerifier =  (hashedKey) => {
            return (hashedKey === msg.fingerprint) ;};
    node.server.algorithms = {serverHostKey: ['ssh-rsa'],};
        };
为此,我也声明我的 node.server.alogrithms .有了它,这是一个尝试和错误。
所以我把所有东西放在一起:
await sftp.connect({ 
                host: node.server.host,
                port: node.server.port,
                username: node.server.username,
                password: node.server.password,
                hostHash: node.server.hostHash,
                hostVerifier: node.server.hostVerifier, 
                algorithms: node.server.algorithms,
                });

关于javascript - 在 ssh2-sftp-client 中使用 ssh 指纹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65184683/

相关文章:

javascript - 如何从jquery中的特定选项卡隐藏字段值中获取值

node.js - 无法使用选项 'outFile' 编译模块,除非 '--module' 标志为 'amd' 或 'system'

git - 如何确定客户端的 SSH 指纹以在 Git Hook 中使用

mysql - 使用Ruby on Rails和SSH访问远程服务器上的远程MySQL数据库

javascript - Electron 定位器的使用方法

javascript - 阻止了源为 "http://localhost:8084"的框架访问跨域框架

javascript - 如何将选定的链接/文本输入发布为输出答案

node.js - 无法将 "mutateIn"与 couchbase Node sdk 一起使用

javascript - Node 模块。私有(private)变量被重置

Git错误权限被拒绝(公钥),无法粘贴公钥