parse.cs 28 KB

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