IPTools
是一个用于快速查询全球 IP 地址信息的库,支持国内和国际 IP 查询,提供详细的地理位置信息(如国家、省份、城市)以及经纬度等数据。
1. IPTools.China
IPTools.China
专注于快速查询中国 IP 地址信息,包括国家、省份、城市和网络运营商。对于非中国 IP,仅支持查询国家信息。
1.1 安装
shell
Install-Package IPTools.China
1.2 下载数据库文件
从 GitHub 下载数据库文件,并将其放置在项目根目录(与 .csproj
文件同级),设置为"复制到输出目录"。
shell
https://github.com/stulzq/IPTools/raw/master/db/ip2region.db
注意:1.2.0 版本开始取消了将数据库文件嵌入程序集,以方便更新并减少程序集大小。
![](assets/1534995762038.png)
1.3 使用
csharp
var ipinfo = IpTool.Search("171.210.12.163");
Console.WriteLine(ipinfo.Country); // 中国
Console.WriteLine(ipinfo.Province); // 四川省
Console.WriteLine(ipinfo.City); // 成都市
Console.WriteLine(ipinfo.NetworkOperator); // 电信
1.4 性能测试
单线程双重 for
循环查询 65,025 个 IP 地址,花费约 170 毫秒。
1.5 自定义 IP 数据库文件
csharp
IpToolSettings.ChinaDbPath = "path/to/your/ip2region.db";
2. IPTools.International
IPTools.International
支持全球 IP 地址查询,提供多语言支持,地理信息包括国家、省份、城市、邮政编码、纬度和经度。
2.1 安装
shell
Install-Package IPTools.International
2.2 下载数据库文件
从 GitHub 下载数据库文件,并将其放置在项目根目录(与 .csproj
文件同级),设置为"复制到输出目录"。
shell
https://github.com/stulzq/IPTools/raw/master/db/GeoLite2-City.mmdb
注意:1.2.0 版本开始取消了将数据库文件嵌入程序集,以方便更新并减少程序集大小。
![](assets/1534995856116.png)
2.3 使用
csharp
var ipinfo = IpTool.SearchWithI18N("171.210.12.163");
Console.WriteLine(ipinfo.Country); // China
Console.WriteLine(ipinfo.CountryCode); // CN
Console.WriteLine(ipinfo.Province); // Sichuan
Console.WriteLine(ipinfo.ProvinceCode); // SC
Console.WriteLine(ipinfo.City); // Chengdu
Console.WriteLine(ipinfo.Latitude); // 30.6667
Console.WriteLine(ipinfo.Longitude); // 104.6667
Console.WriteLine(ipinfo.AccuracyRadius); // 50
2.4 国际化
默认语言为中文,可以通过以下代码设置其他语言(例如英文):
csharp
IpToolSettings.DefaultLanguage = "en";
示例:
csharp
var ipinfo = IpTool.SearchWithI18N("171.210.12.163", "en");
Console.WriteLine(ipinfo.Country); // China
Console.WriteLine(ipinfo.CountryCode); // CN
Console.WriteLine(ipinfo.Province); // Sichuan
Console.WriteLine(ipinfo.ProvinceCode); // SC
Console.WriteLine(ipinfo.City); // Chengdu
Console.WriteLine(ipinfo.Latitude); // 30.6667
Console.WriteLine(ipinfo.Longitude); // 104.6667
Console.WriteLine(ipinfo.AccuracyRadius); // 50
2.5 提升查询速度
通过将数据库文件加载到内存中,可以显著提升查询速度(大约提升一倍),但会增加 60-70MB 内存占用。
csharp
IpToolSettings.LoadInternationalDbToMemory = true;
版本要求:>= 1.2.0
2.6 性能测试
单线程双重 for
循环查询 65,025 个 IP 地址,花费约 1500 毫秒(内存模式)。
2.7 自定义 IP 数据库文件
csharp
IpToolSettings.InternationalDbPath = "path/to/your/GeoLite2-City.mmdb";
3. ASP.NET Core 支持
IPTools
提供了对 HttpContext
对象的扩展方法,便于在 ASP.NET Core 应用中获取远程 IP 信息。
csharp
var ipInfo = HttpContext.GetRemoteIpInfo();
// 或者从请求头获取 IP 地址信息(适用于 Nginx、HAProxy 等代理)
var ipInfoFromHeader = HttpContext.GetRemoteIpInfo("X-Forwarded-For");
4. 同时使用 IPTools.China 和 IPTools.International
IPTools.China
和 IPTools.International
都实现了 IIpSearcher
接口,IpTool
类在加载时会根据已安装的程序包进行初始化。
DefaultSearcher
:默认使用的 IP 搜索器。IpChinaSearcher
:对应IPTools.China
实现的搜索器。IpAllSearcher
:对应IPTools.International
实现的搜索器。
如果你只安装了 IPTools.China
,那么 DefaultSearcher
将是 IpChinaSearcher
,IpAllSearcher
为 null
。
如果你只安装了 IPTools.International
,那么 DefaultSearcher
将是 IpAllSearcher
,IpChinaSearcher
为 null
。
如果你同时安装了两个组件,默认情况下 DefaultSearcher
将是 IpChinaSearcher
,IpChinaSearcher
和 IpAllSearcher
都不会为 null
。
可以通过以下代码更改默认的搜索器:
csharp
IpToolSettings.DefalutSearcherType = IpSearcherType.China;
IpToolSettings.DefalutSearcherType = IpSearcherType.International;
欢迎关注我的公众号"Net分享",技术文章第一时间推送,随缘更新 , 分享一些你可能注意不到的细节