base.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using System.Text.RegularExpressions;
  11. using Org.BouncyCastle.Ocsp;
  12. using System.Diagnostics;
  13. using TencentCloud.Fmu.V20191213.Models;
  14. using Microsoft.VisualBasic;
  15. using System.Text.Json;
  16. using static System.Runtime.InteropServices.JavaScript.JSType;
  17. using System.Security.Cryptography;
  18. using System.Security.Policy;
  19. using Google.Protobuf.WellKnownTypes;
  20. using Microsoft.AspNetCore.Components;
  21. using Org.BouncyCastle.Pqc.Crypto.Ntru;
  22. using System.Net;
  23. namespace molilian.core
  24. {
  25. public partial class PddUnionPlus
  26. {
  27. static Random _random = new Random();
  28. private WebProxy? _proxy = null;
  29. public TkConfigDTO _config;
  30. private static PddPoolDTO _account;
  31. private static string _end_point;
  32. static PddUnionPlus()
  33. {
  34. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  35. }
  36. public PddUnionPlus(PddPoolDTO account)
  37. {
  38. _account = account;
  39. _config = TkConfigCore.Get();
  40. if (!string.IsNullOrEmpty(account.nodeName))
  41. {
  42. _proxy = ProxyNodesCore.GetOne(account.nodeName);
  43. }
  44. }
  45. public static bool ShouldIgnoreRequest(TkConfigDTO config, string ip, string oaid, out string reason)
  46. {
  47. reason = string.Empty;
  48. try
  49. {
  50. // IP和流量控制
  51. string ignorePercentageCity = config.pddIgnorePercentageCity;
  52. string? ipInfo = IP2RegionPlus.Search(ip);
  53. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  54. {
  55. reason = $"地区控制:{regionInfo}";
  56. return true;
  57. }
  58. int limit_num = config.pdd_limit_per_ip_24h;
  59. if (limit_num > 0)
  60. {
  61. int num = TkLogCore.getClientRequestTotalByIp(TkChannelEnum.pdd, ip);
  62. if (num > limit_num)
  63. {
  64. reason = "IP控制";
  65. return true;
  66. }
  67. }
  68. limit_num = config.pdd_limit_per_oaid_24h;
  69. if (limit_num > 0)
  70. {
  71. int num = TkLogCore.getClientRequestTotalByOAID(TkChannelEnum.pdd, oaid);
  72. if (num > limit_num)
  73. {
  74. reason = "OAID控制";
  75. return true;
  76. }
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. reason = "配置异常";
  82. }
  83. return false;
  84. }
  85. public static bool FlowControlIgnoreRequest(TkConfigDTO config, out string reason)
  86. {
  87. reason = string.Empty;
  88. try
  89. {
  90. // IP和流量控制
  91. int ignorePercentage = config.pddIgnorePercentage;
  92. if (ignorePercentage == 0) return false;
  93. var randomValue = (decimal)_random.Next(0, 100);
  94. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  95. if (result)
  96. {
  97. reason = "流量控制";
  98. return true;
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. reason = $"配置异常";
  104. }
  105. return false;
  106. }
  107. }
  108. }