TkPoolCore.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 Spire.Pdf.Exporting.XPS.Schema;
  10. using System.Xml.Linq;
  11. using System.Net;
  12. using TencentCloud.Mrs.V20200910.Models;
  13. namespace molilian.core
  14. {
  15. public partial class TkPoolCore
  16. {
  17. private static string _end_point;
  18. static TkPoolCore()
  19. {
  20. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  21. }
  22. private static readonly object _lockObj = new();
  23. private static IEnumerable<TkPoolDTO> _cached;
  24. private static Dictionary<string, decimal> _incomeAmt = new();
  25. public static TkPoolDTO? GetOne()
  26. {
  27. var list = List();
  28. if (!list.Any()) return null;
  29. return list.Where(IsNotExceedDailyIncomeLimit).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  30. }
  31. public static TkPoolDTO? GetOne(int id)
  32. {
  33. var list = List();
  34. if (!list.Any()) return null;
  35. return list.Where(e => e.id == id).FirstOrDefault();
  36. }
  37. internal static void SaveIncomeAmt(string accountName, decimal income_amt)
  38. {
  39. string key = $"{accountName}:{DateTime.Now:yyyyMMdd}";
  40. if (_incomeAmt.ContainsKey(key))
  41. {
  42. _incomeAmt[key] = income_amt;
  43. }
  44. else
  45. {
  46. _incomeAmt.Add(key, income_amt);
  47. }
  48. string cache_key = $"cache:tk_pool:{key}:income_amt";
  49. RedisHelper.Set(cache_key, income_amt, 3 * 86400);
  50. }
  51. public static decimal GetIncomeAmt(string accountName)
  52. {
  53. try
  54. {
  55. string key = $"{accountName}:{DateTime.Now:yyyyMMdd}";
  56. if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
  57. string cache_key = $"cache:tk_pool:{key}:income_amt";
  58. return RedisHelper.Get<decimal>(cache_key);
  59. }
  60. catch (Exception ex) { return 0; }
  61. }
  62. private static bool IsNotExceedDailyIncomeLimit(TkPoolDTO item)
  63. {
  64. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  65. {
  66. if (item.end_point != _end_point) return false;
  67. }
  68. if (item.daily_income_limit == 0) return true;
  69. decimal income_amt = GetIncomeAmt(item.name);
  70. return income_amt < item.daily_income_limit;
  71. }
  72. public static IEnumerable<TkPoolDTO> List(bool force = false)
  73. {
  74. if (!force && _cached != null) return _cached;
  75. string cache_key = $"cache:tk_pool";
  76. var list = RedisHelper.Get<IEnumerable<TkPoolDTO>>(cache_key);
  77. if (force || list == null)
  78. {
  79. lock (_lockObj)
  80. {
  81. list = new DBContext.Table("tk_pool")
  82. .Where("status=@status", new { status = 1 })
  83. .Select<TkPoolDTO>();
  84. if (list == null) return default;
  85. RedisHelper.Set(cache_key, list, 30 * 86400);
  86. }
  87. }
  88. _cached = list;
  89. return list;
  90. }
  91. public static void Refresh()
  92. {
  93. _cached = null;
  94. _incomeAmt = [];
  95. _ = List(true);
  96. }
  97. public static void UpdateDrawBalance(string name, decimal amout)
  98. {
  99. new DBContext.Table("tk_pool")
  100. .Add("draw_balance", amout)
  101. .Where("name=@name", new { name })
  102. .Update();
  103. _ = List(true);
  104. }
  105. public static void Disabled(string name, string content)
  106. {
  107. #if DEBUG
  108. #else
  109. string cache_key = $"cache:tk_pool:{name}:disabled";
  110. long count = RedisHelper.IncrBy(cache_key);
  111. RedisHelper.Expire(cache_key, 10);
  112. if (count > 1) return;
  113. new DBContext.Table("tk_pool")
  114. .Add("status", 0)
  115. .Where("name=@name", new { name })
  116. .Update();
  117. _ = List(true);
  118. NotifyCore.Notify(new NifyMessage
  119. {
  120. message = $"【淘宝联盟:{name}】cookie 掉线\n\n{content}",
  121. priority = NifyMessagePriority.high,
  122. tags = ["red_circle"]
  123. });
  124. NotifyCore.AnPushNotify("掉线", $"【淘宝联盟:{name}】cookie 掉线");
  125. #endif
  126. }
  127. public static bool UpdateCookies(string cookies, string user_agent)
  128. {
  129. if (string.IsNullOrEmpty(cookies)) return false;
  130. cookies += ";";
  131. string dnk = cookies.GetContentPart("dnk=", ";");
  132. string tb_token = cookies.GetContentPart("_tb_token_=", ";");
  133. if (string.IsNullOrEmpty(dnk) || string.IsNullOrEmpty(tb_token)) return false;
  134. var exist = new DBContext.Table("tk_pool").Fields("id, name, refpid, status").Get<dynamic>("name=@dnk", new { dnk });
  135. if (exist != null)
  136. {
  137. int status = exist.status;
  138. string refpid = exist.refpid;
  139. if (!string.IsNullOrEmpty(refpid)) status = 1;
  140. new DBContext.Table("tk_pool")
  141. .Add("name", dnk)
  142. .Add("tb_token", tb_token)
  143. .Add("cookies", cookies)
  144. .Add("user_agent", user_agent)
  145. .Add("status", status)
  146. .Add("last_time", DateTime.Now)
  147. .Where("id=@id", new { exist.id })
  148. .Update();
  149. if (status == 1) _ = List(true);
  150. }
  151. else
  152. {
  153. new DBContext.Table("tk_pool")
  154. .Add("name", dnk)
  155. .Add("description", "由cookies上报创建此记录")
  156. .Add("tb_token", tb_token)
  157. .Add("refpid", string.Empty)
  158. .Add("cookies", cookies)
  159. .Add("user_agent", user_agent)
  160. .Add("create_time", DateTime.Now)
  161. .Add("last_time", DateTime.Now)
  162. .Add("status", 0)
  163. .Create();
  164. }
  165. NotifyCore.Notify(new NifyMessage
  166. {
  167. message = $"【淘宝联盟:{dnk}】cookie 上线",
  168. tags = ["green_circle"]
  169. });
  170. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  171. return true;
  172. }
  173. internal static void AccountExhausted()
  174. {
  175. string cache_key = $"cache:tk_pool:account:exhausted";
  176. long count = RedisHelper.IncrBy(cache_key);
  177. if (count > 1) return;
  178. RedisHelper.Expire(cache_key, 3600);
  179. NotifyCore.Notify(new NifyMessage
  180. {
  181. message = $"【淘宝联盟】没有匹配账号",
  182. priority = NifyMessagePriority.high,
  183. tags = ["red_circle"]
  184. });
  185. NotifyCore.AnPushNotify("没账号", $"【淘宝联盟】没有匹配账号");
  186. }
  187. }
  188. }