NodeJS TCP Socket不显示客户端主机名信息 [英] NodeJS TCP Socket does not show client hostname information

查看:27
本文介绍了NodeJS TCP Socket不显示客户端主机名信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经创建了一个简单的 node.js TCP 套接字服务器,如下所示.

var server = net.createServer(function (socket) {socket.end("再见\n");});//侦听端口 80,就像网络服务器一样,并配置为接受与多个主机的连接,即 test.example.com、sdfsdfsdf.example.com、example2.com.server.listen(80,函数(){地址 = server.address();console.log("在 %j 上打开服务器", address);});server.on('connection', function(socket) {//log('连接数据', socket);日志('连接的套接字数据',socket.address());log('CONNECTED LOCAL, %s:%s', socket.localAddress, socket.localPort);log('CONNECTED %s:%s', socket.remoteAddress, socket.remotePort);//如何找到客户端连接的服务器主机名?//例如 test.example.com 或 example2.com});

一旦建立了 tcp connection,我想解析客户端尝试连接的主机名.但是 socket 似乎没有这个信息.

以下是我运行之前的代码时得到的(去掉了ip地址)

CONNECTED SOCKET DATA { port: 80, family: 2, address: '[SERVER IP ADDRESS]' }本地连接,未定义:未定义已连接 [客户端 IP 地址]:13263

我已经阅读了 nodejs socket 文档,但我不能查找与主机名相关的任何内容.文档实际上说 socket.localAddress 将是一个 IP 地址,在我的情况下使它无用.

解决方案

该信息在 TCP 层不可用.当请求到达时,您需要查看 HTTP 层的 Host 标头.当请求到达时,这将作为 req.headers.host 可用.http://nodejs.org/docs/latest/api/all.html#all_message_headers

同样根据您的评论,尝试在端口 80 上运行非 HTTP 服务器是自找麻烦.您将始终从网络蜘蛛和僵尸网络获取连接,因此请确保您的程序正确处理 HTTP 客户端连接并断开它们的连接,或者发送 HTTP 错误消息.

Joe 的评论是正确的,如果您不使用 HTTP,您的自定义协议将需要在自定义协议的第一个数据包/标头消息中将虚拟主机"名称从客户端传输到服务器.

lets say I've created a simple node.js TCP Socket server as follows.

var server = net.createServer(function (socket) {
  socket.end("goodbye\n");
});
// listen on port 80, like a webserver would, and configured to accept connections to multiple hosts, ie test.example.com, sdfsdfsdf.example.com, example2.com. 
server.listen(80, function() {
  address = server.address();
  console.log("opened server on %j", address);
});

server.on('connection', function(socket) {
    //log('connection data', socket);
    log('CONNECTED SOCKET DATA', socket.address());
    log('CONNECTED LOCAL, %s:%s', socket.localAddress, socket.localPort);
    log('CONNECTED %s:%s', socket.remoteAddress, socket.remotePort);

    //How do I find the server hostname that the client connected to?
    // eg test.example.com or example2.com
});

Once a tcp connection is made, I want to parse the hostname that the client attempted to connect to. However the socket does not seem to have this information.

The following is what I got when I ran the previous code (removed ip addresses)

CONNECTED SOCKET DATA { port: 80, family: 2, address: '[SERVER IP ADDRESS]' }
CONNECTED LOCAL, undefined:undefined
CONNECTED [CLIENT IP ADDRESS]:13263

I've gone through the nodejs socket documentation but I cant find anything related to host name. The documentation actually says that the socket.localAddress will be an IP Address, with makes it useless in my case.

解决方案

That information is not available at the TCP layer. You need to look at the Host header at the HTTP layer when a request arrives. This will be available as req.headers.host when a request arrives. http://nodejs.org/docs/latest/api/all.html#all_message_headers

Also based on your comment, trying to run a non-HTTP server on port 80 is asking for trouble. You are going to get connections from web spiders and botnets all the time, so make sure your program properly handles HTTP clients connecting and disconnects them or perhaps sends an HTTP error message.

Joe is correct in the comment that if you are not using HTTP, your custom protocol will need to transmit the "virtual host" name from the client to the server in the first packet/header message of your custom protocol.

这篇关于NodeJS TCP Socket不显示客户端主机名信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆