PddPoolCore.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using Google.Protobuf.WellKnownTypes;
  11. using System.Diagnostics;
  12. using System.Text.Json;
  13. using TencentCloud.Tcm.V20210413.Models;
  14. using System.Security.Cryptography;
  15. using System.Collections.Concurrent;
  16. using YunhuiKit;
  17. using System.Xml.Linq;
  18. namespace molilian.core
  19. {
  20. public partial class PddPoolCore
  21. {
  22. private static readonly object _lockObj = new();
  23. private static IEnumerable<PddPoolDTO> _cached;
  24. private static IEnumerable<PddPoolDTO> _all_cached;
  25. private static string _end_point;
  26. private static Dictionary<string, decimal> _incomeAmt = new();
  27. private static ConcurrentDictionary<int, DateTime> _suspend = new();
  28. // 移除本地内存统计,改用 Redis 存储
  29. private static SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  30. static PddPoolCore()
  31. {
  32. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  33. }
  34. /// <summary>
  35. /// 获取一个账号用于处理请求
  36. /// 使用平滑加权轮询算法(Smooth Weighted Round-Robin)实现流量均衡分配
  37. /// </summary>
  38. public static PddPoolDTO? GetOne(PddUnionWorkMode mode, int accountid = 0, string parse_type = "")
  39. {
  40. var list = List();
  41. if (!list.Any()) return null;
  42. // 按 parse_type 过滤
  43. if ("dp".Equals(parse_type))
  44. {
  45. list = list.Where(item => "dp".Equals(item.parse_type)).ToList();
  46. if (!list.Any()) return null;
  47. }
  48. else
  49. {
  50. list = list.Where(item => string.IsNullOrEmpty(item.parse_type)).ToList();
  51. if (!list.Any()) return null;
  52. }
  53. // 过滤出符合条件的候选账号
  54. var candidates = list.Where(e => IsMatch(e, mode, accountid)).ToList();
  55. if (!candidates.Any()) return null;
  56. // 使用平滑加权轮询算法选择账号
  57. return SmoothWeightedRoundRobin(candidates, parse_type);
  58. }
  59. /// <summary>
  60. /// 基于最后使用时间的调度策略(并发安全版本)
  61. /// 选择"距离上次使用时间最长"且"超过cis_limit间隔"的账号
  62. ///
  63. /// 优势:
  64. /// 1. 不区分新老账号,一视同仁(无历史记录的账号返回DateTime.MinValue,自然会被优先选中)
  65. /// 2. 自然满足cis_limit要求(距离时间 < limit 会被跳过)
  66. /// 3. 优先选择"休息最久"的账号,最大化每个账号的休息时间
  67. /// 4. 简单直观,易于调试和理解
  68. /// 5. 并发安全:使用Redis原子操作预留账号,避免多个请求竞争同一账号
  69. /// </summary>
  70. private static PddPoolDTO? SmoothWeightedRoundRobin(List<PddPoolDTO> candidates, string parse_type)
  71. {
  72. if (!candidates.Any()) return null;
  73. if (candidates.Count == 1)
  74. {
  75. var single = candidates[0];
  76. // 尝试预留账号(并发安全)
  77. if (TryReserveAccount(single.id, parse_type, single.cis_limit))
  78. {
  79. return single;
  80. }
  81. return null; // 预留失败,说明账号已被其他请求占用
  82. }
  83. try
  84. {
  85. string groupKey = string.IsNullOrEmpty(parse_type) ? "default" : parse_type;
  86. var now = DateTime.Now;
  87. // 候选账号列表:记录每个账号的空闲时长
  88. var accountsWithIdleTime = new List<(PddPoolDTO account, long idleMs)>();
  89. foreach (var account in candidates)
  90. {
  91. // 获取账号最后使用时间
  92. var lastUsedTime = GetAccountLastUsedTime(account.id, groupKey);
  93. // 计算距离现在的时间差(毫秒)
  94. long idleMs = (long)(now - lastUsedTime).TotalMilliseconds;
  95. // 如果设置了 cis_limit,必须满足间隔要求
  96. if (account.cis_limit > 0 && idleMs < account.cis_limit)
  97. {
  98. continue; // 跳过未满足间隔要求的账号
  99. }
  100. accountsWithIdleTime.Add((account, idleMs));
  101. }
  102. // 如果没有满足条件的账号,返回null
  103. if (!accountsWithIdleTime.Any()) return null;
  104. // 按照空闲时间降序排序,优先尝试"休息最久"的账号
  105. var sortedAccounts = accountsWithIdleTime
  106. .OrderByDescending(x => x.idleMs)
  107. .ToList();
  108. // 依次尝试预留账号(从空闲时间最长的开始)
  109. foreach (var (account, idleMs) in sortedAccounts)
  110. {
  111. // 尝试原子预留此账号
  112. if (TryReserveAccount(account.id, groupKey, account.cis_limit))
  113. {
  114. // 预留成功,更新最后使用时间
  115. SetAccountLastUsedTime(account.id, groupKey, now);
  116. Console.WriteLine($"[PDD] Selected account {account.id} ({account.name}), idle time: {Math.Round(idleMs / 1000.0, 2)}s");
  117. return account;
  118. }
  119. else
  120. {
  121. // 预留失败,说明此账号已被其他并发请求占用,尝试下一个
  122. Console.WriteLine($"[PDD] Account {account.id} ({account.name}) is reserved by another request, trying next...");
  123. }
  124. }
  125. // 所有满足条件的账号都已被占用
  126. Console.WriteLine($"[PDD] All available accounts are reserved, no account available");
  127. return null;
  128. }
  129. catch (Exception ex)
  130. {
  131. Console.WriteLine($"Idle time selection failed: {ex.Message}, fallback to simple strategy");
  132. // 降级策略:使用原有的最少使用次数算法
  133. return candidates
  134. .OrderBy(account => GetCurrentDayUsageFromRedis(account.id))
  135. .ThenBy(account => Guid.NewGuid())
  136. .FirstOrDefault();
  137. }
  138. }
  139. /// <summary>
  140. /// 尝试原子预留账号(并发安全)
  141. /// 使用 Redis SET NX EX 实现原子操作,避免多个请求竞争同一账号
  142. /// </summary>
  143. /// <param name="accountId">账号ID</param>
  144. /// <param name="groupKey">分组键</param>
  145. /// <param name="cisLimit">请求间隔限制(毫秒),0表示无限制</param>
  146. /// <returns>true=预留成功,false=账号已被占用</returns>
  147. private static bool TryReserveAccount(int accountId, string groupKey, int cisLimit)
  148. {
  149. try
  150. {
  151. string reserveKey = $"pdd_account_reserved:{groupKey}:{accountId}";
  152. // 如果没有设置 cis_limit,使用默认100ms的预留时间(防止极端并发)
  153. int expireSeconds = cisLimit > 0 ? (int)Math.Ceiling(cisLimit / 1000.0) : 1;
  154. // 使用 Redis SET NX EX 原子操作(一次性完成设置和过期时间)
  155. // 等价于 Redis 命令: SET key value NX EX seconds
  156. // 如果 key 不存在则设置成功(返回true),如果已存在则失败(返回false)
  157. // 注意:必须使用原子操作,SetNx + Expire 两步操作会有竞态条件!
  158. bool reserved = RedisHelper.Set(reserveKey, 1, expireSeconds, CSRedis.RedisExistence.Nx);
  159. return reserved;
  160. }
  161. catch (Exception ex)
  162. {
  163. Console.WriteLine($"Failed to reserve account {accountId}: {ex.Message}");
  164. // 预留失败时保守处理:假设账号不可用
  165. return false;
  166. }
  167. }
  168. /// <summary>
  169. /// 获取账号最后使用时间
  170. /// </summary>
  171. private static DateTime GetAccountLastUsedTime(int accountId, string groupKey)
  172. {
  173. try
  174. {
  175. string key = $"pdd_last_used_time:{groupKey}:{accountId}";
  176. var timestamp = RedisHelper.Get<long>(key);
  177. if (timestamp > 0)
  178. {
  179. return DateTimeOffset.FromUnixTimeMilliseconds(timestamp).LocalDateTime;
  180. }
  181. // 如果没有记录,返回很久以前的时间(确保新账号和长期未使用的账号能被优先选中)
  182. return DateTime.MinValue;
  183. }
  184. catch
  185. {
  186. return DateTime.MinValue;
  187. }
  188. }
  189. /// <summary>
  190. /// 设置账号最后使用时间
  191. /// </summary>
  192. private static void SetAccountLastUsedTime(int accountId, string groupKey, DateTime time)
  193. {
  194. try
  195. {
  196. string key = $"pdd_last_used_time:{groupKey}:{accountId}";
  197. long timestamp = new DateTimeOffset(time).ToUnixTimeMilliseconds();
  198. // 设置过期时间为24小时,避免数据积累
  199. RedisHelper.Set(key, timestamp, 86400);
  200. }
  201. catch (Exception ex)
  202. {
  203. Console.WriteLine($"Failed to set last used time for {accountId}: {ex.Message}");
  204. }
  205. }
  206. /// <summary>
  207. /// 获取账号的静态权重
  208. /// 可以根据账号的 daily_calls_limit 或其他因素动态计算
  209. /// </summary>
  210. private static int GetAccountStaticWeight(int accountId)
  211. {
  212. try
  213. {
  214. string key = $"pdd_account_weight:{accountId}";
  215. int weight = RedisHelper.Get<int>(key);
  216. // 如果没有设置,返回默认权重100
  217. if (weight <= 0)
  218. {
  219. // 可以基于账号的限额动态计算默认权重
  220. var account = List()?.FirstOrDefault(a => a.id == accountId);
  221. if (account != null && account.daily_calls_limit > 0)
  222. {
  223. // 将日限额映射到权重:每1000次调用对应权重10
  224. weight = Math.Max(10, Math.Min(1000, account.daily_calls_limit / 100));
  225. }
  226. else
  227. {
  228. weight = 100; // 默认权重
  229. }
  230. }
  231. return weight;
  232. }
  233. catch
  234. {
  235. return 100; // 异常时返回默认权重
  236. }
  237. }
  238. /// <summary>
  239. /// 设置账号的静态权重
  240. /// </summary>
  241. public static void SetAccountStaticWeight(int accountId, int weight)
  242. {
  243. try
  244. {
  245. if (weight < 1) weight = 1;
  246. if (weight > 1000) weight = 1000;
  247. string key = $"pdd_account_weight:{accountId}";
  248. RedisHelper.Set(key, weight, 30 * 86400); // 30天过期
  249. }
  250. catch (Exception ex)
  251. {
  252. Console.WriteLine($"Failed to set account weight for {accountId}: {ex.Message}");
  253. }
  254. }
  255. /// <summary>
  256. /// 获取账号的当前动态权重
  257. /// </summary>
  258. private static int GetAccountCurrentWeight(int accountId, string groupKey)
  259. {
  260. try
  261. {
  262. string key = $"pdd_current_weight:{groupKey}:{accountId}";
  263. return RedisHelper.Get<int>(key);
  264. }
  265. catch
  266. {
  267. return 0;
  268. }
  269. }
  270. /// <summary>
  271. /// 设置账号的当前动态权重
  272. /// </summary>
  273. private static void SetAccountCurrentWeight(int accountId, string groupKey, int weight)
  274. {
  275. try
  276. {
  277. string key = $"pdd_current_weight:{groupKey}:{accountId}";
  278. RedisHelper.Set(key, weight, 3600); // 1小时过期,自动重置
  279. }
  280. catch (Exception ex)
  281. {
  282. Console.WriteLine($"Failed to set current weight for {accountId}: {ex.Message}");
  283. }
  284. }
  285. /// <summary>
  286. /// 重置所有账号的动态权重(用于调试或重新初始化)
  287. /// </summary>
  288. public static void ResetAllWeights(string groupKey = "default")
  289. {
  290. try
  291. {
  292. var accounts = List()?.Where(a => a.status && a.enable_parse).ToList();
  293. if (accounts == null || !accounts.Any()) return;
  294. foreach (var account in accounts)
  295. {
  296. string key = $"pdd_current_weight:{groupKey}:{account.id}";
  297. RedisHelper.Del(key);
  298. }
  299. Console.WriteLine($"Reset weights for {accounts.Count} accounts in group '{groupKey}'");
  300. }
  301. catch (Exception ex)
  302. {
  303. Console.WriteLine($"Failed to reset weights: {ex.Message}");
  304. }
  305. }
  306. private static int GetCurrentDayUsageFromRedis(int accountId)
  307. {
  308. try
  309. {
  310. var today = DateTime.Now.ToString("yyyyMMdd");
  311. string key = $"pdd_daily_usage:{accountId}:{today}";
  312. return RedisHelper.Get<int>(key);
  313. }
  314. catch (Exception)
  315. {
  316. return 0;
  317. }
  318. }
  319. internal static void UpdateAccountUsage(int accountId)
  320. {
  321. try
  322. {
  323. var today = DateTime.Now.ToString("yyyyMMdd");
  324. string key = $"pdd_daily_usage:{accountId}:{today}";
  325. // 递增计数并设置24小时过期时间
  326. RedisHelper.IncrBy(key);
  327. RedisHelper.Expire(key, 86400); // 24小时后自动过期
  328. }
  329. catch (Exception ex)
  330. {
  331. // 记录错误但不影响主流程
  332. Console.WriteLine($"Failed to update account usage for {accountId}: {ex.Message}");
  333. }
  334. }
  335. /// <summary>
  336. /// 获取当前所有账号的平均使用次数
  337. /// </summary>
  338. private static int GetAverageUsageCount()
  339. {
  340. try
  341. {
  342. var activeAccounts = List()?.Where(a => a.enable_parse).ToList();
  343. if (activeAccounts == null || !activeAccounts.Any()) return 0;
  344. var today = DateTime.Now.ToString("yyyyMMdd");
  345. var totalUsage = 0;
  346. var validCount = 0;
  347. foreach (var account in activeAccounts)
  348. {
  349. var usage = GetCurrentDayUsageFromRedis(account.id);
  350. totalUsage += usage;
  351. validCount++;
  352. }
  353. return validCount > 0 ? totalUsage / validCount : 0;
  354. }
  355. catch (Exception ex)
  356. {
  357. Console.WriteLine($"Failed to get average usage count: {ex.Message}");
  358. return 0;
  359. }
  360. }
  361. /// <summary>
  362. /// 重置所有在线账号的当日调用次数Redis计数器
  363. /// 随机排序账号后,每个账号间隔1递增value
  364. /// </summary>
  365. public static int RechargeAllOnlineAccountUsage()
  366. {
  367. try
  368. {
  369. var onlineAccounts = List()?.Where(a => a.status && a.enable_parse).ToList();
  370. if (onlineAccounts == null || !onlineAccounts.Any())
  371. {
  372. return 0;
  373. }
  374. var today = DateTime.Now.ToString("yyyyMMdd");
  375. // 随机排序账号
  376. var random = new Random();
  377. var shuffledAccounts = onlineAccounts.OrderBy(x => random.Next()).ToList();
  378. int incrementValue = 1;
  379. int rechargedCount = 0;
  380. foreach (var account in shuffledAccounts)
  381. {
  382. string key = $"pdd_daily_usage:{account.id}:{today}";
  383. // 设置递增的value并设置24小时过期时间
  384. RedisHelper.Set(key, incrementValue, 86400);
  385. RedisHelper.Set(key, 0, 86400);
  386. incrementValue++;
  387. rechargedCount++;
  388. }
  389. return rechargedCount;
  390. }
  391. catch (Exception ex)
  392. {
  393. }
  394. return 0;
  395. }
  396. /// <summary>
  397. /// 为新上线的账号设置平均使用次数(仅用于降级策略的统计)
  398. /// 注意:基于时间间隔的调度策略不需要初始化权重,因为无历史记录的账号会自动返回DateTime.MinValue
  399. /// </summary>
  400. public static void InitializeNewAccountUsage(int accountId, string groupKey = "default")
  401. {
  402. try
  403. {
  404. var today = DateTime.Now.ToString("yyyyMMdd");
  405. string usageKey = $"pdd_daily_usage:{accountId}:{today}";
  406. // 检查该账号今天是否已有使用记录
  407. var currentUsage = RedisHelper.Get<int>(usageKey);
  408. if (currentUsage > 0)
  409. {
  410. Console.WriteLine($"Account {accountId} already has usage record: {currentUsage}, skip initialization");
  411. return; // 已有记录,不需要初始化
  412. }
  413. // 获取所有在线账号的平均值(仅用于降级策略)
  414. var activeAccounts = List()?.Where(a => a.status && a.enable_parse && a.id != accountId).ToList();
  415. if (activeAccounts == null || !activeAccounts.Any())
  416. {
  417. Console.WriteLine($"No other active accounts found, account {accountId} will use default value");
  418. return; // 没有其他账号,使用默认值0即可
  419. }
  420. // 初始化 daily_usage(用于降级策略的统计)
  421. var totalUsage = 0;
  422. foreach (var account in activeAccounts)
  423. {
  424. totalUsage += GetCurrentDayUsageFromRedis(account.id);
  425. }
  426. var averageUsage = totalUsage / activeAccounts.Count;
  427. if (averageUsage > 0)
  428. {
  429. RedisHelper.Set(usageKey, averageUsage, 86400);
  430. Console.WriteLine($"Initialized account {accountId} daily_usage with average: {averageUsage}");
  431. }
  432. Console.WriteLine($"Account {accountId} initialization complete. Time-based scheduling will use DateTime.MinValue for idle time calculation.");
  433. }
  434. catch (Exception ex)
  435. {
  436. Console.WriteLine($"Failed to initialize new account for {accountId}: {ex.Message}");
  437. }
  438. }
  439. /// <summary>
  440. /// 获取当前统计信息(用于监控和调试)- 改为从Redis获取
  441. /// </summary>
  442. internal static Dictionary<string, int> GetCurrentUsageSnapshot()
  443. {
  444. try
  445. {
  446. var result = new Dictionary<string, int>();
  447. var accounts = List()?.Where(a => a.status && a.enable_parse).ToList();
  448. if (accounts == null || !accounts.Any()) return result;
  449. var today = DateTime.Now.ToString("yyyyMMdd");
  450. foreach (var account in accounts)
  451. {
  452. string key = $"pdd_daily_usage:{account.id}:{today}";
  453. int usage = RedisHelper.Get<int>(key);
  454. result[$"Account_{account.id}_{account.name}"] = usage;
  455. }
  456. return result;
  457. }
  458. catch (Exception)
  459. {
  460. return new Dictionary<string, int>();
  461. }
  462. }
  463. /// <summary>
  464. /// 获取账号调度时间间隔信息(用于监控和调试)
  465. /// </summary>
  466. public static Dictionary<string, object> GetScheduleWeightSnapshot(string groupKey = "default")
  467. {
  468. try
  469. {
  470. var result = new Dictionary<string, object>();
  471. var accounts = List()?.Where(a => a.status && a.enable_parse).ToList();
  472. if (accounts == null || !accounts.Any()) return result;
  473. var now = DateTime.Now;
  474. var accountInfos = new List<Dictionary<string, object>>();
  475. foreach (var account in accounts)
  476. {
  477. var lastUsedTime = GetAccountLastUsedTime(account.id, groupKey);
  478. long idleMs = (long)(now - lastUsedTime).TotalMilliseconds;
  479. bool canUse = account.cis_limit <= 0 || idleMs >= account.cis_limit;
  480. var info = new Dictionary<string, object>
  481. {
  482. ["account_id"] = account.id,
  483. ["account_name"] = account.name,
  484. ["cis_limit_ms"] = account.cis_limit,
  485. ["last_used_time"] = lastUsedTime == DateTime.MinValue ? "从未使用" : lastUsedTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
  486. ["idle_time_ms"] = idleMs,
  487. ["idle_time_seconds"] = Math.Round(idleMs / 1000.0, 2),
  488. ["can_use"] = canUse,
  489. ["daily_usage"] = GetCurrentDayUsageFromRedis(account.id),
  490. ["daily_limit"] = account.daily_calls_limit
  491. };
  492. accountInfos.Add(info);
  493. }
  494. // 按照空闲时间降序排序(与选择逻辑一致)
  495. accountInfos = accountInfos.OrderByDescending(x => (long)x["idle_time_ms"]).ToList();
  496. result["accounts"] = accountInfos;
  497. result["group_key"] = groupKey;
  498. result["timestamp"] = now.ToString("yyyy-MM-dd HH:mm:ss.fff");
  499. result["total_accounts"] = accountInfos.Count;
  500. result["available_accounts"] = accountInfos.Count(x => (bool)x["can_use"]);
  501. return result;
  502. }
  503. catch (Exception ex)
  504. {
  505. return new Dictionary<string, object> { ["error"] = ex.Message };
  506. }
  507. }
  508. /// <summary>
  509. /// 强制清理统计数据(仅用于测试或紧急情况)- Redis版本
  510. /// </summary>
  511. internal static void ForceClearStats()
  512. {
  513. try
  514. {
  515. var today = DateTime.Now.ToString("yyyyMMdd");
  516. var pattern = $"pdd_daily_usage:*:{today}";
  517. // TODO: 实现Redis批量删除相关keys
  518. // 生产环境中需要谨慎使用
  519. Console.WriteLine("Force clear stats - Redis keys will auto-expire in 24h");
  520. }
  521. catch (Exception ex)
  522. {
  523. Console.WriteLine($"Failed to force clear stats: {ex.Message}");
  524. }
  525. }
  526. internal static void TempSuspend(int accountId)
  527. {
  528. _suspend.AddOrUpdate(accountId, DateTime.Now, (key, oldValue) => DateTime.Now);
  529. }
  530. private static bool IsMatch(PddPoolDTO item, PddUnionWorkMode mode, int accountid)
  531. {
  532. if (accountid != 0 && accountid != item.id) return false;
  533. if (!item.enable_parse) return false;
  534. if (mode != PddUnionWorkMode.All && mode != item.work_mode) return false;
  535. if (item.work_mode == PddUnionWorkMode.Crawler && !item.cookie_status) return false;
  536. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  537. {
  538. if (item.end_point != _end_point) return false;
  539. }
  540. // 使用初始化参数创建工作时间表
  541. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  542. if (_suspend.TryGetValue(accountid, out DateTime suspendTime))
  543. {
  544. var ts = DateTime.Now - suspendTime;
  545. if (ts.TotalSeconds < 70) return false;
  546. }
  547. // cis_limit 检查已移至 SmoothWeightedRoundRobin 方法中基于时间间隔判断
  548. // 这里不再需要检查 Redis 锁
  549. if (item.rpm_limit > 0)
  550. {
  551. int rpm_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHHmm"));
  552. if (rpm_num >= item.rpm_limit) return false;
  553. }
  554. if (item.daily_calls_limit > 0)
  555. {
  556. int daily_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMdd"));
  557. if (daily_num >= item.daily_calls_limit) return false;
  558. }
  559. if (item.hourly_calls_limit > 0)
  560. {
  561. int hourly_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHH"));
  562. if (hourly_num >= item.hourly_calls_limit) return false;
  563. }
  564. return true;
  565. }
  566. public static async Task<IEnumerable<PddPoolDTO>> AllListAsync(bool force = false)
  567. {
  568. if (!force && _all_cached != default) return _all_cached;
  569. try
  570. {
  571. await _semaphore.WaitAsync();
  572. string cache_key = $"cache:all_pdd_pool";
  573. var list = await RedisKit.GetAsync<IEnumerable<PddPoolDTO>>(cache_key);
  574. if (force || list == default)
  575. {
  576. list = new DBContext.Table("pdd_pool").Select<PddPoolDTO>();
  577. if (list == null) return default;
  578. // 等待 Redis 写入完成
  579. await RedisKit.SetAsync(cache_key, list, 30 * 86400);
  580. }
  581. _all_cached = list;
  582. return list;
  583. }
  584. catch (Exception)
  585. {
  586. // 发生异常时返回上一次的缓存,如果没有则返回默认值
  587. return _all_cached ?? default;
  588. }
  589. finally
  590. {
  591. _semaphore.Release();
  592. }
  593. }
  594. public static IEnumerable<PddPoolDTO> List(bool force = false)
  595. {
  596. if (!force && _cached != null) return _cached;
  597. string cache_key = $"cache:pdd_pool";
  598. var list = RedisHelper.Get<IEnumerable<PddPoolDTO>>(cache_key);
  599. if (force || list == null)
  600. {
  601. lock (_lockObj)
  602. {
  603. list = new DBContext.Table("pdd_pool")
  604. .Where("status=@status", new { status = 1 })
  605. .Select<PddPoolDTO>();
  606. if (list == null) return default;
  607. foreach (var item in list)
  608. {
  609. RiskControlCore.SetCallsAsync(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
  610. RiskControlCore.SetCallsAsync(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
  611. }
  612. RedisHelper.Set(cache_key, list, 30 * 86400);
  613. }
  614. }
  615. _cached = list;
  616. return list;
  617. }
  618. public static void Refresh()
  619. {
  620. _cached = null;
  621. _all_cached = null;
  622. _ = List(true);
  623. _ = AllListAsync(true);
  624. }
  625. public static void Disabled(string name)
  626. {
  627. string cache_key = $"cache:pdd_pool:{name}:disabled";
  628. long count = RedisHelper.IncrBy(cache_key);
  629. RedisHelper.Expire(cache_key, 10);
  630. if (count > 1) return;
  631. new DBContext.Table("pdd_pool")
  632. .Add("status", 0)
  633. .Where("name=@name", new { name })
  634. .Update();
  635. _ = List(true);
  636. NotifyCore.Notify(new NifyMessage
  637. {
  638. message = $"【多多:{name}】调用异常",
  639. priority = NifyMessagePriority.high,
  640. tags = ["red_circle"]
  641. });
  642. }
  643. public static void Disabled(int accountId, string name, string content)
  644. {
  645. string cache_key = $"cache:pdd_pool:{name}:disabled";
  646. long count = RedisHelper.IncrBy(cache_key);
  647. RedisHelper.Expire(cache_key, 10);
  648. if (count > 1) return;
  649. var update = new DBContext.Table("pdd_pool").Add("status", 0);
  650. if (accountId > 0)
  651. {
  652. update.Where("id=@accountId", new { accountId }).Update();
  653. }
  654. else
  655. {
  656. update.Where("name=@name", new { name }).Update();
  657. }
  658. _ = List(true);
  659. NotifyCore.Notify(new NifyMessage
  660. {
  661. message = $"【多多{accountId}:{name}】cookie 掉线\n\n{content}",
  662. priority = NifyMessagePriority.high,
  663. tags = ["red_circle"]
  664. });
  665. //NotifyCore.QYWeixinPushNotifyAsync("掉线", $"【多多{accountId}:{name}】cookie 掉线");
  666. EndPointCore.NotifyReload(true);
  667. }
  668. internal static void AccountExhausted()
  669. {
  670. string cache_key = $"cache:pdd_pool:account:exhausted";
  671. long count = RedisHelper.IncrBy(cache_key);
  672. if (count > 1) return;
  673. RedisHelper.Expire(cache_key, 3600);
  674. NotifyCore.Notify(new NifyMessage
  675. {
  676. message = $"【多多】没有匹配账号",
  677. priority = NifyMessagePriority.high,
  678. tags = ["red_circle"]
  679. });
  680. //_ = NotifyCore.QYWeixinPushNotifyAsync("没账号", $"【多多】没有匹配账号");
  681. }
  682. internal static async Task<JsonElement> GetUserInfo(string cookies)
  683. {
  684. JsonElement result = default;
  685. try
  686. {
  687. string url = "https://jinbao.pinduoduo.com/network/api/account/userInfo";
  688. WebClientUtility client = new WebClientUtility();
  689. client.Post("{}");
  690. client.SetCookies(cookies);
  691. client.SetContentType("application/json");
  692. var response = await client.RequestAsync(url, "POST");
  693. var body = response.Body();
  694. result = body.Convert2JsonElement();
  695. }
  696. catch (Exception ex)
  697. {
  698. }
  699. return result;
  700. }
  701. public static async Task<int> UpdateCookies(string cookies, string user_agent, int id = 0)
  702. {
  703. if (string.IsNullOrEmpty(cookies)) return 0;
  704. var userinfo = await GetUserInfo(cookies);
  705. if (userinfo.ValueKind != JsonValueKind.Object) return 0;
  706. int duoId = userinfo.PathRead<int>("result.duoId", 0);
  707. string company = userinfo.PathRead<string>("result.mobile", string.Empty);
  708. string lastPid = userinfo.PathRead<string>("result.lastPid", string.Empty);
  709. int accountId = 0;
  710. if (duoId == 0 && id == 0) return 0;
  711. string filter = id != 0 ? "id=@id" : "duoId=@duoId";
  712. var exist = new DBContext.Table("pdd_pool").Get<PddPoolDTO>(filter, new { id, duoId });
  713. if (exist != null)
  714. {
  715. var status = exist.status;
  716. var work_mode = exist.work_mode;
  717. accountId = exist.id;
  718. if (work_mode == PddUnionWorkMode.Crawler) status = true;
  719. new DBContext.Table("pdd_pool")
  720. .Add("cookies", cookies)
  721. .Add("user_agent", user_agent)
  722. .Add("status", status)
  723. .Add("cookie_status", 1)
  724. .Add("last_time", DateTime.Now)
  725. .Add("login_time", DateTime.Now)
  726. .Where("id=@id", new { exist.id })
  727. .Update();
  728. if (status)
  729. {
  730. _ = List(true);
  731. // 为重新上线的账号初始化使用次数和权重(针对所有分组)
  732. InitializeNewAccountUsage(exist.id, "default");
  733. InitializeNewAccountUsage(exist.id, "dp");
  734. }
  735. }
  736. else
  737. {
  738. accountId = new DBContext.Table("pdd_pool")
  739. .Add("duoId", duoId)
  740. .Add("name", company)
  741. .Add("company", company)
  742. .Add("description", "由cookies上报创建此记录")
  743. .Add("cookies", cookies)
  744. .Add("user_agent", user_agent)
  745. .Add("pid", lastPid)
  746. .Add("cookie_status", 1)
  747. .Add("create_time", DateTime.Now)
  748. .Add("last_time", DateTime.Now)
  749. .Add("login_time", DateTime.Now)
  750. .Add("status", 0)
  751. .Create();
  752. // 为新创建的账号初始化使用次数(如果将来会启用的话)
  753. // InitializeNewAccountUsage(accountId); // 暂不调用,因为新创建的账号status=0
  754. }
  755. NotifyCore.Notify(new NifyMessage
  756. {
  757. message = $"【拼多多{accountId}:{company}】cookie 上线",
  758. tags = ["green_circle"]
  759. });
  760. EndPointCore.NotifyReload(true);
  761. //NotifyCore.QYWeixinPushNotifyAsync("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  762. return accountId;
  763. }
  764. public static int CookieDisabled(int id)
  765. {
  766. return new DBContext.Table("pdd_pool")
  767. .Add("cookie_status", 0)
  768. .Add("last_time", DateTime.Now)
  769. .Where("id=@id", new { id })
  770. .Update();
  771. }
  772. public static int Update(PddPoolDTO account)
  773. {
  774. return new DBContext.Table("pdd_pool")
  775. .Add("current_hourly_calls", account.current_hourly_calls)
  776. .Add("current_daily_calls", account.current_daily_calls)
  777. //.Add("today_clickNum", account.today_clickNum)
  778. //.Add("today_cosFee", account.today_cosFee)
  779. //.Add("today_cosPrice", account.today_cosPrice)
  780. //.Add("today_finishCosFee", account.today_finishCosFee)
  781. //.Add("today_finishCosPrice", account.today_finishCosPrice)
  782. //.Add("today_finishOrderNum", account.today_finishOrderNum)
  783. //.Add("today_orderNum", account.today_orderNum)
  784. .Add("last_time", DateTime.Now)
  785. .Where("id=@id", new { account.id })
  786. .Update();
  787. }
  788. public static void AccountRisk(PddPoolDTO account)
  789. {
  790. string cache_key = $"cache:pdd_pool:{account.name}:disabled";
  791. long count = RedisHelper.IncrBy(cache_key);
  792. RedisHelper.Expire(cache_key, 10);
  793. if (count > 1) return;
  794. // 发送通知
  795. string message = $"【PDD风控】【{account.id}:{account.name}】出现30007";
  796. NotifyCore.Notify(new NifyMessage
  797. {
  798. message = message,
  799. priority = NifyMessagePriority.high,
  800. tags = ["red_circle"]
  801. });
  802. if (!account.is_hide)
  803. {
  804. _ = NotifyCore.QYWeixinPushNotifyAsync("PDD风控", message);
  805. }
  806. // 设置风控标记
  807. RiskControlCore.SetRiskCookie($"pdd:{account.id}", "30007");
  808. // 更新数据库状态
  809. new DBContext.Table("pdd_pool")
  810. .Add("enable_parse", 0)
  811. .Where("id=@id", new { account.id })
  812. .Update();
  813. _ = List(true);
  814. _ = EndPointCore.NotifyReload(true);
  815. }
  816. }
  817. }