diff --git a/app/Console/Commands/Panel.php b/app/Console/Commands/Panel.php index 8bd7a815..785d5711 100644 --- a/app/Console/Commands/Panel.php +++ b/app/Console/Commands/Panel.php @@ -44,6 +44,9 @@ class Panel extends Command case 'getInfo': $this->getInfo(); break; + case 'getPort': + $this->getPort(); + break; case 'writePluginInstall': $this->writePluginInstall(); break; @@ -132,9 +135,32 @@ class Panel extends Command 'password' => Hash::make($password), ]); + // 从nginx配置文件中获取面板端口 + $nginxConf = file_get_contents('/www/server/nginx/conf/nginx.conf'); + preg_match('/listen\s+(\d+)/', $nginxConf, $matches); + + if (!isset($matches[1])) { + $this->info('获取面板端口失败,请检查nginx主配置文件'); + } + $this->info('面板用户名:'.$username); $this->info('面板密码:'.$password); - $this->info('访问地址:http://IP:8888'); + $this->info('访问地址:http://IP:'.$matches[1]); + } + + /** + * 获取端口 + * @return void + */ + private function getPort(): void + { + // 从nginx配置文件中获取面板端口 + $nginxConf = file_get_contents('/www/server/nginx/conf/nginx.conf'); + preg_match('/listen\s+(\d+)/', $nginxConf, $matches); + + if (!isset($matches[1])) { + $this->info($matches[1]); + } } /** diff --git a/app/Http/Controllers/Api/SettingsController.php b/app/Http/Controllers/Api/SettingsController.php index b083217a..7cf6f626 100644 --- a/app/Http/Controllers/Api/SettingsController.php +++ b/app/Http/Controllers/Api/SettingsController.php @@ -28,10 +28,20 @@ class SettingsController extends Controller $settings->makeHidden('updated_at'); $settingArr = $settings->pluck('value', 'name'); + // 从nginx配置文件中获取面板端口 + $nginxConf = file_get_contents('/www/server/nginx/conf/nginx.conf'); + preg_match('/listen\s+(\d+)/', $nginxConf, $matches); + + if (!isset($matches[1])) { + $res['code'] = 1; + $res['msg'] = '获取面板端口失败,请检查nginx主配置文件'; + return response()->json($res); + } $data = [ 'name' => $settingArr['name'], 'username' => $request->user()->username, 'password' => '', + 'port' => $matches[1], ]; $res['code'] = 0; $res['msg'] = 'success'; @@ -62,6 +72,24 @@ class SettingsController extends Controller if ($request->input('password') != '') { $request->user()->update(['password' => Hash::make($request->input('password'))]); } + // 处理面板端口 + $port = $request->input('port'); + $nginxConf = file_get_contents('/www/server/nginx/conf/nginx.conf'); + preg_match('/listen\s+(\d+)/', $nginxConf, $matches); + if (!isset($matches[1])) { + $res['code'] = 1; + $res['msg'] = '获取面板端口失败,请检查nginx主配置文件'; + return response()->json($res); + } + if ($port != $matches[1]) { + $nginxConf = preg_replace('/listen\s+(\d+)/', 'listen '.$port, $nginxConf); + file_put_contents('/www/server/nginx/conf/nginx.conf', $nginxConf); + // 重载nginx + shell_exec('systemctl reload nginx'); + // 防火墙放行端口 + shell_exec('firewall-cmd --permanent --zone=public --add-port='.$port.'/tcp >/dev/null 2>&1'); + shell_exec('firewall-cmd --reload'); + } $res['code'] = 0; $res['msg'] = 'success'; return response()->json($res); diff --git a/resources/views/setting.blade.php b/resources/views/setting.blade.php index c4ddc5f7..3e96588f 100755 --- a/resources/views/setting.blade.php +++ b/resources/views/setting.blade.php @@ -33,6 +33,13 @@ Date: 2022-12-01
修改面板的登录密码(留空不修改)
+
+ +
+ +
+
修改面板的访问端口(保存后需要手动修改浏览器地址栏的端口为新端口以访问面板
+