Search code examples
phpapachewebsocketmod-proxy

Get client IP while using mode_proxy_wstunnel


I'm using the mod_proxy_wstunnel on Apache 2.4.18. So, all the requests are forwarded from

wss://url.com ==> ws://10.22.22.12:12345

in the WebSocket server code, I used

socket_getpeername($client, $clientIP);
echo $clientIP;

to get the Client IP address But, IP is always unexpected it shows server's IP i.e: 10.22.22.12 It works normally with no proxy. So, is there a way to get client's IP instead of Server's while using mod_proxy_wstunnel?


Solution

  • After looking at the header of websocket connection, I found the following string.

    GET / HTTP/1.1
    Host: 145.24.35.40:12345
    Pragma: no-cache
    Cache-Control: no-cache
    Origin: https://www.domain.com
    Sec-WebSocket-Version: 13
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: en-US,en;q=0.8,id;q=0.6
    Cookie: __zlcmid=Y7fAKwRoP7ssZA; _ga=GA1.2.259560791.1452107273
    Sec-WebSocket-Key: w8Mp2n2lp6R5vnYaeEWsPw==
    Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
    X-Forwarded-For: 22.22.22.22       <<=== Your IP
    X-Forwarded-Host: ws.domain.com
    X-Forwarded-Server: ws.domain.com
    Upgrade: WebSocket
    Connection: Upgrade
    

    X-Forwarded-For contains what I was looking for. but I'm not sure if its the correct way to achieve the goal. I'll go for it until there is a better solution.