NodeJS,在TLS服务器上获取客户端IP

时间:2018-08-09 08:14:10

标签: javascript node.js ssl tls1.2

我有以下Node.JS用于创建将数据传输到另一台服务器的TLS server

var server = net.createServer(function (localsocket) {

  var remotesocket = tls.connect(options, function() {

    remotesocket.on('data', function(data) {
      var flushed = localsocket.write(data);
      if (!flushed) {
        remotesocket.pause();
      }
    });

    remotesocket.on('drain', function() {
      localsocket.resume();
    });

    remotesocket.on('close', function(had_error) {
      localsocket.end();
    });

  });

  localsocket.on('data', function (data) {
    var flushed = remotesocket.write(data);
    if (!flushed) {
      localsocket.pause();
    }
  });

  localsocket.on('drain', function() {
    remotesocket.resume();
  });

  localsocket.on('close', function(had_error) {
    remotesocket.end();
  });

});

一切似乎都正常,但是我想根据客户端IP更改options

我尝试做console.log(localsocket),但这只给我服务器本地IP,而不是客户端向我的TLS server发送数据包。

1 个答案:

答案 0 :(得分:1)

您的connectListener(提供给createServer的函数)将获得socket_object,它是net.Socket类型的对象。有一个属性remoteAddress,具有远程主机的地址。

示例:

var server = net.createServer(function (localsocket) {
    console.log(localsocket.remoteAddress);
}