parse.cs 20 KB

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