| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- 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;
- public AlimamaPlus(TkPoolDTO account)
- {
- // string accountName, string tb_token, string floorId, string refpid,
- // string cookies, string user_agent, string nodeName
- _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)
- {
- (_, _, var link_type, _) = InternalTextProcessing(content);
- return link_type == LinkTypeEnum.other_aff;
- }
- public TkDataDTO UnionParse(string content, ref TkDataDTO 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);
- }
- 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<TkDataDTO> UnionParseAsync(string content, TkDataDTO 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);
- }
- else
- {
- //处理过后的正文 用于转链
- if (result.success) content = result.content;
- result.content = content;
- result = await alimamaParseAsync(content, result);
- }
- 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<Match>())
- {
- 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]+)?";
- 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);
- content = content.Replace(url, string.Empty);
- content = content.Replace("点击链接直接打开 或者 淘宝搜索直接打开", string.Empty).Trim();
- string[] words = Regex.Split(content, @"\s+");
- string word = words[^1];
- 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 ShouldIgnoreRequest(string ip, string oaid, out string reason)
- {
- reason = string.Empty;
- try
- {
- 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 = GetLink(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,
- 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;
- }
- }
- }
|