parse.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. }
  86. else
  87. {
  88. //处理过后的正文 用于转链
  89. if (!result.success) { result.content = content; }
  90. result = await alimamaParseAsync(result.content, result);
  91. if (string.IsNullOrEmpty(result.itemName)) result.itemName = GetTitle(content);
  92. }
  93. if (string.IsNullOrEmpty(result.itemName) || result.link_type == LinkTypeEnum.profile)
  94. {
  95. result.itemName = "点击打开淘宝APP";
  96. }
  97. result.link_type = result.link_type;
  98. return result;
  99. }
  100. public static string ReplaceUrls(string content, string shortLink)
  101. {
  102. if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(shortLink)) return content;
  103. string result = content;
  104. string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  105. Regex regex = new(pattern);
  106. MatchCollection matches = regex.Matches(content);
  107. foreach (Match m in matches.Cast<Match>())
  108. {
  109. string url = m.Value;
  110. if (url.Contains("https://m.tb.cn/"))
  111. {
  112. result = result.Replace(url, shortLink);
  113. }
  114. }
  115. return result;
  116. }
  117. public static string GetLink(string content)
  118. {
  119. if (string.IsNullOrEmpty(content)) return content;
  120. string result = content;
  121. string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  122. Regex regex = new(pattern);
  123. MatchCollection matches = regex.Matches(content);
  124. if (matches.Count > 0)
  125. {
  126. return matches[^1].Value; // 返回第一个匹配到的 URL
  127. }
  128. return string.Empty;
  129. }
  130. public static string GetDeeplink(string url)
  131. {
  132. if (string.IsNullOrEmpty(url)) return "tbopen://m.taobao.com/tbopen/index.html";
  133. string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&h5Url={url.UrlEncode()}&backURL=__back_url__";
  134. return result;
  135. }
  136. public static string GetTitle(string content)
  137. {
  138. if (string.IsNullOrEmpty(content)) return string.Empty;
  139. string url = GetLink(content);
  140. content = content.Replace(url, string.Empty);
  141. content = content.Replace("点击链接直接打开 或者 淘宝搜索直接打开", string.Empty).Trim();
  142. string[] words = Regex.Split(content, @"\s+");
  143. string word = words[^1];
  144. word = word.TrimStart('「').TrimEnd('」');
  145. return word;
  146. }
  147. static bool IgnoreRegionIncluded(string _ignoreRegions, string? ipInfo, out string regionInfo)
  148. {
  149. regionInfo = string.Empty;
  150. if (string.IsNullOrEmpty(_ignoreRegions) || string.IsNullOrEmpty(ipInfo)) return false;
  151. string[] parts = ipInfo.Split('|');
  152. string countryInfo = null;
  153. if (parts.Length >= 4)
  154. {
  155. countryInfo = parts[0];
  156. regionInfo = parts[3];
  157. }
  158. if (string.IsNullOrEmpty(regionInfo)) return false;
  159. if (string.IsNullOrEmpty(countryInfo)) return false;
  160. string[] ignoreRegions = _ignoreRegions.Split('|');
  161. if (ignoreRegions.Contains(regionInfo)) return true;
  162. if (ignoreRegions.Contains(countryInfo))
  163. {
  164. regionInfo = countryInfo;
  165. return true;
  166. }
  167. foreach (var region in ignoreRegions)
  168. {
  169. if ("海外".Equals(region) && !"中国".Equals(countryInfo) && !"0".Equals(countryInfo))
  170. {
  171. regionInfo = "海外";
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. public static bool OtherPlatform(string content)
  178. {
  179. try
  180. {
  181. //腾讯会议
  182. string wemeetId = ToolParsePlus.GetWemeetId(content);
  183. if (!string.IsNullOrEmpty(wemeetId)) return true;
  184. //百度网盘
  185. string surl = string.Empty, pwd = string.Empty;
  186. _ = ToolParsePlus.GetPanLink(content, ref surl, ref pwd);
  187. if (!string.IsNullOrEmpty(surl)) return true;
  188. //JD
  189. string url = JdUnionPlus.GetLink(content);
  190. var urlType = JdUnionPlus.GetLinkType(url, out _, out _);
  191. if (urlType != LinkTypeEnum.unknown) return true;
  192. }
  193. catch { }
  194. return false;
  195. }
  196. public static bool ShouldIgnoreRequest(string ip, string oaid, out string reason)
  197. {
  198. reason = string.Empty;
  199. try
  200. {
  201. var config = TkConfigCore.Get();
  202. string ignorePercentageCity = config.ignorePercentageCity;
  203. string? ipInfo = IP2RegionPlus.Search(ip);
  204. if (IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  205. {
  206. reason = $"地区控制:{regionInfo}";
  207. return true;
  208. }
  209. int ignorePercentage = config.ignorePercentage;
  210. if (ignorePercentage == 0) return false;
  211. Random random = new Random();
  212. var randomValue = (decimal)random.Next(0, 100);
  213. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  214. if (result) reason = "流量控制";
  215. return result;
  216. }
  217. catch (Exception ex)
  218. {
  219. reason = $"配置异常";
  220. return true;
  221. }
  222. }
  223. public static TkDataDTO GetFormattedObject(string content, string ip, string oaid)
  224. {
  225. string shortLinkurl = GetLink(content);
  226. string deeplink_url = GetDeeplink(shortLinkurl);
  227. return new TkDataDTO()
  228. {
  229. message = string.Empty,
  230. channel = TkChannelEnum.tb,
  231. accountName = string.Empty,
  232. success = false,
  233. content = content,
  234. link_type = LinkTypeEnum.unknown,
  235. ip = ip,
  236. oaid = oaid,
  237. couponAmount = 0,
  238. taoToken = string.Empty,
  239. elapsedTime = 0,
  240. itemName = "点击打开淘宝APP",
  241. shortLinkurl = shortLinkurl,
  242. deeplink_url = deeplink_url,
  243. create_time = DateTime.Now,
  244. };
  245. }
  246. public static string OppoDecode(string content)
  247. {
  248. try
  249. {
  250. string secretKey = "BwFeIvOEDZy9MFGi0JLZBO4yEhR44DGV";
  251. byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
  252. if (!content.Contains("%IV%")) return content;
  253. string cipherText = content.GetContentPart("", "%IV%");
  254. string ivText = content.GetContentPart("%IV%", "");
  255. byte[] cipherBytes = Convert.FromBase64String(cipherText);
  256. byte[] ivBytes = Convert.FromBase64String(ivText);
  257. using RijndaelManaged aes = new();
  258. aes.Key = keyBytes;
  259. aes.IV = ivBytes;
  260. aes.Mode = CipherMode.CFB;
  261. aes.Padding = PaddingMode.PKCS7;
  262. ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
  263. var outBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
  264. content = outBytes.ConvertToString();
  265. }
  266. catch (Exception e) { }
  267. return content;
  268. }
  269. }
  270. }