AutoBanSubscribeJob.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Http\Models\Config;
  5. use App\Http\Models\UserSubscribe;
  6. use App\Http\Models\UserSubscribeLog;
  7. use Log;
  8. class AutoBanSubscribeJob extends Command
  9. {
  10. protected $signature = 'autoBanSubscribeJob';
  11. protected $description = '自动封禁异常订阅链接';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function handle()
  17. {
  18. /*
  19. * 客户端请求头有多种,常见如下:
  20. * SSR、SSRR安卓客户端:okhttp/3.8.0
  21. * Shadowrocket:Shadowrocket/516 CFNetwork/893.14.2 Darwin/17.3.0
  22. * ShadowsocksR win版:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36
  23. */
  24. $config = $this->systemConfig();
  25. // 封禁24小时访问异常的订阅链接
  26. if ($config['is_subscribe_ban']) {
  27. $subscribeList = UserSubscribe::query()->where('status', 1)->get();
  28. if (!$subscribeList->isEmpty()) {
  29. foreach ($subscribeList as $subscribe) {
  30. // 24小时内不同IP的请求次数
  31. $request_times = UserSubscribeLog::query()->where('sid', $subscribe->id)->where('request_time', '>=', date("Y-m-d H:i:s", strtotime("-24 hours")))->distinct('request_ip')->count('request_ip');
  32. if ($request_times >= $config['subscribe_ban_times']) {
  33. UserSubscribe::query()->where('id', $subscribe->id)->update(['status' => 0, 'ban_time' => time(), 'ban_desc' => '存在异常,自动封禁']);
  34. }
  35. }
  36. }
  37. }
  38. Log::info('定时任务:' . $this->description);
  39. }
  40. // 系统配置
  41. private function systemConfig() {
  42. $config = Config::query()->get();
  43. $data = [];
  44. foreach ($config as $vo) {
  45. $data[$vo->name] = $vo->value;
  46. }
  47. return $data;
  48. }
  49. }