PddPoolCore.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. namespace molilian.core
  17. {
  18. public partial class PddPoolCore
  19. {
  20. private static readonly object _lockObj = new();
  21. private static IEnumerable<PddPoolDTO> _cached;
  22. private static string _end_point;
  23. private static Dictionary<string, decimal> _incomeAmt = new();
  24. private static ConcurrentDictionary<int, DateTime> _suspend = new();
  25. static PddPoolCore()
  26. {
  27. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  28. }
  29. public static PddPoolDTO? GetOne(PddUnionWorkMode mode, int accountid = 0)
  30. {
  31. var list = List();
  32. if (!list.Any()) return null;
  33. return list.Where(e => IsMatch(e, mode, accountid)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  34. }
  35. internal static void TempSuspend(int accountId)
  36. {
  37. _suspend.AddOrUpdate(accountId, DateTime.Now, (key, oldValue) => DateTime.Now);
  38. }
  39. private static bool IsMatch(PddPoolDTO item, PddUnionWorkMode mode, int accountid)
  40. {
  41. if (accountid != 0 && accountid != item.id) return false;
  42. if (mode != PddUnionWorkMode.All && mode != item.work_mode) return false;
  43. if (item.work_mode == PddUnionWorkMode.Crawler && !item.cookie_status) return false;
  44. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  45. {
  46. if (item.end_point != _end_point) return false;
  47. }
  48. // 使用初始化参数创建工作时间表
  49. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  50. if (_suspend.TryGetValue(accountid, out DateTime suspendTime))
  51. {
  52. var ts = DateTime.Now - suspendTime;
  53. if (ts.TotalSeconds < 70) return false;
  54. }
  55. if (item.daily_calls_limit > 0)
  56. {
  57. int daily_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMdd"));
  58. if (daily_num >= item.daily_calls_limit) return false;
  59. }
  60. if (item.hourly_calls_limit > 0)
  61. {
  62. int hourly_num = RiskControlCore.GetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHH"));
  63. if (hourly_num >= item.hourly_calls_limit) return false;
  64. }
  65. return true;
  66. }
  67. public static IEnumerable<PddPoolDTO> List(bool force = false)
  68. {
  69. if (!force && _cached != null) return _cached;
  70. string cache_key = $"cache:pdd_pool";
  71. var list = RedisHelper.Get<IEnumerable<PddPoolDTO>>(cache_key);
  72. if (force || list == null)
  73. {
  74. lock (_lockObj)
  75. {
  76. list = new DBContext.Table("pdd_pool")
  77. .Where("status=@status", new { status = 1 })
  78. .Select<PddPoolDTO>();
  79. if (list == null) return default;
  80. foreach (var item in list)
  81. {
  82. RiskControlCore.SetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
  83. RiskControlCore.SetCalls(TkChannelEnum.pdd, item.id, DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
  84. }
  85. RedisHelper.Set(cache_key, list, 30 * 86400);
  86. }
  87. }
  88. _cached = list;
  89. return list;
  90. }
  91. public static void Refresh()
  92. {
  93. _ = List(true);
  94. }
  95. public static void Disabled(string name)
  96. {
  97. string cache_key = $"cache:pdd_pool:{name}:disabled";
  98. long count = RedisHelper.IncrBy(cache_key);
  99. RedisHelper.Expire(cache_key, 10);
  100. if (count > 1) return;
  101. new DBContext.Table("pdd_pool")
  102. .Add("status", 0)
  103. .Where("name=@name", new { name })
  104. .Update();
  105. _ = List(true);
  106. NotifyCore.Notify(new NifyMessage
  107. {
  108. message = $"【多多:{name}】调用异常",
  109. priority = NifyMessagePriority.high,
  110. tags = ["red_circle"]
  111. });
  112. }
  113. public static void Disabled(int accountId, string name, string content)
  114. {
  115. string cache_key = $"cache:pdd_pool:{name}:disabled";
  116. long count = RedisHelper.IncrBy(cache_key);
  117. RedisHelper.Expire(cache_key, 10);
  118. if (count > 1) return;
  119. var update = new DBContext.Table("pdd_pool").Add("status", 0);
  120. if (accountId > 0)
  121. {
  122. update.Where("id=@accountId", new { accountId }).Update();
  123. }
  124. else
  125. {
  126. update.Where("name=@name", new { name }).Update();
  127. }
  128. _ = List(true);
  129. NotifyCore.Notify(new NifyMessage
  130. {
  131. message = $"【多多{accountId}:{name}】cookie 掉线\n\n{content}",
  132. priority = NifyMessagePriority.high,
  133. tags = ["red_circle"]
  134. });
  135. NotifyCore.AnPushNotify("掉线", $"【多多{accountId}:{name}】cookie 掉线");
  136. EndPointCore.NotifyReload(true);
  137. }
  138. internal static void AccountExhausted()
  139. {
  140. string cache_key = $"cache:pdd_pool:account:exhausted";
  141. long count = RedisHelper.IncrBy(cache_key);
  142. if (count > 1) return;
  143. RedisHelper.Expire(cache_key, 3600);
  144. NotifyCore.Notify(new NifyMessage
  145. {
  146. message = $"【多多】没有匹配账号",
  147. priority = NifyMessagePriority.high,
  148. tags = ["red_circle"]
  149. });
  150. NotifyCore.AnPushNotify("没账号", $"【多多】没有匹配账号");
  151. }
  152. internal static async Task<JsonElement> GetUserInfo(string cookies)
  153. {
  154. JsonElement result = default;
  155. try
  156. {
  157. string url = "https://jinbao.pinduoduo.com/network/api/account/userInfo";
  158. WebClientUtility client = new WebClientUtility();
  159. client.Post("{}");
  160. client.SetCookies(cookies);
  161. client.SetContentType("application/json");
  162. var response = await client.RequestAsync(url, "POST");
  163. var body = response.Body();
  164. result = body.Convert2JsonElement();
  165. }
  166. catch (Exception ex)
  167. {
  168. }
  169. return result;
  170. }
  171. public static async Task<int> UpdateCookies(string cookies, string user_agent)
  172. {
  173. if (string.IsNullOrEmpty(cookies)) return 0;
  174. var userinfo = await GetUserInfo(cookies);
  175. if (userinfo.ValueKind != JsonValueKind.Object) return 0;
  176. int duoId = userinfo.PathRead<int>("result.duoId", 0);
  177. string company = userinfo.PathRead<string>("result.mobile", string.Empty);
  178. string lastPid = userinfo.PathRead<string>("result.lastPid", string.Empty);
  179. int accountId = 0;
  180. if (duoId == 0) return 0;
  181. var exist = new DBContext.Table("pdd_pool").Get<JdPoolDTO>("duoId=@duoId", new { duoId });
  182. if (exist != null)
  183. {
  184. var status = exist.status;
  185. var work_mode = exist.work_mode;
  186. accountId = exist.id;
  187. if (work_mode == JdUnionWorkMode.Crawler) status = true;
  188. new DBContext.Table("pdd_pool")
  189. .Add("duoId", duoId)
  190. .Add("cookies", cookies)
  191. .Add("user_agent", user_agent)
  192. .Add("status", status)
  193. .Add("cookie_status", 1)
  194. .Add("last_time", DateTime.Now)
  195. .Add("login_time", DateTime.Now)
  196. .Where("id=@id", new { exist.id })
  197. .Update();
  198. if (status) _ = List(true);
  199. }
  200. else
  201. {
  202. accountId = new DBContext.Table("pdd_pool")
  203. .Add("duoId", duoId)
  204. .Add("name", company)
  205. .Add("company", company)
  206. .Add("description", "由cookies上报创建此记录")
  207. .Add("cookies", cookies)
  208. .Add("user_agent", user_agent)
  209. .Add("pid", lastPid)
  210. .Add("cookie_status", 1)
  211. .Add("create_time", DateTime.Now)
  212. .Add("last_time", DateTime.Now)
  213. .Add("login_time", DateTime.Now)
  214. .Add("status", 0)
  215. .Create();
  216. }
  217. NotifyCore.Notify(new NifyMessage
  218. {
  219. message = $"【拼多多{accountId}:{company}】cookie 上线",
  220. tags = ["green_circle"]
  221. });
  222. EndPointCore.NotifyReload(true);
  223. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  224. return accountId;
  225. }
  226. public static int CookieDisabled(int id)
  227. {
  228. return new DBContext.Table("pdd_pool")
  229. .Add("cookie_status", 0)
  230. .Add("last_time", DateTime.Now)
  231. .Where("id=@id", new { id })
  232. .Update();
  233. }
  234. public static int Update(PddPoolDTO account)
  235. {
  236. return new DBContext.Table("pdd_pool")
  237. .Add("current_hourly_calls", account.current_hourly_calls)
  238. .Add("current_daily_calls", account.current_daily_calls)
  239. //.Add("today_clickNum", account.today_clickNum)
  240. //.Add("today_cosFee", account.today_cosFee)
  241. //.Add("today_cosPrice", account.today_cosPrice)
  242. //.Add("today_finishCosFee", account.today_finishCosFee)
  243. //.Add("today_finishCosPrice", account.today_finishCosPrice)
  244. //.Add("today_finishOrderNum", account.today_finishOrderNum)
  245. //.Add("today_orderNum", account.today_orderNum)
  246. .Add("last_time", DateTime.Now)
  247. .Where("id=@id", new { account.id })
  248. .Update();
  249. }
  250. }
  251. }