Ottenere l'indirizzo IP del client: REMOTE_ADDR, HTTP_X_FORWARDED_FOR, che altro potrebbe essere utile?

StackOverflow https://stackoverflow.com/questions/527638

Domanda

Capisco che è una pratica standard per guardare entrambe queste variabili. Naturalmente possono essere facilmente falsificati. Sono curioso quanto spesso ci si può aspettare questi valori (in particolare il HTTP_X_FORWARDED_FOR) per contenere informazioni genuina e non solo essere strapazzate o hanno i loro valori strappati via?

Chiunque con l'esperienza o statistiche su questa roba?

C'è qualcos'altro che può essere utile per il compito di ottenere l'indirizzo IP del client?

È stato utile?

Soluzione

E 'dipende dalla natura del sito.

mi capita di lavorare su un po 'di software in cui il tracciamento IP è importante, e all'interno di un campo consumata da siti parter Direi circa 20% - IP o le intestazioni il 40% delle richieste sono o rilevabile contraffatti cancellati fuori, a seconda l'ora del giorno e da dove sono venuti. Per un sito che ottiene il traffico organico (cioè non tramite partner) mi aspetto un molto più elevato rapporto di buoni indirizzi IP.

Come ha detto Kosi, fare attenzione a quello che stai facendo con questo -. IP sono in alcun modo un modo affidabile per identificare i visitatori unici

Altri suggerimenti

Oltre a REMOTE_ADDR e HTTP_X_FORWARDED_FOR ci sono alcune altre intestazioni che possono essere impostati come ad esempio:

  • HTTP_CLIENT_IP
  • HTTP_X_FORWARDED_FOR può essere elenco separato da virgole di indirizzi IP delimitato
  • HTTP_X_FORWARDED
  • HTTP_X_CLUSTER_CLIENT_IP
  • HTTP_FORWARDED_FOR
  • HTTP_FORWARDED

Ho trovato il codice sul seguente sito utile:
http://www.grantburton.com/?p=97

Non ho il codice PHP del porting di Grant Burton a un metodo statico ASP.Net callable contro il HttpRequestBase. Sarà eventualmente saltare tramite qualunque intervalli di indirizzi IP privato.

public static class ClientIP
{
    // based on http://www.grantburton.com/2008/11/30/fix-for-incorrect-ip-addresses-in-wordpress-comments/
    public static string ClientIPFromRequest(this HttpRequestBase request, bool skipPrivate)
    {
        foreach (var item in s_HeaderItems)
        {
            var ipString = request.Headers[item.Key];

        if (String.IsNullOrEmpty(ipString))
            continue;

        if (item.Split)
        {
            foreach (var ip in ipString.Split(','))
                if (ValidIP(ip, skipPrivate))
                    return ip;
        }
        else
        {
            if (ValidIP(ipString, skipPrivate))
                return ipString;
        }
    }

    return request.UserHostAddress;
}

private static bool ValidIP(string ip, bool skipPrivate)
{
    IPAddress ipAddr;

    ip = ip == null ? String.Empty : ip.Trim();

    if (0 == ip.Length
        || false == IPAddress.TryParse(ip, out ipAddr)
        || (ipAddr.AddressFamily != AddressFamily.InterNetwork
            && ipAddr.AddressFamily != AddressFamily.InterNetworkV6))
        return false;

    if (skipPrivate && ipAddr.AddressFamily == AddressFamily.InterNetwork)
    {
        var addr = IpRange.AddrToUInt64(ipAddr);
        foreach (var range in s_PrivateRanges)
        {
            if (range.Encompasses(addr))
                return false;
        }
    }

    return true;
}

/// <summary>
/// Provides a simple class that understands how to parse and
/// compare IP addresses (IPV4) ranges.
/// </summary>
private sealed class IpRange
{
    private readonly UInt64 _start;
    private readonly UInt64 _end;

    public IpRange(string startStr, string endStr)
    {
        _start = ParseToUInt64(startStr);
        _end = ParseToUInt64(endStr);
    }

    public static UInt64 AddrToUInt64(IPAddress ip)
    {
        var ipBytes = ip.GetAddressBytes();
        UInt64 value = 0;

        foreach (var abyte in ipBytes)
        {
            value <<= 8;    // shift
            value += abyte;
        }

        return value;
    }

    public static UInt64 ParseToUInt64(string ipStr)
    {
        var ip = IPAddress.Parse(ipStr);
        return AddrToUInt64(ip);
    }

    public bool Encompasses(UInt64 addrValue)
    {
        return _start <= addrValue && addrValue <= _end;
    }

    public bool Encompasses(IPAddress addr)
    {
        var value = AddrToUInt64(addr);
        return Encompasses(value);
    }
};

private static readonly IpRange[] s_PrivateRanges =
    new IpRange[] { 
            new IpRange("0.0.0.0","2.255.255.255"),
            new IpRange("10.0.0.0","10.255.255.255"),
            new IpRange("127.0.0.0","127.255.255.255"),
            new IpRange("169.254.0.0","169.254.255.255"),
            new IpRange("172.16.0.0","172.31.255.255"),
            new IpRange("192.0.2.0","192.0.2.255"),
            new IpRange("192.168.0.0","192.168.255.255"),
            new IpRange("255.255.255.0","255.255.255.255")
    };


/// <summary>
/// Describes a header item (key) and if it is expected to be 
/// a comma-delimited string
/// </summary>
private sealed class HeaderItem
{
    public readonly string Key;
    public readonly bool Split;

    public HeaderItem(string key, bool split)
    {
        Key = key;
        Split = split;
    }
}

// order is in trust/use order top to bottom
private static readonly HeaderItem[] s_HeaderItems =
    new HeaderItem[] { 
            new HeaderItem("HTTP_CLIENT_IP",false),
            new HeaderItem("HTTP_X_FORWARDED_FOR",true),
            new HeaderItem("HTTP_X_FORWARDED",false),
            new HeaderItem("HTTP_X_CLUSTER_CLIENT_IP",false),
            new HeaderItem("HTTP_FORWARDED_FOR",false),
            new HeaderItem("HTTP_FORWARDED",false),
            new HeaderItem("HTTP_VIA",false),
            new HeaderItem("REMOTE_ADDR",false)
    };
}

Nessun vera risposta alla tua domanda, ma:
In generale basandosi sui client indirizzo IP è a mio parere non è una buona pratica in quanto non è utilizzabile per identificare i clienti in un modo unico.

Problemi sulla strada sono che ci sono un sacco scenari in cui l'IP in realtà non allinearsi ad una cliente:

  • Proxy / Webfilter (mangle quasi tutto)
  • rete Anonymizer (nessuna possibilità neanche qui)
  • NAT (un IP interno non è molto utile per voi)
  • ...

Non posso offrire alcun statistiche sul numero di indirizzi IP sono in media affidabile, ma quello che vi posso dire che è quasi impossibile dire se un dato indirizzo IP è il vero i clienti ad affrontare.

IP + "User Agent" potrebbe essere una migliore per visitatore unico.

Chiamare il seguito Metodo azione dal file JS (Per ottenere l'indirizzo IP IPv4).

    [HttpGet]
    public string GetIP()
    {
        IPAddress[] ipv4Addresses = Array.FindAll(
            Dns.GetHostEntry(string.Empty).AddressList,
            a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
        return ipv4Addresses.ToString();
    }

Controlla dopo aver mantenuto Breakpoint, e utilizzare secondo il vostro requisito. La sua lavorando bene per me.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top