簡體   English   中英

如何獲取訪問者的實際 ip 地址而不是他的公共 ip 地址?

[英]How do I get the actual ip address of the visitor instead of his public ip address?

我正在使用此代碼來獲取訪問者的實際 ip 地址我的網站,但它返回公共 ip 地址,這對我們辦公室的每個人都是相同的,但我想要訪問者系統的實際 ipv4 地址。

    protected string GetIPAddress()
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];




        if (!string.IsNullOrEmpty(ipAddress))
        {
            string[] addresses = ipAddress.Split(',');
            if (addresses.Length != 0)
            {
                return addresses[0];
            }
        }

        return context.Request.ServerVariables["REMOTE_ADDR"];
    }

    protected string GetIPAddressLocal()
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipAddress))
        {
            string[] addresses = ipAddress.Split(',');
            if (addresses.Length != 0)
            {
                return addresses[addresses.Length-1];
            }
        }

        return context.Request.ServerVariables["REMOTE_ADDR"];
    }

    public static IPAddress GetIPAddress3(string hostName)
    {
        Ping ping = new Ping();
        var replay = ping.Send(hostName);

        if (replay.Status == IPStatus.Success)
        {
            return replay.Address;
        }
        return null;
    }

    private string GetIP()
    {
        string strHostName = "";
        strHostName = System.Net.Dns.GetHostName();

        IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

        string ipaddress = "";

        return ipaddress.ToString();

    }

    public static string GetUserIP()
    {
        var ip = (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null
        && System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != "")
        ? System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
        : System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        if (ip.Contains(","))
            ip = ip.Split(',').First().Trim();
        return ip;
    }






string ipaddress = GetIPAddress();
            ViewBag.ip = ipaddress;
            ViewBag.ip2= System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(1).ToString();
            ViewBag.ip3 = Request.ServerVariables["REMOTE_ADDR"];
            ViewBag.ip4 = System.Web.HttpContext.Current.Request.UserHostAddress;
            ViewBag.ip5 = GetIPAddress3(System.Net.Dns.GetHostName());
            ViewBag.ip6 = GetIP();
            ViewBag.ip7 = GetUserIP();

我使用了多個函數,它返回服務器s ip address or the public ip address which is same for all the users. It doesn s ip address or the public ip address which is same for all the users. It doesn返回用戶系統擁有的實際 IP 地址。

我做到了。

這是解決方案:

  $(document).ready(function () {
        // NOTE: window.RTCPeerConnection is "not a constructor" in FF22/23
        var RTCPeerConnection = /*window.RTCPeerConnection ||*/ window.webkitRTCPeerConnection || window.mozRTCPeerConnection;

        if (RTCPeerConnection) (function () {
            var rtc = new RTCPeerConnection({ iceServers: [] });
            console.log(rtc);
            if (1 || window.mozRTCPeerConnection) {      // FF [and now Chrome!] needs a channel/stream to proceed
                rtc.createDataChannel('', { reliable: false });
            };

            rtc.onicecandidate = function (evt) {
                // convert the candidate to SDP so we can run it through our general parser
                // see https://twitter.com/lancestout/status/525796175425720320 for details
                if (evt.candidate) grepSDP("a=" + evt.candidate.candidate);
            };
            rtc.createOffer(function (offerDesc) {
                grepSDP(offerDesc.sdp);
                rtc.setLocalDescription(offerDesc);
            }, function (e) { console.warn("offer failed", e); });


            var addrs = Object.create(null);
            addrs["0.0.0.0"] = false;
            function updateDisplay(newAddr) {
                if (newAddr in addrs) return;
                else addrs[newAddr] = true;
                var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
                if (displayAddrs[0] === null || displayAddrs[0] === '') {
                    $('#txtIpAddress').val(displayAddrs.join(" or perhaps "));
                } else {
                    $('#txtIpAddress').val(displayAddrs[0]);
                }
                //$('#txtIpAddress').val(displayAddrs.join(" or perhaps "));
                //   console.log(displayAddrs[0]);
                //  alert(displayAddrs.join(" or perhaps "));
                // document.getElementById('list').textContent = displayAddrs.join(" or perhaps ") || "n/a";
            }

            function grepSDP(sdp) {
                var hosts = [];
                sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
                    if (~line.indexOf("a=candidate")) {     // http://tools.ietf.org/html/rfc4566#section-5.13
                        var parts = line.split(' '),        // http://tools.ietf.org/html/rfc5245#section-15.1
                            addr = parts[4],
                            type = parts[7];
                        if (type === 'host') updateDisplay(addr);
                    } else if (~line.indexOf("c=")) {       // http://tools.ietf.org/html/rfc4566#section-5.7
                        var parts = line.split(' '),
                            addr = parts[2];
                        updateDisplay(addr);
                    }
                });
            }
        })();


    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM