| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Http\Models\User;
- use App\Http\Models\Config;
- use Log;
- class AutoRegetPortJob extends Command
- {
- protected $signature = 'autoRegetPortJob';
- protected $description = '自动获取端口';
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- $config = $this->systemConfig();
- if ($config['auto_release_port']) {
- $userList = User::query()->where('status', '>=', 0)->where('enable', 1)->where('port', 0)->get();
- if (!$userList->isEmpty()) {
- foreach ($userList as $user) {
- $port = $config['is_rand_port'] ? $this->getRandPort() : $this->getOnlyPort();
- User::query()->where('id', $user->id)->update(['port' => $port]);
- }
- }
- }
- Log::info('定时任务:' . $this->description);
- }
- // 系统配置
- private function systemConfig()
- {
- $config = Config::query()->get();
- $data = [];
- foreach ($config as $vo) {
- $data[$vo->name] = $vo->value;
- }
- return $data;
- }
- // 获取一个随机端口
- public function getRandPort()
- {
- $config = $this->systemConfig();
- $port = mt_rand($config['min_port'], $config['max_port']);
- $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
- $exists_port = User::query()->pluck('port')->toArray();
- if (in_array($port, $exists_port) || in_array($port, $deny_port)) {
- $port = $this->getRandPort();
- }
- return $port;
- }
- // 获取一个端口
- public function getOnlyPort()
- {
- $config = $this->systemConfig();
- $port = $config['min_port'];
- $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
- $exists_port = User::query()->where('port', '>=', $config['min_port'])->where('port', '<=', $config['max_port'])->pluck('port')->toArray();
- while (in_array($port, $exists_port) || in_array($port, $deny_port)) {
- $port = $port + 1;
- }
- return $port;
- }
- }
|