按IP地址识别国家/地区

sko*_*kos 39 ip ip-address geolocation geoip

有没有办法通过查看IP地址来确定国家名称?我的意思是,国家/地区是否有特定的IP地址范围?例如,澳大利亚只能在范围内拥有IP地址123.45.56.89 - 231.54.65.98(仅举例)

Ben*_*ing 28

不,你不能 - IP地址不时被重新分配和重新分配,因此IP到位置的映射也会随着时间的推移而改变.

如果要查找IP地址当前映射到的位置,可以从MaxMind下载地理定位数据库(如GeoLite),也可以使用http://ipinfo.io(我自己的服务)等API ,你有更多细节:

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "loc": "37.385999999999996,-122.0838",
  "org": "AS15169 Google Inc.",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "phone": 650
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*ler 14

我认为您正在寻找的是IP地理位置数据库或服务提供商.那里有很多,有些是免费的(得到你付出的代价).

虽然我之前没有使用过这项服务,但它声称是实时的. https://kickfire.com/kf-api

但只需在IP地理位置上进行Google搜索,您就可以获得比您需要的更多结果.


Moh*_*deh 10

我知道这是一篇非常老的帖子,但为了登陆这里并寻找解决方案的用户,如果您使用 Cloudflare 作为 DNS,那么您可以激活 IP 地理定位并从请求标头获取值,

以下是通过网络选项卡在 Cloudflare 中启用 IP 地理定位后的 C# 代码片段

 var countryCode = HttpContext.Request.Headers.Get("cf-ipcountry"); // in older asp.net versions like webform use HttpContext.Current.Request. ...
 var countryName = new RegionInfo(CountryCode)?.EnglishName;
Run Code Online (Sandbox Code Playgroud)

您可以简单地将其映射到其他编程语言,请在此处查看 Cloudflare 的文档


但是,如果您确实坚持使用第 3 方解决方案来获取有关使用其 IP 的访问者的更准确信息,这里有一个完整的、随时可用的 C# 实现:

我使用的第 3 方是https://ipstack.com,您只需注册一个免费计划并获得一个访问令牌即可用于每月 100 个 API 请求,我使用 JSON 模型来检索并喜欢转换所有API 给我的信息,我们开始吧:

DTO:

    using System;
    using Newtonsoft.Json;

     public partial class GeoLocationModel
     {
        [JsonProperty("ip")]
        public string Ip { get; set; }

        [JsonProperty("hostname")]
        public string Hostname { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("continent_code")]
        public string ContinentCode { get; set; }

        [JsonProperty("continent_name")]
        public string ContinentName { get; set; }

        [JsonProperty("country_code")]
        public string CountryCode { get; set; }

        [JsonProperty("country_name")]
        public string CountryName { get; set; }

        [JsonProperty("region_code")]
        public string RegionCode { get; set; }

        [JsonProperty("region_name")]
        public string RegionName { get; set; }

        [JsonProperty("city")]
        public string City { get; set; }

        [JsonProperty("zip")]
        public long Zip { get; set; }

        [JsonProperty("latitude")]
        public double Latitude { get; set; }

        [JsonProperty("longitude")]
        public double Longitude { get; set; }

        [JsonProperty("location")]
        public Location Location { get; set; }

        [JsonProperty("time_zone")]
        public TimeZone TimeZone { get; set; }

        [JsonProperty("currency")]
        public Currency Currency { get; set; }

        [JsonProperty("connection")]
        public Connection Connection { get; set; }

        [JsonProperty("security")]
        public Security Security { get; set; }
     }

    public partial class Connection
    {
        [JsonProperty("asn")]
        public long Asn { get; set; }

        [JsonProperty("isp")]
        public string Isp { get; set; }
    }

    public partial class Currency
    {
        [JsonProperty("code")]
        public string Code { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("plural")]
        public string Plural { get; set; }

        [JsonProperty("symbol")]
        public string Symbol { get; set; }

        [JsonProperty("symbol_native")]
        public string SymbolNative { get; set; }
    }

    public partial class Location
    {
        [JsonProperty("geoname_id")]
        public long GeonameId { get; set; }

        [JsonProperty("capital")]
        public string Capital { get; set; }

        [JsonProperty("languages")]
        public Language[] Languages { get; set; }

        [JsonProperty("country_flag")]
        public Uri CountryFlag { get; set; }

        [JsonProperty("country_flag_emoji")]
        public string CountryFlagEmoji { get; set; }

        [JsonProperty("country_flag_emoji_unicode")]
        public string CountryFlagEmojiUnicode { get; set; }

        [JsonProperty("calling_code")]
        public long CallingCode { get; set; }

        [JsonProperty("is_eu")]
        public bool IsEu { get; set; }
    }

    public partial class Language
    {
        [JsonProperty("code")]
        public string Code { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("native")]
        public string Native { get; set; }
    }

    public partial class Security
    {
        [JsonProperty("is_proxy")]
        public bool IsProxy { get; set; }

        [JsonProperty("proxy_type")]
        public object ProxyType { get; set; }

        [JsonProperty("is_crawler")]
        public bool IsCrawler { get; set; }

        [JsonProperty("crawler_name")]
        public object CrawlerName { get; set; }

        [JsonProperty("crawler_type")]
        public object CrawlerType { get; set; }

        [JsonProperty("is_tor")]
        public bool IsTor { get; set; }

        [JsonProperty("threat_level")]
        public string ThreatLevel { get; set; }

        [JsonProperty("threat_types")]
        public object ThreatTypes { get; set; }
    }

    public partial class TimeZone
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("current_time")]
        public DateTimeOffset CurrentTime { get; set; }

        [JsonProperty("gmt_offset")]
        public long GmtOffset { get; set; }

        [JsonProperty("code")]
        public string Code { get; set; }

        [JsonProperty("is_daylight_saving")]
        public bool IsDaylightSaving { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

帮手:

    using System.Configuration;
    using System.IO;
    using System.Net;
    using System.Threading.Tasks;

    public class GeoLocationHelper
    {
        public static async Task<GeoLocationModel> GetGeoLocationByIp(string ipAddress)
        {
            var request = WebRequest.Create(string.Format("http://api.ipstack.com/{0}?access_key={1}", ipAddress, ConfigurationManager.AppSettings["ipStackAccessKey"]));
            var response = await request.GetResponseAsync();
            using (var stream = new StreamReader(response.GetResponseStream()))
            {
                var jsonGeoData = await stream.ReadToEndAsync();
                return Newtonsoft.Json.JsonConvert.DeserializeObject<GeoLocationModel>(jsonGeoData);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


Jon*_*han 6

您可以使用ipdata.co来执行查找

\n\n
\n

这个答案使用了一个非常有限的“测试”API 密钥,仅用于测试一些调用。注册您自己的免费 API 密钥,每天最多可收到 1500 个开发请求。

\n
\n\n
curl https://api.ipdata.co/23.221.76.66?api-key=test\n
Run Code Online (Sandbox Code Playgroud)\n\n

Ipdata 在全球有 10 个端点,每个端点每秒能够处理 >10,000 个请求!

\n\n

给予

\n\n
{\n    "ip": "23.221.76.66",\n    "city": "Cambridge",\n    "region": "Massachusetts",\n    "region_code": "MA",\n    "country_name": "United States",\n    "country_code": "US",\n    "continent_name": "North America",\n    "continent_code": "NA",\n    "latitude": 42.3626,\n    "longitude": -71.0843,\n    "asn": "AS20940",\n    "organisation": "Akamai International B.V.",\n    "postal": "02142",\n    "calling_code": "1",\n    "flag": "https://ipdata.co/flags/us.png",\n    "emoji_flag": "\\ud83c\\uddfa\\ud83c\\uddf8",\n    "emoji_unicode": "U+1F1FA U+1F1F8",\n    "is_eu": false,\n    "languages": [\n        {\n            "name": "English",\n            "native": "English"\n        }\n    ],\n    "currency": {\n        "name": "US Dollar",\n        "code": "USD",\n        "symbol": "$",\n        "native": "$",\n        "plural": "US dollars"\n    },\n    "time_zone": {\n        "name": "America/New_York",\n        "abbr": "EDT",\n        "offset": "-0400",\n        "is_dst": true,\n        "current_time": "2018-04-19T06:32:30.690963-04:00"\n    },\n    "threat": {\n        "is_tor": false,\n        "is_proxy": false,\n        "is_anonymous": false,\n        "is_known_attacker": false,\n        "is_known_abuser": false,\n        "is_threat": false,\n        "is_bogon": false\n    }\n}\xe2\x8f\x8e \n
Run Code Online (Sandbox Code Playgroud)\n


Jos*_*son 5

Amazon 的 CloudFront 内容交付网络现在可以配置为将此信息作为标头传递。考虑到亚马逊的规模(它们又大又稳定,不会去任何地方),而且这是通过代码进行配置(无需学习第三方 API 或维护代码),所有人都相信这是最好的选择。

\n\n

如果您不使用 AWS CloudFront,我会查看您的 CDN 是否具有可以打开的类似标头选项。通常,大型提供商会迅速推动功能对等。如果您不使用 CDN,您可以将 CloudFront 放在您的基础设施前面,并简单地将源设置为解析为您当前使用的任何内容。

\n\n

此外,在 CDN 级别解决这个问题也是有意义的。您的 CDN 已经必须确定地理位置以将用户路由到最近的内容节点,不妨传递此信息,而不是通过第三方 API 两次确定它(这会成为您的应用程序的瓶颈,等待地理信息)位置查找来解决)。不需要两次执行此工作(第二次可以说弹性较差[例如,第 3 方地理查找])。

\n\n

https://aws.amazon.com/blogs/aws/enhanced-cloudfront-customization/

\n\n
\n

地理定位\xe2\x80\x93 CloudFront 将检测用户\xe2\x80\x99 的原籍国家/地区\n 并在标头中将国家CloudFront-Viewer-Country/地区代码传递给您。您可以使用此信息来自定义您的响应\n,而无需使用特定于每个国家/地区的 URL。

\n
\n


Onu*_*nur 5

是的,正如您提到的,国家/地区有特定的 IP 地址范围。

例如,澳大利亚在 16777216 - 16777471 之间。中国在 16777472 - 16778239 之间。但一个国家可能有多个范围。例如,澳大利亚也有 16778240 - 16779263 之间的范围

(这些是IP地址的数字转换。这取决于你使用的是IPv4还是IPv6)

有关这些范围的更多信息,请参见:https://lite.ip2location.com/ip-address-ranges-by-country

我们获取网站访问者的 IP 地址,有时希望针对特定国家/地区开展相关活动。我们使用批量转换工具,但后来决定在 Excel 文件中定义规则并在工具中进行转换。我们已经构建了这个 Excel 模板:https://www.someka.net/excel-template/ip-to-country-converter/

现在我们用这个来满足自己的需要,也卖掉它。我不希望它成为一种推销手段,但对于那些正在寻找简单解决方案的人来说可以从中受益。