parse.cs 21 KB

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