UnionCpsCore.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.title = account.title;
  96. result.success = true;
  97. result.message = "OK";
  98. _ = TkLogCore.CpsLogAsync(result);
  99. return new APIResult(new
  100. {
  101. result.success,
  102. result.message,
  103. result.deeplink_url,
  104. result.token,
  105. result.channel,
  106. });
  107. }
  108. //public static async Task<APIResult> EleAsync(string ip, string oaid)
  109. //{
  110. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
  111. // var config = TkConfigCore.Get();
  112. // //============================== 放弃转链-地区过滤 ==============================
  113. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  114. // {
  115. // result.success = false;
  116. // result.message = "流量控制";
  117. // result.reason = reason;
  118. // _ = TkLogCore.CpsLogAsync(result);
  119. // return new APIResult(new
  120. // {
  121. // result.success,
  122. // result.message,
  123. // result.reason,
  124. // channel = result.channel.ToString(),
  125. // });
  126. // }
  127. // var account = ElePoolCore.GetOne();
  128. // if (account == null)
  129. // {
  130. // result.success = false;
  131. // result.message = "放弃转链";
  132. // result.reason = "没有匹配账号";
  133. // _ = TkLogCore.CpsLogAsync(result);
  134. // return new APIResult(new
  135. // {
  136. // result.success,
  137. // result.message,
  138. // result.reason,
  139. // channel = result.channel.ToString(),
  140. // });
  141. // }
  142. // result.accountId = account.id;
  143. // result.accountName = account.name;
  144. // result.deeplink_url = account.deeplink;
  145. // result.success = true;
  146. // result.message = "OK";
  147. // _ = TkLogCore.CpsLogAsync(result);
  148. // return new APIResult(new
  149. // {
  150. // result.success,
  151. // result.message,
  152. // result.deeplink_url,
  153. // channel = result.channel.ToString(),
  154. // });
  155. //}
  156. //public static async Task<APIResult> MeituanAsync(string ip, string oaid)
  157. //{
  158. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
  159. // var config = TkConfigCore.Get();
  160. // //============================== 放弃转链-地区过滤 ==============================
  161. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  162. // {
  163. // result.success = false;
  164. // result.message = "流量控制";
  165. // result.reason = reason;
  166. // _ = TkLogCore.CpsLogAsync(result);
  167. // return new APIResult(new
  168. // {
  169. // result.success,
  170. // result.message,
  171. // result.reason,
  172. // channel = result.channel.ToString(),
  173. // });
  174. // }
  175. // var account = MeituanPoolCore.GetOne();
  176. // if (account == null)
  177. // {
  178. // result.success = false;
  179. // result.message = "放弃转链";
  180. // result.reason = "没有匹配账号";
  181. // _ = TkLogCore.CpsLogAsync(result);
  182. // return new APIResult(new
  183. // {
  184. // result.success,
  185. // result.message,
  186. // result.reason,
  187. // channel = result.channel.ToString(),
  188. // });
  189. // }
  190. // result.accountId = account.id;
  191. // result.accountName = account.name;
  192. // result.deeplink_url = account.deeplink;
  193. // result.success = true;
  194. // result.message = "OK";
  195. // _ = TkLogCore.CpsLogAsync(result);
  196. // return new APIResult(new
  197. // {
  198. // result.success,
  199. // result.message,
  200. // result.deeplink_url,
  201. // channel = result.channel.ToString(),
  202. // });
  203. //}
  204. public static bool CpsShouldIgnoreRequest(TkConfigDTO config, string channel, string ip, string oaid, out string reason)
  205. {
  206. reason = string.Empty;
  207. try
  208. {
  209. // IP和流量控制
  210. string ignorePercentageCity = config.cpsIgnorePercentageCity;
  211. string? ipInfo = IP2RegionPlus.Search(ip);
  212. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  213. {
  214. reason = $"地区控制:{regionInfo}";
  215. return true;
  216. }
  217. int limit_num = config.cps_limit_per_ip_24h;
  218. if (limit_num > 0)
  219. {
  220. int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
  221. if (num > limit_num)
  222. {
  223. reason = "IP控制";
  224. return true;
  225. }
  226. num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
  227. if (num > limit_num)
  228. {
  229. reason = "OAID控制";
  230. return true;
  231. }
  232. }
  233. }
  234. catch (Exception ex)
  235. {
  236. reason = "配置异常";
  237. }
  238. return false;
  239. }
  240. #endregion
  241. }
  242. }