parse.cs 21 KB

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