AutoJob.php 6.6 KB

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