UnionCpsCore.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. using COSXML.Network;
  22. namespace molilian.core
  23. {
  24. public partial class UnionCpsCore
  25. {
  26. private static string _end_point;
  27. static UnionCpsCore()
  28. {
  29. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  30. }
  31. public static UnionCpsDTO GetFormattedObject(string channel, string ip = "", string oaid = "")
  32. {
  33. return new()
  34. {
  35. channel = channel,
  36. end_point = _end_point,
  37. ip = ip,
  38. oaid = oaid
  39. };
  40. }
  41. #region CPS 相关
  42. public static async Task<APIResult> UnionCpsAsync(string channelStr, string style, string ip, string oaid)
  43. {
  44. var channel_list = channelStr.Split(',');
  45. List<dynamic> list = new List<dynamic>();
  46. foreach (var channel in channel_list)
  47. {
  48. var result = await CpsCouponAsync(channel, style, ip, oaid);
  49. list.Add(new
  50. {
  51. result.success,
  52. result.message,
  53. result.title,
  54. result.sub_title,
  55. result.deeplink_url,
  56. result.token,
  57. result.channel,
  58. });
  59. }
  60. return new APIResult(new
  61. {
  62. data = list,
  63. });
  64. }
  65. public static async Task<UnionCpsDTO> CpsCouponAsync(string channel, string style, string ip, string oaid)
  66. {
  67. UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
  68. if (string.IsNullOrEmpty(result.channel))
  69. {
  70. result.success = false;
  71. result.message = "放弃转链";
  72. result.reason = "无效渠道";
  73. _ = TkLogCore.CpsLogAsync(result);
  74. return result;
  75. }
  76. var config = TkConfigCore.Get();
  77. //============================== 放弃转链-地区过滤 ==============================
  78. if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  79. {
  80. result.success = false;
  81. result.message = "流量控制";
  82. result.reason = reason;
  83. _ = TkLogCore.CpsLogAsync(result);
  84. return result;
  85. }
  86. var account = CpsPoolCore.GetOne(result.channel);
  87. if (account == null)
  88. {
  89. result.success = false;
  90. result.message = "放弃转链";
  91. result.reason = "没有匹配账号";
  92. _ = TkLogCore.CpsLogAsync(result);
  93. return result;
  94. }
  95. result.accountId = account.id;
  96. result.accountName = account.name;
  97. result.deeplink_url = account.deeplink;
  98. result.token = account.token;
  99. result.category = account.category;
  100. result.pid = account.pid;
  101. result.title = account.title;
  102. var style_record = CpsPoolCore.GetTitle(account, style);
  103. if (style_record != null)
  104. {
  105. result.title = style_record.title;
  106. result.sub_title = style_record.sub_title;
  107. }
  108. result.success = true;
  109. result.message = "OK";
  110. _ = TkLogCore.CpsLogAsync(result);
  111. return result;
  112. //return new APIResult(new
  113. //{
  114. // result.success,
  115. // result.message,
  116. // result.reason,
  117. // result.channel,
  118. //});
  119. //return new APIResult(new
  120. //{
  121. // result.success,
  122. // result.message,
  123. // result.title,
  124. // result.deeplink_url,
  125. // result.token,
  126. // result.channel,
  127. //});
  128. }
  129. public static async Task<APIResult> CpsCouponAsync22(string channel, string style, string ip, string oaid)
  130. {
  131. UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
  132. if (string.IsNullOrEmpty(result.channel))
  133. {
  134. result.success = false;
  135. result.message = "放弃转链";
  136. result.reason = "无效渠道";
  137. _ = TkLogCore.CpsLogAsync(result);
  138. return new APIResult(new
  139. {
  140. result.success,
  141. result.message,
  142. result.reason,
  143. channel,
  144. });
  145. }
  146. var config = TkConfigCore.Get();
  147. //============================== 放弃转链-地区过滤 ==============================
  148. if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  149. {
  150. result.success = false;
  151. result.message = "流量控制";
  152. result.reason = reason;
  153. _ = TkLogCore.CpsLogAsync(result);
  154. return new APIResult(new
  155. {
  156. result.success,
  157. result.message,
  158. result.reason,
  159. result.channel,
  160. });
  161. }
  162. var account = CpsPoolCore.GetOne(result.channel);
  163. if (account == null)
  164. {
  165. result.success = false;
  166. result.message = "放弃转链";
  167. result.reason = "没有匹配账号";
  168. _ = TkLogCore.CpsLogAsync(result);
  169. return new APIResult(new
  170. {
  171. result.success,
  172. result.message,
  173. result.reason,
  174. result.channel,
  175. });
  176. }
  177. result.accountId = account.id;
  178. result.accountName = account.name;
  179. result.deeplink_url = account.deeplink;
  180. result.token = account.token;
  181. result.category = account.category;
  182. result.pid = account.pid;
  183. result.title = account.title;
  184. var style_record = CpsPoolCore.GetTitle(account, style);
  185. if (style_record != null)
  186. {
  187. result.title = style_record.title;
  188. result.sub_title = style_record.sub_title;
  189. }
  190. result.success = true;
  191. result.message = "OK";
  192. _ = TkLogCore.CpsLogAsync(result);
  193. return new APIResult(new
  194. {
  195. result.success,
  196. result.message,
  197. result.title,
  198. result.sub_title,
  199. result.deeplink_url,
  200. result.token,
  201. result.channel,
  202. });
  203. }
  204. public static async Task<APIResult> TbtCouponAsync(string content, string channel, string ip, string oaid)
  205. {
  206. UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
  207. if (string.IsNullOrEmpty(result.channel))
  208. {
  209. result.success = false;
  210. result.message = "放弃转链";
  211. result.reason = "无效渠道";
  212. _ = TkLogCore.CpsLogAsync(result);
  213. return new APIResult(new
  214. {
  215. result.success,
  216. result.message,
  217. result.reason,
  218. channel,
  219. });
  220. }
  221. AlimamaPlus alimama = null;
  222. content = content.UrlDecode();
  223. content = AlimamaPlus.FakeItemUrl(content, string.Empty);
  224. result.rawContent = content;
  225. if (string.IsNullOrEmpty(content))
  226. {
  227. result.success = false;
  228. result.message = "放弃转链";
  229. result.reason = "无效的参数";
  230. result.rawContent = content;
  231. _ = TkLogCore.CpsLogAsync(result);
  232. return new APIResult(new
  233. {
  234. result.success,
  235. result.reason,
  236. result.message,
  237. channel = result.channel.ToString(),
  238. });
  239. }
  240. try
  241. {
  242. string reason;
  243. if (AlimamaPlus.FilterRiskLink(ip, oaid, content, out reason) ||
  244. AlimamaPlus.ShouldIgnoreRequest(ip, oaid, string.Empty, 0, out reason))
  245. {
  246. result.success = false;
  247. result.message = "放弃转链";
  248. result.reason = reason;
  249. _ = TkLogCore.CpsLogAsync(result);
  250. return new APIResult(new
  251. {
  252. result.success,
  253. result.message,
  254. result.reason,
  255. channel = result.channel.ToString(),
  256. });
  257. }
  258. var account = await TkPoolCore.GetOneAsync(TkPoolCore.TkAction.parse, false, string.Empty, 0);
  259. if (account == null)
  260. {
  261. result.success = false;
  262. result.message = "放弃转链";
  263. result.reason = "没有匹配账号";
  264. _ = TkLogCore.CpsLogAsync(result);
  265. return new APIResult(new
  266. {
  267. result.success,
  268. result.message,
  269. result.reason,
  270. channel = result.channel.ToString(),
  271. });
  272. }
  273. result.accountId = account.id;
  274. result.accountName = account.name;
  275. alimama = new AlimamaPlus(account);
  276. //result = await alimama.UnionCoupon2Async(content, result);
  277. TkDataDTO data = new();
  278. data = await alimama.alimamaParseAsync(content, data);
  279. result.success = data.success;
  280. result.reason = data.reason;
  281. result.message = data.success ? "OK" : data.message;
  282. if (result.success)
  283. {
  284. result.token = data.taoToken;
  285. //result.deeplink_url = data.deeplink_url;
  286. result.couponAmount = data.couponAmount;
  287. result.itemId = data.itemId;
  288. }
  289. }
  290. catch (Exception ex)
  291. {
  292. if (ex.Message.Contains("was canceled"))
  293. {
  294. result.success = false;
  295. result.message = "放弃转链";
  296. result.reason = "请求超时";
  297. }
  298. else
  299. {
  300. _ = new LoggerLibrary("unionCoupon", "tb_error")
  301. .Info(ip, oaid)
  302. .Info(content)
  303. .Info(ex.Message, ex.StackTrace)
  304. .SaveAsync();
  305. NotifyCore.Notify(new NifyMessage
  306. {
  307. message = $"【转链异常TB】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  308. priority = NifyMessagePriority.high,
  309. tags = ["red_circle"]
  310. });
  311. result.success = false;
  312. result.message = "放弃转链";
  313. result.reason = "转链接口异常";
  314. }
  315. }
  316. _ = TkLogCore.CpsLogAsync(result);
  317. return new APIResult(new
  318. {
  319. result.success,
  320. result.message,
  321. result.title,
  322. result.sub_title,
  323. result.deeplink_url,
  324. result.token,
  325. result.channel,
  326. });
  327. }
  328. //public static async Task<APIResult> EleAsync(string ip, string oaid)
  329. //{
  330. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
  331. // var config = TkConfigCore.Get();
  332. // //============================== 放弃转链-地区过滤 ==============================
  333. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  334. // {
  335. // result.success = false;
  336. // result.message = "流量控制";
  337. // result.reason = reason;
  338. // _ = TkLogCore.CpsLogAsync(result);
  339. // return new APIResult(new
  340. // {
  341. // result.success,
  342. // result.message,
  343. // result.reason,
  344. // channel = result.channel.ToString(),
  345. // });
  346. // }
  347. // var account = ElePoolCore.GetOne();
  348. // if (account == null)
  349. // {
  350. // result.success = false;
  351. // result.message = "放弃转链";
  352. // result.reason = "没有匹配账号";
  353. // _ = TkLogCore.CpsLogAsync(result);
  354. // return new APIResult(new
  355. // {
  356. // result.success,
  357. // result.message,
  358. // result.reason,
  359. // channel = result.channel.ToString(),
  360. // });
  361. // }
  362. // result.accountId = account.id;
  363. // result.accountName = account.name;
  364. // result.deeplink_url = account.deeplink;
  365. // result.success = true;
  366. // result.message = "OK";
  367. // _ = TkLogCore.CpsLogAsync(result);
  368. // return new APIResult(new
  369. // {
  370. // result.success,
  371. // result.message,
  372. // result.deeplink_url,
  373. // channel = result.channel.ToString(),
  374. // });
  375. //}
  376. //public static async Task<APIResult> MeituanAsync(string ip, string oaid)
  377. //{
  378. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
  379. // var config = TkConfigCore.Get();
  380. // //============================== 放弃转链-地区过滤 ==============================
  381. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  382. // {
  383. // result.success = false;
  384. // result.message = "流量控制";
  385. // result.reason = reason;
  386. // _ = TkLogCore.CpsLogAsync(result);
  387. // return new APIResult(new
  388. // {
  389. // result.success,
  390. // result.message,
  391. // result.reason,
  392. // channel = result.channel.ToString(),
  393. // });
  394. // }
  395. // var account = MeituanPoolCore.GetOne();
  396. // if (account == null)
  397. // {
  398. // result.success = false;
  399. // result.message = "放弃转链";
  400. // result.reason = "没有匹配账号";
  401. // _ = TkLogCore.CpsLogAsync(result);
  402. // return new APIResult(new
  403. // {
  404. // result.success,
  405. // result.message,
  406. // result.reason,
  407. // channel = result.channel.ToString(),
  408. // });
  409. // }
  410. // result.accountId = account.id;
  411. // result.accountName = account.name;
  412. // result.deeplink_url = account.deeplink;
  413. // result.success = true;
  414. // result.message = "OK";
  415. // _ = TkLogCore.CpsLogAsync(result);
  416. // return new APIResult(new
  417. // {
  418. // result.success,
  419. // result.message,
  420. // result.deeplink_url,
  421. // channel = result.channel.ToString(),
  422. // });
  423. //}
  424. public static bool CpsShouldIgnoreRequest(TkConfigDTO config, string channel, string ip, string oaid, out string reason)
  425. {
  426. reason = string.Empty;
  427. try
  428. {
  429. // IP和流量控制
  430. string ignorePercentageCity = config.cpsIgnorePercentageCity;
  431. string? ipInfo = IP2RegionPlus.Search(ip);
  432. if (!TestParseCore.InWhitelist(ip, oaid) && AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  433. {
  434. reason = $"地区控制:{regionInfo}";
  435. return true;
  436. }
  437. int limit_num = config.cps_limit_per_ip_24h;
  438. if (limit_num > 0)
  439. {
  440. int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
  441. if (num > limit_num)
  442. {
  443. reason = "IP控制";
  444. return true;
  445. }
  446. }
  447. limit_num = config.cps_limit_per_oaid_24h;
  448. if (limit_num > 0)
  449. {
  450. int num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
  451. if (num > limit_num)
  452. {
  453. reason = "OAID控制";
  454. return true;
  455. }
  456. }
  457. }
  458. catch (Exception ex)
  459. {
  460. reason = "配置异常";
  461. }
  462. return false;
  463. }
  464. #endregion
  465. }
  466. }