UnionCpsCore.cs 9.1 KB

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