簡體   English   中英

如何在Node.js中創建TLS隧道

[英]How to create a TLS tunnel in Node.js

我正在嘗試將node.js服務器接收的流量隧道傳輸到TLS連接。 我有一些這樣的代碼:

function tunnel() {
  var c = tls.connect(443, 'myhost', {rejectUnauthorized: false});

  var server = net.createServer(function (socket) {
    socket.addListener("connect", function () {
      console.log("Connection from " + socket.remoteAddress);
      //sync the file descriptors, so that the socket data structures are the same
      c.fd = socket.fd;
      //pipe the incoming data from the client directly onto the server
      c.pipe(socket);
      //and the response from the server back to the client
      socket.pipe(c);
    });

    socket.addListener("data", function (data) {
      console.log("Data received from client");
    });

    socket.addListener("close", function () {
      server.close();
    });
  });

  server.listen(7000);
}

當我運行它並對其進行測試時,我會在終端中看到以下內容:

$ curl --insecure https://myhost:443
hello world

$ curl --insecure https://localhost:7000
# nothing... just hangs

在服務器控制台中,我看到Data received from client ,但是沒有看到connect回調。

我在正確的軌道上嗎?

傳遞給服務器的connection事件處理程序(傳遞給createServer()的回調)的套接字已經連接,因此永遠不會有connect事件(即使用net.connect() / tls.connect()創建的客戶端套接字)。 。

這是僅接受一個連接的代理的樣子:

net.createServer(function(socket) {
  server.close(); // Stop listening for additional connections
  var upstream = tls.connect(443, 'myhost', {rejectUnauthorized: false});
  socket.pipe(upstream).pipe(socket);
}).listen(7000);

我還應該指出,使用rejectUnauthorized: false是不安全的。 如果由於上游服務器使用的是自簽名證書而使用該證書,則應將ca選項設置為自簽名CA。 這將允許CA簽署證書,並防止MITM攻擊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM