parse.cs 15 KB

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