parse.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using System.Text;
  2. using dodohold.core;
  3. using System.Text.RegularExpressions;
  4. using System.Net;
  5. using System.Security.Cryptography;
  6. using TencentCloud.Ecm.V20190719.Models;
  7. using System.Diagnostics;
  8. namespace molilian.core
  9. {
  10. public partial class AlimamaPlus
  11. {
  12. private string _accountName;
  13. private int _accountId;
  14. private string _company;
  15. private string _tb_token;
  16. private string _floorId;
  17. private string _refpid;
  18. private string _user_agent;
  19. private string _cookies;
  20. private WebProxy? _proxy = null;
  21. private string _api;
  22. private int _api_id = 0;
  23. public TkConfigDTO _config;
  24. private static string _end_point;
  25. static Random _random = new Random();
  26. static AlimamaPlus()
  27. {
  28. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  29. }
  30. public AlimamaPlus(TkPoolDTO account)
  31. {
  32. // string accountName, string tb_token, string floorId, string refpid,
  33. // string cookies, string user_agent, string nodeName
  34. _config = TkConfigCore.Get();
  35. _accountId = account.id;
  36. _accountName = account.name;
  37. _company = account.company;
  38. _tb_token = account.tb_token;
  39. _floorId = account.floorId;
  40. _refpid = account.refpid;
  41. _cookies = account.cookies;
  42. _user_agent = account.user_agent;
  43. if (!string.IsNullOrEmpty(account.nodeName))
  44. {
  45. _proxy = ProxyNodesCore.GetOne(account.nodeName);
  46. }
  47. _api = account.api;
  48. _api_id = account.api_id;
  49. }
  50. public bool ShouldIgnoreOtherAff(string content)
  51. {
  52. TkDataDTO result = new();
  53. //(_, _, var link_type, _) = InternalTextProcessing(content, ref result);
  54. InternalTextProcessing(content, ref result);
  55. return result.link_type == LinkTypeEnum.other_aff;
  56. }
  57. public TkDataDTO UnionParse(string content, ref TkDataDTO result)
  58. {
  59. InternalTextProcessing(content, ref result);
  60. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
  61. if (result.link_type == LinkTypeEnum.other_aff)
  62. {
  63. result.channel = TkChannelEnum.tb;
  64. result.channel_type = ChannelTypeEnum.tb;
  65. result.link_type = LinkTypeEnum.unknown;
  66. result.success = false;
  67. result.message = "其他推广链接";
  68. result.accountName = string.Empty;
  69. result.content = content;
  70. result.deeplink_url = GetDeeplink(null);
  71. result.itemName = GetTitle(content);
  72. }
  73. else
  74. {
  75. //处理过后的正文 用于转链
  76. if (result.success) content = result.content;
  77. result = alimamaParse(content, result);
  78. }
  79. result.content = content;
  80. result.link_type = result.link_type;
  81. return result;
  82. }
  83. public async Task<TkDataDTO> UnionParseAsync(string content, TkDataDTO result,
  84. CancellationToken cancellationToken = default,
  85. Dictionary<string, long> swData = null)
  86. {
  87. Stopwatch stopwatch = Stopwatch.StartNew();
  88. result = await InternalTextProcessingAsync(content, result, cancellationToken, swData);
  89. if (swData != null)
  90. {
  91. stopwatch.Stop();
  92. swData.Add("InternalTextProcessingAsync", stopwatch.ElapsedMilliseconds);
  93. }
  94. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content);
  95. if (result.link_type == LinkTypeEnum.other_aff)
  96. {
  97. result.channel = TkChannelEnum.tb;
  98. result.channel_type = ChannelTypeEnum.tb;
  99. result.link_type = LinkTypeEnum.unknown;
  100. result.success = false;
  101. result.message = "其他推广链接";
  102. result.accountName = string.Empty;
  103. result.content = content;
  104. result.deeplink_url = GetDeeplink(null);
  105. //result.itemName = GetTitle(content);
  106. result.itemName = "点击打开淘宝APP";
  107. }
  108. else
  109. {
  110. //处理过后的正文 用于转链
  111. if (!result.success)
  112. {
  113. result.content = content;
  114. return result;
  115. }
  116. if (swData != null)
  117. {
  118. stopwatch.Reset();
  119. stopwatch.Start();
  120. }
  121. result = await alimamaParseAsync(result.content, result, cancellationToken);
  122. if (swData != null)
  123. {
  124. stopwatch.Stop();
  125. swData.Add("alimamaParseAsync", stopwatch.ElapsedMilliseconds);
  126. }
  127. if (!result.success)
  128. {
  129. result.itemName = "点击打开淘宝APP";
  130. }
  131. //if (string.IsNullOrEmpty(result.itemName)) result.itemName = GetTitle(content);
  132. }
  133. if (string.IsNullOrEmpty(result.itemName) || result.link_type == LinkTypeEnum.profile)
  134. {
  135. result.itemName = "点击打开淘宝APP";
  136. }
  137. result.link_type = result.link_type;
  138. return result;
  139. }
  140. public static string ReplaceUrls(string content, string shortLink)
  141. {
  142. if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(shortLink)) return content;
  143. string result = content;
  144. string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  145. Regex regex = new(pattern);
  146. MatchCollection matches = regex.Matches(content);
  147. foreach (Match m in matches.Cast<Match>())
  148. {
  149. string url = m.Value;
  150. if (url.Contains("https://m.tb.cn/"))
  151. {
  152. result = result.Replace(url, shortLink);
  153. }
  154. }
  155. return result;
  156. }
  157. public static string GetLink(string content)
  158. {
  159. if (string.IsNullOrEmpty(content)) return content;
  160. string result = content;
  161. string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  162. //string pattern = @"https?:\/\/[\da-z\.-]+\.[a-z]{2,6}(:\d{1,5})?(\/[\w\.-]*)*";
  163. Regex regex = new(pattern);
  164. MatchCollection matches = regex.Matches(content);
  165. if (matches.Count > 0)
  166. {
  167. return matches[^1].Value; // 返回第一个匹配到的 URL
  168. }
  169. return string.Empty;
  170. }
  171. public static string GetTaobaoLink(string content)
  172. {
  173. if (string.IsNullOrEmpty(content)) return content;
  174. string result = content;
  175. string pattern = @"(https?://(?:[\w-]+\.)*(?:tb\.cn|taobao\.com|juhuasuan.com|tmall\.com|tmall\.hk)(?:/[^\s]*)?)";
  176. Regex regex = new(pattern);
  177. MatchCollection matches = regex.Matches(content);
  178. if (matches.Count > 0)
  179. {
  180. return matches[^1].Value; // 返回第一个匹配到的 URL
  181. }
  182. return string.Empty;
  183. }
  184. public static string GetDeeplink(string url)
  185. {
  186. if (string.IsNullOrEmpty(url)) return "tbopen://m.taobao.com/tbopen/index.html";
  187. string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&h5Url={url.UrlEncode()}&backURL=__back_url__";
  188. return result;
  189. }
  190. public static string GetTitle(string content)
  191. {
  192. if (string.IsNullOrEmpty(content)) return string.Empty;
  193. string url = GetLink(content);
  194. if (!string.IsNullOrEmpty(url)) content = content.Replace(url, string.Empty);
  195. content = content.Replace("点击链接直接打开 或者 淘宝搜索直接打开", string.Empty).Trim();
  196. string[] words = Regex.Split(content, @"\s+");
  197. string word = words[^1];
  198. word = word.TrimStart('「').TrimEnd('」');
  199. return word;
  200. }
  201. static bool IgnoreRegionIncluded(string _ignoreRegions, string? ipInfo, out string regionInfo)
  202. {
  203. regionInfo = string.Empty;
  204. if (string.IsNullOrEmpty(_ignoreRegions) || string.IsNullOrEmpty(ipInfo)) return false;
  205. string[] parts = ipInfo.Split('|');
  206. string countryInfo = null;
  207. if (parts.Length >= 4)
  208. {
  209. countryInfo = parts[0];
  210. regionInfo = parts[3];
  211. }
  212. if (string.IsNullOrEmpty(regionInfo)) return false;
  213. if (string.IsNullOrEmpty(countryInfo)) return false;
  214. string[] ignoreRegions = _ignoreRegions.Split('|');
  215. if (ignoreRegions.Contains(regionInfo)) return true;
  216. if (ignoreRegions.Contains(countryInfo))
  217. {
  218. regionInfo = countryInfo;
  219. return true;
  220. }
  221. foreach (var region in ignoreRegions)
  222. {
  223. if ("海外".Equals(region) && !"中国".Equals(countryInfo) && !"0".Equals(countryInfo))
  224. {
  225. regionInfo = "海外";
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. public static async Task<bool> OtherPlatformAsync(string content)
  232. {
  233. try
  234. {
  235. //腾讯会议
  236. string wemeetId = await ToolParsePlus.GetWemeetIdAsync(content);
  237. if (!string.IsNullOrEmpty(wemeetId)) return true;
  238. //百度网盘
  239. string surl = string.Empty, pwd = string.Empty;
  240. _ = ToolParsePlus.GetPanLink(content, ref surl, ref pwd);
  241. if (!string.IsNullOrEmpty(surl)) return true;
  242. //JD
  243. string url = JdUnionPlus.GetLink(content);
  244. var urlType = JdUnionPlus.GetLinkType(url, out _, out _);
  245. if (urlType != LinkTypeEnum.unknown) return true;
  246. }
  247. catch { }
  248. return false;
  249. }
  250. public static bool MatchRegexes(string text)
  251. {
  252. string regex1 = @"(.*?\d{2}\s[a-zA-Z]{2}\d{4}\s.*?[$¥🗝₴€₤£₳✔《💰🔑🎵✔💲🔐📲]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔💲🔐📲]).* [$¥🗝₴€₤£₳✔《💰🔑🎵✔💲🔐📲]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔💲🔐📲](\s*(https?://[^\s]+)?)?\s*([a-zA-Z]{2}\d{4})?\s*(.*)";
  253. Regex r1 = new(regex1);
  254. bool match1 = r1.IsMatch(text);
  255. return match1;
  256. }
  257. public static bool FirstFilterIgnoreRequest(string content, out string reason)
  258. {
  259. reason = string.Empty;
  260. try
  261. {
  262. // 正则过滤
  263. var matchResult = MatchRegexes(content);
  264. if (matchResult) return false;
  265. reason = "初步筛选";
  266. return true;
  267. }
  268. catch (Exception ex)
  269. {
  270. reason = "配置异常";
  271. return true;
  272. }
  273. }
  274. public static bool ShouldIgnoreRequest(string ip, string oaid, out string reason)
  275. {
  276. reason = string.Empty;
  277. try
  278. {
  279. // IP和流量控制
  280. var config = TkConfigCore.Get();
  281. string ignorePercentageCity = config.ignorePercentageCity;
  282. string? ipInfo = IP2RegionPlus.Search(ip);
  283. if (IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  284. {
  285. reason = $"地区控制:{regionInfo}";
  286. return true;
  287. }
  288. int ignorePercentage = config.ignorePercentage;
  289. if (ignorePercentage == 0) return false;
  290. var randomValue = (decimal)_random.Next(0, 100);
  291. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  292. if (result) reason = "流量控制";
  293. return result;
  294. }
  295. catch (Exception ex)
  296. {
  297. reason = $"配置异常";
  298. return true;
  299. }
  300. }
  301. public static TkDataDTO GetFormattedObject(string content, string ip, string oaid)
  302. {
  303. string shortLinkurl = GetTaobaoLink(content);
  304. string deeplink_url = GetDeeplink(shortLinkurl);
  305. return new TkDataDTO()
  306. {
  307. message = string.Empty,
  308. channel = TkChannelEnum.tb,
  309. accountName = string.Empty,
  310. success = false,
  311. content = content,
  312. link_type = LinkTypeEnum.unknown,
  313. ip = ip,
  314. oaid = oaid,
  315. couponAmount = 0,
  316. taoToken = string.Empty,
  317. elapsedTime = 0,
  318. itemName = "点击打开淘宝APP",
  319. shortLinkurl = shortLinkurl,
  320. deeplink_url = deeplink_url,
  321. create_time = DateTime.Now,
  322. end_point = _end_point,
  323. };
  324. }
  325. public static string OppoDecode(string content)
  326. {
  327. try
  328. {
  329. string secretKey = "BwFeIvOEDZy9MFGi0JLZBO4yEhR44DGV";
  330. byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
  331. if (!content.Contains("%IV%")) return content;
  332. string cipherText = content.GetContentPart("", "%IV%");
  333. string ivText = content.GetContentPart("%IV%", "");
  334. byte[] cipherBytes = Convert.FromBase64String(cipherText);
  335. byte[] ivBytes = Convert.FromBase64String(ivText);
  336. using RijndaelManaged aes = new();
  337. aes.Key = keyBytes;
  338. aes.IV = ivBytes;
  339. aes.Mode = CipherMode.CFB;
  340. aes.Padding = PaddingMode.PKCS7;
  341. ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
  342. var outBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
  343. content = outBytes.ConvertToString();
  344. }
  345. catch (Exception e) { }
  346. return content;
  347. }
  348. }
  349. }