From 8ad42f8c0e71b6d620353651c9fd15ce6e2c02bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 8 Jun 2024 15:26:57 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20frp=20=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/plugins/frp_controller.go | 27 ++++++++++++++++++- pkg/tools/service.go | 27 ++++++++++++++++--- routes/plugin.go | 5 ++-- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/app/http/controllers/plugins/frp_controller.go b/app/http/controllers/plugins/frp_controller.go index ad025f80..9e8839f7 100644 --- a/app/http/controllers/plugins/frp_controller.go +++ b/app/http/controllers/plugins/frp_controller.go @@ -29,7 +29,7 @@ func NewFrpController() *FrpController { func (r *FrpController) Status(ctx http.Context) http.Response { frps, err := tools.ServiceStatus("frps") if err != nil { - return controllers.Error(ctx, http.StatusInternalServerError, "获取 frpc 服务运行状态失败") + return controllers.Error(ctx, http.StatusInternalServerError, "获取 frps 服务运行状态失败") } frpc, err := tools.ServiceStatus("frpc") if err != nil { @@ -42,6 +42,31 @@ func (r *FrpController) Status(ctx http.Context) http.Response { }) } +// IsEnabled +// +// @Summary 是否启用服务 +// @Description 获取是否启用 Frp 服务 +// @Tags 插件-Frp +// @Produce json +// @Security BearerToken +// @Success 200 {object} controllers.SuccessResponse +// @Router /plugins/frp/isEnabled [get] +func (r *FrpController) IsEnabled(ctx http.Context) http.Response { + frps, err := tools.ServiceIsEnabled("frps") + if err != nil { + return controllers.Error(ctx, http.StatusInternalServerError, "获取 frps 服务启用状态失败:"+err.Error()) + } + frpc, err := tools.ServiceIsEnabled("frpc") + if err != nil { + return controllers.Error(ctx, http.StatusInternalServerError, "获取 frpc 服务启用状态失败") + } + + return controllers.Success(ctx, http.Json{ + "frps": frps, + "frpc": frpc, + }) +} + // Enable // // @Summary 启用服务 diff --git a/pkg/tools/service.go b/pkg/tools/service.go index e3bb7fd8..9d4797c0 100644 --- a/pkg/tools/service.go +++ b/pkg/tools/service.go @@ -1,6 +1,11 @@ package tools -import "fmt" +import ( + "errors" + "fmt" + "os/exec" + "strings" +) // ServiceStatus 获取服务状态 func ServiceStatus(name string) (bool, error) { @@ -10,8 +15,24 @@ func ServiceStatus(name string) (bool, error) { // ServiceIsEnabled 服务是否启用 func ServiceIsEnabled(name string) (bool, error) { - output, err := Exec(fmt.Sprintf("systemctl is-enabled %s", name)) - return output == "enabled", err + cmd := exec.Command("systemctl", "is-enabled", name) + output, _ := cmd.CombinedOutput() + status := strings.TrimSpace(string(output)) + + switch status { + case "enabled": + return true, nil + case "disabled": + return false, nil + case "masked": + return false, errors.New("服务已被屏蔽") + case "static": + return false, errors.New("服务已被静态启用") + case "indirect": + return false, errors.New("服务已被间接启用") + default: + return false, errors.New("无法确定服务状态") + } } // ServiceStart 启动服务 diff --git a/routes/plugin.go b/routes/plugin.go index 735d92e9..c19bf6de 100644 --- a/routes/plugin.go +++ b/routes/plugin.go @@ -356,8 +356,9 @@ func Plugin() { r.Prefix("frp").Group(func(route route.Router) { frpController := plugins.NewFrpController() route.Get("status", frpController.Status) - route.Get("enable", frpController.Enable) - route.Get("disable", frpController.Disable) + route.Get("isEnabled", frpController.IsEnabled) + route.Post("enable", frpController.Enable) + route.Post("disable", frpController.Disable) route.Post("start", frpController.Start) route.Post("stop", frpController.Stop) route.Post("restart", frpController.Restart)