2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 09:13:49 +08:00
Files
panel/internal/apps/frp/app.go
Copilot cff71af26b feat(frp): 添加运行用户设置功能 (#1193)
* Initial plan

* feat(frp): 添加运行用户设置功能

Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com>

* refactor(frp): 优化正则表达式和用户/组更新逻辑

Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com>

* 完成 FRP 运行用户设置功能

Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com>
2026-01-08 23:27:54 +08:00

166 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package frp
import (
"fmt"
"net/http"
"regexp"
"github.com/go-chi/chi/v5"
"github.com/acepanel/panel/internal/app"
"github.com/acepanel/panel/internal/service"
"github.com/acepanel/panel/pkg/io"
"github.com/acepanel/panel/pkg/systemctl"
)
// 预编译正则表达式
var (
userCaptureRegex = regexp.MustCompile(`(?m)^User=(.*)$`)
groupCaptureRegex = regexp.MustCompile(`(?m)^Group=(.*)$`)
userRegex = regexp.MustCompile(`(?m)^User=.*$`)
groupRegex = regexp.MustCompile(`(?m)^Group=.*$`)
serviceRegex = regexp.MustCompile(`(?m)^\[Service\]$`)
)
type App struct{}
func NewApp() *App {
return &App{}
}
func (s *App) Route(r chi.Router) {
r.Get("/config", s.GetConfig)
r.Post("/config", s.UpdateConfig)
r.Get("/user", s.GetUser)
r.Post("/user", s.UpdateUser)
}
func (s *App) GetConfig(w http.ResponseWriter, r *http.Request) {
req, err := service.Bind[Name](r)
if err != nil {
service.Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}
config, err := io.Read(fmt.Sprintf("%s/server/frp/%s.toml", app.Root, req.Name))
if err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
service.Success(w, config)
}
func (s *App) UpdateConfig(w http.ResponseWriter, r *http.Request) {
req, err := service.Bind[UpdateConfig](r)
if err != nil {
service.Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}
if err = io.Write(fmt.Sprintf("%s/server/frp/%s.toml", app.Root, req.Name), req.Config, 0644); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
if err = systemctl.Restart(req.Name); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
service.Success(w, nil)
}
// UserInfo 运行用户信息
type UserInfo struct {
User string `json:"user"`
Group string `json:"group"`
}
// GetUser 获取服务的运行用户
func (s *App) GetUser(w http.ResponseWriter, r *http.Request) {
req, err := service.Bind[Name](r)
if err != nil {
service.Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}
servicePath := fmt.Sprintf("/etc/systemd/system/%s.service", req.Name)
content, err := io.Read(servicePath)
if err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
userInfo := UserInfo{
User: "",
Group: "",
}
// 解析 User 和 Group
if matches := userCaptureRegex.FindStringSubmatch(content); len(matches) > 1 {
userInfo.User = matches[1]
}
if matches := groupCaptureRegex.FindStringSubmatch(content); len(matches) > 1 {
userInfo.Group = matches[1]
}
service.Success(w, userInfo)
}
// UpdateUser 更新服务的运行用户
func (s *App) UpdateUser(w http.ResponseWriter, r *http.Request) {
req, err := service.Bind[UpdateUser](r)
if err != nil {
service.Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}
servicePath := fmt.Sprintf("/etc/systemd/system/%s.service", req.Name)
content, err := io.Read(servicePath)
if err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
// 检查 User 和 Group 是否存在
hasUser := userRegex.MatchString(content)
hasGroup := groupRegex.MatchString(content)
// 替换或添加 User 和 Group 配置
if hasUser && hasGroup {
// 两者都存在,分别替换
content = userRegex.ReplaceAllString(content, fmt.Sprintf("User=%s", req.User))
content = groupRegex.ReplaceAllString(content, fmt.Sprintf("Group=%s", req.Group))
} else if hasUser && !hasGroup {
// 只有 User替换 User 并添加 Group
content = userRegex.ReplaceAllString(content, fmt.Sprintf("User=%s\nGroup=%s", req.User, req.Group))
} else if !hasUser && hasGroup {
// 只有 Group添加 User 并替换 Group
content = serviceRegex.ReplaceAllString(content, fmt.Sprintf("[Service]\nUser=%s", req.User))
content = groupRegex.ReplaceAllString(content, fmt.Sprintf("Group=%s", req.Group))
} else {
// 两者都不存在,在 [Service] 后添加两者
content = serviceRegex.ReplaceAllString(content, fmt.Sprintf("[Service]\nUser=%s\nGroup=%s", req.User, req.Group))
}
if err = io.Write(servicePath, content, 0644); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
// 重载 systemd 配置
if err = systemctl.DaemonReload(); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
// 重启服务以应用更改
if err = systemctl.Restart(req.Name); err != nil {
service.Error(w, http.StatusInternalServerError, "%v", err)
return
}
service.Success(w, nil)
}