parse.cs 21 KB

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