PddPoolCore.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 Microsoft.AspNetCore.Components.RenderTree;
  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. // 添加本地内存统计 - 线程安全
  29. private static readonly ConcurrentDictionary<string, int> _dailyUsage = new();
  30. private static volatile string _currentDay = DateTime.Now.ToString("yyyyMMdd");
  31. private static readonly object _dayResetLock = new object(); // 专用于日期重置的锁
  32. private static SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  33. static PddPoolCore()
  34. {
  35. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  36. }
  37. public static PddPoolDTO? GetOne(PddUnionWorkMode mode, int accountid = 0, string parse_type = "")
  38. {
  39. var list = List();
  40. if (!list.Any()) return null;
  41. if ("dp".Equals(parse_type))
  42. {
  43. list = list.Where(item => "dp".Equals(item.parse_type)).ToList();
  44. if (!list.Any()) return null;
  45. }
  46. else
  47. {
  48. list = list.Where(item => string.IsNullOrEmpty(item.parse_type)).ToList();
  49. if (!list.Any()) return null;
  50. }
  51. return list.Where(e => IsMatch(e, mode, accountid))
  52. .OrderBy(account => GetCurrentDayUsage(account.id))
  53. .ThenBy(account => account.id) // 相同使用次数时按ID排序,确保稳定性
  54. .FirstOrDefault();
  55. }
  56. private static int GetCurrentDayUsage(int accountId)
  57. {
  58. CheckAndResetDailyStats();
  59. string currentDay = _currentDay; // 获取当前快照,避免在构建key过程中被修改
  60. string key = $"{accountId}_{currentDay}";
  61. return _dailyUsage.GetOrAdd(key, 0);
  62. }
  63. private static void CheckAndResetDailyStats()
  64. {
  65. var today = DateTime.Now.ToString("yyyyMMdd");
  66. // 使用volatile读取,如果日期相同则直接返回,避免锁开销
  67. if (today == _currentDay) return;
  68. // 只有在日期不同时才尝试获取锁
  69. lock (_dayResetLock)
  70. {
  71. // 双重检查:再次验证日期是否需要重置
  72. if (today != _currentDay)
  73. {
  74. // 清空所有统计数据
  75. _dailyUsage.Clear();
  76. // 原子性更新当前日期(volatile写入)
  77. _currentDay = today;
  78. // 可选:记录日期切换日志
  79. Console.WriteLine($"Daily stats reset for date: {today}");
  80. }
  81. }
  82. }
  83. internal static void UpdateAccountUsage(int accountId)
  84. {
  85. CheckAndResetDailyStats();
  86. string currentDay = _currentDay; // 获取当前快照,确保一致性
  87. string key = $"{accountId}_{currentDay}";
  88. // 使用AddOrUpdate确保原子性递增
  89. _dailyUsage.AddOrUpdate(key, 1, (k, oldValue) => oldValue + 1);
  90. }
  91. /// <summary>
  92. /// 获取当前统计信息(用于监控和调试)
  93. /// </summary>
  94. internal static Dictionary<string, int> GetCurrentUsageSnapshot()
  95. {
  96. CheckAndResetDailyStats();
  97. return new Dictionary<string, int>(_dailyUsage);
  98. }
  99. /// <summary>
  100. /// 强制清理统计数据(仅用于测试或紧急情况)
  101. /// </summary>
  102. internal static void ForceClearStats()
  103. {
  104. lock (_dayResetLock)
  105. {
  106. _dailyUsage.Clear();
  107. Console.WriteLine("Force cleared daily usage stats");
  108. }
  109. }
  110. internal static void TempSuspend(int accountId)
  111. {
  112. _suspend.AddOrUpdate(accountId, DateTime.Now, (key, oldValue) => DateTime.Now);
  113. }
  114. private static bool IsMatch(PddPoolDTO item, PddUnionWorkMode mode, int accountid)
  115. {
  116. if (accountid != 0 && accountid != item.id) return false;
  117. if (!item.enable_parse) return false;
  118. if (mode != PddUnionWorkMode.All && mode != item.work_mode) return false;
  119. if (item.work_mode == PddUnionWorkMode.Crawler && !item.cookie_status) return false;
  120. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  121. {
  122. if (item.end_point != _end_point) return false;
  123. }
  124. // 使用初始化参数创建工作时间表
  125. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  126. if (_suspend.TryGetValue(accountid, out DateTime suspendTime))
  127. {
  128. var ts = DateTime.Now - suspendTime;
  129. if (ts.TotalSeconds < 70) return false;
  130. }
  131. if (item.cis_limit > 0)
  132. {
  133. string lockKey = $"pdd_cis_limit_{accountid}";
  134. int cis_num = RedisHelper.Get<int>(lockKey);
  135. if (cis_num > 0) return false;
  136. }
  137. if (item.rpm_limit > 0)
  138. {
  139. int rpm_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHHmm"));
  140. if (rpm_num >= item.rpm_limit) return false;
  141. }
  142. if (item.daily_calls_limit > 0)
  143. {
  144. int daily_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMdd"));
  145. if (daily_num >= item.daily_calls_limit) return false;
  146. }
  147. if (item.hourly_calls_limit > 0)
  148. {
  149. int hourly_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHH"));
  150. if (hourly_num >= item.hourly_calls_limit) return false;
  151. }
  152. return true;
  153. }
  154. public static async Task<IEnumerable<PddPoolDTO>> AllListAsync(bool force = false)
  155. {
  156. if (!force && _all_cached != default) return _all_cached;
  157. try
  158. {
  159. await _semaphore.WaitAsync();
  160. string cache_key = $"cache:all_pdd_pool";
  161. var list = await RedisKit.GetAsync<IEnumerable<PddPoolDTO>>(cache_key);
  162. if (force || list == default)
  163. {
  164. list = new DBContext.Table("pdd_pool").Select<PddPoolDTO>();
  165. if (list == null) return default;
  166. // 等待 Redis 写入完成
  167. await RedisKit.SetAsync(cache_key, list, 30 * 86400);
  168. }
  169. _all_cached = list;
  170. return list;
  171. }
  172. catch (Exception)
  173. {
  174. // 发生异常时返回上一次的缓存,如果没有则返回默认值
  175. return _all_cached ?? default;
  176. }
  177. finally
  178. {
  179. _semaphore.Release();
  180. }
  181. }
  182. public static IEnumerable<PddPoolDTO> List(bool force = false)
  183. {
  184. if (!force && _cached != null) return _cached;
  185. string cache_key = $"cache:pdd_pool";
  186. var list = RedisHelper.Get<IEnumerable<PddPoolDTO>>(cache_key);
  187. if (force || list == null)
  188. {
  189. lock (_lockObj)
  190. {
  191. list = new DBContext.Table("pdd_pool")
  192. .Where("status=@status", new { status = 1 })
  193. .Select<PddPoolDTO>();
  194. if (list == null) return default;
  195. foreach (var item in list)
  196. {
  197. RiskControlCore.SetCallsAsync(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
  198. RiskControlCore.SetCallsAsync(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
  199. }
  200. RedisHelper.Set(cache_key, list, 30 * 86400);
  201. }
  202. }
  203. _cached = list;
  204. return list;
  205. }
  206. public static void Refresh()
  207. {
  208. _cached = null;
  209. _all_cached = null;
  210. _ = List(true);
  211. }
  212. public static void Disabled(string name)
  213. {
  214. string cache_key = $"cache:pdd_pool:{name}:disabled";
  215. long count = RedisHelper.IncrBy(cache_key);
  216. RedisHelper.Expire(cache_key, 10);
  217. if (count > 1) return;
  218. new DBContext.Table("pdd_pool")
  219. .Add("status", 0)
  220. .Where("name=@name", new { name })
  221. .Update();
  222. _ = List(true);
  223. NotifyCore.Notify(new NifyMessage
  224. {
  225. message = $"【多多:{name}】调用异常",
  226. priority = NifyMessagePriority.high,
  227. tags = ["red_circle"]
  228. });
  229. }
  230. public static void Disabled(int accountId, string name, string content)
  231. {
  232. string cache_key = $"cache:pdd_pool:{name}:disabled";
  233. long count = RedisHelper.IncrBy(cache_key);
  234. RedisHelper.Expire(cache_key, 10);
  235. if (count > 1) return;
  236. var update = new DBContext.Table("pdd_pool").Add("status", 0);
  237. if (accountId > 0)
  238. {
  239. update.Where("id=@accountId", new { accountId }).Update();
  240. }
  241. else
  242. {
  243. update.Where("name=@name", new { name }).Update();
  244. }
  245. _ = List(true);
  246. NotifyCore.Notify(new NifyMessage
  247. {
  248. message = $"【多多{accountId}:{name}】cookie 掉线\n\n{content}",
  249. priority = NifyMessagePriority.high,
  250. tags = ["red_circle"]
  251. });
  252. //NotifyCore.AnPushNotify("掉线", $"【多多{accountId}:{name}】cookie 掉线");
  253. EndPointCore.NotifyReload(true);
  254. }
  255. internal static void AccountExhausted()
  256. {
  257. string cache_key = $"cache:pdd_pool:account:exhausted";
  258. long count = RedisHelper.IncrBy(cache_key);
  259. if (count > 1) return;
  260. RedisHelper.Expire(cache_key, 3600);
  261. NotifyCore.Notify(new NifyMessage
  262. {
  263. message = $"【多多】没有匹配账号",
  264. priority = NifyMessagePriority.high,
  265. tags = ["red_circle"]
  266. });
  267. NotifyCore.AnPushNotify("没账号", $"【多多】没有匹配账号");
  268. }
  269. internal static async Task<JsonElement> GetUserInfo(string cookies)
  270. {
  271. JsonElement result = default;
  272. try
  273. {
  274. string url = "https://jinbao.pinduoduo.com/network/api/account/userInfo";
  275. WebClientUtility client = new WebClientUtility();
  276. client.Post("{}");
  277. client.SetCookies(cookies);
  278. client.SetContentType("application/json");
  279. var response = await client.RequestAsync(url, "POST");
  280. var body = response.Body();
  281. result = body.Convert2JsonElement();
  282. }
  283. catch (Exception ex)
  284. {
  285. }
  286. return result;
  287. }
  288. public static async Task<int> UpdateCookies(string cookies, string user_agent, int id = 0)
  289. {
  290. if (string.IsNullOrEmpty(cookies)) return 0;
  291. var userinfo = await GetUserInfo(cookies);
  292. if (userinfo.ValueKind != JsonValueKind.Object) return 0;
  293. int duoId = userinfo.PathRead<int>("result.duoId", 0);
  294. string company = userinfo.PathRead<string>("result.mobile", string.Empty);
  295. string lastPid = userinfo.PathRead<string>("result.lastPid", string.Empty);
  296. int accountId = 0;
  297. if (duoId == 0 && id == 0) return 0;
  298. string filter = id != 0 ? "id=@id" : "duoId=@duoId";
  299. var exist = new DBContext.Table("pdd_pool").Get<PddPoolDTO>(filter, new { id, duoId });
  300. if (exist != null)
  301. {
  302. var status = exist.status;
  303. var work_mode = exist.work_mode;
  304. accountId = exist.id;
  305. if (work_mode == PddUnionWorkMode.Crawler) status = true;
  306. new DBContext.Table("pdd_pool")
  307. .Add("cookies", cookies)
  308. .Add("user_agent", user_agent)
  309. .Add("status", status)
  310. .Add("cookie_status", 1)
  311. .Add("last_time", DateTime.Now)
  312. .Add("login_time", DateTime.Now)
  313. .Where("id=@id", new { exist.id })
  314. .Update();
  315. if (status) _ = List(true);
  316. }
  317. else
  318. {
  319. accountId = new DBContext.Table("pdd_pool")
  320. .Add("duoId", duoId)
  321. .Add("name", company)
  322. .Add("company", company)
  323. .Add("description", "由cookies上报创建此记录")
  324. .Add("cookies", cookies)
  325. .Add("user_agent", user_agent)
  326. .Add("pid", lastPid)
  327. .Add("cookie_status", 1)
  328. .Add("create_time", DateTime.Now)
  329. .Add("last_time", DateTime.Now)
  330. .Add("login_time", DateTime.Now)
  331. .Add("status", 0)
  332. .Create();
  333. }
  334. NotifyCore.Notify(new NifyMessage
  335. {
  336. message = $"【拼多多{accountId}:{company}】cookie 上线",
  337. tags = ["green_circle"]
  338. });
  339. EndPointCore.NotifyReload(true);
  340. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  341. return accountId;
  342. }
  343. public static int CookieDisabled(int id)
  344. {
  345. return new DBContext.Table("pdd_pool")
  346. .Add("cookie_status", 0)
  347. .Add("last_time", DateTime.Now)
  348. .Where("id=@id", new { id })
  349. .Update();
  350. }
  351. public static int Update(PddPoolDTO account)
  352. {
  353. return new DBContext.Table("pdd_pool")
  354. .Add("current_hourly_calls", account.current_hourly_calls)
  355. .Add("current_daily_calls", account.current_daily_calls)
  356. //.Add("today_clickNum", account.today_clickNum)
  357. //.Add("today_cosFee", account.today_cosFee)
  358. //.Add("today_cosPrice", account.today_cosPrice)
  359. //.Add("today_finishCosFee", account.today_finishCosFee)
  360. //.Add("today_finishCosPrice", account.today_finishCosPrice)
  361. //.Add("today_finishOrderNum", account.today_finishOrderNum)
  362. //.Add("today_orderNum", account.today_orderNum)
  363. .Add("last_time", DateTime.Now)
  364. .Where("id=@id", new { account.id })
  365. .Update();
  366. }
  367. }
  368. }