mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 06:47:20 +08:00
fix: frp 相关接口
This commit is contained in:
@@ -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 启用服务
|
||||
|
||||
@@ -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 启动服务
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user