Browse Source

refactor: remove IClientIpAddressProvider and replace with IWebClientInfoProvider in operation rate limiting

pull/25024/head
maliming 3 weeks ago
parent
commit
c67db4ff15
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 2
      framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/AbpAspNetCoreAbstractionsModule.cs
  2. 6
      framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/IClientIpAddressProvider.cs
  3. 6
      framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/NullClientIpAddressProvider.cs
  4. 36
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ClientIpAddress/HttpContextClientIpAddressProvider.cs
  5. 12
      framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/FixedWindowOperationRateLimitRule.cs
  6. 10
      framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitChecker.cs
  7. 2
      framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitRuleBuilder.cs
  8. 8
      framework/test/Volo.Abp.OperationRateLimit.Tests/Volo/Abp/OperationRateLimit/AbpOperationRateLimitTestModule.cs

2
framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/AbpAspNetCoreAbstractionsModule.cs

@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.ClientIpAddress;
using Volo.Abp.AspNetCore.VirtualFileSystem; using Volo.Abp.AspNetCore.VirtualFileSystem;
using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.AspNetCore.WebClientInfo;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
@ -12,6 +11,5 @@ public class AbpAspNetCoreAbstractionsModule : AbpModule
{ {
context.Services.AddSingleton<IWebContentFileProvider, NullWebContentFileProvider>(); context.Services.AddSingleton<IWebContentFileProvider, NullWebContentFileProvider>();
context.Services.AddSingleton<IWebClientInfoProvider, NullWebClientInfoProvider>(); context.Services.AddSingleton<IWebClientInfoProvider, NullWebClientInfoProvider>();
context.Services.AddSingleton<IClientIpAddressProvider, NullClientIpAddressProvider>();
} }
} }

6
framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/IClientIpAddressProvider.cs

@ -1,6 +0,0 @@
namespace Volo.Abp.AspNetCore.ClientIpAddress;
public interface IClientIpAddressProvider
{
string? ClientIpAddress { get; }
}

6
framework/src/Volo.Abp.AspNetCore.Abstractions/Volo/Abp/AspNetCore/ClientIpAddress/NullClientIpAddressProvider.cs

@ -1,6 +0,0 @@
namespace Volo.Abp.AspNetCore.ClientIpAddress;
public class NullClientIpAddressProvider : IClientIpAddressProvider
{
public string? ClientIpAddress => null;
}

36
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ClientIpAddress/HttpContextClientIpAddressProvider.cs

@ -1,36 +0,0 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.ClientIpAddress;
[Dependency(ReplaceServices = true)]
public class HttpContextClientIpAddressProvider : IClientIpAddressProvider, ITransientDependency
{
protected ILogger<HttpContextClientIpAddressProvider> Logger { get; }
protected IHttpContextAccessor HttpContextAccessor { get; }
public HttpContextClientIpAddressProvider(
ILogger<HttpContextClientIpAddressProvider> logger,
IHttpContextAccessor httpContextAccessor)
{
Logger = logger;
HttpContextAccessor = httpContextAccessor;
}
public string? ClientIpAddress => GetClientIpAddress();
protected virtual string? GetClientIpAddress()
{
try
{
return HttpContextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
}
catch (Exception ex)
{
Logger.LogException(ex, LogLevel.Warning);
return null;
}
}
}

12
framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/FixedWindowOperationRateLimitRule.cs

@ -1,5 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.AspNetCore.ClientIpAddress; using Volo.Abp.AspNetCore.WebClientInfo;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.Users; using Volo.Abp.Users;
@ -15,7 +15,7 @@ public class FixedWindowOperationRateLimitRule : IOperationRateLimitRule
protected IOperationRateLimitStore Store { get; } protected IOperationRateLimitStore Store { get; }
protected ICurrentUser CurrentUser { get; } protected ICurrentUser CurrentUser { get; }
protected ICurrentTenant CurrentTenant { get; } protected ICurrentTenant CurrentTenant { get; }
protected IClientIpAddressProvider ClientIpAddressProvider { get; } protected IWebClientInfoProvider WebClientInfoProvider { get; }
public FixedWindowOperationRateLimitRule( public FixedWindowOperationRateLimitRule(
string policyName, string policyName,
@ -24,7 +24,7 @@ public class FixedWindowOperationRateLimitRule : IOperationRateLimitRule
IOperationRateLimitStore store, IOperationRateLimitStore store,
ICurrentUser currentUser, ICurrentUser currentUser,
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
IClientIpAddressProvider clientIpAddressProvider) IWebClientInfoProvider webClientInfoProvider)
{ {
PolicyName = policyName; PolicyName = policyName;
RuleIndex = ruleIndex; RuleIndex = ruleIndex;
@ -32,7 +32,7 @@ public class FixedWindowOperationRateLimitRule : IOperationRateLimitRule
Store = store; Store = store;
CurrentUser = currentUser; CurrentUser = currentUser;
CurrentTenant = currentTenant; CurrentTenant = currentTenant;
ClientIpAddressProvider = clientIpAddressProvider; WebClientInfoProvider = webClientInfoProvider;
} }
public virtual async Task<OperationRateLimitRuleResult> AcquireAsync( public virtual async Task<OperationRateLimitRuleResult> AcquireAsync(
@ -78,10 +78,10 @@ public class FixedWindowOperationRateLimitRule : IOperationRateLimitRule
CurrentTenant.Id?.ToString() ?? HostTenantKey, CurrentTenant.Id?.ToString() ?? HostTenantKey,
OperationRateLimitPartitionType.ClientIp => OperationRateLimitPartitionType.ClientIp =>
ClientIpAddressProvider.ClientIpAddress WebClientInfoProvider.ClientIpAddress
?? throw new AbpException( ?? throw new AbpException(
$"Client IP address could not be determined. Policy '{PolicyName}' requires PartitionByClientIp. " + $"Client IP address could not be determined. Policy '{PolicyName}' requires PartitionByClientIp. " +
"Ensure IClientIpAddressProvider is properly configured."), "Ensure IWebClientInfoProvider is properly configured."),
OperationRateLimitPartitionType.Email => OperationRateLimitPartitionType.Email =>
context.Parameter context.Parameter

10
framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitChecker.cs

@ -4,7 +4,7 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.ClientIpAddress; using Volo.Abp.AspNetCore.WebClientInfo;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.Users; using Volo.Abp.Users;
@ -19,7 +19,7 @@ public class OperationRateLimitChecker : IOperationRateLimitChecker, ITransientD
protected IOperationRateLimitStore Store { get; } protected IOperationRateLimitStore Store { get; }
protected ICurrentUser CurrentUser { get; } protected ICurrentUser CurrentUser { get; }
protected ICurrentTenant CurrentTenant { get; } protected ICurrentTenant CurrentTenant { get; }
protected IClientIpAddressProvider ClientIpAddressProvider { get; } protected IWebClientInfoProvider WebClientInfoProvider { get; }
public OperationRateLimitChecker( public OperationRateLimitChecker(
IOptions<AbpOperationRateLimitOptions> options, IOptions<AbpOperationRateLimitOptions> options,
@ -28,7 +28,7 @@ public class OperationRateLimitChecker : IOperationRateLimitChecker, ITransientD
IOperationRateLimitStore store, IOperationRateLimitStore store,
ICurrentUser currentUser, ICurrentUser currentUser,
ICurrentTenant currentTenant, ICurrentTenant currentTenant,
IClientIpAddressProvider clientIpAddressProvider) IWebClientInfoProvider webClientInfoProvider)
{ {
Options = options.Value; Options = options.Value;
PolicyProvider = policyProvider; PolicyProvider = policyProvider;
@ -36,7 +36,7 @@ public class OperationRateLimitChecker : IOperationRateLimitChecker, ITransientD
Store = store; Store = store;
CurrentUser = currentUser; CurrentUser = currentUser;
CurrentTenant = currentTenant; CurrentTenant = currentTenant;
ClientIpAddressProvider = clientIpAddressProvider; WebClientInfoProvider = webClientInfoProvider;
} }
public virtual async Task CheckAsync(string policyName, OperationRateLimitContext? context = null) public virtual async Task CheckAsync(string policyName, OperationRateLimitContext? context = null)
@ -162,7 +162,7 @@ public class OperationRateLimitChecker : IOperationRateLimitChecker, ITransientD
Store, Store,
CurrentUser, CurrentUser,
CurrentTenant, CurrentTenant,
ClientIpAddressProvider)); WebClientInfoProvider));
} }
foreach (var customRuleType in policy.CustomRuleTypes) foreach (var customRuleType in policy.CustomRuleTypes)

2
framework/src/Volo.Abp.OperationRateLimit/Volo/Abp/OperationRateLimit/OperationRateLimitRuleBuilder.cs

@ -63,7 +63,7 @@ public class OperationRateLimitRuleBuilder
} }
/// <summary> /// <summary>
/// Auto resolve from IClientIpAddressProvider.ClientIpAddress. /// Auto resolve from IWebClientInfoProvider.ClientIpAddress.
/// </summary> /// </summary>
public OperationRateLimitPolicyBuilder PartitionByClientIp() public OperationRateLimitPolicyBuilder PartitionByClientIp()
{ {

8
framework/test/Volo.Abp.OperationRateLimit.Tests/Volo/Abp/OperationRateLimit/AbpOperationRateLimitTestModule.cs

@ -1,7 +1,7 @@
using System; using System;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NSubstitute; using NSubstitute;
using Volo.Abp.AspNetCore.ClientIpAddress; using Volo.Abp.AspNetCore.WebClientInfo;
using Volo.Abp.Autofac; using Volo.Abp.Autofac;
using Volo.Abp.ExceptionHandling; using Volo.Abp.ExceptionHandling;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
@ -18,9 +18,9 @@ public class AbpOperationRateLimitTestModule : AbpModule
{ {
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
var mockIpProvider = Substitute.For<IClientIpAddressProvider>(); var mockWebClientInfoProvider = Substitute.For<IWebClientInfoProvider>();
mockIpProvider.ClientIpAddress.Returns("127.0.0.1"); mockWebClientInfoProvider.ClientIpAddress.Returns("127.0.0.1");
context.Services.AddSingleton<IClientIpAddressProvider>(mockIpProvider); context.Services.AddSingleton<IWebClientInfoProvider>(mockWebClientInfoProvider);
Configure<AbpOperationRateLimitOptions>(options => Configure<AbpOperationRateLimitOptions>(options =>
{ {

Loading…
Cancel
Save