parse.cs 18 KB

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