PddPoolCore.cs 38 KB

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