UnionCpsCore.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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(string 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> CpsCouponAsync(string channel, string ip, string oaid)
  42. {
  43. UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
  44. if (string.IsNullOrEmpty(result.channel))
  45. {
  46. result.success = false;
  47. result.message = "放弃转链";
  48. result.reason = "无效渠道";
  49. _ = TkLogCore.CpsLogAsync(result);
  50. return new APIResult(new
  51. {
  52. result.success,
  53. result.message,
  54. result.reason,
  55. channel,
  56. });
  57. }
  58. var config = TkConfigCore.Get();
  59. //============================== 放弃转链-地区过滤 ==============================
  60. if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  61. {
  62. result.success = false;
  63. result.message = "流量控制";
  64. result.reason = reason;
  65. _ = TkLogCore.CpsLogAsync(result);
  66. return new APIResult(new
  67. {
  68. result.success,
  69. result.message,
  70. result.reason,
  71. result.channel,
  72. });
  73. }
  74. var account = CpsPoolCore.GetOne(result.channel);
  75. if (account == null)
  76. {
  77. result.success = false;
  78. result.message = "放弃转链";
  79. result.reason = "没有匹配账号";
  80. _ = TkLogCore.CpsLogAsync(result);
  81. return new APIResult(new
  82. {
  83. result.success,
  84. result.message,
  85. result.reason,
  86. result.channel,
  87. });
  88. }
  89. result.accountId = account.id;
  90. result.accountName = account.name;
  91. result.deeplink_url = account.deeplink;
  92. result.token = account.token;
  93. result.category = account.category;
  94. result.pid = account.pid;
  95. result.success = true;
  96. result.message = "OK";
  97. _ = TkLogCore.CpsLogAsync(result);
  98. return new APIResult(new
  99. {
  100. result.success,
  101. result.message,
  102. result.deeplink_url,
  103. result.token,
  104. result.channel,
  105. });
  106. }
  107. //public static async Task<APIResult> EleAsync(string ip, string oaid)
  108. //{
  109. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
  110. // var config = TkConfigCore.Get();
  111. // //============================== 放弃转链-地区过滤 ==============================
  112. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  113. // {
  114. // result.success = false;
  115. // result.message = "流量控制";
  116. // result.reason = reason;
  117. // _ = TkLogCore.CpsLogAsync(result);
  118. // return new APIResult(new
  119. // {
  120. // result.success,
  121. // result.message,
  122. // result.reason,
  123. // channel = result.channel.ToString(),
  124. // });
  125. // }
  126. // var account = ElePoolCore.GetOne();
  127. // if (account == null)
  128. // {
  129. // result.success = false;
  130. // result.message = "放弃转链";
  131. // result.reason = "没有匹配账号";
  132. // _ = TkLogCore.CpsLogAsync(result);
  133. // return new APIResult(new
  134. // {
  135. // result.success,
  136. // result.message,
  137. // result.reason,
  138. // channel = result.channel.ToString(),
  139. // });
  140. // }
  141. // result.accountId = account.id;
  142. // result.accountName = account.name;
  143. // result.deeplink_url = account.deeplink;
  144. // result.success = true;
  145. // result.message = "OK";
  146. // _ = TkLogCore.CpsLogAsync(result);
  147. // return new APIResult(new
  148. // {
  149. // result.success,
  150. // result.message,
  151. // result.deeplink_url,
  152. // channel = result.channel.ToString(),
  153. // });
  154. //}
  155. //public static async Task<APIResult> MeituanAsync(string ip, string oaid)
  156. //{
  157. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
  158. // var config = TkConfigCore.Get();
  159. // //============================== 放弃转链-地区过滤 ==============================
  160. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  161. // {
  162. // result.success = false;
  163. // result.message = "流量控制";
  164. // result.reason = reason;
  165. // _ = TkLogCore.CpsLogAsync(result);
  166. // return new APIResult(new
  167. // {
  168. // result.success,
  169. // result.message,
  170. // result.reason,
  171. // channel = result.channel.ToString(),
  172. // });
  173. // }
  174. // var account = MeituanPoolCore.GetOne();
  175. // if (account == null)
  176. // {
  177. // result.success = false;
  178. // result.message = "放弃转链";
  179. // result.reason = "没有匹配账号";
  180. // _ = TkLogCore.CpsLogAsync(result);
  181. // return new APIResult(new
  182. // {
  183. // result.success,
  184. // result.message,
  185. // result.reason,
  186. // channel = result.channel.ToString(),
  187. // });
  188. // }
  189. // result.accountId = account.id;
  190. // result.accountName = account.name;
  191. // result.deeplink_url = account.deeplink;
  192. // result.success = true;
  193. // result.message = "OK";
  194. // _ = TkLogCore.CpsLogAsync(result);
  195. // return new APIResult(new
  196. // {
  197. // result.success,
  198. // result.message,
  199. // result.deeplink_url,
  200. // channel = result.channel.ToString(),
  201. // });
  202. //}
  203. public static bool CpsShouldIgnoreRequest(TkConfigDTO config, string channel, string ip, string oaid, out string reason)
  204. {
  205. reason = string.Empty;
  206. try
  207. {
  208. // IP和流量控制
  209. string ignorePercentageCity = config.cpsIgnorePercentageCity;
  210. string? ipInfo = IP2RegionPlus.Search(ip);
  211. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  212. {
  213. reason = $"地区控制:{regionInfo}";
  214. return true;
  215. }
  216. int limit_num = config.cps_limit_per_ip_24h;
  217. if (limit_num > 0)
  218. {
  219. int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
  220. if (num > limit_num)
  221. {
  222. reason = "IP控制";
  223. return true;
  224. }
  225. num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
  226. if (num > limit_num)
  227. {
  228. reason = "OAID控制";
  229. return true;
  230. }
  231. }
  232. }
  233. catch (Exception ex)
  234. {
  235. reason = "配置异常";
  236. }
  237. return false;
  238. }
  239. #endregion
  240. }
  241. }