NodeJs (A) 通过 SSH 连接到远程服务器 (B) 将 API 请求发送到另一台服务器 (C)

时间:2021-04-24 18:47:56

标签: node.js ssh

enter image description here

有一个远程服务器 (C),它只允许从服务器 (B) 向它发送 API 调用。我可以通过 ssh 访问服务器 B。

在我的 NodeJS 应用程序中,为了开发和测试,我想通过 SSH 连接到服务器 B(在代码中)并将我的 API 调用发送到服务器 C,就像它在服务器 B 上运行一样,即使我是在我的服务器上开发本地机器(A)。

我以前做过这样的事情:

tunnel = ssh({
          host: '<remote server>',
          port: 22,
          username: 'test',
          privateKey: require('fs').readFileSync('/home/node/app/.ssh/id_rsa'),
          dstHost: '127.0.0.1',
          dstPort: 5000,
          srcHost: '127.0.0.1',
          srcPort: 3030
        }, (e, c)=>{
          if(e) throw e;
          // This gets called everytime a connection happens
        })

但这只转发端口,我必须在远程服务器上有一个代理。这在这种情况下没有意义。

I found this 但它需要一个 dstPort 这让我感到困惑,因为我只是从 B 向服务器 C 发出 https 请求并期望直接隧道响应......而不是对特定端口的响应(我是我肯定误会了什么)。

经典的 ssh2 包最有可能是我需要的地方,但恐怕我对端口转发的理解在这里可能会失败。

这是我尝试过的(除其他外):

let server = tunnel({
                host: '<server B>',
                username: '<my username>',
                port: 22,
                privateKey: signing_key,
                dstHost: '<both server B and server C>',
                dstPort: '443',
            }, async (err, server)=>{
                console.log('No Error', err)
                console.log('This works', server)
                // here I want to send a http request to 
                let response = await <some http request here I've tried both http://localhost and https://<server c>>
                res.json({
                    results: response
                })
            })

0 个答案:

没有答案