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; namespace molilian.core { public partial class PddPoolCore { private static readonly object _lockObj = new(); private static IEnumerable _cached; private static IEnumerable _all_cached; private static string _end_point; private static Dictionary _incomeAmt = new(); private static ConcurrentDictionary _suspend = new(); private static SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); static PddPoolCore() { _end_point = Environment.GetEnvironmentVariable("EndPoint"); } public static PddPoolDTO? GetOne(PddUnionWorkMode mode, int accountid = 0) { var list = List(); if (!list.Any()) return null; return list.Where(e => IsMatch(e, mode, accountid)).OrderBy(l => Guid.NewGuid()).FirstOrDefault(); } 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 (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.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> 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>(cache_key); if (force || list == default) { list = new DBContext.Table("pdd_pool").Select(); 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 List(bool force = false) { if (!force && _cached != null) return _cached; string cache_key = $"cache:pdd_pool"; var list = RedisHelper.Get>(cache_key); if (force || list == null) { lock (_lockObj) { list = new DBContext.Table("pdd_pool") .Where("status=@status", new { status = 1 }) .Select(); 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() { _ = List(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.AnPushNotify("掉线", $"【多多{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.AnPushNotify("没账号", $"【多多】没有匹配账号"); } internal static async Task 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 UpdateCookies(string cookies, string user_agent) { if (string.IsNullOrEmpty(cookies)) return 0; var userinfo = await GetUserInfo(cookies); if (userinfo.ValueKind != JsonValueKind.Object) return 0; int duoId = userinfo.PathRead("result.duoId", 0); string company = userinfo.PathRead("result.mobile", string.Empty); string lastPid = userinfo.PathRead("result.lastPid", string.Empty); int accountId = 0; if (duoId == 0) return 0; var exist = new DBContext.Table("pdd_pool").Get("duoId=@duoId", new { duoId }); if (exist != null) { var status = exist.status; var work_mode = exist.work_mode; accountId = exist.id; if (work_mode == JdUnionWorkMode.Crawler) status = true; new DBContext.Table("pdd_pool") .Add("duoId", duoId) .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); } 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(); } NotifyCore.Notify(new NifyMessage { message = $"【拼多多{accountId}:{company}】cookie 上线", tags = ["green_circle"] }); EndPointCore.NotifyReload(true); //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{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(); } } }