base.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if (TkLogCore.InBlacklist(config.blacklist_oaid, oaid))
  59. {
  60. reason = "OAID黑名单";
  61. return true;
  62. }
  63. int limit_num = config.pdd_limit_per_ip_24h;
  64. if (limit_num > 0)
  65. {
  66. int num = TkLogCore.getClientRequestTotalByIp(TkChannelEnum.pdd, ip);
  67. if (num >= limit_num)
  68. {
  69. reason = "IP控制";
  70. return true;
  71. }
  72. }
  73. limit_num = config.pdd_limit_per_oaid_24h;
  74. if (limit_num > 0)
  75. {
  76. int num = TkLogCore.getClientRequestTotalByOAID(TkChannelEnum.pdd, oaid);
  77. if (num >= limit_num)
  78. {
  79. reason = "OAID控制";
  80. return true;
  81. }
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. reason = "配置异常";
  87. }
  88. return false;
  89. }
  90. public static bool FlowControlIgnoreRequest(TkConfigDTO config, out string reason)
  91. {
  92. reason = string.Empty;
  93. try
  94. {
  95. // IP和流量控制
  96. int ignorePercentage = config.pddIgnorePercentage;
  97. if (ignorePercentage == 0) return false;
  98. var randomValue = (decimal)_random.Next(0, 100);
  99. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  100. if (result)
  101. {
  102. reason = "流量控制";
  103. return true;
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. reason = $"配置异常";
  109. }
  110. return false;
  111. }
  112. }
  113. }