parse.cs 26 KB

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