How to detect the Remote/Client IP Address of a WebSocket connection on OpenShift

天涯浪子 提交于 2019-12-23 15:34:17

问题


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.

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

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?


回答1:


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 ...

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

When the 0x00 packet reaches the client :

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

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

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.



来源:https://stackoverflow.com/questions/28687910/how-to-detect-the-remote-client-ip-address-of-a-websocket-connection-on-openshif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!