PddPoolCore.cs 10 KB

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