AutoJob.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use App\Models\Config;
  5. use App\Models\Coupon;
  6. use App\Models\Invite;
  7. use App\Models\Node;
  8. use App\Models\NodeHeartbeat;
  9. use App\Models\Order;
  10. use App\Models\User;
  11. use App\Models\VerifyCode;
  12. use App\Notifications\NodeOffline;
  13. use Cache;
  14. use Illuminate\Console\Command;
  15. use Log;
  16. use Notification;
  17. class AutoJob extends Command
  18. {
  19. protected $signature = 'autoJob';
  20. protected $description = '自动化任务';
  21. /*
  22. * 警告:除非熟悉业务流程,否则不推荐更改以下执行顺序,随意变更以下顺序可能导致系统异常
  23. */
  24. public function handle(): void
  25. {
  26. $jobStartTime = microtime(true);
  27. // 关闭超时未支付本地订单
  28. Order::query()->recentUnPay()->update(['status' => -1]);
  29. //过期验证码、优惠券、邀请码无效化
  30. $this->expireCode();
  31. // 封禁访问异常的订阅链接
  32. $this->blockSubscribe();
  33. // 封禁账号
  34. $this->blockUsers();
  35. // 解封被封禁的账号
  36. $this->unblockUsers();
  37. // 端口回收与分配
  38. if (sysConfig('auto_release_port')) {
  39. $this->dispatchPort();
  40. }
  41. // 检测节点是否离线
  42. $this->checkNodeStatus();
  43. // 检查维护模式
  44. if (sysConfig('maintenance_mode') && sysConfig('maintenance_time') && sysConfig('maintenance_time') <= date('c')) {
  45. Config::whereIn('name', ['maintenance_mode', 'maintenance_content', 'maintenance_time'])->update(['value' => null]);
  46. }
  47. $jobEndTime = microtime(true);
  48. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  49. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  50. }
  51. // 注册验证码自动置无效 & 优惠券无效化
  52. private function expireCode(): void
  53. {
  54. // 注册验证码自动置无效
  55. VerifyCode::recentUnused()->update(['status' => 2]);
  56. // 优惠券到期 / 用尽的 自动置无效
  57. Coupon::withTrashed()->whereStatus(0)->where('end_time', '<=', time())->orWhereIn('type', [1, 2])->whereUsableTimes(0)->update(['status' => 2]);
  58. // 邀请码到期自动置无效
  59. Invite::whereStatus(0)->where('dateline', '<=', date('Y-m-d H:i:s'))->update(['status' => 2]);
  60. }
  61. // 封禁访问异常的订阅链接
  62. private function blockSubscribe(): void
  63. {
  64. if (sysConfig('is_subscribe_ban')) {
  65. $subscribe_ban_times = sysConfig('subscribe_ban_times');
  66. foreach (User::activeUser()->with('subscribe')->get() as $user) {
  67. if (! $user->subscribe || $user->subscribe->status === 0) { // 无订阅链接 或 已封
  68. continue;
  69. }
  70. // 24小时内不同IP的请求次数
  71. $request_times = $user->subscribeLogs()
  72. ->where('request_time', '>=', date('Y-m-d H:i:s', strtotime('-1 days')))
  73. ->distinct()
  74. ->count('request_ip');
  75. if ($request_times >= $subscribe_ban_times) {
  76. $user->subscribe->update([
  77. 'status' => 0,
  78. 'ban_time' => strtotime('+'.sysConfig('traffic_ban_time').' minutes'),
  79. 'ban_desc' => '存在异常,自动封禁',
  80. ]);
  81. // 记录封禁日志
  82. Helpers::addUserBanLog($user->id, 0, '【完全封禁订阅】-订阅24小时内请求异常');
  83. }
  84. }
  85. }
  86. }
  87. // 封禁账号
  88. private function blockUsers(): void
  89. {
  90. // 禁用流量超限用户
  91. foreach (User::activeUser()->whereRaw('u + d >= transfer_enable')->get() as $user) {
  92. $user->update(['enable' => 0]);
  93. // 写入日志
  94. Helpers::addUserBanLog($user->id, 0, '【封禁代理】-流量已用完');
  95. }
  96. // 封禁1小时内流量异常账号
  97. if (sysConfig('is_traffic_ban')) {
  98. $trafficBanTime = sysConfig('traffic_ban_time');
  99. foreach (User::activeUser()->whereBanTime(null)->get() as $user) {
  100. // 多往前取5分钟,防止数据统计任务执行时间过长导致没有数据
  101. if ($user->isTrafficWarning()) {
  102. $user->update([
  103. 'enable' => 0,
  104. 'ban_time' => strtotime('+'.$trafficBanTime.' minutes'),
  105. ]);
  106. // 写入日志
  107. Helpers::addUserBanLog($user->id, $trafficBanTime, '【临时封禁代理】-1小时内流量异常');
  108. }
  109. }
  110. }
  111. }
  112. // 解封被临时封禁的账号
  113. private function unblockUsers(): void
  114. {
  115. // 解封被临时封禁的账号
  116. $userList = User::whereEnable(0)->where('status', '>=', 0)->whereNotNull('ban_time')->where('ban_time', '<', time())->get();
  117. foreach ($userList as $user) {
  118. $user->update(['enable' => 1, 'ban_time' => null]);
  119. // 写入操作日志
  120. Helpers::addUserBanLog($user->id, 0, '【自动解封】-临时封禁到期');
  121. }
  122. // 可用流量大于已用流量也解封(比如:邀请返利自动加了流量)
  123. $userList = User::whereEnable(0)
  124. ->where('status', '>=', 0)
  125. ->whereBanTime(null)
  126. ->where('expired_at', '>=', date('Y-m-d'))
  127. ->whereRaw('u + d < transfer_enable')
  128. ->get();
  129. foreach ($userList as $user) {
  130. $user->update(['enable' => 1]);
  131. // 写入操作日志
  132. Helpers::addUserBanLog($user->id, 0, '【自动解封】-有流量解封');
  133. }
  134. }
  135. // 端口回收与分配
  136. private function dispatchPort(): void
  137. {
  138. // 自动分配端口
  139. User::activeUser()->wherePort(0)->get()->each(function ($user) {
  140. $user->update(['port' => Helpers::getPort()]);
  141. });
  142. // 被封禁 / 过期一个月 的账号自动释放端口
  143. User::where('port', '<>', 0)
  144. ->whereStatus(-1)
  145. ->orWhere('expired_at', '<=', date('Y-m-d', strtotime('-1 months')))
  146. ->update(['port' => 0]);
  147. }
  148. // 检测节点是否离线
  149. private function checkNodeStatus(): void
  150. {
  151. if (sysConfig('is_node_offline')) {
  152. $offlineCheckTimes = sysConfig('offline_check_times');
  153. $onlineNode = NodeHeartbeat::recently()->distinct()->pluck('node_id')->toArray();
  154. foreach (Node::whereIsRelay(0)->whereStatus(1)->get() as $node) {
  155. // 10分钟内无节点负载信息则认为是后端炸了
  156. $nodeTTL = ! in_array($node->id, $onlineNode, true);
  157. if ($nodeTTL && $offlineCheckTimes) {
  158. // 已通知次数
  159. $cacheKey = 'offline_check_times'.$node->id;
  160. if (Cache::has($cacheKey)) {
  161. $times = Cache::get($cacheKey);
  162. } else {
  163. // 键将保留24小时
  164. Cache::put($cacheKey, 1, Day);
  165. $times = 1;
  166. }
  167. if ($times < $offlineCheckTimes) {
  168. Cache::increment($cacheKey);
  169. Notification::send(User::permission('admin.node.edit,update')->orWhere(function ($query) {
  170. return $query->role('Super Admin');
  171. })->get(), new NodeOffline($node->name, $node->ip));
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }