parse.cs 10 KB

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