如何在OpenShift上检测WebSocket连接的远程/客户端IP地址 [英] How to detect the Remote/Client IP Address of a WebSocket connection on OpenShift

查看:759
本文介绍了如何在OpenShift上检测WebSocket连接的远程/客户端IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OpenShift云中,通常的Apache代理将发送HTTP标头 x-client-ip x-forwarded-for ,我可以用它来确定客户端IP。

In the OpenShift cloud, the usual Apache proxy will send the HTTP headers x-client-ip and x-forwarded-for, which I can use to determine the client IP.

但对于更新的代理,这是WebSocket用户的唯一选择,这些是唯一发送的标题:

But for the newer proxy, which is the only option for WebSocket users, these are the only sent headers:


  • connection

  • upgrade

  • sec-websocket-version

  • sec-websocket-key

  • origin

  • host

  • connection
  • upgrade
  • sec-websocket-version
  • sec-websocket-key
  • origin
  • host

这些标题都不能帮助我检测远程地址和 socket.remoteAddress 的NodeJS属性没用,因为它会检测代理IP。

None of these headers can help me to detect the remote address and socket.remoteAddress's NodeJS property is useless, as it will detect the proxy IP.

我可以使用任何解决方案吗?

Any solution I can use?

推荐答案

以下是 node.js & socket.io @ OpenShift。



我必须找到解决方法 socket.handshake.address ,它不会按预期工作,所以这是我记录client.ip地址的实现...

Heres my experience with node.js & socket.io @ OpenShift.

I had to find a way around socket.handshake.address, it wouldn't work as expected, so this was my implementation for logging client.ip address ...

客户端连接时,服务器'为带有数据包的client.ip'询问'。 0x00

When a client connects, the server 'asks' for client.ip with a packet. 0x00

0x00 数据包到达客户端时:

socket.on('0x00', function(){
        $.getJSON('http://jsonip.com/?callback=?', function(r){ 
            var myIP = r.ip;
            socket.emit('0x01', myIP);
        }); 

将包含client.ip的 0x01 数据包发送到服务器

Sending the 0x01 packet containing the client.ip to the server.

这是服务器接收数据包的示例 0x01

This would be an example of the server receiving packet 0x01 :

socket.on('0x01', function(myIP){
    console.log('[+] Client @',myIP,':',socket.id);
  });

这对我来说非常合适,祝你好运。

This works perfect for me, good luck with your implementation.

这篇关于如何在OpenShift上检测WebSocket连接的远程/客户端IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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