语境

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

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

为此,他们不需要客户端安装,我可以在命令行中键入以下内容:

ssh -R 80:localhost:3000 serveo.net

其中80是我想绑定(bind)到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)

我已经尝试了很多事情,但都没有成功。如果有人对我的示例中可能存在的问题有所了解,我将不胜感激。谢谢!

最佳答案

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客户端相同的结果。

关于javascript - NodeJS反向SSH隧道:无法绑定(bind)到serveo.net:80,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55547703/

10-10 22:18