parse.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. using System.Text;
  2. using dodohold.core;
  3. using System.Text.RegularExpressions;
  4. using System.Net;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using TencentCloud.Ecm.V20190719.Models;
  8. using System.Diagnostics;
  9. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  10. using System.Text.Json;
  11. using TencentCloud.Tcm.V20210413.Models;
  12. using TencentCloud.Tione.V20211111.Models;
  13. using System.Runtime.InteropServices.Marshalling;
  14. namespace molilian.core
  15. {
  16. public partial class AlimamaPlus
  17. {
  18. private string _accountName;
  19. private int _accountId;
  20. private string _company;
  21. private string _tb_token;
  22. private string _floorId;
  23. private string _refpid;
  24. private string _user_agent;
  25. private string _cookies;
  26. public string _proxyName;
  27. public WebProxy? _proxy = null;
  28. private string _api;
  29. private int _api_id = 0;
  30. public TkConfigDTO _config;
  31. private static string _end_point;
  32. private TkPoolDTO _account;
  33. static Random _random = new Random();
  34. public static string _url_pattern = @"https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
  35. static AlimamaPlus()
  36. {
  37. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  38. }
  39. public AlimamaPlus(TkPoolDTO account)
  40. {
  41. // string accountName, string tb_token, string floorId, string refpid,
  42. // string cookies, string user_agent, string nodeName
  43. _config = TkConfigCore.Get();
  44. _account = account;
  45. _accountId = account.id;
  46. _accountName = account.name;
  47. _company = account.company;
  48. _tb_token = account.tb_token;
  49. _floorId = account.floorId;
  50. _refpid = account.refpid;
  51. _cookies = account.cookies;
  52. _user_agent = account.user_agent?.Trim();
  53. if (!string.IsNullOrEmpty(account.nodeName))
  54. {
  55. _proxyName = ProxyNodesCore.GetSelectOne(account.nodeName);
  56. _proxy = ProxyNodesCore.GetOne(_proxyName);
  57. }
  58. _api = account.api;
  59. _api_id = account.api_id;
  60. }
  61. public bool ShouldIgnoreOtherAff(string content)
  62. {
  63. TkDataDTO result = new();
  64. //(_, _, var link_type, _) = InternalTextProcessing(content, ref result);
  65. InternalTextProcessing(content, ref result);
  66. return result.link_type == LinkTypeEnum.other_aff;
  67. }
  68. public async Task<TkDataDTO> UnionParseAsync(string ip, string oaid, string content, int commerceType, TkDataDTO result,
  69. bool isTaobaoUrl, string riskStrategy, int launchScene,
  70. CancellationToken cancellationToken = default,
  71. Dictionary<string, long> swData = null)
  72. {
  73. result.itemName = "点击打开淘宝APP";
  74. result = await InternalTextProcessingAsync(content, result, cancellationToken, swData);
  75. if (result.link_type == LinkTypeEnum.other_aff)
  76. {
  77. //============================== 其他推广链接 ==============================
  78. result.channel = TkChannelEnum.tb;
  79. result.channel_type = ChannelTypeEnum.tb;
  80. result.link_type = LinkTypeEnum.other_aff;
  81. result.success = false;
  82. result.message = "放弃转链";
  83. result.reason = "其他推广链接";
  84. result.subCode = TkSubCodeEnum.OtherPromo;
  85. result.accountName = string.Empty;
  86. result.content = content;
  87. //result.deeplink_url = GetDeeplink(null);
  88. //result.itemName = GetTitle(content);
  89. //if (!content.Contains("助我领红包") && !content.Contains("双11超级红包主会场"))
  90. //{
  91. // //20241108 特例
  92. // string ruleKey = $"tmp_rules:{DateTime.Now:yyyyMMdd}_2";
  93. // var count = RedisHelper.IncrBy(ruleKey);
  94. // if (DateTime.Now < DateTime.Parse("2024-11-11") && count < 1000)
  95. // {
  96. // string hb_url = "https://s.click.taobao.com/t?union_lens=lensId%3APUB%401730991020%400bbb0a39_0e75_193071ad936_c566%4001%40eyJmbG9vcklkIjozODg1Miiwiic3BtQiiI6Il9wb3J0YWxfdjJfcGFnZXNfYWN0aXZpdHlfb2ZmaWNpYWxfaW5kZXhfaHRtIn0ie%3BeventPageId%3A20150318020007201&e=m%3D2%26s%3DipuVKjYImbNw4vFB6t2Z2iperVdZeJviU%2F9%2F0taeK29yINtkUhsv0KWvzu%2FHMi3%2BFPhC4MirEWStw6LBB4w3HYXT%2BqQ2P5OIQVNSryTmvs7YddWYbxmvGTMNfihJN%2FGnAGIx0oe2X2hZfJ7ZQxC1%2FU3rpAhWgptfEBnEBk3xaGylLmcSHKfaX1CIFRJhZoJ2keMqUwSQcLRC0TzsMy8kp7Hfv3%2BBYcPW3eHn14rW1PB5SunYCHQHRbw6LwQYf%2F1JuyhJem%2BM5OoR2cbDLktPLiIOenYkFIXa2HzNl%2Bm1VXoil%2F2L3b1aA6a%2FoVHlY%2BqZ2kyLuBX5NHCAQVYCmAmuozC39JfnbJNrE%2FSR3F9BSjOYe76q6w0hyfybhwAASyyp5XMWEyz25ulHMwkL2n%2F7nNBiFmadXyjFDPu12TgSnbZtfjDbrVTLHb6MowVEq7z9LgOFRSuRLesvlLykGd2CaRk8ezhE61zXa8yR1eex6ip3KqbOh7CEmgCLaFxcnT%2BrGuviRFaBil8Mlu5kMKse3g%3D%3D";
  97. // result.deeplink_url = GetDeeplink(hb_url);
  98. // RedisHelper.Expire(ruleKey, 86400 * 10);
  99. // }
  100. //}
  101. return result;
  102. }
  103. //处理过后的正文 用于转链
  104. if (!result.success)
  105. {
  106. switch (result.link_type)
  107. {
  108. case LinkTypeEnum.profile:
  109. case LinkTypeEnum.live:
  110. case LinkTypeEnum.video:
  111. case LinkTypeEnum.note:
  112. case LinkTypeEnum.goods:
  113. case LinkTypeEnum.baseDomain:
  114. result.reason = string.Empty;
  115. break;
  116. default:
  117. result.reason = "非标准链接";
  118. result.subCode = TkSubCodeEnum.NonStdLink;
  119. return result;
  120. }
  121. }
  122. //============================== 放弃转链-地区过滤 ==============================
  123. bool is_ignore2 = FlowControlIgnoreRequest(ip, oaid, riskStrategy, launchScene, out string reason);
  124. string message = "放弃转链";
  125. if (is_ignore2)
  126. {
  127. if ("7".Equals(_account.parseEndpoint))
  128. {
  129. message = "转链失败";
  130. result.reason = "fail2";
  131. }
  132. result.success = false;
  133. result.message = message;
  134. result.reason = reason;
  135. result.subCode = TkSubCodeEnum.TrafficCtrl;
  136. return result;
  137. }
  138. switch (commerceType)
  139. {
  140. case 1:
  141. case 2:
  142. result.success = false;
  143. result.message = "放弃转链";
  144. result.reason = $"厂商放弃{commerceType}";
  145. result.subCode = TkSubCodeEnum.PartnersDrop;
  146. return result;
  147. }
  148. //2025-02-06 增加重试机制
  149. int try_count = 0;
  150. while (try_count == 0 || try_count <= _config.tk_parse_retry_count)
  151. {
  152. if (try_count >= 3) break;
  153. (int ep_id, string tkEndpoint) = await TkEndpointManager.GetConvertApiAsync(_accountId, isTaobaoUrl, _account.parseEndpoint);
  154. result.tkEndpoint = ep_id;
  155. switch (tkEndpoint)
  156. {
  157. case "moose_share":
  158. result = await endpoint_moose_share(result.content, result, cancellationToken);
  159. break;
  160. case "promo_goods":
  161. result = await endpoint_promo_goods(result.content, result, cancellationToken);
  162. break;
  163. case "tool_links":
  164. result = await endpoint_tool_links(result.content, result, cancellationToken);
  165. break;
  166. case "aliyun":
  167. result = await endpoint_aliyun(result.content, result, cancellationToken);
  168. break;
  169. case "tbk_dg":
  170. result = await endpoint_tbk_dg(result.content, result, cancellationToken);
  171. break;
  172. case "veapi":
  173. case "ddx":
  174. case "other_api":
  175. if (!string.IsNullOrEmpty(_api) && _api_id > 0)
  176. {
  177. var account = OtherApiPoolCore.Get(_api_id);
  178. if (account != null)
  179. {
  180. var core = new OtherApiPlus(account.name, account.key, account.sessionKey, tkEndpoint, account.api_url);
  181. string refpid = string.IsNullOrEmpty(account.refpid) ? _refpid : account.refpid;
  182. result = await core.UnionParseAsync(result.content, refpid, result, cancellationToken);
  183. return result;
  184. }
  185. }
  186. else
  187. {
  188. result.link_type = LinkTypeEnum.unknown;
  189. result.subCode = TkSubCodeEnum.Other;
  190. result.success = false;
  191. result.message = "放弃转链";
  192. result.reason = "接口配置失败";
  193. return result;
  194. }
  195. break;
  196. //case "ALL":
  197. // //没有可以用的接口了
  198. // result.link_type = LinkTypeEnum.unknown;
  199. // result.subCode = TkSubCodeEnum.ParseDeny;
  200. // result.success = false;
  201. // result.message = "放弃转链";
  202. // result.reason = "接口风控ALL";
  203. // return result;
  204. default:
  205. result.link_type = LinkTypeEnum.unknown;
  206. result.subCode = TkSubCodeEnum.ParseDeny;
  207. result.success = false;
  208. result.message = "放弃转链";
  209. result.reason = $"接口风控{tkEndpoint}";
  210. break;
  211. }
  212. if (!"该链接不支持转化,请更换链接尝试".Equals(result.message)) break;
  213. try_count++;
  214. }
  215. result.retry_count = try_count;
  216. result.commercial = result.success;
  217. if (!result.success)
  218. {
  219. result.itemName = "点击打开淘宝APP";
  220. }
  221. if (string.IsNullOrEmpty(result.itemName) || result.link_type == LinkTypeEnum.profile)
  222. {
  223. result.itemName = "点击打开淘宝APP";
  224. }
  225. return result;
  226. }
  227. public static bool ContainsTaobaoId(string content, string rawContent)
  228. {
  229. var match = Regex.Match(content, @"(?<=[?&]id=)\d+(?=&|$)");
  230. if (!match.Success) return false;
  231. string id = match.Value;
  232. return Regex.IsMatch(rawContent, $@"(^|\D){id}($|\D)");
  233. }
  234. public static string ReplaceUrls(string content, string shortLink)
  235. {
  236. if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(shortLink)) return content;
  237. string result = content;
  238. Regex regex = new(_url_pattern);
  239. MatchCollection matches = regex.Matches(content);
  240. foreach (Match m in matches.Cast<Match>())
  241. {
  242. string url = m.Value;
  243. if (url.Contains("https://s.tb.cn/") ||
  244. url.Contains("http://s.tb.cn/") ||
  245. url.Contains("http://m.tb.cn/") ||
  246. url.Contains("http://m.tb.cn/") ||
  247. url.Contains("http://e.tb.cn/") ||
  248. url.Contains("https://e.tb.cn/"))
  249. {
  250. result = result.Replace(url, shortLink);
  251. }
  252. }
  253. return result;
  254. }
  255. public static string RemoveUrls(string content)
  256. {
  257. if (string.IsNullOrEmpty(content)) return content;
  258. string result = content;
  259. Regex regex = new(_url_pattern);
  260. MatchCollection matches = regex.Matches(content);
  261. foreach (Match m in matches.Cast<Match>())
  262. {
  263. string url = m.Value;
  264. result = result.Replace(url, string.Empty);
  265. }
  266. return result;
  267. }
  268. public static string GetLink(string content)
  269. {
  270. if (string.IsNullOrEmpty(content)) return content;
  271. //string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  272. Regex regex = new(_url_pattern);
  273. MatchCollection matches = regex.Matches(content);
  274. string result = string.Empty;
  275. if (matches.Count > 0)
  276. {
  277. result = Regex.Replace(matches[^1].Value, @"\++$", "");
  278. }
  279. result = result.Replace("&nbsp;", "");
  280. if (result.StartsWith("http://e.tb.cn/")) result = result.Replace("http://", "https://");
  281. return result;
  282. }
  283. public static string GetTaobaoLink(string content)
  284. {
  285. if (string.IsNullOrEmpty(content)) return content;
  286. string pattern = @"(https?://(?:[\w-]+\.)*(?:tb\.cn|taobao\.com|juhuasuan\.com|tmall\.com|tmall\.hk)(?:/[a-zA-Z0-9\-\._~:/?#[\]@!$&'()*+,;=%]*)?)";
  287. Regex regex = new(pattern, RegexOptions.IgnoreCase);
  288. MatchCollection matches = regex.Matches(content);
  289. if (matches.Count > 0)
  290. {
  291. string result = Regex.Replace(matches[^1].Value, @"\++$", "");
  292. return result;
  293. }
  294. return string.Empty;
  295. }
  296. public static string GetDeeplink(string url)
  297. {
  298. if (string.IsNullOrEmpty(url)) return "tbopen://m.taobao.com/tbopen/index.html";
  299. string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&h5Url={url.UrlEncode()}";
  300. //string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&bootImage=0&afcPromotionOpen=false&bc_fl_src=h5_huanduan&source=slk_dp&h5Url={url.UrlEncode()}";
  301. return result;
  302. }
  303. public static string GetTitle(string content)
  304. {
  305. if (string.IsNullOrEmpty(content)) return string.Empty;
  306. string url = GetLink(content);
  307. if (!string.IsNullOrEmpty(url)) content = content.Replace(url, string.Empty);
  308. content = content.Replace("点击链接直接打开 或者 淘宝搜索直接打开", string.Empty).Trim();
  309. string[] words = Regex.Split(content, @"\s+");
  310. string word = words[^1];
  311. word = word.TrimStart('「').TrimEnd('」');
  312. return word;
  313. }
  314. public static bool IgnoreRegionIncluded(string _ignoreRegions, string? ipInfo, out string regionInfo)
  315. {
  316. regionInfo = string.Empty;
  317. if (string.IsNullOrEmpty(_ignoreRegions) || string.IsNullOrEmpty(ipInfo)) return false;
  318. string[] parts = ipInfo.Split('|');
  319. string countryInfo = null;
  320. if (parts.Length >= 4)
  321. {
  322. countryInfo = parts[0];
  323. regionInfo = parts[3];
  324. }
  325. if (string.IsNullOrEmpty(regionInfo)) return false;
  326. if (string.IsNullOrEmpty(countryInfo)) return false;
  327. string[] ignoreRegions = _ignoreRegions.Split('|');
  328. if (ignoreRegions.Contains(regionInfo)) return true;
  329. if (ignoreRegions.Contains(countryInfo))
  330. {
  331. regionInfo = countryInfo;
  332. return true;
  333. }
  334. foreach (var region in ignoreRegions)
  335. {
  336. if ("海外".Equals(region) && !"中国".Equals(countryInfo) && !"0".Equals(countryInfo))
  337. {
  338. regionInfo = "海外";
  339. return true;
  340. }
  341. }
  342. return false;
  343. }
  344. public static async Task<bool> OtherPlatformAsync(string content)
  345. {
  346. try
  347. {
  348. //腾讯会议
  349. string wemeetId = await ToolParsePlus.GetWemeetIdAsync(content);
  350. if (!string.IsNullOrEmpty(wemeetId)) return true;
  351. //百度网盘
  352. string surl = string.Empty, pwd = string.Empty;
  353. _ = ToolParsePlus.GetPanLink(content, ref surl, ref pwd);
  354. if (!string.IsNullOrEmpty(surl)) return true;
  355. //JD
  356. string url = JdUnionPlus.GetLink(content);
  357. var urlType = JdUnionPlus.GetLinkType(url, out _, out _);
  358. if (urlType != LinkTypeEnum.unknown) return true;
  359. //DY
  360. url = DyUnionPlus.GetLink(content);
  361. urlType = DyUnionPlus.GetLinkType(content, url);
  362. if (urlType != LinkTypeEnum.unknown) return true;
  363. }
  364. catch { }
  365. return false;
  366. }
  367. public static bool MatchRegexes(string text, List<string> extended)
  368. {
  369. text = RemoveUrls(text);
  370. string special_symbols = $"\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲";
  371. string[] base_patterns =
  372. [
  373. $@"(.*?\d{{2}}\s[a-zA-Z]{{2}}\d{{4}}\s.*?[{special_symbols}]+[a-zA-Z0-9\s]+[{special_symbols}]).*",
  374. $@"[{special_symbols}]+\s*([a-zA-Z]{{1,2}}\d{{2,4}}\s[a-zA-Z0-9]{{11}}|[a-zA-Z0-9]{{11}}[a-zA-Z]{{1,2}}\d{{2,4}}\s|[a-zA-Z0-9]{{11}})\s*[{special_symbols}]+",
  375. @"🍑♭ɑ◑下单|Tao宝"
  376. ];
  377. List<string> mergedArray = extended.Union(base_patterns).ToList();
  378. /*
  379. (.*?\\d{2}\\s[a-zA-Z]{2}\\d{4}\\s.*?[\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲]+[a-zA-Z0-9\\s]+[\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲])).*\n[\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲]+\\s*([a-zA-Z]{1,2}\\d{2,4}\\s[a-zA-Z0-9]{11}|[a-zA-Z0-9]{11}[a-zA-Z]{1,2}\\d{2,4}\\s|[a-zA-Z0-9]{11})\\s*[\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲]+\n🍑♭ɑ◑下单|Tao宝
  380. 【3瓶】恩威万蓝莓叶黄素酯软糖儿童成人上班族如酸涩肿胀可服用,<z5TYWAblvTw >:// HU7709,打開/
  381. 置顶小助手,方便下次查询\r\n1★【劵】无\r\n2★【返.佣】 0.29圆 \r\n3★【付款.价】24.9 圆\r\n \r\n 1 ¢HZv5WAboi0D¢(/:/ HU8866\r\
  382. n长按覆制本内容,打开TaoBao/\r\n-\r\n返佣是指商家的推广佣.金\r\n通过我回复的口令下単收货后,我
  383. 会微信红.苞返给你!!\r\n\r\n有疑问可回复“人工”咨询客服*/
  384. //🍑🔗上 水和肌精华水& CZ0 VWdCWC2gj97&,
  385. //6฿04ZDWzrwxjN)/ CA97
  386. //1😘 腹制这条(CZ00 iHvtWzrXa7O(:/
  387. //¥EC8DG4Zj0dhd8DOw¥MF6563 oIo0WzOF1Cp##X-PyVpHiPlyj1OP##
  388. //6.6₵vU2jWzIW3Qx₲/ CZ5224
  389. //$F56nWzIDSFZ$
  390. //0復製这条口令( CZ00 D0M4WzI0jFJ¢,📱咑開[Tao宝]就可下单:/
  391. //8fuzhi本条消息:¤ CZ3458 JSR1WzrPCBU¤,打开🍑♭ɑ◑下单/
  392. //5€Lz6SWzrkLDw¥:// CZ13
  393. //3₲Igq3Wzkc5np₳:// CZ15
  394. //9.9🎈【康师傅易拉罐6罐】口伶:6₰TnxKWzr0wff₪:// CZ69
  395. //4₲99Y0WzJdK6S₳:// CZ46
  396. //4fuzhi本条消息:¤ CZ0002 WVCSWzrk6Ix¤,打开🍑♭ɑ◑下单/
  397. bool anyMatch = false;
  398. foreach (string pattern in mergedArray)
  399. {
  400. if (Regex.IsMatch(text, pattern, RegexOptions.IgnoreCase))
  401. {
  402. anyMatch = true;
  403. }
  404. }
  405. return anyMatch;
  406. }
  407. public static bool MatchOtherInfo(string text, List<string> extended)
  408. {
  409. string[] base_patterns =
  410. [
  411. "[ďȌƯƔɪƝɖƟɏƖԂȎȗϓɫɧơȲǏđǫƲƳɨǹƊȮǚƴɬɲƿȖȳɳɗʮϔӢǙɦɒɎƥʘưΊƞǪȈʠΌǓϒίȠƠƱƗǸþŌŬÿĩŅÞÓũŸÎŎûįŇĎÖÙŷÏňÜÝÍñĎÒüŶľńĎ0ųŷÏŇdǒǔyí℡抖yí℡ÞŐûŷÌñ]{6}",
  412. @"(?:.+)极速版(?:口令|搜索)",
  413. @"##[a-zA-Z0-9]+##\[抖音(?:极速版)?(?:口令|搜索)\]",
  414. @"岽|亰|菄|京东|婛",
  415. "打开【剪映】",
  416. "weishi://|baiduhaokan://",
  417. "复制打开「UC浏览器」|UC瀏覽器",
  418. "番茄免费小说",
  419. @"\?chanTag=[^&\s]*口令",
  420. @"(https?://pan\.baidu\.com/s/[A-Za-z0-9?\-_=]+)",
  421. @"https?:\/\/meeting\.tencent\.com[^\s]*",
  422. @"#腾讯会议:(\d{3}-\d{3}-\d{3})"
  423. ];
  424. List<string> mergedArray = extended.Union(base_patterns).ToList();
  425. bool anyMatch = false;
  426. foreach (string pattern in mergedArray)
  427. {
  428. if (Regex.IsMatch(text, pattern, RegexOptions.IgnoreCase))
  429. {
  430. anyMatch = true;
  431. }
  432. }
  433. return anyMatch;
  434. }
  435. public static bool MatchMultiToken(string text, List<string> extended)
  436. {
  437. text = RemoveUrls(text);
  438. string special_symbols = $"\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲";
  439. string[] base_patterns =
  440. [
  441. @"[!$¥%]+([a-zA-Z0-9]{16})[!$¥%]+",
  442. @"\^v(\d+#[a-zA-Z0-9]+)(?:\?.*)?\^",
  443. "##([a-zA-Z0-9\\-]+)##",
  444. $@"(.*?\d{{2}}\s[a-zA-Z]{{2}}\d{{4}}\s.*?[{special_symbols}]+[a-zA-Z0-9\s]+[{special_symbols}]).*",
  445. $@"[{special_symbols}]+\s*([a-zA-Z]{{1,2}}\d{{2,4}}\s[a-zA-Z0-9]{{11}}|[a-zA-Z0-9]{{11}}[a-zA-Z]{{1,2}}\d{{2,4}}\s|[a-zA-Z0-9]{{11}})\s*[{special_symbols}]+",
  446. ];
  447. // PAGE6:/^v130091#P2J6EDxN8p8?chanTag=d9dc522bdea27437514052ea50f6a787^
  448. List<string> mergedArray = extended.Union(base_patterns).ToList();
  449. int matchCount = 0;
  450. foreach (string pattern in mergedArray)
  451. {
  452. Match match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
  453. if (match.Success)
  454. {
  455. matchCount++;
  456. text = text.Replace(match.Groups[^1].Value, ""); // 移除匹配的部分文本
  457. }
  458. }
  459. return matchCount >= 2;
  460. }
  461. public static bool FilterRiskLink(string ip, string oaid, string content, out string reason)
  462. {
  463. reason = string.Empty;
  464. string itemId = ExtractItemId(content);
  465. if (string.IsNullOrEmpty(itemId)) return false;
  466. string cacheKey = $":cache:parse:{ip}_{oaid}_{itemId}";
  467. int flag = RedisHelper.Get<int>(cacheKey);
  468. if (flag > 0)
  469. {
  470. reason = $"口令用户";
  471. return true;
  472. }
  473. return false;
  474. }
  475. public static bool ShouldIgnoreRequest(string ip, string oaid, string riskStrategy, int launchScene, out string reason)
  476. {
  477. reason = string.Empty;
  478. ////2025-05-11 brw不做风控
  479. //if (!string.IsNullOrEmpty(riskStrategy) && !"os".Equals(riskStrategy) && launchScene != -1) return false;
  480. try
  481. {
  482. // IP和流量控制
  483. var config = TkConfigCore.Get();
  484. string ignorePercentageCity = config.ignorePercentageCity;
  485. string? ipInfo = IP2RegionPlus.Search(ip);
  486. if (!TestParseCore.InWhitelist(ip, oaid) && IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  487. {
  488. reason = $"地区控制:{regionInfo}";
  489. return true;
  490. }
  491. if (TkLogCore.InBlacklist(config.blacklist_oaid, oaid))
  492. {
  493. reason = "OAID黑名单";
  494. return true;
  495. }
  496. int limit_num = config.tk_limit_per_ip_24h;
  497. if (!TestParseCore.InWhitelist(ip, oaid) && limit_num > 0)
  498. {
  499. int num = TkLogCore.getClientRequestTotalByIp(TkChannelEnum.tb, ip);
  500. if (num >= limit_num)
  501. {
  502. reason = "IP控制";
  503. return true;
  504. }
  505. }
  506. limit_num = config.tk_limit_per_oaid_24h;
  507. if (!TestParseCore.InWhitelist(ip, oaid) && limit_num > 0)
  508. {
  509. int num = TkLogCore.getClientRequestTotalByOAID(TkChannelEnum.tb, oaid);
  510. if (num >= limit_num)
  511. {
  512. reason = "OAID控制";
  513. return true;
  514. }
  515. }
  516. }
  517. catch (Exception ex)
  518. {
  519. reason = "配置异常";
  520. }
  521. return false;
  522. }
  523. public static bool FlowControlIgnoreRequest(string ip, string oaid, string riskStrategy, int launchScene, out string reason)
  524. {
  525. reason = string.Empty;
  526. ////2025-05-11 brw不做风控
  527. //if (!string.IsNullOrEmpty(riskStrategy) && !"os".Equals(riskStrategy) && launchScene != -1) return false;
  528. if (TestParseCore.InWhitelist(ip, oaid)) return false;
  529. try
  530. {
  531. // IP和流量控制
  532. var config = TkConfigCore.Get();
  533. int ignorePercentage = config.ignorePercentage;
  534. if (ignorePercentage == 0) return false;
  535. var randomValue = (decimal)_random.Next(0, 100);
  536. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  537. if (result)
  538. {
  539. reason = "流量控制";
  540. return true;
  541. }
  542. }
  543. catch (Exception ex)
  544. {
  545. reason = $"配置异常";
  546. }
  547. return false;
  548. }
  549. public static TkDataDTO GetFormattedObject(UnionParseRequest request)
  550. {
  551. string shortLinkurl = GetTaobaoLink(request.Content);
  552. string deeplink_url = GetDeeplink(shortLinkurl);
  553. return new TkDataDTO()
  554. {
  555. message = string.Empty,
  556. subCode = TkSubCodeEnum.Other,
  557. channel = TkChannelEnum.tb,
  558. accountName = string.Empty,
  559. success = false,
  560. content = request.Content,
  561. link_type = LinkTypeEnum.unknown,
  562. ip = request.Ip,
  563. oaid = request.Oaid,
  564. couponAmount = 0,
  565. taoToken = string.Empty,
  566. elapsedTime = 0,
  567. itemName = "点击打开淘宝APP",
  568. shortLinkurl = shortLinkurl,
  569. deeplink_url = deeplink_url,
  570. create_time = DateTime.Now,
  571. end_point = _end_point,
  572. parse_type = request.Type,
  573. };
  574. }
  575. public static string OppoDecode(string content)
  576. {
  577. try
  578. {
  579. string secretKey = "BwFeIvOEDZy9MFGi0JLZBO4yEhR44DGV";
  580. byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
  581. if (!content.Contains("%IV%")) return content;
  582. string cipherText = content.GetContentPart("", "%IV%");
  583. string ivText = content.GetContentPart("%IV%", "");
  584. byte[] cipherBytes = Convert.FromBase64String(cipherText);
  585. byte[] ivBytes = Convert.FromBase64String(ivText);
  586. using RijndaelManaged aes = new();
  587. aes.Key = keyBytes;
  588. aes.IV = ivBytes;
  589. aes.Mode = CipherMode.CFB;
  590. aes.Padding = PaddingMode.PKCS7;
  591. ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
  592. var outBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
  593. content = outBytes.ConvertToString();
  594. }
  595. catch (Exception e) { }
  596. return content;
  597. }
  598. }
  599. }