HiddenAccountCore.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using dodohold.core;
  2. namespace molilian.core
  3. {
  4. /// <summary>
  5. /// 隐藏号判断:隐藏号命中的转链只返回 deeplink,照常写转链日志,但不产生任何统计、上报数据
  6. /// </summary>
  7. public static class HiddenAccountCore
  8. {
  9. private const int LimitExpireSeconds = 86400;
  10. public static Task<bool> IsHiddenAsync(TkChannelEnum channel, int accountId)
  11. {
  12. return IsHiddenAsync(channel.ToString(), accountId);
  13. }
  14. public static async Task<bool> IsHiddenAsync(string channel, int accountId)
  15. {
  16. if (accountId <= 0) return false;
  17. switch ((channel ?? string.Empty).ToLowerInvariant())
  18. {
  19. case "tb":
  20. var tbAccount = await TkPoolCore.ALLGetOneAsync(accountId);
  21. return tbAccount != null && tbAccount.is_hide;
  22. case "pdd":
  23. var pddAccounts = await PddPoolCore.AllListAsync();
  24. var pddAccount = pddAccounts?.FirstOrDefault(e => e.id == accountId);
  25. return pddAccount != null && pddAccount.is_hide;
  26. default:
  27. return false;
  28. }
  29. }
  30. /// <summary>
  31. /// 隐藏号专用限制(config 配置,所有隐藏号共用,淘宝/拼多多分开计数):
  32. /// 城市过滤 + 同 IP / OAID 每天转成功次数。通过时占用名额,转链结束后调用 CompleteLimitAsync。
  33. /// 返回具体拦截原因供日志记录;调用方对外仍按隐藏号失败返回(转链失败/无法转链)。
  34. /// </summary>
  35. public static bool TryReserveLimit(TkChannelEnum channel, string ip, string oaid, out HiddenLimitTicket ticket, out string reason)
  36. {
  37. ticket = new HiddenLimitTicket();
  38. reason = string.Empty;
  39. if (TestParseCore.InWhitelist(ip, oaid)) return true;
  40. try
  41. {
  42. var config = TkConfigCore.Get();
  43. if (config == null) return true;
  44. if (!string.IsNullOrEmpty(config.hide_ignore_city))
  45. {
  46. string? ipInfo = IP2RegionPlus.Search(ip);
  47. if (AlimamaPlus.IgnoreRegionIncluded(config.hide_ignore_city, ipInfo, out string regionInfo))
  48. {
  49. reason = $"地区控制:{regionInfo}";
  50. return false;
  51. }
  52. }
  53. string date = DateTime.Now.ToString("yyyyMMdd");
  54. if (!TryReserveCount(BuildLimitKey(channel, date, "ip", ip), config.hide_limit_per_ip_24h, ticket))
  55. {
  56. reason = "IP控制";
  57. ReleaseReserved(ticket);
  58. return false;
  59. }
  60. if (!TryReserveCount(BuildLimitKey(channel, date, "oaid", oaid), config.hide_limit_per_oaid_24h, ticket))
  61. {
  62. reason = "OAID控制";
  63. ReleaseReserved(ticket);
  64. return false;
  65. }
  66. return true;
  67. }
  68. catch (Exception ex)
  69. {
  70. // 与全局限制一致:异常时不拦截
  71. _ = new LoggerLibrary("HiddenAccountCore", "TryReserveLimit")
  72. .Info(ex.Message, ex.StackTrace)
  73. .SaveAsync();
  74. return true;
  75. }
  76. }
  77. /// <summary>
  78. /// 转链结束:成功则计入当天次数(同步到其他节点),失败则归还占用的名额
  79. /// </summary>
  80. public static async Task CompleteLimitAsync(HiddenLimitTicket ticket, bool success)
  81. {
  82. if (ticket == null || ticket.Keys.Count == 0) return;
  83. try
  84. {
  85. if (!success)
  86. {
  87. ReleaseReserved(ticket);
  88. return;
  89. }
  90. // 本节点在占用时已计数,只同步到其他节点
  91. await EndPointCore.ProcessEndPointNodesAsync(node =>
  92. {
  93. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  94. if (!node.is_public_api) return Task.CompletedTask;
  95. if (string.Equals(node.name, EndPointCore.CurrentEndPoint, StringComparison.OrdinalIgnoreCase)) return Task.CompletedTask;
  96. try
  97. {
  98. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  99. foreach (var key in ticket.Keys)
  100. {
  101. redis.IncrBy(key);
  102. redis.Expire(key, LimitExpireSeconds);
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. _ = new LoggerLibrary("HiddenAccountCore", "CompleteLimit")
  108. .Info(node.name, ex.Message)
  109. .SaveAsync();
  110. }
  111. return Task.CompletedTask;
  112. });
  113. }
  114. catch (Exception ex)
  115. {
  116. _ = new LoggerLibrary("HiddenAccountCore", "CompleteLimit")
  117. .Info(ex.Message, ex.StackTrace)
  118. .SaveAsync();
  119. }
  120. }
  121. private static bool TryReserveCount(string key, int limit, HiddenLimitTicket ticket)
  122. {
  123. if (limit <= 0 || string.IsNullOrEmpty(key)) return true;
  124. long count = RedisHelper.IncrBy(key);
  125. RedisHelper.Expire(key, LimitExpireSeconds);
  126. if (count > limit)
  127. {
  128. RedisHelper.IncrBy(key, -1);
  129. return false;
  130. }
  131. ticket.Keys.Add(key);
  132. return true;
  133. }
  134. private static void ReleaseReserved(HiddenLimitTicket ticket)
  135. {
  136. foreach (var key in ticket.Keys) RedisHelper.IncrBy(key, -1);
  137. ticket.Keys.Clear();
  138. }
  139. private static string BuildLimitKey(TkChannelEnum channel, string date, string type, string value)
  140. {
  141. if (string.IsNullOrEmpty(value)) return string.Empty;
  142. return $":hide_limit:{channel}:{date}:{type}:{value}";
  143. }
  144. }
  145. /// <summary>
  146. /// 隐藏号限制占用的名额(Redis 计数 key)
  147. /// </summary>
  148. public sealed class HiddenLimitTicket
  149. {
  150. public List<string> Keys { get; } = [];
  151. }
  152. }