How to detect the Remote/Client IP Address of a We

2019-06-26 08:47发布

问题:

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.