-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #998 from colinin/ip-location-session
feat(session): add ip geolocation resolution to user sessions
- Loading branch information
Showing
34 changed files
with
684,618 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
aspnet-core/framework/common/LINGYUN.Abp.IP2Region/FodyWeavers.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> | ||
<ConfigureAwait ContinueOnCapturedContext="false" /> | ||
</Weavers> |
30 changes: 30 additions & 0 deletions
30
aspnet-core/framework/common/LINGYUN.Abp.IP2Region/FodyWeavers.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> | ||
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. --> | ||
<xs:element name="Weavers"> | ||
<xs:complexType> | ||
<xs:all> | ||
<xs:element name="ConfigureAwait" minOccurs="0" maxOccurs="1"> | ||
<xs:complexType> | ||
<xs:attribute name="ContinueOnCapturedContext" type="xs:boolean" /> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:all> | ||
<xs:attribute name="VerifyAssembly" type="xs:boolean"> | ||
<xs:annotation> | ||
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation> | ||
</xs:annotation> | ||
</xs:attribute> | ||
<xs:attribute name="VerifyIgnoreCodes" type="xs:string"> | ||
<xs:annotation> | ||
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation> | ||
</xs:annotation> | ||
</xs:attribute> | ||
<xs:attribute name="GenerateXsd" type="xs:boolean"> | ||
<xs:annotation> | ||
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation> | ||
</xs:annotation> | ||
</xs:attribute> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:schema> |
65 changes: 65 additions & 0 deletions
65
...common/LINGYUN.Abp.IP2Region/IP2Region/Net/Internal/Abstractions/AbstractCacheStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2023 The Ip2Region Authors. All rights reserved. | ||
// Use of this source code is governed by a Apache2.0-style | ||
// license that can be found in the LICENSE file. | ||
// @Author Alan <[email protected]> | ||
// @Date 2023/07/25 | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.IO; | ||
|
||
namespace IP2Region.Net.Internal.Abstractions; | ||
|
||
internal abstract class AbstractCacheStrategy | ||
{ | ||
protected const int HeaderInfoLength = 256; | ||
protected const int VectorIndexRows = 256; | ||
protected const int VectorIndexCols = 256; | ||
protected const int VectorIndexSize = 8; | ||
|
||
protected readonly Stream XdbStream; | ||
private const int BufferSize = 4096; | ||
|
||
internal int IoCount { get; private set; } | ||
|
||
protected AbstractCacheStrategy(Stream xdbStream) | ||
{ | ||
XdbStream = xdbStream; | ||
} | ||
|
||
protected int GetVectorIndexStartPos(uint ip) | ||
{ | ||
var il0 = ip >> 24 & 0xFF; | ||
var il1 = ip >> 16 & 0xFF; | ||
var idx = il0 * VectorIndexCols * VectorIndexSize + il1 * VectorIndexSize; | ||
return (int)idx; | ||
} | ||
|
||
internal abstract ReadOnlyMemory<byte> GetVectorIndex(uint ip); | ||
|
||
internal virtual ReadOnlyMemory<byte> GetData(int offset, int length) | ||
{ | ||
byte[] buffer = ArrayPool<byte>.Shared.Rent(length); | ||
int totalBytesRead = 0; | ||
try | ||
{ | ||
XdbStream.Seek(offset, SeekOrigin.Begin); | ||
|
||
int bytesRead; | ||
do | ||
{ | ||
int bytesToRead = Math.Min(BufferSize, length - totalBytesRead); | ||
bytesRead = XdbStream.Read(buffer, totalBytesRead, bytesToRead); | ||
totalBytesRead += bytesRead; | ||
|
||
IoCount++; | ||
} while (bytesRead > 0 && totalBytesRead < length); | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(buffer); | ||
} | ||
|
||
return new ReadOnlyMemory<byte>(buffer, 0, totalBytesRead); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ore/framework/common/LINGYUN.Abp.IP2Region/IP2Region/Net/Internal/CacheStrategyFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2023 The Ip2Region Authors. All rights reserved. | ||
// Use of this source code is governed by a Apache2.0-style | ||
// license that can be found in the LICENSE file. | ||
// @Author Alan <[email protected]> | ||
// @Date 2023/07/25 | ||
|
||
using IP2Region.Net.Internal.Abstractions; | ||
using IP2Region.Net.XDB; | ||
using System; | ||
using System.IO; | ||
|
||
namespace IP2Region.Net.Internal; | ||
|
||
internal class CacheStrategyFactory | ||
{ | ||
private readonly Stream _xdbStream; | ||
|
||
public CacheStrategyFactory(Stream xdbStream) | ||
{ | ||
_xdbStream = xdbStream; | ||
} | ||
|
||
public AbstractCacheStrategy CreateCacheStrategy(CachePolicy cachePolicy) | ||
{ | ||
return cachePolicy switch | ||
{ | ||
CachePolicy.Content => new ContentCacheStrategy(_xdbStream), | ||
CachePolicy.VectorIndex => new VectorIndexCacheStrategy(_xdbStream), | ||
CachePolicy.File => new FileCacheStrategy(_xdbStream), | ||
_ => throw new ArgumentException(nameof(cachePolicy)) | ||
}; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ore/framework/common/LINGYUN.Abp.IP2Region/IP2Region/Net/Internal/ContentCacheStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2023 The Ip2Region Authors. All rights reserved. | ||
// Use of this source code is governed by a Apache2.0-style | ||
// license that can be found in the LICENSE file. | ||
// @Author Alan <[email protected]> | ||
// @Date 2023/07/25 | ||
|
||
using IP2Region.Net.Internal.Abstractions; | ||
using System; | ||
using System.IO; | ||
|
||
namespace IP2Region.Net.Internal; | ||
|
||
internal class ContentCacheStrategy : AbstractCacheStrategy | ||
{ | ||
private readonly ReadOnlyMemory<byte> _cacheData; | ||
|
||
public ContentCacheStrategy(Stream xdbStream) : base(xdbStream) | ||
{ | ||
_cacheData = base.GetData(0, (int)XdbStream.Length); | ||
XdbStream.Close(); | ||
XdbStream.Dispose(); | ||
} | ||
|
||
internal override ReadOnlyMemory<byte> GetVectorIndex(uint ip) | ||
{ | ||
int idx = GetVectorIndexStartPos(ip); | ||
return _cacheData.Slice(HeaderInfoLength + idx, VectorIndexSize); | ||
} | ||
|
||
internal override ReadOnlyMemory<byte> GetData(int offset, int length) | ||
{ | ||
return _cacheData.Slice(offset, length); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...t-core/framework/common/LINGYUN.Abp.IP2Region/IP2Region/Net/Internal/FileCacheStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2023 The Ip2Region Authors. All rights reserved. | ||
// Use of this source code is governed by a Apache2.0-style | ||
// license that can be found in the LICENSE file. | ||
// @Author Alan <[email protected]> | ||
// @Date 2023/07/25 | ||
|
||
using IP2Region.Net.Internal.Abstractions; | ||
using System; | ||
using System.IO; | ||
|
||
namespace IP2Region.Net.Internal; | ||
|
||
internal class FileCacheStrategy : AbstractCacheStrategy | ||
{ | ||
public FileCacheStrategy(Stream xdbStream) : base(xdbStream) | ||
{ | ||
} | ||
|
||
internal override ReadOnlyMemory<byte> GetVectorIndex(uint ip) | ||
{ | ||
var idx = GetVectorIndexStartPos(ip); | ||
return GetData(HeaderInfoLength + idx, VectorIndexSize); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...framework/common/LINGYUN.Abp.IP2Region/IP2Region/Net/Internal/VectorIndexCacheStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2023 The Ip2Region Authors. All rights reserved. | ||
// Use of this source code is governed by a Apache2.0-style | ||
// license that can be found in the LICENSE file. | ||
// @Author Alan <[email protected]> | ||
// @Date 2023/07/25 | ||
|
||
using IP2Region.Net.Internal.Abstractions; | ||
using System; | ||
using System.IO; | ||
|
||
namespace IP2Region.Net.Internal; | ||
|
||
internal class VectorIndexCacheStrategy : AbstractCacheStrategy | ||
{ | ||
private readonly ReadOnlyMemory<byte> _vectorIndex; | ||
|
||
public VectorIndexCacheStrategy(Stream xdbStream) : base(xdbStream) | ||
{ | ||
var vectorLength = VectorIndexRows * VectorIndexCols * VectorIndexSize; | ||
_vectorIndex = base.GetData(HeaderInfoLength, vectorLength); | ||
} | ||
|
||
internal override ReadOnlyMemory<byte> GetVectorIndex(uint ip) | ||
{ | ||
var idx = GetVectorIndexStartPos(ip); | ||
return _vectorIndex.Slice(idx, VectorIndexSize); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
aspnet-core/framework/common/LINGYUN.Abp.IP2Region/LINGYUN.Abp.IP2Region.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\..\..\..\configureawait.props" /> | ||
<Import Project="..\..\..\..\common.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;netstandard2.1;net8.0</TargetFrameworks> | ||
<AssemblyName>LINGYUN.Abp.IP2Region</AssemblyName> | ||
<PackageId>LINGYUN.Abp.IP2Region</PackageId> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<Nullable>enable</Nullable> | ||
<RootNamespace /> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="LINGYUN\Abp\IP2Region\Resources\ip2region.xdb" /> | ||
<EmbeddedResource Include="LINGYUN\Abp\IP2Region\Resources\ip2region.xdb" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Volo.Abp.VirtualFileSystem" /> | ||
<PackageReference Include="IP2Region.Net" /> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
...t-core/framework/common/LINGYUN.Abp.IP2Region/LINGYUN/Abp/IP2Region/AbpIP2RegionModule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using IP2Region.Net.Abstractions; | ||
using IP2Region.Net.XDB; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Volo.Abp.Modularity; | ||
using Volo.Abp.VirtualFileSystem; | ||
|
||
namespace LINGYUN.Abp.IP2Region; | ||
|
||
[DependsOn(typeof(AbpVirtualFileSystemModule))] | ||
public class AbpIP2RegionModule : AbpModule | ||
{ | ||
public override void ConfigureServices(ServiceConfigurationContext context) | ||
{ | ||
Configure<AbpVirtualFileSystemOptions>(options => | ||
{ | ||
options.FileSets.AddEmbedded<AbpIP2RegionModule>(); | ||
}); | ||
|
||
context.Services.AddSingleton<ISearcher, AbpSearcher>((serviceProvider) => | ||
{ | ||
var virtualFileProvider = serviceProvider.GetRequiredService<IVirtualFileProvider>(); | ||
var xdbFile = virtualFileProvider.GetFileInfo("/LINGYUN/Abp/IP2Region/Resources/ip2region.xdb"); | ||
var searcher = new AbpSearcher(CachePolicy.File, xdbFile.CreateReadStream()); | ||
return searcher; | ||
}); | ||
} | ||
} |
Oops, something went wrong.