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

时间:2019-04-06 09:29:39

标签: javascript node.js ssh ssh-tunnel ssh2

上下文

不久前,我发现了一项名为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)

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

1 个答案:

答案 0 :(得分: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客户端相同的结果。