项目作者: donnytian

项目描述 :
ASP.NET request throttle
高级语言: C#
项目地址: git://github.com/donnytian/AspNetThrottle.git
创建时间: 2017-11-13T00:14:33Z
项目社区:https://github.com/donnytian/AspNetThrottle

开源协议:

下载


AspNetThrottle

ASP.NET Core request throttle

Install from NuGet

In the Package Manager Console:

PM> Install-Package AspNetThrottle.NetCore

Use middleware in Startup.cs

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddThrottle()
  4. .AddMemoryCacheCounterStore();
  5. //...
  6. }
  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  2. {
  3. //...
  4. /*
  5. * You can use either of IP or ID throttle as of below. Or both.
  6. */
  7. // IP address throttle.
  8. var ipOptions = new ThrottleOptions();
  9. Configuration.GetSection("IpThrottle").Bind(ipOptions);
  10. app.UseIpThrottle(ipOptions);
  11. // ID throttle.
  12. var idOptions = new ThrottleOptions();
  13. Configuration.GetSection("IdThrottle").Bind(idOptions);
  14. idOptions.ConfigureRequest((context, request) =>
  15. {
  16. // Demo for custom ID setting.
  17. // Actually this is the default implementation.
  18. request.ClientId = context.User?.Identity?.Name;
  19. });
  20. app.UseIdThrottle(idOptions);
  21. //...
  22. }

Configuration by appsettings.json

IP throttle

  1. "IpThrottle": {
  2. "ThrottleName": "myIpThrottle",
  3. "HttpStatusCode": 403,
  4. "QuotaExceededMessage": null,
  5. "ClientWhitelist": [ "10.10.1.100-10.10.1.255", "192.168.0.0/24" ],
  6. "EndpointWhitelist": [ "delete:/api/values", "*:/api/another" ],
  7. "GeneralRules": [
  8. {
  9. "Endpoint": "*",
  10. "Period": "15s",
  11. "Limit": 2
  12. },
  13. {
  14. "Endpoint": "*",
  15. "Period": "1m",
  16. "Limit": 3
  17. },
  18. {
  19. "Endpoint": "post:/api/values",
  20. "Period": "2m",
  21. "Limit": 3
  22. }
  23. ],
  24. "ClientPolicies": [
  25. {
  26. "ClientId": "::1",
  27. "Rules": [
  28. {
  29. "Endpoint": "*",
  30. "Period": "8s",
  31. "Limit": 2
  32. }
  33. ]
  34. },
  35. {
  36. "ClientId": "192.168.0.1",
  37. "Rules": [
  38. {
  39. "Endpoint": "*",
  40. "Period": "9s",
  41. "Limit": 3
  42. }
  43. ]
  44. }
  45. ]
  46. }

ID throttle

  1. "IdThrottle": {
  2. "ThrottleName": "myIdThrottle",
  3. "HttpStatusCode": 401,
  4. "QuotaExceededMessage": "Quota on your ID exceeded!",
  5. "ClientWhitelist": [ "David", "Donny" ],
  6. "EndpointWhitelist": [ "delete:/api/values", "*:/api/another" ],
  7. "GeneralRules": [
  8. {
  9. "Endpoint": "*",
  10. "Period": "15s",
  11. "Limit": 2
  12. },
  13. {
  14. "Endpoint": "*",
  15. "Period": "1m",
  16. "Limit": 3
  17. },
  18. {
  19. "Endpoint": "post:/api/values",
  20. "Period": "2m",
  21. "Limit": 3
  22. }
  23. ],
  24. "ClientPolicies": [
  25. {
  26. "ClientId": "Alice",
  27. "Rules": [
  28. {
  29. "Endpoint": "*",
  30. "Period": "11s",
  31. "Cooldown": "20s",
  32. "Limit": 2
  33. }
  34. ]
  35. },
  36. {
  37. "ClientId": "Bob",
  38. "Rules": [
  39. {
  40. "Endpoint": "*",
  41. "Period": "12s",
  42. "Limit": 3
  43. }
  44. ]
  45. }
  46. ]
  47. }