UnionCpsCore.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. if (AlimamaPlus.FilterRiskLink(ip, oaid, content, out reason) ||
  245. AlimamaPlus.ShouldIgnoreRequest(ip, oaid, string.Empty, 0, out reason))
  246. {
  247. result.success = false;
  248. result.message = "放弃转链";
  249. result.reason = reason;
  250. _ = TkLogCore.CpsLogAsync(result);
  251. return new APIResult(new
  252. {
  253. result.success,
  254. result.message,
  255. result.reason,
  256. channel = result.channel.ToString(),
  257. });
  258. }
  259. var account = await TkPoolCore.GetOneAsync(TkPoolCore.TkAction.parse, false, string.Empty, 0);
  260. if (account == null)
  261. {
  262. result.success = false;
  263. result.message = "放弃转链";
  264. result.reason = "没有匹配账号";
  265. _ = TkLogCore.CpsLogAsync(result);
  266. return new APIResult(new
  267. {
  268. result.success,
  269. result.message,
  270. result.reason,
  271. channel = result.channel.ToString(),
  272. });
  273. }
  274. result.accountId = account.id;
  275. result.accountName = account.name;
  276. alimama = new AlimamaPlus(account);
  277. //result = await alimama.UnionCoupon2Async(content, result);
  278. TkDataDTO data = new();
  279. data = await alimama.alimamaParseAsync(content, data);
  280. result.success = data.success;
  281. result.reason = data.reason;
  282. result.message = data.success ? "OK" : data.message;
  283. if (result.success)
  284. {
  285. result.token = data.taoToken;
  286. //result.deeplink_url = data.deeplink_url;
  287. result.couponAmount = data.couponAmount;
  288. result.shortLinkurl = data.shortLinkurl;
  289. result.itemId = data.itemId;
  290. }
  291. }
  292. catch (Exception ex)
  293. {
  294. if (ex.Message.Contains("was canceled"))
  295. {
  296. result.success = false;
  297. result.message = "放弃转链";
  298. result.reason = "请求超时";
  299. }
  300. else
  301. {
  302. _ = new LoggerLibrary("unionCoupon", "tb_error")
  303. .Info(ip, oaid)
  304. .Info(content)
  305. .Info(ex.Message, ex.StackTrace)
  306. .SaveAsync();
  307. NotifyCore.Notify(new NifyMessage
  308. {
  309. message = $"【转链异常TB】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
  310. priority = NifyMessagePriority.high,
  311. tags = ["red_circle"]
  312. });
  313. result.success = false;
  314. result.message = "放弃转链";
  315. result.reason = "转链接口异常";
  316. }
  317. }
  318. _ = TkLogCore.CpsLogAsync(result);
  319. return new APIResult(new
  320. {
  321. result.success,
  322. result.message,
  323. result.title,
  324. result.sub_title,
  325. result.deeplink_url,
  326. result.token,
  327. result.channel,
  328. });
  329. }
  330. //public static async Task<APIResult> EleAsync(string ip, string oaid)
  331. //{
  332. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
  333. // var config = TkConfigCore.Get();
  334. // //============================== 放弃转链-地区过滤 ==============================
  335. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  336. // {
  337. // result.success = false;
  338. // result.message = "流量控制";
  339. // result.reason = reason;
  340. // _ = TkLogCore.CpsLogAsync(result);
  341. // return new APIResult(new
  342. // {
  343. // result.success,
  344. // result.message,
  345. // result.reason,
  346. // channel = result.channel.ToString(),
  347. // });
  348. // }
  349. // var account = ElePoolCore.GetOne();
  350. // if (account == null)
  351. // {
  352. // result.success = false;
  353. // result.message = "放弃转链";
  354. // result.reason = "没有匹配账号";
  355. // _ = TkLogCore.CpsLogAsync(result);
  356. // return new APIResult(new
  357. // {
  358. // result.success,
  359. // result.message,
  360. // result.reason,
  361. // channel = result.channel.ToString(),
  362. // });
  363. // }
  364. // result.accountId = account.id;
  365. // result.accountName = account.name;
  366. // result.deeplink_url = account.deeplink;
  367. // result.success = true;
  368. // result.message = "OK";
  369. // _ = TkLogCore.CpsLogAsync(result);
  370. // return new APIResult(new
  371. // {
  372. // result.success,
  373. // result.message,
  374. // result.deeplink_url,
  375. // channel = result.channel.ToString(),
  376. // });
  377. //}
  378. //public static async Task<APIResult> MeituanAsync(string ip, string oaid)
  379. //{
  380. // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
  381. // var config = TkConfigCore.Get();
  382. // //============================== 放弃转链-地区过滤 ==============================
  383. // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  384. // {
  385. // result.success = false;
  386. // result.message = "流量控制";
  387. // result.reason = reason;
  388. // _ = TkLogCore.CpsLogAsync(result);
  389. // return new APIResult(new
  390. // {
  391. // result.success,
  392. // result.message,
  393. // result.reason,
  394. // channel = result.channel.ToString(),
  395. // });
  396. // }
  397. // var account = MeituanPoolCore.GetOne();
  398. // if (account == null)
  399. // {
  400. // result.success = false;
  401. // result.message = "放弃转链";
  402. // result.reason = "没有匹配账号";
  403. // _ = TkLogCore.CpsLogAsync(result);
  404. // return new APIResult(new
  405. // {
  406. // result.success,
  407. // result.message,
  408. // result.reason,
  409. // channel = result.channel.ToString(),
  410. // });
  411. // }
  412. // result.accountId = account.id;
  413. // result.accountName = account.name;
  414. // result.deeplink_url = account.deeplink;
  415. // result.success = true;
  416. // result.message = "OK";
  417. // _ = TkLogCore.CpsLogAsync(result);
  418. // return new APIResult(new
  419. // {
  420. // result.success,
  421. // result.message,
  422. // result.deeplink_url,
  423. // channel = result.channel.ToString(),
  424. // });
  425. //}
  426. public static bool CpsShouldIgnoreRequest(TkConfigDTO config, string channel, string ip, string oaid, out string reason)
  427. {
  428. reason = string.Empty;
  429. try
  430. {
  431. // IP和流量控制
  432. string ignorePercentageCity = config.cpsIgnorePercentageCity;
  433. string? ipInfo = IP2RegionPlus.Search(ip);
  434. if (!TestParseCore.InWhitelist(ip, oaid) && AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  435. {
  436. reason = $"地区控制:{regionInfo}";
  437. return true;
  438. }
  439. int limit_num = config.cps_limit_per_ip_24h;
  440. if (limit_num > 0)
  441. {
  442. int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
  443. if (num > limit_num)
  444. {
  445. reason = "IP控制";
  446. return true;
  447. }
  448. }
  449. limit_num = config.cps_limit_per_oaid_24h;
  450. if (limit_num > 0)
  451. {
  452. int num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
  453. if (num > limit_num)
  454. {
  455. reason = "OAID控制";
  456. return true;
  457. }
  458. }
  459. }
  460. catch (Exception ex)
  461. {
  462. reason = "配置异常";
  463. }
  464. return false;
  465. }
  466. #endregion
  467. }
  468. }