parse.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 GetDeeplink(string url)
  137. {
  138. if (string.IsNullOrEmpty(url)) return "tbopen://m.taobao.com/tbopen/index.html";
  139. string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&h5Url={url.UrlEncode()}&backURL=__back_url__";
  140. return result;
  141. }
  142. public static string GetTitle(string content)
  143. {
  144. if (string.IsNullOrEmpty(content)) return string.Empty;
  145. string url = GetLink(content);
  146. if (!string.IsNullOrEmpty(url)) content = content.Replace(url, string.Empty);
  147. content = content.Replace("点击链接直接打开 或者 淘宝搜索直接打开", string.Empty).Trim();
  148. string[] words = Regex.Split(content, @"\s+");
  149. string word = words[^1];
  150. word = word.TrimStart('「').TrimEnd('」');
  151. return word;
  152. }
  153. static bool IgnoreRegionIncluded(string _ignoreRegions, string? ipInfo, out string regionInfo)
  154. {
  155. regionInfo = string.Empty;
  156. if (string.IsNullOrEmpty(_ignoreRegions) || string.IsNullOrEmpty(ipInfo)) return false;
  157. string[] parts = ipInfo.Split('|');
  158. string countryInfo = null;
  159. if (parts.Length >= 4)
  160. {
  161. countryInfo = parts[0];
  162. regionInfo = parts[3];
  163. }
  164. if (string.IsNullOrEmpty(regionInfo)) return false;
  165. if (string.IsNullOrEmpty(countryInfo)) return false;
  166. string[] ignoreRegions = _ignoreRegions.Split('|');
  167. if (ignoreRegions.Contains(regionInfo)) return true;
  168. if (ignoreRegions.Contains(countryInfo))
  169. {
  170. regionInfo = countryInfo;
  171. return true;
  172. }
  173. foreach (var region in ignoreRegions)
  174. {
  175. if ("海外".Equals(region) && !"中国".Equals(countryInfo) && !"0".Equals(countryInfo))
  176. {
  177. regionInfo = "海外";
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. public static bool OtherPlatform(string content)
  184. {
  185. try
  186. {
  187. //腾讯会议
  188. string wemeetId = ToolParsePlus.GetWemeetId(content);
  189. if (!string.IsNullOrEmpty(wemeetId)) return true;
  190. //百度网盘
  191. string surl = string.Empty, pwd = string.Empty;
  192. _ = ToolParsePlus.GetPanLink(content, ref surl, ref pwd);
  193. if (!string.IsNullOrEmpty(surl)) return true;
  194. //JD
  195. string url = JdUnionPlus.GetLink(content);
  196. var urlType = JdUnionPlus.GetLinkType(url, out _, out _);
  197. if (urlType != LinkTypeEnum.unknown) return true;
  198. }
  199. catch { }
  200. return false;
  201. }
  202. public static bool ShouldIgnoreRequest(string ip, string oaid, out string reason)
  203. {
  204. reason = string.Empty;
  205. try
  206. {
  207. var config = TkConfigCore.Get();
  208. string ignorePercentageCity = config.ignorePercentageCity;
  209. string? ipInfo = IP2RegionPlus.Search(ip);
  210. if (IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  211. {
  212. reason = $"地区控制:{regionInfo}";
  213. return true;
  214. }
  215. int ignorePercentage = config.ignorePercentage;
  216. if (ignorePercentage == 0) return false;
  217. Random random = new Random();
  218. var randomValue = (decimal)random.Next(0, 100);
  219. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  220. if (result) reason = "流量控制";
  221. return result;
  222. }
  223. catch (Exception ex)
  224. {
  225. reason = $"配置异常";
  226. return true;
  227. }
  228. }
  229. public static TkDataDTO GetFormattedObject(string content, string ip, string oaid)
  230. {
  231. string shortLinkurl = GetLink(content);
  232. string deeplink_url = GetDeeplink(shortLinkurl);
  233. return new TkDataDTO()
  234. {
  235. message = string.Empty,
  236. channel = TkChannelEnum.tb,
  237. accountName = string.Empty,
  238. success = false,
  239. content = content,
  240. link_type = LinkTypeEnum.unknown,
  241. ip = ip,
  242. oaid = oaid,
  243. couponAmount = 0,
  244. taoToken = string.Empty,
  245. elapsedTime = 0,
  246. itemName = "点击打开淘宝APP",
  247. shortLinkurl = shortLinkurl,
  248. deeplink_url = deeplink_url,
  249. create_time = DateTime.Now,
  250. };
  251. }
  252. public static string OppoDecode(string content)
  253. {
  254. try
  255. {
  256. string secretKey = "BwFeIvOEDZy9MFGi0JLZBO4yEhR44DGV";
  257. byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
  258. if (!content.Contains("%IV%")) return content;
  259. string cipherText = content.GetContentPart("", "%IV%");
  260. string ivText = content.GetContentPart("%IV%", "");
  261. byte[] cipherBytes = Convert.FromBase64String(cipherText);
  262. byte[] ivBytes = Convert.FromBase64String(ivText);
  263. using RijndaelManaged aes = new();
  264. aes.Key = keyBytes;
  265. aes.IV = ivBytes;
  266. aes.Mode = CipherMode.CFB;
  267. aes.Padding = PaddingMode.PKCS7;
  268. ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
  269. var outBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
  270. content = outBytes.ConvertToString();
  271. }
  272. catch (Exception e) { }
  273. return content;
  274. }
  275. }
  276. }