parse.cs 10 KB

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