duanlong4890 2014-11-20 16:40
浏览 45
已采纳

当客户端通过单元格连接时,远程IP地址会有所不同

I am building a solution in php which requires me to accurately know the ip of the requestor. For most cases, the classic call to

$ip = $_SERVER['REMOTE_HOST'];

works just fine.

However, I've noted that in the case where the client is making their request over a tethered connection, I get a completely different address than what google and my firewall report.

I can verify this on the client (tethered) side by searching for "my ip" on google which gives me an IP that matches what my server's firewall is reporting as well. Neither of these match what the $_SERVER['REMOTE_HOST'] contains on the server side though.

My questions then are:

  • Why do these addresses differ in general?
  • How do I get to the ip address that my firewall and google see, specifically using php?
  • 写回答

3条回答 默认 最新

  • dtmsaqtly798322992 2015-01-28 17:51
    关注

    Huh, turns out this was being done using a javascript "feature" (what I'd think about calling a vulnerability instead) all along. Scary stuff, as it also exposes your internal IP address as well. Anyway it's something called WebRTC, that just started getting a lot of attention this week as it's been officially implemented by firefox and chrome:

    //get the IP addresses associated with an account
    function getIPs(callback){
        var ip_dups = {};
    
        //compatibility for firefox and chrome
        var RTCPeerConnection = window.RTCPeerConnection
            || window.mozRTCPeerConnection
            || window.webkitRTCPeerConnection;
        var mediaConstraints = {
            optional: [{RtpDataChannels: true}]
        };
    
        //firefox already has a default stun server in about:config
        //    media.peerconnection.default_iceservers =
        //    [{"url": "stun:stun.services.mozilla.com"}]
        var servers = undefined;
    
        //add same stun server for chrome
        if(window.webkitRTCPeerConnection)
            servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]};
    
        //construct a new RTCPeerConnection
        var pc = new RTCPeerConnection(servers, mediaConstraints);
    
        //listen for candidate events
        pc.onicecandidate = function(ice){
    
            //skip non-candidate events
            if(ice.candidate){
    
                //match just the IP address
                var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/
                var ip_addr = ip_regex.exec(ice.candidate.candidate)[1];
    
                //remove duplicates
                if(ip_dups[ip_addr] === undefined)
                    callback(ip_addr);
    
                ip_dups[ip_addr] = true;
            }
        };
    
        //create a bogus data channel
        pc.createDataChannel("");
    
        //create an offer sdp
        pc.createOffer(function(result){
    
            //trigger the stun server request
            pc.setLocalDescription(result, function(){});
    
        }, function(){});
    }
    
    //Test: Print the IP addresses into the console
    getIPs(function(ip){console.log(ip);});
    

    Source: https://github.com/diafygi/webrtc-ips

    Demo: https://diafygi.github.io/webrtc-ips/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?