UnionCpsCore.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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.shortLinkurl = account.shortLinkurl;
  99. result.token = account.token;
  100. result.category = account.category;
  101. result.pid = account.pid;
  102. result.title = account.title;
  103. var style_record = CpsPoolCore.GetTitle(account, style);
  104. if (style_record != null)
  105. {
  106. result.title = style_record.title;
  107. result.sub_title = style_record.sub_title;
  108. }
  109. result.success = true;
  110. result.message = "OK";
  111. _ = TkLogCore.CpsLogAsync(result);
  112. return result;
  113. //return new APIResult(new
  114. //{
  115. // result.success,
  116. // result.message,
  117. // result.reason,
  118. // result.channel,
  119. //});
  120. //return new APIResult(new
  121. //{
  122. // result.success,
  123. // result.message,
  124. // result.title,
  125. // result.deeplink_url,
  126. // result.token,
  127. // result.channel,
  128. //});
  129. }
  130. public static async Task<APIResult> CpsCouponAsync22(string channel, string style, string ip, string oaid)
  131. {
  132. UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
  133. if (string.IsNullOrEmpty(result.channel))
  134. {
  135. result.success = false;
  136. result.message = "放弃转链";
  137. result.reason = "无效渠道";
  138. _ = TkLogCore.CpsLogAsync(result);
  139. return new APIResult(new
  140. {
  141. result.success,
  142. result.message,
  143. result.reason,
  144. channel,
  145. });
  146. }
  147. var config = TkConfigCore.Get();
  148. //============================== 放弃转链-地区过滤 ==============================
  149. if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string 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. result.channel,
  161. });
  162. }
  163. var account = CpsPoolCore.GetOne(result.channel);
  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. result.channel,
  176. });
  177. }
  178. result.accountId = account.id;
  179. result.accountName = account.name;
  180. result.deeplink_url = account.deeplink;
  181. result.token = account.token;
  182. result.category = account.category;
  183. result.pid = account.pid;
  184. result.title = account.title;
  185. var style_record = CpsPoolCore.GetTitle(account, style);
  186. if (style_record != null)
  187. {
  188. result.title = style_record.title;
  189. result.sub_title = style_record.sub_title;
  190. }
  191. result.success = true;
  192. result.message = "OK";
  193. _ = TkLogCore.CpsLogAsync(result);
  194. return new APIResult(new
  195. {
  196. result.success,
  197. result.message,
  198. result.title,
  199. result.sub_title,
  200. result.deeplink_url,
  201. result.token,
  202. result.channel,
  203. });
  204. }
  205. public static async Task<APIResult> TbtCouponAsync(string content, string channel, string ip, string oaid)
  206. {
  207. UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
  208. if (string.IsNullOrEmpty(result.channel))
  209. {
  210. result.success = false;
  211. result.message = "放弃转链";
  212. result.reason = "无效渠道";
  213. _ = TkLogCore.CpsLogAsync(result);
  214. return new APIResult(new
  215. {
  216. result.success,
  217. result.message,
  218. result.reason,
  219. channel,
  220. });
  221. }
  222. AlimamaPlus alimama = null;
  223. content = content.UrlDecode();
  224. content = AlimamaPlus.FakeItemUrl(content, string.Empty);
  225. result.rawContent = content;
  226. if (string.IsNullOrEmpty(content))
  227. {
  228. result.success = false;
  229. result.message = "放弃转链";
  230. result.reason = "无效的参数";
  231. result.rawContent = content;
  232. _ = TkLogCore.CpsLogAsync(result);
  233. return new APIResult(new
  234. {
  235. result.success,
  236. result.reason,
  237. result.message,
  238. channel = result.channel.ToString(),
  239. });
  240. }
  241. try
  242. {
  243. string reason;
  244. UnionParseRequest request = new()
  245. {
  246. Ip = ip,
  247. Oaid = oaid,
  248. RiskStrategy = string.Empty,
  249. LaunchScene = 0,
  250. };
  251. if (AlimamaPlus.FilterRiskLink(ip, oaid, content, out reason) || AlimamaPlus.ShouldIgnoreRequest(request, out reason))
  252. {
  253. result.success = false;
  254. result.message = "放弃转链";
  255. result.reason = reason;
  256. _ = TkLogCore.CpsLogAsync(result);
  257. return new APIResult(new
  258. {
  259. result.success,
  260. result.message,
  261. result.reason,
  262. channel = result.channel.ToString(),
  263. });
  264. }
  265. var account = await TkPoolCore.GetOneAsync(TkPoolCore.TkAction.parse, false, string.Empty, 0);
  266. if (account == null)
  267. {
  268. result.success = false;
  269. result.message = "放弃转链";
  270. result.reason = "没有匹配账号";
  271. _ = TkLogCore.CpsLogAsync(result);
  272. return new APIResult(new
  273. {
  274. result.success,
  275. result.message,
  276. result.reason,
  277. channel = result.channel.ToString(),
  278. });
  279. }
  280. result.accountId = account.id;
  281. result.accountName = account.name;
  282. alimama = new AlimamaPlus(account);
  283. //result = await alimama.UnionCoupon2Async(content, result);
  284. TkDataDTO data = new();
  285. data = await alimama.alimamaParseAsync(content, data);
  286. result.success = data.success;
  287. result.reason = data.reason;
  288. result.message = data.success ? "OK" : data.message;
  289. if (result.success)
  290. {
  291. result.token = data.taoToken;
  292. //result.deeplink_url = data.deeplink_url;
  293. result.couponAmount = data.couponAmount;
  294. result.shortLinkurl = data.shortLinkurl;
  295. result.itemId = data.itemId;
  296. }
  297. }
  298. catch (Exception ex)
  299. {
  300. if (ex.Message.Contains("was canceled"))
  301. {
  302. result.success = false;
  303. result.message = "放弃转链";
  304. result.reason = "请求超时";
  305. }
  306. else
  307. {
  308. _ = new LoggerLibrary("unionCoupon", "tb_error")
  309. .Info(ip, oaid)
  310. .Info(content)
  311. .Info(ex.Message, ex.StackTrace)
  312. .SaveAsync();
  313. NotifyCore.Notify(new NifyMessage
  314. {
  315. message = $"【转链异常TB】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  316. priority = NifyMessagePriority.high,
  317. tags = ["red_circle"]
  318. });
  319. result.success = false;
  320. result.message = "放弃转链";
  321. result.reason = "转链接口异常";
  322. }
  323. }
  324. _ = TkLogCore.CpsLogAsync(result);
  325. return new APIResult(new
  326. {
  327. result.success,
  328. result.message,
  329. result.title,
  330. result.sub_title,
  331. result.deeplink_url,
  332. result.token,
  333. result.channel,
  334. });
  335. }
  336. //public static async Task<APIResult> EleAsync(string ip, string oaid)
  337. //{
  338. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
  339. // var config = TkConfigCore.Get();
  340. // //============================== 放弃转链-地区过滤 ==============================
  341. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  342. // {
  343. // result.success = false;
  344. // result.message = "流量控制";
  345. // result.reason = reason;
  346. // _ = TkLogCore.CpsLogAsync(result);
  347. // return new APIResult(new
  348. // {
  349. // result.success,
  350. // result.message,
  351. // result.reason,
  352. // channel = result.channel.ToString(),
  353. // });
  354. // }
  355. // var account = ElePoolCore.GetOne();
  356. // if (account == null)
  357. // {
  358. // result.success = false;
  359. // result.message = "放弃转链";
  360. // result.reason = "没有匹配账号";
  361. // _ = TkLogCore.CpsLogAsync(result);
  362. // return new APIResult(new
  363. // {
  364. // result.success,
  365. // result.message,
  366. // result.reason,
  367. // channel = result.channel.ToString(),
  368. // });
  369. // }
  370. // result.accountId = account.id;
  371. // result.accountName = account.name;
  372. // result.deeplink_url = account.deeplink;
  373. // result.success = true;
  374. // result.message = "OK";
  375. // _ = TkLogCore.CpsLogAsync(result);
  376. // return new APIResult(new
  377. // {
  378. // result.success,
  379. // result.message,
  380. // result.deeplink_url,
  381. // channel = result.channel.ToString(),
  382. // });
  383. //}
  384. //public static async Task<APIResult> MeituanAsync(string ip, string oaid)
  385. //{
  386. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
  387. // var config = TkConfigCore.Get();
  388. // //============================== 放弃转链-地区过滤 ==============================
  389. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  390. // {
  391. // result.success = false;
  392. // result.message = "流量控制";
  393. // result.reason = reason;
  394. // _ = TkLogCore.CpsLogAsync(result);
  395. // return new APIResult(new
  396. // {
  397. // result.success,
  398. // result.message,
  399. // result.reason,
  400. // channel = result.channel.ToString(),
  401. // });
  402. // }
  403. // var account = MeituanPoolCore.GetOne();
  404. // if (account == null)
  405. // {
  406. // result.success = false;
  407. // result.message = "放弃转链";
  408. // result.reason = "没有匹配账号";
  409. // _ = TkLogCore.CpsLogAsync(result);
  410. // return new APIResult(new
  411. // {
  412. // result.success,
  413. // result.message,
  414. // result.reason,
  415. // channel = result.channel.ToString(),
  416. // });
  417. // }
  418. // result.accountId = account.id;
  419. // result.accountName = account.name;
  420. // result.deeplink_url = account.deeplink;
  421. // result.success = true;
  422. // result.message = "OK";
  423. // _ = TkLogCore.CpsLogAsync(result);
  424. // return new APIResult(new
  425. // {
  426. // result.success,
  427. // result.message,
  428. // result.deeplink_url,
  429. // channel = result.channel.ToString(),
  430. // });
  431. //}
  432. public static bool CpsShouldIgnoreRequest(TkConfigDTO config, string channel, string ip, string oaid, out string reason)
  433. {
  434. reason = string.Empty;
  435. try
  436. {
  437. // IP和流量控制
  438. string ignorePercentageCity = config.cpsIgnorePercentageCity;
  439. string? ipInfo = IP2RegionPlus.Search(ip);
  440. if (!TestParseCore.InWhitelist(ip, oaid) && AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  441. {
  442. reason = $"地区控制:{regionInfo}";
  443. return true;
  444. }
  445. int limit_num = config.cps_limit_per_ip_24h;
  446. if (limit_num > 0)
  447. {
  448. int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
  449. if (num > limit_num)
  450. {
  451. reason = "IP控制";
  452. return true;
  453. }
  454. }
  455. limit_num = config.cps_limit_per_oaid_24h;
  456. if (limit_num > 0)
  457. {
  458. int num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
  459. if (num > limit_num)
  460. {
  461. reason = "OAID控制";
  462. return true;
  463. }
  464. }
  465. }
  466. catch (Exception ex)
  467. {
  468. reason = "配置异常";
  469. }
  470. return false;
  471. }
  472. #endregion
  473. }
  474. }