parse.cs 28 KB

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