UnionCpsCore.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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> TbtCouponAsync(string content, string channel, string ip, string oaid)
  110. {
  111. UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
  112. if (string.IsNullOrEmpty(result.channel))
  113. {
  114. result.success = false;
  115. result.message = "放弃转链";
  116. result.reason = "无效渠道";
  117. _ = TkLogCore.CpsLogAsync(result);
  118. return new APIResult(new
  119. {
  120. result.success,
  121. result.message,
  122. result.reason,
  123. channel,
  124. });
  125. }
  126. AlimamaPlus alimama = null;
  127. content = content.UrlDecode();
  128. content = AlimamaPlus.FakeItemUrl(content, string.Empty);
  129. result.rawContent = content;
  130. if (string.IsNullOrEmpty(content))
  131. {
  132. result.success = false;
  133. result.message = "放弃转链";
  134. result.reason = "无效的参数";
  135. result.rawContent = content;
  136. _ = TkLogCore.CpsLogAsync(result);
  137. return new APIResult(new
  138. {
  139. result.success,
  140. result.reason,
  141. result.message,
  142. channel = result.channel.ToString(),
  143. });
  144. }
  145. try
  146. {
  147. string reason;
  148. if (AlimamaPlus.FilterRiskLink(ip, oaid, content, out reason) ||
  149. AlimamaPlus.ShouldIgnoreRequest(ip, oaid, out reason))
  150. {
  151. result.success = false;
  152. result.message = "放弃转链";
  153. result.reason = reason;
  154. _ = TkLogCore.CpsLogAsync(result);
  155. return new APIResult(new
  156. {
  157. result.success,
  158. result.message,
  159. result.reason,
  160. channel = result.channel.ToString(),
  161. });
  162. }
  163. var account = TkPoolCore.GetOne(TkPoolCore.TkAction.parse);
  164. if (account == null)
  165. {
  166. result.success = false;
  167. result.message = "放弃转链";
  168. result.reason = "没有匹配账号";
  169. _ = TkLogCore.CpsLogAsync(result);
  170. return new APIResult(new
  171. {
  172. result.success,
  173. result.message,
  174. result.reason,
  175. channel = result.channel.ToString(),
  176. });
  177. }
  178. result.accountId = account.id;
  179. result.accountName = account.name;
  180. alimama = new AlimamaPlus(account);
  181. //result = await alimama.UnionCoupon2Async(content, result);
  182. TkDataDTO data = new();
  183. data = await alimama.alimamaParseAsync(content, data);
  184. result.success = data.success;
  185. result.reason = data.reason;
  186. result.message = data.success ? "OK" : data.message;
  187. if (result.success)
  188. {
  189. result.token = data.taoToken;
  190. //result.deeplink_url = data.deeplink_url;
  191. result.couponAmount = data.couponAmount;
  192. result.itemId = data.itemId;
  193. }
  194. }
  195. catch (Exception ex)
  196. {
  197. if (ex.Message.Contains("was canceled"))
  198. {
  199. result.success = false;
  200. result.message = "放弃转链";
  201. result.reason = "请求超时";
  202. }
  203. else
  204. {
  205. _ = new LoggerLibrary("unionCoupon", "tb_error")
  206. .Info(ip, oaid)
  207. .Info(content)
  208. .Info(ex.Message, ex.StackTrace)
  209. .SaveAsync();
  210. NotifyCore.Notify(new NifyMessage
  211. {
  212. message = $"【转链异常TB】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  213. priority = NifyMessagePriority.high,
  214. tags = ["red_circle"]
  215. });
  216. result.success = false;
  217. result.message = "放弃转链";
  218. result.reason = "转链接口异常";
  219. }
  220. }
  221. _ = TkLogCore.CpsLogAsync(result);
  222. return new APIResult(new
  223. {
  224. result.success,
  225. result.message,
  226. result.title,
  227. result.deeplink_url,
  228. result.token,
  229. result.channel,
  230. });
  231. }
  232. //public static async Task<APIResult> EleAsync(string ip, string oaid)
  233. //{
  234. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
  235. // var config = TkConfigCore.Get();
  236. // //============================== 放弃转链-地区过滤 ==============================
  237. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  238. // {
  239. // result.success = false;
  240. // result.message = "流量控制";
  241. // result.reason = reason;
  242. // _ = TkLogCore.CpsLogAsync(result);
  243. // return new APIResult(new
  244. // {
  245. // result.success,
  246. // result.message,
  247. // result.reason,
  248. // channel = result.channel.ToString(),
  249. // });
  250. // }
  251. // var account = ElePoolCore.GetOne();
  252. // if (account == null)
  253. // {
  254. // result.success = false;
  255. // result.message = "放弃转链";
  256. // result.reason = "没有匹配账号";
  257. // _ = TkLogCore.CpsLogAsync(result);
  258. // return new APIResult(new
  259. // {
  260. // result.success,
  261. // result.message,
  262. // result.reason,
  263. // channel = result.channel.ToString(),
  264. // });
  265. // }
  266. // result.accountId = account.id;
  267. // result.accountName = account.name;
  268. // result.deeplink_url = account.deeplink;
  269. // result.success = true;
  270. // result.message = "OK";
  271. // _ = TkLogCore.CpsLogAsync(result);
  272. // return new APIResult(new
  273. // {
  274. // result.success,
  275. // result.message,
  276. // result.deeplink_url,
  277. // channel = result.channel.ToString(),
  278. // });
  279. //}
  280. //public static async Task<APIResult> MeituanAsync(string ip, string oaid)
  281. //{
  282. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
  283. // var config = TkConfigCore.Get();
  284. // //============================== 放弃转链-地区过滤 ==============================
  285. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  286. // {
  287. // result.success = false;
  288. // result.message = "流量控制";
  289. // result.reason = reason;
  290. // _ = TkLogCore.CpsLogAsync(result);
  291. // return new APIResult(new
  292. // {
  293. // result.success,
  294. // result.message,
  295. // result.reason,
  296. // channel = result.channel.ToString(),
  297. // });
  298. // }
  299. // var account = MeituanPoolCore.GetOne();
  300. // if (account == null)
  301. // {
  302. // result.success = false;
  303. // result.message = "放弃转链";
  304. // result.reason = "没有匹配账号";
  305. // _ = TkLogCore.CpsLogAsync(result);
  306. // return new APIResult(new
  307. // {
  308. // result.success,
  309. // result.message,
  310. // result.reason,
  311. // channel = result.channel.ToString(),
  312. // });
  313. // }
  314. // result.accountId = account.id;
  315. // result.accountName = account.name;
  316. // result.deeplink_url = account.deeplink;
  317. // result.success = true;
  318. // result.message = "OK";
  319. // _ = TkLogCore.CpsLogAsync(result);
  320. // return new APIResult(new
  321. // {
  322. // result.success,
  323. // result.message,
  324. // result.deeplink_url,
  325. // channel = result.channel.ToString(),
  326. // });
  327. //}
  328. public static bool CpsShouldIgnoreRequest(TkConfigDTO config, string channel, string ip, string oaid, out string reason)
  329. {
  330. reason = string.Empty;
  331. try
  332. {
  333. // IP和流量控制
  334. string ignorePercentageCity = config.cpsIgnorePercentageCity;
  335. string? ipInfo = IP2RegionPlus.Search(ip);
  336. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  337. {
  338. reason = $"地区控制:{regionInfo}";
  339. return true;
  340. }
  341. int limit_num = config.cps_limit_per_ip_24h;
  342. if (limit_num > 0)
  343. {
  344. int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
  345. if (num > limit_num)
  346. {
  347. reason = "IP控制";
  348. return true;
  349. }
  350. }
  351. limit_num = config.cps_limit_per_oaid_24h;
  352. if (limit_num > 0)
  353. {
  354. int num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
  355. if (num > limit_num)
  356. {
  357. reason = "OAID控制";
  358. return true;
  359. }
  360. }
  361. }
  362. catch (Exception ex)
  363. {
  364. reason = "配置异常";
  365. }
  366. return false;
  367. }
  368. #endregion
  369. }
  370. }