UnionCpsCore.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Google.Protobuf.WellKnownTypes;
  19. using System.Threading.Channels;
  20. using TencentCloud.Tione.V20211111.Models;
  21. namespace molilian.core
  22. {
  23. public partial class UnionCpsCore
  24. {
  25. private static string _end_point;
  26. static UnionCpsCore()
  27. {
  28. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  29. }
  30. public static UnionCpsDTO GetFormattedObject(CpsChannelEnum channel, string ip = "", string oaid = "")
  31. {
  32. return new()
  33. {
  34. channel = channel,
  35. end_point = _end_point,
  36. ip = ip,
  37. oaid = oaid
  38. };
  39. }
  40. #region CPS 相关
  41. public static async Task<APIResult> EleAsync(string ip, string oaid)
  42. {
  43. UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
  44. var config = TkConfigCore.Get();
  45. //============================== 放弃转链-地区过滤 ==============================
  46. if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  47. {
  48. result.success = false;
  49. result.message = "流量控制";
  50. result.reason = reason;
  51. _ = TkLogCore.CpsLogAsync(result);
  52. return new APIResult(new
  53. {
  54. result.success,
  55. result.message,
  56. result.reason,
  57. channel = result.channel.ToString(),
  58. });
  59. }
  60. var account = ElePoolCore.GetOne();
  61. if (account == null)
  62. {
  63. result.success = false;
  64. result.message = "放弃转链";
  65. result.reason = "没有匹配账号";
  66. _ = TkLogCore.CpsLogAsync(result);
  67. return new APIResult(new
  68. {
  69. result.success,
  70. result.message,
  71. result.reason,
  72. channel = result.channel.ToString(),
  73. });
  74. }
  75. result.accountId = account.id;
  76. result.accountName = account.name;
  77. result.deeplink_url = account.deeplink;
  78. result.success = true;
  79. result.message = "OK";
  80. _ = TkLogCore.CpsLogAsync(result);
  81. return new APIResult(new
  82. {
  83. result.success,
  84. result.message,
  85. result.deeplink_url,
  86. channel = result.channel.ToString(),
  87. });
  88. }
  89. public static async Task<APIResult> MeituanAsync(string ip, string oaid)
  90. {
  91. UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
  92. var config = TkConfigCore.Get();
  93. //============================== 放弃转链-地区过滤 ==============================
  94. if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  95. {
  96. result.success = false;
  97. result.message = "流量控制";
  98. result.reason = reason;
  99. _ = TkLogCore.CpsLogAsync(result);
  100. return new APIResult(new
  101. {
  102. result.success,
  103. result.message,
  104. result.reason,
  105. channel = result.channel.ToString(),
  106. });
  107. }
  108. var account = MeituanPoolCore.GetOne();
  109. if (account == null)
  110. {
  111. result.success = false;
  112. result.message = "放弃转链";
  113. result.reason = "没有匹配账号";
  114. _ = TkLogCore.CpsLogAsync(result);
  115. return new APIResult(new
  116. {
  117. result.success,
  118. result.message,
  119. result.reason,
  120. channel = result.channel.ToString(),
  121. });
  122. }
  123. result.accountId = account.id;
  124. result.accountName = account.name;
  125. result.deeplink_url = account.deeplink;
  126. result.success = true;
  127. result.message = "OK";
  128. _ = TkLogCore.CpsLogAsync(result);
  129. return new APIResult(new
  130. {
  131. result.success,
  132. result.message,
  133. result.deeplink_url,
  134. channel = result.channel.ToString(),
  135. });
  136. }
  137. public static bool CpsShouldIgnoreRequest(TkConfigDTO config, CpsChannelEnum channel, string ip, string oaid, out string reason)
  138. {
  139. reason = string.Empty;
  140. try
  141. {
  142. // IP和流量控制
  143. string ignorePercentageCity = config.cpsIgnorePercentageCity;
  144. string? ipInfo = IP2RegionPlus.Search(ip);
  145. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  146. {
  147. reason = $"地区控制:{regionInfo}";
  148. return true;
  149. }
  150. int limit_num = config.cps_limit_per_ip_24h;
  151. if (limit_num > 0)
  152. {
  153. int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
  154. if (num > limit_num)
  155. {
  156. reason = "IP控制";
  157. return true;
  158. }
  159. num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
  160. if (num > limit_num)
  161. {
  162. reason = "OAID控制";
  163. return true;
  164. }
  165. }
  166. }
  167. catch (Exception ex)
  168. {
  169. reason = "配置异常";
  170. }
  171. return false;
  172. }
  173. #endregion
  174. }
  175. }