Skip to content

Commit

Permalink
feat: frp管理器
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jun 2, 2024
1 parent c3f9f56 commit 0c3c473
Show file tree
Hide file tree
Showing 15 changed files with 1,299 additions and 70 deletions.
225 changes: 225 additions & 0 deletions app/http/controllers/plugins/frp_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package plugins

import (
"fmt"
"github.com/goravel/framework/contracts/http"

"github.com/TheTNB/panel/app/http/controllers"
requests "github.com/TheTNB/panel/app/http/requests/plugins/frp"
"github.com/TheTNB/panel/pkg/tools"
)

type FrpController struct {
}

func NewFrpController() *FrpController {
return &FrpController{}
}

// Status
//
// @Summary 服务状态
// @Description 获取 Frp 服务状态
// @Tags 插件-Frp
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/frp/status [get]
func (r *FrpController) Status(ctx http.Context) http.Response {
frps, err := tools.ServiceStatus("frps")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 frpc 服务运行状态失败")
}
frpc, err := tools.ServiceStatus("frpc")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 frpc 服务运行状态失败")
}

return controllers.Success(ctx, http.Json{
"frps": frps,
"frpc": frpc,
})
}

// Enable
//
// @Summary 启用服务
// @Description 启用 Frp 服务
// @Tags 插件-Frp
// @Produce json
// @Security BearerToken
// @Param data body requests.Service true "request"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/frp/enable [post]
func (r *FrpController) Enable(ctx http.Context) http.Response {
var serviceRequest requests.Service
sanitize := controllers.Sanitize(ctx, &serviceRequest)
if sanitize != nil {
return sanitize
}

if err := tools.ServiceEnable(serviceRequest.Service); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("启用 %s 服务失败", serviceRequest.Service))
}

return controllers.Success(ctx, nil)
}

// Disable
//
// @Summary 禁用服务
// @Description 禁用 Frp 服务
// @Tags 插件-Frp
// @Produce json
// @Security BearerToken
// @Param data body requests.Service true "request"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/frp/disable [post]
func (r *FrpController) Disable(ctx http.Context) http.Response {
var serviceRequest requests.Service
sanitize := controllers.Sanitize(ctx, &serviceRequest)
if sanitize != nil {
return sanitize
}

if err := tools.ServiceDisable(serviceRequest.Service); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("禁用 %s 服务失败", serviceRequest.Service))
}

return controllers.Success(ctx, nil)
}

// Restart
//
// @Summary 重启服务
// @Description 重启 Frp 服务
// @Tags 插件-Frp
// @Produce json
// @Security BearerToken
// @param data body requests.Service true "request"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/frp/restart [post]
func (r *FrpController) Restart(ctx http.Context) http.Response {
var serviceRequest requests.Service
sanitize := controllers.Sanitize(ctx, &serviceRequest)
if sanitize != nil {
return sanitize
}

if err := tools.ServiceRestart(serviceRequest.Service); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("重启 %s 服务失败", serviceRequest.Service))
}

return controllers.Success(ctx, nil)
}

// Start
//
// @Summary 启动服务
// @Description 启动 Frp 服务
// @Tags 插件-Frp
// @Produce json
// @Security BearerToken
// @Param data body requests.Service true "request"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/frp/start [post]
func (r *FrpController) Start(ctx http.Context) http.Response {
var serviceRequest requests.Service
sanitize := controllers.Sanitize(ctx, &serviceRequest)
if sanitize != nil {
return sanitize
}

if err := tools.ServiceStart(serviceRequest.Service); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("启动 %s 服务失败", serviceRequest.Service))
}

status, err := tools.ServiceStatus(serviceRequest.Service)
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("获取 %s 服务运行状态失败", serviceRequest.Service))
}

return controllers.Success(ctx, status)
}

// Stop
//
// @Summary 停止服务
// @Description 停止 Frp 服务
// @Tags 插件-Frp
// @Produce json
// @Security BearerToken
// @Param data body requests.Service true "request"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/frp/stop [post]
func (r *FrpController) Stop(ctx http.Context) http.Response {
var serviceRequest requests.Service
sanitize := controllers.Sanitize(ctx, &serviceRequest)
if sanitize != nil {
return sanitize
}

if err := tools.ServiceStop(serviceRequest.Service); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("停止 %s 服务失败", serviceRequest.Service))
}

status, err := tools.ServiceStatus(serviceRequest.Service)
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, fmt.Sprintf("获取 %s 服务运行状态失败", serviceRequest.Service))
}

return controllers.Success(ctx, !status)
}

// GetConfig
//
// @Summary 获取配置
// @Description 获取 Frp 配置
// @Tags 插件-Frp
// @Produce json
// @Security BearerToken
// @Param service query string false "服务"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/frp/config [get]
func (r *FrpController) GetConfig(ctx http.Context) http.Response {
var serviceRequest requests.Service
sanitize := controllers.Sanitize(ctx, &serviceRequest)
if sanitize != nil {
return sanitize
}

config, err := tools.Read(fmt.Sprintf("/www/server/frp/%s.toml", serviceRequest.Service))
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, config)
}

// UpdateConfig
//
// @Summary 更新配置
// @Description 更新 Frp 配置
// @Tags 插件-Frp
// @Produce json
// @Security BearerToken
// @Param data body requests.UpdateConfig true "request"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/frp/config [post]
func (r *FrpController) UpdateConfig(ctx http.Context) http.Response {
var updateRequest requests.UpdateConfig
sanitize := controllers.Sanitize(ctx, &updateRequest)
if sanitize != nil {
return sanitize
}

if err := tools.Write(fmt.Sprintf("/www/server/frp/%s.toml", updateRequest.Service), updateRequest.Config, 0644); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

if err := tools.ServiceRestart(updateRequest.Service); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, nil)
}
7 changes: 1 addition & 6 deletions app/http/controllers/plugins/rsync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,15 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
commonrequests "github.com/TheTNB/panel/app/http/requests/common"
requests "github.com/TheTNB/panel/app/http/requests/plugins/rsync"
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)

type RsyncController struct {
setting internal.Setting
}

func NewRsyncController() *RsyncController {
return &RsyncController{
setting: services.NewSettingImpl(),
}
return &RsyncController{}
}

// Status
Expand Down
32 changes: 32 additions & 0 deletions app/http/requests/plugins/frp/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type Service struct {
Service string `form:"service" json:"service"`
}

func (r *Service) Authorize(ctx http.Context) error {
return nil
}

func (r *Service) Rules(ctx http.Context) map[string]string {
return map[string]string{
"service": "required|string|in:frps,frpc",
}
}

func (r *Service) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Service) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *Service) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
34 changes: 34 additions & 0 deletions app/http/requests/plugins/frp/update_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type UpdateConfig struct {
Service string `form:"service" json:"service"`
Config string `form:"config" json:"config"`
}

func (r *UpdateConfig) Authorize(ctx http.Context) error {
return nil
}

func (r *UpdateConfig) Rules(ctx http.Context) map[string]string {
return map[string]string{
"service": "required|string|in:frps,frpc",
"config": "required|string",
}
}

func (r *UpdateConfig) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UpdateConfig) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UpdateConfig) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
Loading

0 comments on commit 0c3c473

Please sign in to comment.