base.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Net;
  2. using dodohold.core.kwaixiaodian;
  3. namespace molilian.core
  4. {
  5. public partial class KsUnionPlus
  6. {
  7. static Random _random = new Random();
  8. private WebProxy? _proxy = null;
  9. public TkConfigDTO _config;
  10. private static KsPoolDTO _account;
  11. private static string _end_point;
  12. string server = "https://openapi.kwaixiaodian.com";
  13. static KsUnionPlus()
  14. {
  15. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  16. }
  17. KwaiXiaodianPlus plus;
  18. public KsUnionPlus(KsPoolDTO account)
  19. {
  20. _account = account;
  21. _config = TkConfigCore.Get();
  22. if (!string.IsNullOrEmpty(account.nodeName))
  23. {
  24. _proxy = ProxyNodesCore.GetOne(account.nodeName);
  25. }
  26. plus = new KwaiXiaodianPlus(server, _account.app_key, _account.app_secret, _account.sign_secret,
  27. _account.event_secret, string.Empty, 0);
  28. }
  29. public static bool ShouldIgnoreRequest(TkConfigDTO config, string ip, string oaid, out string reason)
  30. {
  31. reason = string.Empty;
  32. try
  33. {
  34. // IP和流量控制
  35. string ignorePercentageCity = config.ksIgnorePercentageCity;
  36. string? ipInfo = IP2RegionPlus.Search(ip);
  37. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  38. {
  39. reason = $"地区控制:{regionInfo}";
  40. return true;
  41. }
  42. int limit_num = config.ks_limit_per_ip_24h;
  43. if (limit_num > 0)
  44. {
  45. int num = TkLogCore.getClientRequestTotalByIp(TkChannelEnum.ks, ip);
  46. if (num > limit_num)
  47. {
  48. reason = "IP控制";
  49. return true;
  50. }
  51. }
  52. limit_num = config.ks_limit_per_oaid_24h;
  53. if (limit_num > 0)
  54. {
  55. int num = TkLogCore.getClientRequestTotalByOAID(TkChannelEnum.ks, oaid);
  56. if (num > limit_num)
  57. {
  58. reason = "OAID控制";
  59. return true;
  60. }
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. reason = "配置异常";
  66. }
  67. return false;
  68. }
  69. public static bool FlowControlIgnoreRequest(TkConfigDTO config, out string reason)
  70. {
  71. reason = string.Empty;
  72. try
  73. {
  74. // IP和流量控制
  75. int ignorePercentage = config.ksIgnorePercentage;
  76. if (ignorePercentage == 0) return false;
  77. var randomValue = (decimal)_random.Next(0, 100);
  78. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  79. if (result)
  80. {
  81. reason = "流量控制";
  82. return true;
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. reason = $"配置异常";
  88. }
  89. return false;
  90. }
  91. }
  92. }