| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- using Dataoke;
- using Google.Protobuf.WellKnownTypes;
- using System.Diagnostics;
- using System.Text.Json;
- using TencentCloud.Tcm.V20210413.Models;
- using System.Security.Cryptography;
- using System.Collections.Concurrent;
- using YunhuiKit;
- using Microsoft.AspNetCore.Components.RenderTree;
- namespace molilian.core
- {
- public partial class PddPoolCore
- {
- private static readonly object _lockObj = new();
- private static IEnumerable<PddPoolDTO> _cached;
- private static IEnumerable<PddPoolDTO> _all_cached;
- private static string _end_point;
- private static Dictionary<string, decimal> _incomeAmt = new();
- private static ConcurrentDictionary<int, DateTime> _suspend = new();
- // 移除本地内存统计,改用 Redis 存储
- private static SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
- static PddPoolCore()
- {
- _end_point = Environment.GetEnvironmentVariable("EndPoint");
- }
- public static PddPoolDTO? GetOne(PddUnionWorkMode mode, int accountid = 0, string parse_type = "")
- {
- var list = List();
- if (!list.Any()) return null;
- if ("dp".Equals(parse_type))
- {
- list = list.Where(item => "dp".Equals(item.parse_type)).ToList();
- if (!list.Any()) return null;
- }
- else
- {
- list = list.Where(item => string.IsNullOrEmpty(item.parse_type)).ToList();
- if (!list.Any()) return null;
- }
- return list.Where(e => IsMatch(e, mode, accountid))
- .OrderBy(account => GetCurrentDayUsageFromRedis(account.id))
- .ThenBy(account => account.id) // 相同使用次数时按ID排序,确保稳定性
- .FirstOrDefault();
- }
- private static int GetCurrentDayUsageFromRedis(int accountId)
- {
- try
- {
- var today = DateTime.Now.ToString("yyyyMMdd");
- string key = $"pdd_daily_usage:{accountId}:{today}";
- return RedisHelper.Get<int>(key);
- }
- catch (Exception)
- {
- return 0;
- }
- }
- internal static void UpdateAccountUsage(int accountId)
- {
- try
- {
- var today = DateTime.Now.ToString("yyyyMMdd");
- string key = $"pdd_daily_usage:{accountId}:{today}";
- // 递增计数并设置24小时过期时间
- RedisHelper.IncrBy(key);
- RedisHelper.Expire(key, 86400); // 24小时后自动过期
- }
- catch (Exception ex)
- {
- // 记录错误但不影响主流程
- Console.WriteLine($"Failed to update account usage for {accountId}: {ex.Message}");
- }
- }
- /// <summary>
- /// 获取当前所有账号的平均使用次数
- /// </summary>
- private static int GetAverageUsageCount()
- {
- try
- {
- var activeAccounts = List()?.Where(a => a.enable_parse).ToList();
- if (activeAccounts == null || !activeAccounts.Any()) return 0;
- var today = DateTime.Now.ToString("yyyyMMdd");
- var totalUsage = 0;
- var validCount = 0;
- foreach (var account in activeAccounts)
- {
- var usage = GetCurrentDayUsageFromRedis(account.id);
- totalUsage += usage;
- validCount++;
- }
- return validCount > 0 ? totalUsage / validCount : 0;
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Failed to get average usage count: {ex.Message}");
- return 0;
- }
- }
- /// <summary>
- /// 为新上线的账号设置平均使用次数,避免流量集中
- /// </summary>
- public static void InitializeNewAccountUsage(int accountId)
- {
- try
- {
- var today = DateTime.Now.ToString("yyyyMMdd");
- string key = $"pdd_daily_usage:{accountId}:{today}";
- // 检查该账号今天是否已有使用记录
- var currentUsage = RedisHelper.Get<int>(key);
- if (currentUsage > 0) return; // 已有记录,不需要初始化
- // 获取平均使用次数
- var averageUsage = GetAverageUsageCount();
- if (averageUsage > 0)
- {
- // 设置为平均值,避免新账号因为使用次数为0而被优先选择
- RedisHelper.Set(key, averageUsage, 86400);
- Console.WriteLine($"Initialized account {accountId} with average usage: {averageUsage}");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Failed to initialize new account usage for {accountId}: {ex.Message}");
- }
- }
- /// <summary>
- /// 获取当前统计信息(用于监控和调试)- 改为从Redis获取
- /// </summary>
- internal static Dictionary<string, int> GetCurrentUsageSnapshot()
- {
- try
- {
- var today = DateTime.Now.ToString("yyyyMMdd");
- var pattern = $"pdd_daily_usage:*:{today}";
- // 注意:这里只是示例,实际实现可能需要根据Redis客户端API调整
- // 生产环境中应该避免使用KEYS命令,可以考虑其他方案
- var result = new Dictionary<string, int>();
- // TODO: 实现Redis pattern匹配获取所有相关keys
- return result;
- }
- catch (Exception)
- {
- return new Dictionary<string, int>();
- }
- }
- /// <summary>
- /// 强制清理统计数据(仅用于测试或紧急情况)- Redis版本
- /// </summary>
- internal static void ForceClearStats()
- {
- try
- {
- var today = DateTime.Now.ToString("yyyyMMdd");
- var pattern = $"pdd_daily_usage:*:{today}";
- // TODO: 实现Redis批量删除相关keys
- // 生产环境中需要谨慎使用
- Console.WriteLine("Force clear stats - Redis keys will auto-expire in 24h");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Failed to force clear stats: {ex.Message}");
- }
- }
- internal static void TempSuspend(int accountId)
- {
- _suspend.AddOrUpdate(accountId, DateTime.Now, (key, oldValue) => DateTime.Now);
- }
- private static bool IsMatch(PddPoolDTO item, PddUnionWorkMode mode, int accountid)
- {
- if (accountid != 0 && accountid != item.id) return false;
- if (!item.enable_parse) return false;
- if (mode != PddUnionWorkMode.All && mode != item.work_mode) return false;
- if (item.work_mode == PddUnionWorkMode.Crawler && !item.cookie_status) return false;
- if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
- {
- if (item.end_point != _end_point) return false;
- }
- // 使用初始化参数创建工作时间表
- if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
- if (_suspend.TryGetValue(accountid, out DateTime suspendTime))
- {
- var ts = DateTime.Now - suspendTime;
- if (ts.TotalSeconds < 70) return false;
- }
- if (item.cis_limit > 0)
- {
- string lockKey = $"pdd_cis_limit_{accountid}";
- int cis_num = RedisHelper.Get<int>(lockKey);
- if (cis_num > 0) return false;
- }
- if (item.rpm_limit > 0)
- {
- int rpm_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHHmm"));
- if (rpm_num >= item.rpm_limit) return false;
- }
- if (item.daily_calls_limit > 0)
- {
- int daily_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMdd"));
- if (daily_num >= item.daily_calls_limit) return false;
- }
- if (item.hourly_calls_limit > 0)
- {
- int hourly_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHH"));
- if (hourly_num >= item.hourly_calls_limit) return false;
- }
- return true;
- }
- public static async Task<IEnumerable<PddPoolDTO>> AllListAsync(bool force = false)
- {
- if (!force && _all_cached != default) return _all_cached;
- try
- {
- await _semaphore.WaitAsync();
- string cache_key = $"cache:all_pdd_pool";
- var list = await RedisKit.GetAsync<IEnumerable<PddPoolDTO>>(cache_key);
- if (force || list == default)
- {
- list = new DBContext.Table("pdd_pool").Select<PddPoolDTO>();
- if (list == null) return default;
- // 等待 Redis 写入完成
- await RedisKit.SetAsync(cache_key, list, 30 * 86400);
- }
- _all_cached = list;
- return list;
- }
- catch (Exception)
- {
- // 发生异常时返回上一次的缓存,如果没有则返回默认值
- return _all_cached ?? default;
- }
- finally
- {
- _semaphore.Release();
- }
- }
- public static IEnumerable<PddPoolDTO> List(bool force = false)
- {
- if (!force && _cached != null) return _cached;
- string cache_key = $"cache:pdd_pool";
- var list = RedisHelper.Get<IEnumerable<PddPoolDTO>>(cache_key);
- if (force || list == null)
- {
- lock (_lockObj)
- {
- list = new DBContext.Table("pdd_pool")
- .Where("status=@status", new { status = 1 })
- .Select<PddPoolDTO>();
- if (list == null) return default;
- foreach (var item in list)
- {
- RiskControlCore.SetCallsAsync(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
- RiskControlCore.SetCallsAsync(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
- }
- RedisHelper.Set(cache_key, list, 30 * 86400);
- }
- }
- _cached = list;
- return list;
- }
- public static void Refresh()
- {
- _cached = null;
- _all_cached = null;
- _ = List(true);
- _ = AllListAsync(true);
- }
- public static void Disabled(string name)
- {
- string cache_key = $"cache:pdd_pool:{name}:disabled";
- long count = RedisHelper.IncrBy(cache_key);
- RedisHelper.Expire(cache_key, 10);
- if (count > 1) return;
- new DBContext.Table("pdd_pool")
- .Add("status", 0)
- .Where("name=@name", new { name })
- .Update();
- _ = List(true);
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【多多:{name}】调用异常",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- }
- public static void Disabled(int accountId, string name, string content)
- {
- string cache_key = $"cache:pdd_pool:{name}:disabled";
- long count = RedisHelper.IncrBy(cache_key);
- RedisHelper.Expire(cache_key, 10);
- if (count > 1) return;
- var update = new DBContext.Table("pdd_pool").Add("status", 0);
- if (accountId > 0)
- {
- update.Where("id=@accountId", new { accountId }).Update();
- }
- else
- {
- update.Where("name=@name", new { name }).Update();
- }
- _ = List(true);
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【多多{accountId}:{name}】cookie 掉线\n\n{content}",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- //NotifyCore.QYWeixinPushNotifyAsync("掉线", $"【多多{accountId}:{name}】cookie 掉线");
- EndPointCore.NotifyReload(true);
- }
- internal static void AccountExhausted()
- {
- string cache_key = $"cache:pdd_pool:account:exhausted";
- long count = RedisHelper.IncrBy(cache_key);
- if (count > 1) return;
- RedisHelper.Expire(cache_key, 3600);
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【多多】没有匹配账号",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- _ = NotifyCore.QYWeixinPushNotifyAsync("没账号", $"【多多】没有匹配账号");
- }
- internal static async Task<JsonElement> GetUserInfo(string cookies)
- {
- JsonElement result = default;
- try
- {
- string url = "https://jinbao.pinduoduo.com/network/api/account/userInfo";
- WebClientUtility client = new WebClientUtility();
- client.Post("{}");
- client.SetCookies(cookies);
- client.SetContentType("application/json");
- var response = await client.RequestAsync(url, "POST");
- var body = response.Body();
- result = body.Convert2JsonElement();
- }
- catch (Exception ex)
- {
- }
- return result;
- }
- public static async Task<int> UpdateCookies(string cookies, string user_agent, int id = 0)
- {
- if (string.IsNullOrEmpty(cookies)) return 0;
- var userinfo = await GetUserInfo(cookies);
- if (userinfo.ValueKind != JsonValueKind.Object) return 0;
- int duoId = userinfo.PathRead<int>("result.duoId", 0);
- string company = userinfo.PathRead<string>("result.mobile", string.Empty);
- string lastPid = userinfo.PathRead<string>("result.lastPid", string.Empty);
- int accountId = 0;
- if (duoId == 0 && id == 0) return 0;
- string filter = id != 0 ? "id=@id" : "duoId=@duoId";
- var exist = new DBContext.Table("pdd_pool").Get<PddPoolDTO>(filter, new { id, duoId });
- if (exist != null)
- {
- var status = exist.status;
- var work_mode = exist.work_mode;
- accountId = exist.id;
- if (work_mode == PddUnionWorkMode.Crawler) status = true;
- new DBContext.Table("pdd_pool")
- .Add("cookies", cookies)
- .Add("user_agent", user_agent)
- .Add("status", status)
- .Add("cookie_status", 1)
- .Add("last_time", DateTime.Now)
- .Add("login_time", DateTime.Now)
- .Where("id=@id", new { exist.id })
- .Update();
- if (status)
- {
- _ = List(true);
- // 为重新上线的账号初始化使用次数
- InitializeNewAccountUsage(exist.id);
- }
- }
- else
- {
- accountId = new DBContext.Table("pdd_pool")
- .Add("duoId", duoId)
- .Add("name", company)
- .Add("company", company)
- .Add("description", "由cookies上报创建此记录")
- .Add("cookies", cookies)
- .Add("user_agent", user_agent)
- .Add("pid", lastPid)
- .Add("cookie_status", 1)
- .Add("create_time", DateTime.Now)
- .Add("last_time", DateTime.Now)
- .Add("login_time", DateTime.Now)
- .Add("status", 0)
- .Create();
- // 为新创建的账号初始化使用次数(如果将来会启用的话)
- // InitializeNewAccountUsage(accountId); // 暂不调用,因为新创建的账号status=0
- }
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【拼多多{accountId}:{company}】cookie 上线",
- tags = ["green_circle"]
- });
- EndPointCore.NotifyReload(true);
- //NotifyCore.QYWeixinPushNotifyAsync("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
- return accountId;
- }
- public static int CookieDisabled(int id)
- {
- return new DBContext.Table("pdd_pool")
- .Add("cookie_status", 0)
- .Add("last_time", DateTime.Now)
- .Where("id=@id", new { id })
- .Update();
- }
- public static int Update(PddPoolDTO account)
- {
- return new DBContext.Table("pdd_pool")
- .Add("current_hourly_calls", account.current_hourly_calls)
- .Add("current_daily_calls", account.current_daily_calls)
- //.Add("today_clickNum", account.today_clickNum)
- //.Add("today_cosFee", account.today_cosFee)
- //.Add("today_cosPrice", account.today_cosPrice)
- //.Add("today_finishCosFee", account.today_finishCosFee)
- //.Add("today_finishCosPrice", account.today_finishCosPrice)
- //.Add("today_finishOrderNum", account.today_finishOrderNum)
- //.Add("today_orderNum", account.today_orderNum)
- .Add("last_time", DateTime.Now)
- .Where("id=@id", new { account.id })
- .Update();
- }
- }
- }
|