NodeJS反向SSH隧道:无法绑定到serveo.net:80

问题描述 投票:0回答:1

上下文

不久前,我发现了一项名为Serveo的优质服务。它允许我使用反向SSH隧道将我的本地应用程序公开到Internet。

例如与https://abc.serveo.net的连接被转发到我的机器上的http://localhost:3000

为此,它们不需要客户端安装,我只需在命令行中输入:

ssh -R 80:localhost:3000 serveo.net

其中80是我要绑定的serveo.net上的远程端口,localhost:3000是我的应用程序的本地地址。

如果我只在左侧输入80,Serveo将回答Forwarding HTTP traffic from https://xxxx.serveo.net,其中xxxx是一个可用的子域名,支持https。

但是,如果我输入另一个端口,例如59000,该应用程序将通过serveo.net:59000提供,但没有SSL。

问题

现在,我想用NodeJS做这件事,在我为同事和我公司的合作伙伴建立的工具中实现自动化,这样他们就不必担心它,也不需要在他们的机器上安装SSH客户端。我正在使用SSH2 Node module

下面是一个使用自定义端口配置(此处为59000)的工作代码示例,其中一个应用程序在http://localhost:3000上进行监听:

/**
 * Want to try it out?
 * Go to https://github.com/blex41/demo-ssh2-tunnel
 */
const Client = require("ssh2").Client; // To communicate with Serveo
const Socket = require("net").Socket; // To accept forwarded connections (native module)

// Create an SSH client
const conn = new Client();
// Config, just like the second example in my question
const config = {
  remoteHost: "",
  remotePort: 59000,
  localHost: "localhost",
  localPort: 3000
};

conn
  .on("ready", () => {
    // When the connection is ready
    console.log("Connection ready");
    // Start an interactive shell session
    conn.shell((err, stream) => {
      if (err) throw err;
      // And display the shell output (so I can see how Serveo responds)
      stream.on("data", data => {
        console.log("SHELL OUTPUT: " + data);
      });
    });
    // Request port forwarding from the remote server
    conn.forwardIn(config.remoteHost, config.remotePort, (err, port) => {
      if (err) throw err;
      conn.emit("forward-in", port);
    });
  })
  // ===== Note: this part is irrelevant to my problem, but here for the demo to work
  .on("tcp connection", (info, accept, reject) => {
    console.log("Incoming TCP connection", JSON.stringify(info));
    let remote;
    const srcSocket = new Socket();
    srcSocket
      .on("error", err => {
        if (remote === undefined) reject();
        else remote.end();
      })
      .connect(config.localPort, config.localPort, () => {
        remote = accept()
          .on("close", () => {
            console.log("TCP :: CLOSED");
          })
          .on("data", data => {
            console.log(
              "TCP :: DATA: " +
              data
              .toString()
              .split(/\n/g)
              .slice(0, 2)
              .join("\n")
            );
          });
        console.log("Accept remote connection");
        srcSocket.pipe(remote).pipe(srcSocket);
      });
  })
  // ===== End Note
  // Connect to Serveo
  .connect({
    host: "serveo.net",
    username: "johndoe",
    tryKeyboard: true
  });

// Just for the demo, create a server listening on port 3000
// Accessible both on:
// http://localhost:3000
// https://serveo.net:59000
const http = require("http"); // native module
http
  .createServer((req, res) => {
    res.writeHead(200, {
      "Content-Type": "text/plain"
    });
    res.write("Hello world!");
    res.end();
  })
  .listen(config.localPort);

这很好,我可以在http://serveo.net:59000上访问我的应用程序。但它不支持HTTPS,这是我的要求之一。如果我想要HTTPS,我需要将端口设置为80,并将远程主机留空,就像上面给出的普通SSH命令一样,这样Servo就会为我分配一个可用的子域:

// equivalent to `ssh -R 80:localhost:3000 serveo.net`
const config = {
  remoteHost: "",
  remotePort: 80,
  localHost: "localhost",
  localPort: 3000
};

但是,这是一个错误:

Error: Unable to bind to :80
at C:\workspace\demo-ssh2-tunnel\node_modules\ssh2\lib\client.js:939:21
at SSH2Stream.<anonymous> (C:\workspace\demo-ssh2-tunnel\node_modules\ssh2\lib\client.js:628:24)
at SSH2Stream.emit (events.js:182:13)
at parsePacket (C:\workspace\demo-ssh2-tunnel\node_modules\ssh2-streams\lib\ssh.js:3851:10)
at SSH2Stream._transform (C:\workspace\demo-ssh2-tunnel\node_modules\ssh2-streams\lib\ssh.js:693:13)
at SSH2Stream.Transform._read (_stream_transform.js:190:10)
at SSH2Stream._read (C:\workspace\demo-ssh2-tunnel\node_modules\ssh2-streams\lib\ssh.js:252:15)
at SSH2Stream.Transform._write (_stream_transform.js:178:12)
at doWrite (_stream_writable.js:410:12)
at writeOrBuffer (_stream_writable.js:394:5)

我尝试过很多事情但没有成功。如果有人对我的例子中可能出现的问题有所了解,我将非常感激。谢谢!

javascript node.js ssh ssh-tunnel ssh2
1个回答
1
投票

OpenSSH defaults to "localhost" for the remote host when it's not specified。您还可以通过将-vvv添加到命令行来检查OpenSSH客户端的调试输出来验证这一点。您应该看到如下行:

debug1: Remote connections from LOCALHOST:80 forwarded to local address localhost:3000

如果你通过在JS代码中设置config.remoteHost = 'localhost'来模仿这个,你应该得到与OpenSSH客户端相同的结果。

© www.soinside.com 2019 - 2024. All rights reserved.