parse.cs 14 KB

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