UnionCpsCore.cs 17 KB

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