mirror of
https://github.com/tnb-labs/panel.git
synced 2026-09-21 05:11:14 +08:00
依赖注入:
- 移除 google/wire(含 wire.go/wire_gen.go 与全部 ProviderSet),
改用 samber/do v2 单注入器 + 双入口惰性构建
- 贡献模型替代命令式注册:路由 routes:、命令 commands:、
任务 jobs: 前缀经 internal/registry 收集与校验
- 构造函数统一 func NewXxx(i do.Injector) (T, error)
三层架构:
- 补全用例层:每个 biz.XxxRepo 配 XxxUsecase,
service/command/job 表现层只依赖用例,不再直接引用仓储
路由与文档:
- route/http.go 按域拆为声明式 Endpoint 贡献,
Endpoint 承载登录白名单与端点限流语义
- 调试模式下提供 OpenAPI 3.1 文档:/openapi.json 与 /docs(Scalar),
从 validate 标签生成
目录与依赖:
- internal/http/{middleware,request,rule} 拍平至 internal/*
- CLI 命令拆至 internal/command
- 日志轮转 timberjack 换为 libtnb/logrotate
- 升级 validator/cron/sessions/gormstore/sqlite/securecookie
- 数据库迁移保持 gormigrate 不变
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
150 lines
4.1 KiB
Go
150 lines
4.1 KiB
Go
package frp
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/samber/do/v2"
|
|
|
|
"github.com/acepanel/panel/v3/internal/app"
|
|
"github.com/acepanel/panel/v3/internal/service"
|
|
"github.com/acepanel/panel/v3/pkg/io"
|
|
"github.com/acepanel/panel/v3/pkg/systemctl"
|
|
"github.com/acepanel/panel/v3/pkg/types"
|
|
)
|
|
|
|
type App struct{}
|
|
|
|
func NewApp(i do.Injector) (*App, error) {
|
|
return &App{}, nil
|
|
}
|
|
|
|
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) Status() string {
|
|
frps, _ := systemctl.Status("frps")
|
|
frpc, _ := systemctl.Status("frpc")
|
|
return types.AggregateAppStatus(frps, frpc)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
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
|
|
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)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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)
|
|
}
|