parse.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. using System.Text;
  2. using dodohold.core;
  3. using System.Text.RegularExpressions;
  4. using System.Net;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using TencentCloud.Ecm.V20190719.Models;
  8. using System.Diagnostics;
  9. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  10. namespace molilian.core
  11. {
  12. public partial class AlimamaPlus
  13. {
  14. private string _accountName;
  15. private int _accountId;
  16. private string _company;
  17. private string _tb_token;
  18. private string _floorId;
  19. private string _refpid;
  20. private string _user_agent;
  21. private string _cookies;
  22. private WebProxy? _proxy = null;
  23. private string _api;
  24. private int _api_id = 0;
  25. public TkConfigDTO _config;
  26. private static string _end_point;
  27. private static TkPoolDTO _account;
  28. static Random _random = new Random();
  29. public static string _url_pattern = @"https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
  30. static AlimamaPlus()
  31. {
  32. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  33. }
  34. public AlimamaPlus(TkPoolDTO account)
  35. {
  36. // string accountName, string tb_token, string floorId, string refpid,
  37. // string cookies, string user_agent, string nodeName
  38. _config = TkConfigCore.Get();
  39. _account = account;
  40. _accountId = account.id;
  41. _accountName = account.name;
  42. _company = account.company;
  43. _tb_token = account.tb_token;
  44. _floorId = account.floorId;
  45. _refpid = account.refpid;
  46. _cookies = account.cookies;
  47. _user_agent = account.user_agent?.Trim();
  48. if (!string.IsNullOrEmpty(account.nodeName))
  49. {
  50. _proxy = ProxyNodesCore.GetOne(account.nodeName);
  51. }
  52. _api = account.api;
  53. _api_id = account.api_id;
  54. }
  55. public bool ShouldIgnoreOtherAff(string content)
  56. {
  57. TkDataDTO result = new();
  58. //(_, _, var link_type, _) = InternalTextProcessing(content, ref result);
  59. InternalTextProcessing(content, ref result);
  60. return result.link_type == LinkTypeEnum.other_aff;
  61. }
  62. public TkDataDTO UnionParse(string content, ref TkDataDTO result)
  63. {
  64. InternalTextProcessing(content, ref result);
  65. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
  66. if (result.link_type == LinkTypeEnum.other_aff)
  67. {
  68. result.channel = TkChannelEnum.tb;
  69. result.channel_type = ChannelTypeEnum.tb;
  70. result.link_type = LinkTypeEnum.unknown;
  71. result.success = false;
  72. result.message = "放弃转链";
  73. result.reason = "其他推广链接";
  74. result.subCode = TkSubCodeEnum.OtherPromo;
  75. result.accountName = string.Empty;
  76. result.content = content;
  77. //result.deeplink_url = GetDeeplink(null);
  78. result.itemName = GetTitle(content);
  79. }
  80. else
  81. {
  82. //处理过后的正文 用于转链
  83. if (result.success) content = result.content;
  84. result = alimamaParse(content, result);
  85. }
  86. result.content = content;
  87. result.link_type = result.link_type;
  88. return result;
  89. }
  90. public async Task<TkDataDTO> UnionParseAsync(string content, int commerceType, TkDataDTO result,
  91. CancellationToken cancellationToken = default,
  92. Dictionary<string, long> swData = null)
  93. {
  94. result.itemName = "点击打开淘宝APP";
  95. result = await InternalTextProcessingAsync(content, result, cancellationToken, swData);
  96. if (result.link_type == LinkTypeEnum.other_aff)
  97. {
  98. //============================== 其他推广链接 ==============================
  99. result.channel = TkChannelEnum.tb;
  100. result.channel_type = ChannelTypeEnum.tb;
  101. result.link_type = LinkTypeEnum.other_aff;
  102. result.success = false;
  103. result.message = "放弃转链";
  104. result.reason = "其他推广链接";
  105. result.subCode = TkSubCodeEnum.OtherPromo;
  106. result.accountName = string.Empty;
  107. result.content = content;
  108. //result.deeplink_url = GetDeeplink(null);
  109. //result.itemName = GetTitle(content);
  110. if (!content.Contains("助我领红包") && !content.Contains("双11超级红包主会场"))
  111. {
  112. //20241108 特例
  113. string ruleKey = $"tmp_rules:{DateTime.Now:yyyyMMdd}_2";
  114. var count = RedisHelper.IncrBy(ruleKey);
  115. if (DateTime.Now < DateTime.Parse("2024-11-11") && count < 1000)
  116. {
  117. string hb_url = "https://s.click.taobao.com/t?union_lens=lensId%3APUB%401730991020%400bbb0a39_0e75_193071ad936_c566%4001%40eyJmbG9vcklkIjozODg1Miiwiic3BtQiiI6Il9wb3J0YWxfdjJfcGFnZXNfYWN0aXZpdHlfb2ZmaWNpYWxfaW5kZXhfaHRtIn0ie%3BeventPageId%3A20150318020007201&e=m%3D2%26s%3DipuVKjYImbNw4vFB6t2Z2iperVdZeJviU%2F9%2F0taeK29yINtkUhsv0KWvzu%2FHMi3%2BFPhC4MirEWStw6LBB4w3HYXT%2BqQ2P5OIQVNSryTmvs7YddWYbxmvGTMNfihJN%2FGnAGIx0oe2X2hZfJ7ZQxC1%2FU3rpAhWgptfEBnEBk3xaGylLmcSHKfaX1CIFRJhZoJ2keMqUwSQcLRC0TzsMy8kp7Hfv3%2BBYcPW3eHn14rW1PB5SunYCHQHRbw6LwQYf%2F1JuyhJem%2BM5OoR2cbDLktPLiIOenYkFIXa2HzNl%2Bm1VXoil%2F2L3b1aA6a%2FoVHlY%2BqZ2kyLuBX5NHCAQVYCmAmuozC39JfnbJNrE%2FSR3F9BSjOYe76q6w0hyfybhwAASyyp5XMWEyz25ulHMwkL2n%2F7nNBiFmadXyjFDPu12TgSnbZtfjDbrVTLHb6MowVEq7z9LgOFRSuRLesvlLykGd2CaRk8ezhE61zXa8yR1eex6ip3KqbOh7CEmgCLaFxcnT%2BrGuviRFaBil8Mlu5kMKse3g%3D%3D";
  118. result.deeplink_url = GetDeeplink(hb_url);
  119. RedisHelper.Expire(ruleKey, 86400 * 10);
  120. }
  121. }
  122. return result;
  123. }
  124. //处理过后的正文 用于转链
  125. if (!result.success)
  126. {
  127. switch (result.link_type)
  128. {
  129. case LinkTypeEnum.profile:
  130. case LinkTypeEnum.live:
  131. case LinkTypeEnum.video:
  132. case LinkTypeEnum.note:
  133. case LinkTypeEnum.goods:
  134. case LinkTypeEnum.baseDomain:
  135. result.reason = string.Empty;
  136. break;
  137. default:
  138. result.reason = "非标准链接";
  139. result.subCode = TkSubCodeEnum.NonStdLink;
  140. return result;
  141. }
  142. }
  143. //============================== 放弃转链-地区过滤 ==============================
  144. bool is_ignore2 = FlowControlIgnoreRequest(out string reason);
  145. if (is_ignore2)
  146. {
  147. result.success = false;
  148. result.message = "放弃转链";
  149. result.reason = reason;
  150. result.subCode = TkSubCodeEnum.TrafficCtrl;
  151. return result;
  152. }
  153. switch (commerceType)
  154. {
  155. case 1:
  156. case 2:
  157. result.success = false;
  158. result.message = "放弃转链";
  159. result.reason = $"厂商放弃{commerceType}";
  160. result.subCode = TkSubCodeEnum.PartnersDrop;
  161. return result;
  162. }
  163. result = await alimamaParseAsync(result.content, result, cancellationToken);
  164. result.commercial = result.success;
  165. if (!result.success)
  166. {
  167. result.itemName = "点击打开淘宝APP";
  168. }
  169. if (string.IsNullOrEmpty(result.itemName) || result.link_type == LinkTypeEnum.profile)
  170. {
  171. result.itemName = "点击打开淘宝APP";
  172. }
  173. return result;
  174. }
  175. public static string ReplaceUrls(string content, string shortLink)
  176. {
  177. if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(shortLink)) return content;
  178. string result = content;
  179. Regex regex = new(_url_pattern);
  180. MatchCollection matches = regex.Matches(content);
  181. foreach (Match m in matches.Cast<Match>())
  182. {
  183. string url = m.Value;
  184. if (url.Contains("https://s.tb.cn/") ||
  185. url.Contains("http://s.tb.cn/") ||
  186. url.Contains("http://m.tb.cn/") ||
  187. url.Contains("http://m.tb.cn/") ||
  188. url.Contains("http://e.tb.cn/") ||
  189. url.Contains("https://e.tb.cn/"))
  190. {
  191. result = result.Replace(url, shortLink);
  192. }
  193. }
  194. return result;
  195. }
  196. public static string RemoveUrls(string content)
  197. {
  198. if (string.IsNullOrEmpty(content)) return content;
  199. string result = content;
  200. Regex regex = new(_url_pattern);
  201. MatchCollection matches = regex.Matches(content);
  202. foreach (Match m in matches.Cast<Match>())
  203. {
  204. string url = m.Value;
  205. result = result.Replace(url, string.Empty);
  206. }
  207. return result;
  208. }
  209. public static string GetLink(string content)
  210. {
  211. if (string.IsNullOrEmpty(content)) return content;
  212. //string pattern = @"https?:\/\/?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?(\?[\S]+)?";
  213. Regex regex = new(_url_pattern);
  214. MatchCollection matches = regex.Matches(content);
  215. string result = string.Empty;
  216. if (matches.Count > 0)
  217. {
  218. result = Regex.Replace(matches[^1].Value, @"\++$", "");
  219. }
  220. result = result.Replace("&nbsp;", "");
  221. if (result.StartsWith("http://e.tb.cn/")) result = result.Replace("http://", "https://");
  222. return result;
  223. }
  224. public static string GetTaobaoLink(string content)
  225. {
  226. if (string.IsNullOrEmpty(content)) return content;
  227. string pattern = @"(https?://(?:[\w-]+\.)*(?:tb\.cn|taobao\.com|juhuasuan\.com|tmall\.com|tmall\.hk)(?:/[a-zA-Z0-9\-\._~:/?#[\]@!$&'()*+,;=%]*)?)";
  228. Regex regex = new(pattern, RegexOptions.IgnoreCase);
  229. MatchCollection matches = regex.Matches(content);
  230. if (matches.Count > 0)
  231. {
  232. string result = Regex.Replace(matches[^1].Value, @"\++$", "");
  233. return result;
  234. }
  235. return string.Empty;
  236. }
  237. public static string GetDeeplink(string url)
  238. {
  239. if (string.IsNullOrEmpty(url)) return "tbopen://m.taobao.com/tbopen/index.html";
  240. string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&h5Url={url.UrlEncode()}";
  241. //string result = $"tbopen://m.taobao.com/tbopen/index.html?action=ali.open.nav&module=h5&bootImage=0&afcPromotionOpen=false&bc_fl_src=h5_huanduan&source=slk_dp&h5Url={url.UrlEncode()}";
  242. return result;
  243. }
  244. public static string GetTitle(string content)
  245. {
  246. if (string.IsNullOrEmpty(content)) return string.Empty;
  247. string url = GetLink(content);
  248. if (!string.IsNullOrEmpty(url)) content = content.Replace(url, string.Empty);
  249. content = content.Replace("点击链接直接打开 或者 淘宝搜索直接打开", string.Empty).Trim();
  250. string[] words = Regex.Split(content, @"\s+");
  251. string word = words[^1];
  252. word = word.TrimStart('「').TrimEnd('」');
  253. return word;
  254. }
  255. public static bool IgnoreRegionIncluded(string _ignoreRegions, string? ipInfo, out string regionInfo)
  256. {
  257. regionInfo = string.Empty;
  258. if (string.IsNullOrEmpty(_ignoreRegions) || string.IsNullOrEmpty(ipInfo)) return false;
  259. string[] parts = ipInfo.Split('|');
  260. string countryInfo = null;
  261. if (parts.Length >= 4)
  262. {
  263. countryInfo = parts[0];
  264. regionInfo = parts[3];
  265. }
  266. if (string.IsNullOrEmpty(regionInfo)) return false;
  267. if (string.IsNullOrEmpty(countryInfo)) return false;
  268. string[] ignoreRegions = _ignoreRegions.Split('|');
  269. if (ignoreRegions.Contains(regionInfo)) return true;
  270. if (ignoreRegions.Contains(countryInfo))
  271. {
  272. regionInfo = countryInfo;
  273. return true;
  274. }
  275. foreach (var region in ignoreRegions)
  276. {
  277. if ("海外".Equals(region) && !"中国".Equals(countryInfo) && !"0".Equals(countryInfo))
  278. {
  279. regionInfo = "海外";
  280. return true;
  281. }
  282. }
  283. return false;
  284. }
  285. public static async Task<bool> OtherPlatformAsync(string content)
  286. {
  287. try
  288. {
  289. //腾讯会议
  290. string wemeetId = await ToolParsePlus.GetWemeetIdAsync(content);
  291. if (!string.IsNullOrEmpty(wemeetId)) return true;
  292. //百度网盘
  293. string surl = string.Empty, pwd = string.Empty;
  294. _ = ToolParsePlus.GetPanLink(content, ref surl, ref pwd);
  295. if (!string.IsNullOrEmpty(surl)) return true;
  296. //JD
  297. string url = JdUnionPlus.GetLink(content);
  298. var urlType = JdUnionPlus.GetLinkType(url, out _, out _);
  299. if (urlType != LinkTypeEnum.unknown) return true;
  300. //DY
  301. url = DyUnionPlus.GetLink(content);
  302. urlType = DyUnionPlus.GetLinkType(content, url);
  303. if (urlType != LinkTypeEnum.unknown) return true;
  304. }
  305. catch { }
  306. return false;
  307. }
  308. public static bool MatchRegexes(string text, List<string> extended)
  309. {
  310. text = RemoveUrls(text);
  311. string special_symbols = $"\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲";
  312. string[] base_patterns =
  313. [
  314. $@"(.*?\d{{2}}\s[a-zA-Z]{{2}}\d{{4}}\s.*?[{special_symbols}]+[a-zA-Z0-9\s]+[{special_symbols}]).*",
  315. $@"[{special_symbols}]+\s*([a-zA-Z]{{1,2}}\d{{2,4}}\s[a-zA-Z0-9]{{11}}|[a-zA-Z0-9]{{11}}[a-zA-Z]{{1,2}}\d{{2,4}}\s|[a-zA-Z0-9]{{11}})\s*[{special_symbols}]+",
  316. @"🍑♭ɑ◑下单|Tao宝"
  317. ];
  318. List<string> mergedArray = extended.Union(base_patterns).ToList();
  319. /*
  320. (.*?\\d{2}\\s[a-zA-Z]{2}\\d{4}\\s.*?[\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲]+[a-zA-Z0-9\\s]+[\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲])).*\n[\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲]+\\s*([a-zA-Z]{1,2}\\d{2,4}\\s[a-zA-Z0-9]{11}|[a-zA-Z0-9]{11}[a-zA-Z]{1,2}\\d{2,4}\\s|[a-zA-Z0-9]{11})\\s*[\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲]+\n🍑♭ɑ◑下单|Tao宝
  321. 【3瓶】恩威万蓝莓叶黄素酯软糖儿童成人上班族如酸涩肿胀可服用,<z5TYWAblvTw >:// HU7709,打開/
  322. 置顶小助手,方便下次查询\r\n1★【劵】无\r\n2★【返.佣】 0.29圆 \r\n3★【付款.价】24.9 圆\r\n \r\n 1 ¢HZv5WAboi0D¢(/:/ HU8866\r\
  323. n长按覆制本内容,打开TaoBao/\r\n-\r\n返佣是指商家的推广佣.金\r\n通过我回复的口令下単收货后,我
  324. 会微信红.苞返给你!!\r\n\r\n有疑问可回复“人工”咨询客服*/
  325. //🍑🔗上 水和肌精华水& CZ0 VWdCWC2gj97&,
  326. //6฿04ZDWzrwxjN)/ CA97
  327. //1😘 腹制这条(CZ00 iHvtWzrXa7O(:/
  328. //¥EC8DG4Zj0dhd8DOw¥MF6563 oIo0WzOF1Cp##X-PyVpHiPlyj1OP##
  329. //6.6₵vU2jWzIW3Qx₲/ CZ5224
  330. //$F56nWzIDSFZ$
  331. //0復製这条口令( CZ00 D0M4WzI0jFJ¢,📱咑開[Tao宝]就可下单:/
  332. //8fuzhi本条消息:¤ CZ3458 JSR1WzrPCBU¤,打开🍑♭ɑ◑下单/
  333. //5€Lz6SWzrkLDw¥:// CZ13
  334. //3₲Igq3Wzkc5np₳:// CZ15
  335. //9.9🎈【康师傅易拉罐6罐】口伶:6₰TnxKWzr0wff₪:// CZ69
  336. //4₲99Y0WzJdK6S₳:// CZ46
  337. //4fuzhi本条消息:¤ CZ0002 WVCSWzrk6Ix¤,打开🍑♭ɑ◑下单/
  338. bool anyMatch = false;
  339. foreach (string pattern in mergedArray)
  340. {
  341. if (Regex.IsMatch(text, pattern, RegexOptions.IgnoreCase))
  342. {
  343. anyMatch = true;
  344. }
  345. }
  346. return anyMatch;
  347. }
  348. public static bool MatchOtherInfo(string text, List<string> extended)
  349. {
  350. string[] base_patterns =
  351. [
  352. "[ďȌƯƔɪƝɖƟɏƖԂȎȗϓɫɧơȲǏđǫƲƳɨǹƊȮǚƴɬɲƿȖȳɳɗʮϔӢǙɦɒɎƥʘưΊƞǪȈʠΌǓϒίȠƠƱƗǸþŌŬÿĩŅÞÓũŸÎŎûįŇĎÖÙŷÏňÜÝÍñĎÒüŶľńĎ0ųŷÏŇdǒǔyí℡抖yí℡ÞŐûŷÌñ]{6}",
  353. @"(?:.+)极速版(?:口令|搜索)",
  354. @"##[a-zA-Z0-9]+##\[抖音(?:极速版)?(?:口令|搜索)\]",
  355. @"岽|亰|菄|京东|婛",
  356. "打开【剪映】",
  357. "weishi://|baiduhaokan://",
  358. "复制打开「UC浏览器」|UC瀏覽器",
  359. "番茄免费小说",
  360. @"\?chanTag=[^&\s]*口令",
  361. @"(https?://pan\.baidu\.com/s/[A-Za-z0-9?\-_=]+)",
  362. @"https?:\/\/meeting\.tencent\.com[^\s]*",
  363. @"#腾讯会议:(\d{3}-\d{3}-\d{3})"
  364. ];
  365. List<string> mergedArray = extended.Union(base_patterns).ToList();
  366. bool anyMatch = false;
  367. foreach (string pattern in mergedArray)
  368. {
  369. if (Regex.IsMatch(text, pattern, RegexOptions.IgnoreCase))
  370. {
  371. anyMatch = true;
  372. }
  373. }
  374. return anyMatch;
  375. }
  376. public static bool MatchMultiToken(string text, List<string> extended)
  377. {
  378. text = RemoveUrls(text);
  379. string special_symbols = $"\\$\\(\\)\\/\\#\\&\\<\\>฿¢¥¥$🗝₰₵₲¤₪₴€₤£₳《¢💰🔑🎵✔💲🔐📲";
  380. string[] base_patterns =
  381. [
  382. @"[!$¥%]+([a-zA-Z0-9]{16})[!$¥%]+",
  383. @"\^v(\d+#[a-zA-Z0-9]+)(?:\?.*)?\^",
  384. "##([a-zA-Z0-9\\-]+)##",
  385. $@"(.*?\d{{2}}\s[a-zA-Z]{{2}}\d{{4}}\s.*?[{special_symbols}]+[a-zA-Z0-9\s]+[{special_symbols}]).*",
  386. $@"[{special_symbols}]+\s*([a-zA-Z]{{1,2}}\d{{2,4}}\s[a-zA-Z0-9]{{11}}|[a-zA-Z0-9]{{11}}[a-zA-Z]{{1,2}}\d{{2,4}}\s|[a-zA-Z0-9]{{11}})\s*[{special_symbols}]+",
  387. ];
  388. // PAGE6:/^v130091#P2J6EDxN8p8?chanTag=d9dc522bdea27437514052ea50f6a787^
  389. List<string> mergedArray = extended.Union(base_patterns).ToList();
  390. int matchCount = 0;
  391. foreach (string pattern in mergedArray)
  392. {
  393. Match match = Regex.Match(text, pattern, RegexOptions.IgnoreCase);
  394. if (match.Success)
  395. {
  396. matchCount++;
  397. text = text.Replace(match.Groups[^1].Value, ""); // 移除匹配的部分文本
  398. }
  399. }
  400. return matchCount >= 2;
  401. }
  402. public static bool FilterRiskLink(string ip, string oaid, string content, out string reason)
  403. {
  404. reason = string.Empty;
  405. string itemId = ExtractItemId(content);
  406. if (string.IsNullOrEmpty(itemId)) return false;
  407. string cacheKey = $":cache:parse:{ip}_{oaid}_{itemId}";
  408. int flag = RedisHelper.Get<int>(cacheKey);
  409. if (flag > 0)
  410. {
  411. reason = $"口令用户";
  412. return true;
  413. }
  414. return false;
  415. }
  416. public static bool ShouldIgnoreRequest(string ip, string oaid, out string reason)
  417. {
  418. reason = string.Empty;
  419. try
  420. {
  421. // IP和流量控制
  422. var config = TkConfigCore.Get();
  423. string ignorePercentageCity = config.ignorePercentageCity;
  424. string? ipInfo = IP2RegionPlus.Search(ip);
  425. if (IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  426. {
  427. reason = $"地区控制:{regionInfo}";
  428. return true;
  429. }
  430. if (TkLogCore.InBlacklist(config.blacklist_oaid, oaid))
  431. {
  432. reason = "OAID黑名单";
  433. return true;
  434. }
  435. int limit_num = config.tk_limit_per_ip_24h;
  436. if (!"127.0.0.1".Equals(ip) && limit_num > 0)
  437. {
  438. int num = TkLogCore.getClientRequestTotalByIp(TkChannelEnum.tb, ip);
  439. if (num >= limit_num)
  440. {
  441. reason = "IP控制";
  442. return true;
  443. }
  444. }
  445. limit_num = config.tk_limit_per_oaid_24h;
  446. if (limit_num > 0)
  447. {
  448. int num = TkLogCore.getClientRequestTotalByOAID(TkChannelEnum.tb, oaid);
  449. if (num >= limit_num)
  450. {
  451. reason = "OAID控制";
  452. return true;
  453. }
  454. }
  455. }
  456. catch (Exception ex)
  457. {
  458. reason = "配置异常";
  459. }
  460. return false;
  461. }
  462. public static bool FlowControlIgnoreRequest(out string reason)
  463. {
  464. reason = string.Empty;
  465. try
  466. {
  467. // IP和流量控制
  468. var config = TkConfigCore.Get();
  469. int ignorePercentage = config.ignorePercentage;
  470. if (ignorePercentage == 0) return false;
  471. var randomValue = (decimal)_random.Next(0, 100);
  472. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  473. if (result)
  474. {
  475. reason = "流量控制";
  476. return true;
  477. }
  478. }
  479. catch (Exception ex)
  480. {
  481. reason = $"配置异常";
  482. }
  483. return false;
  484. }
  485. public static TkDataDTO GetFormattedObject(string content, string ip, string oaid)
  486. {
  487. string shortLinkurl = GetTaobaoLink(content);
  488. string deeplink_url = GetDeeplink(shortLinkurl);
  489. return new TkDataDTO()
  490. {
  491. message = string.Empty,
  492. subCode = TkSubCodeEnum.Other,
  493. channel = TkChannelEnum.tb,
  494. accountName = string.Empty,
  495. success = false,
  496. content = content,
  497. link_type = LinkTypeEnum.unknown,
  498. ip = ip,
  499. oaid = oaid,
  500. couponAmount = 0,
  501. taoToken = string.Empty,
  502. elapsedTime = 0,
  503. itemName = "点击打开淘宝APP",
  504. shortLinkurl = shortLinkurl,
  505. deeplink_url = deeplink_url,
  506. create_time = DateTime.Now,
  507. end_point = _end_point,
  508. };
  509. }
  510. public static string OppoDecode(string content)
  511. {
  512. try
  513. {
  514. string secretKey = "BwFeIvOEDZy9MFGi0JLZBO4yEhR44DGV";
  515. byte[] keyBytes = Encoding.UTF8.GetBytes(secretKey);
  516. if (!content.Contains("%IV%")) return content;
  517. string cipherText = content.GetContentPart("", "%IV%");
  518. string ivText = content.GetContentPart("%IV%", "");
  519. byte[] cipherBytes = Convert.FromBase64String(cipherText);
  520. byte[] ivBytes = Convert.FromBase64String(ivText);
  521. using RijndaelManaged aes = new();
  522. aes.Key = keyBytes;
  523. aes.IV = ivBytes;
  524. aes.Mode = CipherMode.CFB;
  525. aes.Padding = PaddingMode.PKCS7;
  526. ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
  527. var outBytes = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
  528. content = outBytes.ConvertToString();
  529. }
  530. catch (Exception e) { }
  531. return content;
  532. }
  533. }
  534. }