parse.cs 11 KB

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