parse.cs 14 KB

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