UnionCpsCore.cs 17 KB

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