parse.cs 25 KB

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