Skip to content

Commit 230c42e

Browse files
authored
feat(IP2Region): support IPv6 locate (#921)
* chore: 更新数据文件增加 IPv6 数据 * chore: 更新依赖到最新 * feat: 增加 CachePolicy 配置项 * chore: 更新数据包拷贝脚本 * feat: 增加 IPv4/6 自适应逻辑
1 parent 02f668d commit 230c42e

7 files changed

Lines changed: 55 additions & 22 deletions

File tree

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

3+
<PropertyGroup>
4+
<Version>10.0.1</Version>
5+
</PropertyGroup>
6+
37
<PropertyGroup>
48
<PackageTags>Bootstrap Blazor WebAssembly wasm UI Components Ip Region</PackageTags>
59
<Description>Bootstrap UI components extensions of IP2Region.NET</Description>
610
</PropertyGroup>
711

812
<ItemGroup>
913
<Content Include="Build\*.targets" PackagePath="build\" />
10-
<Content Include="Data\ip2region.xdb" PackagePath="contentFiles\ip2region.xdb" />
14+
<Content Include="Data\ip2region_v4.xdb" PackagePath="contentFiles\ip2region_v4.xdb" />
15+
<Content Include="Data\ip2region_v6.xdb" PackagePath="contentFiles\ip2region_v6.xdb" />
1116
</ItemGroup>
1217

1318
<ItemGroup>
1419
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
15-
<PackageReference Include="IP2Region.Net" Version="2.0.2" />
20+
<PackageReference Include="IP2Region.Net" Version="3.0.1" />
1621
</ItemGroup>
1722

1823
</Project>

src/components/BootstrapBlazor.IP2Region/Build/BootstrapBlazor.IP2Region.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<ItemGroup>
4-
<IP2RegionFiles Include="$(MSBuildThisFileDirectory)/../contentFiles/ip2region.xdb"/>
4+
<IP2RegionFiles Include="$(MSBuildThisFileDirectory)/../contentFiles/ip2region_v*.xdb"/>
55
</ItemGroup>
66

77
<Target Name="IP2RegionCopyFilesAfterBuild" AfterTargets="Build">

src/components/BootstrapBlazor.IP2Region/Data/ip2region.xdb renamed to src/components/BootstrapBlazor.IP2Region/Data/ip2region_v4.xdb

10.6 MB
Binary file not shown.
34.4 MB
Binary file not shown.

src/components/BootstrapBlazor.IP2Region/Extensions/ServiceCollectionExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

@@ -15,14 +15,19 @@ public static class ServiceCollectionExtensions
1515
/// 添加 IP2RegionService 服务
1616
/// </summary>
1717
/// <param name="services"></param>
18-
public static IServiceCollection AddBootstrapBlazorIP2RegionService(this IServiceCollection services)
18+
/// <param name="configureOptions"></param>
19+
public static IServiceCollection AddBootstrapBlazorIP2RegionService(this IServiceCollection services, Action<IP2RegionOptions>? configureOptions = null)
1920
{
2021
services.AddSingleton<IIpLocatorProvider, IP2RegionService>();
2122
#if NET8_0_OR_GREATER
2223
services.AddKeyedSingleton<IIpLocatorProvider, IP2RegionService>("BootstrapBlazor.IP2Region");
2324
#endif
2425

2526
services.AddOptionsMonitor<IP2RegionOptions>();
27+
services.Configure<IP2RegionOptions>(options =>
28+
{
29+
configureOptions?.Invoke(options);
30+
});
2631
return services;
2732
}
2833
}

src/components/BootstrapBlazor.IP2Region/Options/IP2RegionOptions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5+
using IP2Region.Net.XDB;
6+
57
namespace BootstrapBlazor.Components;
68

79
/// <summary>
@@ -13,4 +15,9 @@ public class IP2RegionOptions
1315
/// 获取/设置 IP2Region 数据文件路径
1416
/// </summary>
1517
public string? XdbPath { get; set; }
18+
19+
/// <summary>
20+
/// 获得/设置 缓存策略 默认 <see cref="CachePolicy.Content"/>
21+
/// </summary>
22+
public CachePolicy CachePolicy { get; set; } = CachePolicy.Content;
1623
}
Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5+
using IP2Region.Net.Abstractions;
56
using IP2Region.Net.XDB;
67
using Microsoft.Extensions.Logging;
78
using Microsoft.Extensions.Options;
9+
using System.Collections.Concurrent;
10+
using System.Net;
811

912
namespace BootstrapBlazor.Components;
1013

@@ -24,51 +27,64 @@ public IP2RegionService(IOptions<BootstrapBlazorOptions> options, IOptions<IP2Re
2427
_options = options;
2528
_ipOptions = ipRegionOptions;
2629
_logger = logger;
27-
28-
Task.Run(InitSearch, CancellationToken.None).ConfigureAwait(false);
2930
}
3031

3132
private readonly IOptions<BootstrapBlazorOptions> _options;
3233
private readonly IOptions<IP2RegionOptions> _ipOptions;
3334
private readonly ILogger<IP2RegionService> _logger;
3435
private readonly TaskCompletionSource _tcs = new();
35-
private Searcher? _search;
36+
private ConcurrentDictionary<string, ISearcher?> _searchCache = [];
3637

3738
/// <summary>
3839
/// <inheritdoc/>
3940
/// </summary>
4041
/// <param name="ip"></param>
41-
protected override async Task<string?> LocateByIp(string ip)
42+
protected override Task<string?> LocateByIp(string ip)
4243
{
43-
await _tcs.Task;
44-
4544
string? result = null;
46-
if (_search != null && _options.Value.WebClientOptions.EnableIpLocator)
45+
if (_options.Value.WebClientOptions.EnableIpLocator && IPAddress.TryParse(ip, out var address))
4746
{
48-
result = _search.Search(ip);
47+
ISearcher? searcher = null;
48+
if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
49+
{
50+
searcher = CreateSearcher("ip2region_v4.xdb");
51+
}
52+
else if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
53+
{
54+
searcher = CreateSearcher("ip2region_v6.xdb");
55+
}
56+
result = searcher?.Search(ip);
4957
}
50-
return result;
58+
return Task.FromResult(result);
5159
}
5260

53-
private void InitSearch()
61+
private ISearcher? CreateSearcher(string xdbFile) => _searchCache.GetOrAdd(xdbFile, key =>
5462
{
55-
var xdbPath = _ipOptions.Value.XdbPath ?? Path.Combine(AppContext.BaseDirectory, "ip2region", "ip2region.xdb");
63+
var xdbPath = _ipOptions.Value.XdbPath ?? Path.Combine(AppContext.BaseDirectory, "ip2region", xdbFile);
5664
if (!File.Exists(xdbPath))
5765
{
5866
_logger.LogWarning("IP2Region xdb file not found, please check the file path: {dbPath}", xdbPath);
59-
return;
67+
return null;
6068
}
6169

70+
ISearcher? searcher = null;
6271
try
6372
{
64-
_search = new Searcher(CachePolicy.Content, xdbPath);
65-
_logger.LogInformation("IP2Region xdb file {dbPath} loaded", xdbPath);
73+
var cachePolicy = _ipOptions.Value.CachePolicy;
74+
searcher = new Searcher(cachePolicy, xdbPath);
75+
76+
if (_logger.IsEnabled(LogLevel.Information))
77+
{
78+
_logger.LogInformation("IP2Region xdb file {dbPath} loaded", xdbPath);
79+
}
6680
_tcs.TrySetResult();
6781
}
6882
catch (Exception ex)
6983
{
7084
_tcs.TrySetException(ex);
7185
_logger.LogError(ex, "IP2Region xdb file path: {dbPath}", xdbPath);
7286
}
73-
}
87+
88+
return searcher;
89+
});
7490
}

0 commit comments

Comments
 (0)