Adding RateLimit Feature ...
This commit is contained in:
@@ -14,6 +14,7 @@ using Swashbuckle.AspNetCore.SwaggerUI;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using xExceptions.Constants;
|
||||
|
||||
namespace xCommons.Extensions
|
||||
{
|
||||
@@ -39,7 +40,7 @@ namespace xCommons.Extensions
|
||||
/// <param name="configuration"></param>
|
||||
/// <param name="environment"></param>
|
||||
public static void AddXAppConfiguration(
|
||||
this IServiceCollection services,
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
string environment = null
|
||||
)
|
||||
@@ -52,6 +53,41 @@ namespace xCommons.Extensions
|
||||
services.AddSingleton(appConfiguration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Rate Limit Configurations and Register it ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXRateLimitConfiguration(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
var config = configuration.GetXRateLimitConfiguration();
|
||||
source.AddXRateLimitConfiguration(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register Rate Limit Configuration ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXRateLimitConfiguration(
|
||||
this IServiceCollection source,
|
||||
XRateLimitConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
if (source.IsNull())
|
||||
{
|
||||
XException.InvalidConfiguration.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
source.AddSingleton(configuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Environment name to XAppConfiguration ...
|
||||
/// </summary>
|
||||
@@ -74,7 +110,7 @@ namespace xCommons.Extensions
|
||||
{
|
||||
appConfiguration.Environment = environmentName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -509,6 +545,56 @@ namespace xCommons.Extensions
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register RateLimit Service ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
public static void AddXRateLimit(
|
||||
this IServiceCollection source
|
||||
)
|
||||
{
|
||||
//
|
||||
source.AddMemoryCache();
|
||||
source.AddSingleton<IXRateLimitProvider, XRateLimitProvider>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register RateLimit Service ...
|
||||
/// Configs Retrieved from AppSettings ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXRateLimit(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
var config = configuration.GetXRateLimitConfiguration();
|
||||
source.AddXRateLimit(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register RateLimit Service ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXRateLimit(
|
||||
this IServiceCollection source,
|
||||
XRateLimitConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
if (configuration.IsNull())
|
||||
{
|
||||
XException.InvalidConfiguration.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
source.AddXRateLimitConfiguration(configuration);
|
||||
source.AddXRateLimit();
|
||||
}
|
||||
|
||||
public static void AddXFileTypeDetector(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration
|
||||
|
||||
@@ -51,6 +51,22 @@ namespace xCommons.Extensions
|
||||
return xSwaggerConfigSection.Get<XSwaggerConfiguration>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve RateLimit Configurations ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static XRateLimitConfiguration GetXRateLimitConfiguration(this IConfiguration source)
|
||||
{
|
||||
//
|
||||
var section = source
|
||||
.GetSection(ConfigurationNodeNames.RATE_LIMIT_NODE);
|
||||
|
||||
//
|
||||
var result = section.Get<XRateLimitConfiguration>();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve File Type Detector Configuration ...
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
using System.Linq;
|
||||
using xCommons.Configurations;
|
||||
|
||||
namespace xCommons.Extensions
|
||||
{
|
||||
public static class XCommonsExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extract RateLimit Rule based on given Path ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public static XRateLimitRule GetMatchingRule(
|
||||
this XRateLimitConfiguration source,
|
||||
string path
|
||||
)
|
||||
{
|
||||
//
|
||||
if (!source.Enabled)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
//
|
||||
var normalizedPath = path.IsNullOrEmpty()
|
||||
? "/"
|
||||
: path.ToNormalString();
|
||||
|
||||
//
|
||||
var rule = source.Rules.IsNull()
|
||||
? null
|
||||
: source.Rules
|
||||
.Where(r =>
|
||||
!r.IsNull() &&
|
||||
r.Enabled &&
|
||||
!r.PathPrefix.IsNullOrEmpty() &&
|
||||
normalizedPath.StartsWith(r.PathPrefix.ToNormalString()))
|
||||
.OrderByDescending(r => r.PathPrefix.Length)
|
||||
.FirstOrDefault();
|
||||
|
||||
//
|
||||
if (!rule.IsNull())
|
||||
{
|
||||
return rule;
|
||||
}
|
||||
|
||||
//
|
||||
if (source.ApplyToAllRoutes)
|
||||
{
|
||||
//
|
||||
return new XRateLimitRule
|
||||
{
|
||||
Enabled = true,
|
||||
Name = "Default",
|
||||
PathPrefix = "/",
|
||||
Limit = source.DefaultLimit,
|
||||
KeyMode = source.DefaultKeyMode,
|
||||
PeriodSeconds = source.DefaultPeriodSeconds,
|
||||
};
|
||||
}
|
||||
|
||||
//
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,105 @@
|
||||
using System.IO;
|
||||
using xCommons.Constants;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace xCommons.Extensions {
|
||||
public static partial class XHttpContextExtensions {
|
||||
namespace xCommons.Extensions
|
||||
{
|
||||
public static partial class XHttpContextExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert IFormFile to Bytes Array
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] ToByteArray (this IFormFile source) {
|
||||
public static byte[] ToByteArray(this IFormFile source)
|
||||
{
|
||||
//
|
||||
byte[] data;
|
||||
using (var br = new BinaryReader (source.OpenReadStream ())) {
|
||||
data = br.ReadBytes ((int) source.OpenReadStream ().Length);
|
||||
using (var br = new BinaryReader(source.OpenReadStream()))
|
||||
{
|
||||
data = br.ReadBytes((int)source.OpenReadStream().Length);
|
||||
}
|
||||
|
||||
//
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract Rate Limit Key ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="keyMode"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetXRateLimitKey(
|
||||
this HttpContext source,
|
||||
XRateLimitKeyMode keyMode
|
||||
)
|
||||
{
|
||||
//
|
||||
if (source.IsNull())
|
||||
{
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
//
|
||||
switch (keyMode)
|
||||
{
|
||||
//
|
||||
case XRateLimitKeyMode.ClientId:
|
||||
{
|
||||
//
|
||||
var clientId = source.Request.Headers[XRateLimitConstants.ClientIdHeader].ToString();
|
||||
return clientId.IsNullOrEmpty()
|
||||
? "anonymous"
|
||||
: clientId.ToNormalString();
|
||||
}
|
||||
|
||||
//
|
||||
case XRateLimitKeyMode.User:
|
||||
{
|
||||
//
|
||||
var userId = source.User?.FindFirst(XCustomClaims.UserId)?.Value;
|
||||
if (userId.IsNullOrEmpty())
|
||||
{
|
||||
userId = source.User?.Identity?.Name;
|
||||
}
|
||||
|
||||
//
|
||||
return userId.IsNullOrEmpty()
|
||||
? "anonymous"
|
||||
: userId.ToNormalString();
|
||||
}
|
||||
|
||||
//
|
||||
case XRateLimitKeyMode.Route:
|
||||
{
|
||||
//
|
||||
return string.Format(
|
||||
"{0}:{1}",
|
||||
source.Request.Method,
|
||||
source.Request.Path
|
||||
).ToNormalString();
|
||||
}
|
||||
|
||||
//
|
||||
case XRateLimitKeyMode.Custom:
|
||||
{
|
||||
return "custom";
|
||||
}
|
||||
|
||||
//
|
||||
case XRateLimitKeyMode.Ip:
|
||||
default:
|
||||
{
|
||||
//
|
||||
return source.Connection.RemoteIpAddress
|
||||
.IsNull()
|
||||
? "unknown"
|
||||
: source.Connection.RemoteIpAddress
|
||||
.ToString()
|
||||
.ToNormalString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,20 @@
|
||||
using xCommons.Middlewares;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace xCommons.Extensions
|
||||
{
|
||||
public static class XMiddlewareExtensions
|
||||
{ }
|
||||
public static partial class XMiddlewareExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Use XRateLimit Middleware ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseXRateLimit(
|
||||
this IApplicationBuilder source
|
||||
)
|
||||
{
|
||||
return source.UseMiddleware<XRateLimitMiddleware>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user