parse.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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) { result.content = content; }
  112. if (swData != null)
  113. {
  114. stopwatch.Reset();
  115. stopwatch.Start();
  116. }
  117. result = await alimamaParseAsync(result.content, result, cancellationToken);
  118. if (swData != null)
  119. {
  120. stopwatch.Stop();
  121. swData.Add("alimamaParseAsync", stopwatch.ElapsedMilliseconds);
  122. }
  123. if (!result.success)
  124. {
  125. result.itemName = "点击打开淘宝APP";
  126. }
  127. //if (string.IsNullOrEmpty(result.itemName)) result.itemName = GetTitle(content);
  128. }
  129. if (string.IsNullOrEmpty(result.itemName) || result.link_type == LinkTypeEnum.profile)
  130. {
  131. result.itemName = "点击打开淘宝APP";
  132. }
  133. result.link_type = result.link_type;
  134. return result;
  135. }
  136. public static string ReplaceUrls(string content, string shortLink)
  137. {
  138. if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(shortLink)) return content;
  139. string result = content;
  140. string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  141. Regex regex = new(pattern);
  142. MatchCollection matches = regex.Matches(content);
  143. foreach (Match m in matches.Cast<Match>())
  144. {
  145. string url = m.Value;
  146. if (url.Contains("https://m.tb.cn/"))
  147. {
  148. result = result.Replace(url, shortLink);
  149. }
  150. }
  151. return result;
  152. }
  153. public static string GetLink(string content)
  154. {
  155. if (string.IsNullOrEmpty(content)) return content;
  156. string result = content;
  157. string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  158. //string pattern = @"https?:\/\/[\da-z\.-]+\.[a-z]{2,6}(:\d{1,5})?(\/[\w\.-]*)*";
  159. Regex regex = new(pattern);
  160. MatchCollection matches = regex.Matches(content);
  161. if (matches.Count > 0)
  162. {
  163. return matches[^1].Value; // 返回第一个匹配到的 URL
  164. }
  165. return string.Empty;
  166. }
  167. public static string GetTaobaoLink(string content)
  168. {
  169. if (string.IsNullOrEmpty(content)) return content;
  170. string result = content;
  171. string pattern = @"(https?://(?:[\w-]+\.)*(?:tb\.cn|taobao\.com|juhuasuan.com|tmall\.com|tmall\.hk)(?:/[^\s]*)?)";
  172. Regex regex = new(pattern);
  173. MatchCollection matches = regex.Matches(content);
  174. if (matches.Count > 0)
  175. {
  176. return matches[^1].Value; // 返回第一个匹配到的 URL
  177. }
  178. return string.Empty;
  179. }
  180. public static string GetDeeplink(string url)
  181. {
  182. if (string.IsNullOrEmpty(url)) return "tbopen://m.taobao.com/tbopen/index.html";
  183. string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&h5Url={url.UrlEncode()}&backURL=__back_url__";
  184. return result;
  185. }
  186. public static string GetTitle(string content)
  187. {
  188. if (string.IsNullOrEmpty(content)) return string.Empty;
  189. string url = GetLink(content);
  190. if (!string.IsNullOrEmpty(url)) content = content.Replace(url, string.Empty);
  191. content = content.Replace("点击链接直接打开 或者 淘宝搜索直接打开", string.Empty).Trim();
  192. string[] words = Regex.Split(content, @"\s+");
  193. string word = words[^1];
  194. word = word.TrimStart('「').TrimEnd('」');
  195. return word;
  196. }
  197. static bool IgnoreRegionIncluded(string _ignoreRegions, string? ipInfo, out string regionInfo)
  198. {
  199. regionInfo = string.Empty;
  200. if (string.IsNullOrEmpty(_ignoreRegions) || string.IsNullOrEmpty(ipInfo)) return false;
  201. string[] parts = ipInfo.Split('|');
  202. string countryInfo = null;
  203. if (parts.Length >= 4)
  204. {
  205. countryInfo = parts[0];
  206. regionInfo = parts[3];
  207. }
  208. if (string.IsNullOrEmpty(regionInfo)) return false;
  209. if (string.IsNullOrEmpty(countryInfo)) return false;
  210. string[] ignoreRegions = _ignoreRegions.Split('|');
  211. if (ignoreRegions.Contains(regionInfo)) return true;
  212. if (ignoreRegions.Contains(countryInfo))
  213. {
  214. regionInfo = countryInfo;
  215. return true;
  216. }
  217. foreach (var region in ignoreRegions)
  218. {
  219. if ("海外".Equals(region) && !"中国".Equals(countryInfo) && !"0".Equals(countryInfo))
  220. {
  221. regionInfo = "海外";
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. public static bool OtherPlatform(string content)
  228. {
  229. try
  230. {
  231. //腾讯会议
  232. string wemeetId = ToolParsePlus.GetWemeetId(content);
  233. if (!string.IsNullOrEmpty(wemeetId)) return true;
  234. //百度网盘
  235. string surl = string.Empty, pwd = string.Empty;
  236. _ = ToolParsePlus.GetPanLink(content, ref surl, ref pwd);
  237. if (!string.IsNullOrEmpty(surl)) return true;
  238. //JD
  239. string url = JdUnionPlus.GetLink(content);
  240. var urlType = JdUnionPlus.GetLinkType(url, out _, out _);
  241. if (urlType != LinkTypeEnum.unknown) return true;
  242. }
  243. catch { }
  244. return false;
  245. }
  246. public static bool MatchRegexes(string text)
  247. {
  248. 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*(.*)";
  249. Regex r1 = new(regex1);
  250. bool match1 = r1.IsMatch(text);
  251. return match1;
  252. }
  253. public static bool FirstFilterIgnoreRequest(string content, out string reason)
  254. {
  255. reason = string.Empty;
  256. try
  257. {
  258. // 正则过滤
  259. var matchResult = MatchRegexes(content);
  260. if (matchResult) return false;
  261. reason = "初步筛选";
  262. return true;
  263. }
  264. catch (Exception ex)
  265. {
  266. reason = "配置异常";
  267. return true;
  268. }
  269. }
  270. public static bool ShouldIgnoreRequest(string ip, string oaid, out string reason)
  271. {
  272. reason = string.Empty;
  273. try
  274. {
  275. // IP和流量控制
  276. var config = TkConfigCore.Get();
  277. string ignorePercentageCity = config.ignorePercentageCity;
  278. string? ipInfo = IP2RegionPlus.Search(ip);
  279. if (IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  280. {
  281. reason = $"地区控制:{regionInfo}";
  282. return true;
  283. }
  284. int ignorePercentage = config.ignorePercentage;
  285. if (ignorePercentage == 0) return false;
  286. var randomValue = (decimal)_random.Next(0, 100);
  287. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  288. if (result) reason = "流量控制";
  289. return result;
  290. }
  291. catch (Exception ex)
  292. {
  293. reason = $"配置异常";
  294. return true;
  295. }
  296. }
  297. public static TkDataDTO GetFormattedObject(string content, string ip, string oaid)
  298. {
  299. string shortLinkurl = GetTaobaoLink(content);
  300. string deeplink_url = GetDeeplink(shortLinkurl);
  301. return new TkDataDTO()
  302. {
  303. message = string.Empty,
  304. channel = TkChannelEnum.tb,
  305. accountName = string.Empty,
  306. success = false,
  307. content = content,
  308. link_type = LinkTypeEnum.unknown,
  309. ip = ip,
  310. oaid = oaid,
  311. couponAmount = 0,
  312. taoToken = string.Empty,
  313. elapsedTime = 0,
  314. itemName = "点击打开淘宝APP",
  315. shortLinkurl = shortLinkurl,
  316. deeplink_url = deeplink_url,
  317. create_time = DateTime.Now,
  318. end_point = _end_point,
  319. };
  320. }
  321. public static string OppoDecode(string content)
  322. {
  323. try
  324. {
  325. string secretKey = "BwFeIvOEDZy9MFGi0JLZBO4yEhR44DGV";
  326. byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
  327. if (!content.Contains("%IV%")) return content;
  328. string cipherText = content.GetContentPart("", "%IV%");
  329. string ivText = content.GetContentPart("%IV%", "");
  330. byte[] cipherBytes = Convert.FromBase64String(cipherText);
  331. byte[] ivBytes = Convert.FromBase64String(ivText);
  332. using RijndaelManaged aes = new();
  333. aes.Key = keyBytes;
  334. aes.IV = ivBytes;
  335. aes.Mode = CipherMode.CFB;
  336. aes.Padding = PaddingMode.PKCS7;
  337. ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
  338. var outBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
  339. content = outBytes.ConvertToString();
  340. }
  341. catch (Exception e) { }
  342. return content;
  343. }
  344. }
  345. }