parse.cs 15 KB

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