parse.cs 13 KB

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