using System.Text; using dodohold.core; using System.Text.RegularExpressions; using System.Net; using System.Security.Cryptography; using TencentCloud.Ecm.V20190719.Models; namespace molilian.core { public partial class AlimamaPlus { private string _accountName; private int _accountId; private string _company; private string _tb_token; private string _floorId; private string _refpid; private string _user_agent; private string _cookies; private WebProxy? _proxy = null; private string _api; private int _api_id = 0; private TkConfigDTO _config; public AlimamaPlus(TkPoolDTO account) { // string accountName, string tb_token, string floorId, string refpid, // string cookies, string user_agent, string nodeName _config = TkConfigCore.Get(); _accountId = account.id; _accountName = account.name; _company = account.company; _tb_token = account.tb_token; _floorId = account.floorId; _refpid = account.refpid; _cookies = account.cookies; _user_agent = account.user_agent; if (!string.IsNullOrEmpty(account.nodeName)) { _proxy = ProxyNodesCore.GetOne(account.nodeName); } _api = account.api; _api_id = account.api_id; } public bool ShouldIgnoreOtherAff(string content) { TkDataDTO result = new(); //(_, _, var link_type, _) = InternalTextProcessing(content, ref result); InternalTextProcessing(content, ref result); return result.link_type == LinkTypeEnum.other_aff; } public TkDataDTO UnionParse(string content, ref TkDataDTO result) { InternalTextProcessing(content, ref result); //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result); if (result.link_type == LinkTypeEnum.other_aff) { result.channel = TkChannelEnum.tb; result.channel_type = ChannelTypeEnum.tb; result.link_type = LinkTypeEnum.unknown; result.success = false; result.message = "其他推广链接"; result.accountName = string.Empty; result.content = content; result.deeplink_url = GetDeeplink(null); result.itemName = GetTitle(content); } else { //处理过后的正文 用于转链 if (result.success) content = result.content; alimamaParse(content, ref result); } result.content = content; result.link_type = result.link_type; return result; } public async Task UnionParseAsync(string content, TkDataDTO result) { InternalTextProcessing(content, ref result); //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content); if (result.link_type == LinkTypeEnum.other_aff) { result.channel = TkChannelEnum.tb; result.channel_type = ChannelTypeEnum.tb; result.link_type = LinkTypeEnum.unknown; result.success = false; result.message = "其他推广链接"; result.accountName = string.Empty; result.content = content; result.deeplink_url = GetDeeplink(null); //result.itemName = GetTitle(content); result.itemName = "点击打开淘宝APP"; } else { //处理过后的正文 用于转链 if (!result.success) { result.content = content; } result = await alimamaParseAsync(result.content, result); if (!result.success) { result.itemName = "点击打开淘宝APP"; } //if (string.IsNullOrEmpty(result.itemName)) result.itemName = GetTitle(content); } if (string.IsNullOrEmpty(result.itemName) || result.link_type == LinkTypeEnum.profile) { result.itemName = "点击打开淘宝APP"; } result.link_type = result.link_type; return result; } public static string ReplaceUrls(string content, string shortLink) { if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(shortLink)) return content; string result = content; string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?"; Regex regex = new(pattern); MatchCollection matches = regex.Matches(content); foreach (Match m in matches.Cast()) { string url = m.Value; if (url.Contains("https://m.tb.cn/")) { result = result.Replace(url, shortLink); } } return result; } public static string GetLink(string content) { if (string.IsNullOrEmpty(content)) return content; string result = content; string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?"; //string pattern = @"https?:\/\/[\da-z\.-]+\.[a-z]{2,6}(:\d{1,5})?(\/[\w\.-]*)*"; Regex regex = new(pattern); MatchCollection matches = regex.Matches(content); if (matches.Count > 0) { return matches[^1].Value; // 返回第一个匹配到的 URL } return string.Empty; } public static string GetTaobaoLink(string content) { if (string.IsNullOrEmpty(content)) return content; string result = content; string pattern = @"(https?://(?:[\w-]+\.)*(?:tb\.cn|taobao\.com|juhuasuan.com|tmall\.com|tmall\.hk)(?:/[^\s]*)?)"; Regex regex = new(pattern); MatchCollection matches = regex.Matches(content); if (matches.Count > 0) { return matches[^1].Value; // 返回第一个匹配到的 URL } return string.Empty; } public static string GetDeeplink(string url) { if (string.IsNullOrEmpty(url)) return "tbopen://m.taobao.com/tbopen/index.html"; string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&h5Url={url.UrlEncode()}&backURL=__back_url__"; return result; } public static string GetTitle(string content) { if (string.IsNullOrEmpty(content)) return string.Empty; string url = GetLink(content); if (!string.IsNullOrEmpty(url)) content = content.Replace(url, string.Empty); content = content.Replace("点击链接直接打开 或者 淘宝搜索直接打开", string.Empty).Trim(); string[] words = Regex.Split(content, @"\s+"); string word = words[^1]; word = word.TrimStart('「').TrimEnd('」'); return word; } static bool IgnoreRegionIncluded(string _ignoreRegions, string? ipInfo, out string regionInfo) { regionInfo = string.Empty; if (string.IsNullOrEmpty(_ignoreRegions) || string.IsNullOrEmpty(ipInfo)) return false; string[] parts = ipInfo.Split('|'); string countryInfo = null; if (parts.Length >= 4) { countryInfo = parts[0]; regionInfo = parts[3]; } if (string.IsNullOrEmpty(regionInfo)) return false; if (string.IsNullOrEmpty(countryInfo)) return false; string[] ignoreRegions = _ignoreRegions.Split('|'); if (ignoreRegions.Contains(regionInfo)) return true; if (ignoreRegions.Contains(countryInfo)) { regionInfo = countryInfo; return true; } foreach (var region in ignoreRegions) { if ("海外".Equals(region) && !"中国".Equals(countryInfo) && !"0".Equals(countryInfo)) { regionInfo = "海外"; return true; } } return false; } public static bool OtherPlatform(string content) { try { //腾讯会议 string wemeetId = ToolParsePlus.GetWemeetId(content); if (!string.IsNullOrEmpty(wemeetId)) return true; //百度网盘 string surl = string.Empty, pwd = string.Empty; _ = ToolParsePlus.GetPanLink(content, ref surl, ref pwd); if (!string.IsNullOrEmpty(surl)) return true; //JD string url = JdUnionPlus.GetLink(content); var urlType = JdUnionPlus.GetLinkType(url, out _, out _); if (urlType != LinkTypeEnum.unknown) return true; } catch { } return false; } public static bool MatchRegexes(string text) { string regex1 = @"(.*?\d{2}\s[a-zA-Z]{2}\d{4}\s.*?[$¥🗝₴€₤£₳✔《💰🔑🎵✔💲🔐📲]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔💲🔐📲]).* [$¥🗝₴€₤£₳✔《💰🔑🎵✔💲🔐📲]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔💲🔐📲](\s*(https?://[^\s]+)?)?\s*([a-zA-Z]{2}\d{4})?\s*(.*)"; Regex r1 = new(regex1); bool match1 = r1.IsMatch(text); return match1; } public static bool FirstFilterIgnoreRequest(string content, out string reason) { reason = string.Empty; try { // 正则过滤 var matchResult = MatchRegexes(content); if (matchResult) return false; reason = "初步筛选"; return true; } catch (Exception ex) { reason = "配置异常"; return true; } } public static bool ShouldIgnoreRequest(string ip, string oaid, out string reason) { reason = string.Empty; try { // IP和流量控制 var config = TkConfigCore.Get(); string ignorePercentageCity = config.ignorePercentageCity; string? ipInfo = IP2RegionPlus.Search(ip); if (IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo)) { reason = $"地区控制:{regionInfo}"; return true; } int ignorePercentage = config.ignorePercentage; if (ignorePercentage == 0) return false; Random random = new Random(); var randomValue = (decimal)random.Next(0, 100); bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求 if (result) reason = "流量控制"; return result; } catch (Exception ex) { reason = $"配置异常"; return true; } } public static TkDataDTO GetFormattedObject(string content, string ip, string oaid) { string shortLinkurl = GetTaobaoLink(content); string deeplink_url = GetDeeplink(shortLinkurl); return new TkDataDTO() { message = string.Empty, channel = TkChannelEnum.tb, accountName = string.Empty, success = false, content = content, link_type = LinkTypeEnum.unknown, ip = ip, oaid = oaid, couponAmount = 0, taoToken = string.Empty, elapsedTime = 0, itemName = "点击打开淘宝APP", shortLinkurl = shortLinkurl, deeplink_url = deeplink_url, create_time = DateTime.Now, }; } public static string OppoDecode(string content) { try { string secretKey = "BwFeIvOEDZy9MFGi0JLZBO4yEhR44DGV"; byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey); if (!content.Contains("%IV%")) return content; string cipherText = content.GetContentPart("", "%IV%"); string ivText = content.GetContentPart("%IV%", ""); byte[] cipherBytes = Convert.FromBase64String(cipherText); byte[] ivBytes = Convert.FromBase64String(ivText); using RijndaelManaged aes = new(); aes.Key = keyBytes; aes.IV = ivBytes; aes.Mode = CipherMode.CFB; aes.Padding = PaddingMode.PKCS7; ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); var outBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length); content = outBytes.ConvertToString(); } catch (Exception e) { } return content; } } }