From 4bb07f1a2030961b1da407c81f1182a0e8ef113a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Tue, 4 Jul 2023 02:25:47 +0800 Subject: [PATCH] fix: plugin check --- .../plugins/openresty_controller.go | 40 +++++++++++++++++-- app/http/controllers/plugins/plugins.go | 17 +++++--- public/panel/adminui/src/modules/admin.js | 2 +- public/panel/views/index.html | 4 +- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/app/http/controllers/plugins/openresty_controller.go b/app/http/controllers/plugins/openresty_controller.go index 85c8d679..8d21921a 100644 --- a/app/http/controllers/plugins/openresty_controller.go +++ b/app/http/controllers/plugins/openresty_controller.go @@ -29,7 +29,9 @@ func NewOpenrestyController() *OpenRestyController { // Status 获取运行状态 func (r *OpenRestyController) Status(ctx http.Context) { - Check(ctx, "openresty") + if !Check(ctx, "openresty") { + return + } cmd := exec.Command("bash", "-c", "systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") out, err := cmd.CombinedOutput() @@ -53,7 +55,9 @@ func (r *OpenRestyController) Status(ctx http.Context) { // Reload 重载配置 func (r *OpenRestyController) Reload(ctx http.Context) { - Check(ctx, "openresty") + if !Check(ctx, "openresty") { + return + } cmd := exec.Command("bash", "-c", "systemctl reload openresty") _, err := cmd.CombinedOutput() @@ -86,7 +90,9 @@ func (r *OpenRestyController) Reload(ctx http.Context) { // Start 启动OpenResty func (r *OpenRestyController) Start(ctx http.Context) { - Check(ctx, "openresty") + if !Check(ctx, "openresty") { + return + } cmd := exec.Command("bash", "-c", "systemctl start openresty") _, err := cmd.CombinedOutput() @@ -119,7 +125,9 @@ func (r *OpenRestyController) Start(ctx http.Context) { // Stop 停止OpenResty func (r *OpenRestyController) Stop(ctx http.Context) { - Check(ctx, "openresty") + if !Check(ctx, "openresty") { + return + } cmd := exec.Command("bash", "-c", "systemctl stop openresty") _, err := cmd.CombinedOutput() @@ -152,6 +160,10 @@ func (r *OpenRestyController) Stop(ctx http.Context) { // Restart 重启OpenResty func (r *OpenRestyController) Restart(ctx http.Context) { + if !Check(ctx, "openresty") { + return + } + cmd := exec.Command("bash", "-c", "systemctl restart openresty") _, err := cmd.CombinedOutput() if err != nil { @@ -183,6 +195,10 @@ func (r *OpenRestyController) Restart(ctx http.Context) { // GetConfig 获取配置 func (r *OpenRestyController) GetConfig(ctx http.Context) { + if !Check(ctx, "openresty") { + return + } + config, err := os.ReadFile("/www/server/openresty/conf/nginx.conf") if err != nil { controllers.Error(ctx, http.StatusInternalServerError, "获取OpenResty配置失败") @@ -194,6 +210,10 @@ func (r *OpenRestyController) GetConfig(ctx http.Context) { // SaveConfig 保存配置 func (r *OpenRestyController) SaveConfig(ctx http.Context) { + if !Check(ctx, "openresty") { + return + } + config := ctx.Request().Input("config") if len(config) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "配置不能为空") @@ -245,6 +265,10 @@ func (r *OpenRestyController) SaveConfig(ctx http.Context) { // ErrorLog 获取错误日志 func (r *OpenRestyController) ErrorLog(ctx http.Context) { + if !Check(ctx, "openresty") { + return + } + cmd := exec.Command("bash", "-c", "tail -n 100 /www/wwwlogs/nginx_error.log") out, err := cmd.CombinedOutput() if err != nil { @@ -258,6 +282,10 @@ func (r *OpenRestyController) ErrorLog(ctx http.Context) { // ClearErrorLog 清空错误日志 func (r *OpenRestyController) ClearErrorLog(ctx http.Context) { + if !Check(ctx, "openresty") { + return + } + cmd := exec.Command("bash", "-c", "echo '' > /www/wwwlogs/nginx_error.log") _, err := cmd.CombinedOutput() if err != nil { @@ -271,6 +299,10 @@ func (r *OpenRestyController) ClearErrorLog(ctx http.Context) { // Load 获取负载 func (r *OpenRestyController) Load(ctx http.Context) { + if !Check(ctx, "openresty") { + return + } + client := req.C().SetTimeout(10 * time.Second) resp, err := client.R().Get("http://127.0.0.1/nginx_status") if err != nil || !resp.IsSuccessState() { diff --git a/app/http/controllers/plugins/plugins.go b/app/http/controllers/plugins/plugins.go index c18b7a45..8797c709 100644 --- a/app/http/controllers/plugins/plugins.go +++ b/app/http/controllers/plugins/plugins.go @@ -6,21 +6,24 @@ import ( "github.com/goravel/framework/contracts/http" "github.com/goravel/framework/facades" + "panel/app/http/controllers" "panel/app/services" ) // Check 检查插件是否可用 -func Check(ctx http.Context, slug string) { +func Check(ctx http.Context, slug string) bool { plugin := services.NewPluginImpl().GetBySlug(slug) installedPlugin := services.NewPluginImpl().GetInstalledBySlug(slug) installedPlugins, err := services.NewPluginImpl().AllInstalled() if err != nil { facades.Log().Error("[面板][插件] 获取已安装插件失败") - ctx.Request().AbortWithStatusJson(http.StatusInternalServerError, "系统内部错误") + controllers.Error(ctx, http.StatusInternalServerError, "系统内部错误") + return false } if installedPlugin.Version != plugin.Version || installedPlugin.Slug != plugin.Slug { - ctx.Request().AbortWithStatusJson(http.StatusForbidden, "插件 "+slug+" 需要更新至 "+plugin.Version+" 版本") + controllers.Error(ctx, http.StatusForbidden, "插件 "+slug+" 需要更新至 "+plugin.Version+" 版本") + return false } var lock sync.RWMutex @@ -37,7 +40,8 @@ func Check(ctx http.Context, slug string) { _, requireFound := pluginsMap[require] lock.RUnlock() if !requireFound { - ctx.Request().AbortWithStatusJson(http.StatusForbidden, "插件 "+slug+" 需要依赖 "+require+" 插件") + controllers.Error(ctx, http.StatusForbidden, "插件 "+slug+" 需要依赖 "+require+" 插件") + return false } } @@ -46,7 +50,10 @@ func Check(ctx http.Context, slug string) { _, excludeFound := pluginsMap[exclude] lock.RUnlock() if excludeFound { - ctx.Request().AbortWithStatusJson(http.StatusForbidden, "插件 "+slug+" 不兼容 "+exclude+" 插件") + controllers.Error(ctx, http.StatusForbidden, "插件 "+slug+" 不兼容 "+exclude+" 插件") + return false } } + + return true } diff --git a/public/panel/adminui/src/modules/admin.js b/public/panel/adminui/src/modules/admin.js index 5dec0834..cca9140b 100644 --- a/public/panel/adminui/src/modules/admin.js +++ b/public/panel/adminui/src/modules/admin.js @@ -798,7 +798,7 @@ layui.define('view', function (exports) { } // 清理所有定时器 - var highestTimeoutId = setTimeout('') + var highestTimeoutId = setTimeout(';') for (var i = 0; i < highestTimeoutId; i++) { clearTimeout(i) } diff --git a/public/panel/views/index.html b/public/panel/views/index.html index 2f6d76dd..4cc2a3aa 100644 --- a/public/panel/views/index.html +++ b/public/panel/views/index.html @@ -87,8 +87,8 @@ Date: 2023-06-22 {{# }); }} - {{# if(d.data.length === 0){ }} - 这里好像啥也没有... + {{# if(d.data == null){ }} + 这里好像啥也没有,去插件中心看看吧! {{# } }}