mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 06:47:20 +08:00
* Initial plan * feat: 添加操作日志记录功能和前端日志查看页面 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: 修复 HttpLog 中 IP 字符串处理的类型安全问题 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint * feat: 完善面板内日志记录,添加网站/数据库/项目/计划任务/备份CRUD日志 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * feat: 完善更多模块的日志记录(证书/SSH/Webhook/用户/设置/安全) Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: 改进cert_dns删除方法,在删除前获取实体信息用于日志记录 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint * feat: 从session获取操作员ID并在前端显示用户名 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> Co-authored-by: 耗子 <haozi@loli.email>
129 lines
2.9 KiB
Go
129 lines
2.9 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/leonelquinteros/gotext"
|
|
"github.com/libtnb/chix"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/acepanel/panel/internal/biz"
|
|
"github.com/acepanel/panel/internal/http/request"
|
|
"github.com/acepanel/panel/pkg/tools"
|
|
)
|
|
|
|
type SettingService struct {
|
|
t *gotext.Locale
|
|
db *gorm.DB
|
|
settingRepo biz.SettingRepo
|
|
certRepo biz.CertRepo
|
|
certAccountRepo biz.CertAccountRepo
|
|
}
|
|
|
|
func NewSettingService(t *gotext.Locale, db *gorm.DB, setting biz.SettingRepo, cert biz.CertRepo, certAccount biz.CertAccountRepo) *SettingService {
|
|
return &SettingService{
|
|
t: t,
|
|
db: db,
|
|
settingRepo: setting,
|
|
certRepo: cert,
|
|
certAccountRepo: certAccount,
|
|
}
|
|
}
|
|
|
|
func (s *SettingService) Get(w http.ResponseWriter, r *http.Request) {
|
|
setting, err := s.settingRepo.GetPanel()
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
Success(w, setting)
|
|
}
|
|
|
|
func (s *SettingService) Update(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.SettingPanel](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
restart := false
|
|
if restart, err = s.settingRepo.UpdatePanel(r.Context(), req); err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
if restart {
|
|
tools.RestartPanel()
|
|
}
|
|
|
|
Success(w, chix.M{
|
|
"restart": restart,
|
|
})
|
|
}
|
|
|
|
func (s *SettingService) ObtainCert(w http.ResponseWriter, r *http.Request) {
|
|
ip, err := s.settingRepo.Get(biz.SettingKeyPublicIPs)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
var ips []string
|
|
if err = json.Unmarshal([]byte(ip), &ips); err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
if len(ips) == 0 {
|
|
Error(w, http.StatusBadRequest, s.t.Get("please set public ips first"))
|
|
return
|
|
}
|
|
|
|
var user biz.User
|
|
if err = s.db.First(&user).Error; err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
account, err := s.certAccountRepo.GetDefault(user.ID)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
crt, key, err := s.certRepo.ObtainPanel(account, ips)
|
|
if err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
if err = s.settingRepo.UpdateCert(&request.SettingCert{
|
|
Cert: string(crt),
|
|
Key: string(key),
|
|
}); err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
tools.RestartPanel()
|
|
|
|
Success(w, nil)
|
|
}
|
|
|
|
// UpdateCert 用于自动化工具更新证书
|
|
func (s *SettingService) UpdateCert(w http.ResponseWriter, r *http.Request) {
|
|
req, err := Bind[request.SettingCert](r)
|
|
if err != nil {
|
|
Error(w, http.StatusUnprocessableEntity, "%v", err)
|
|
return
|
|
}
|
|
|
|
if err = s.settingRepo.UpdateCert(req); err != nil {
|
|
Error(w, http.StatusInternalServerError, "%v", err)
|
|
return
|
|
}
|
|
|
|
tools.RestartPanel()
|
|
|
|
Success(w, nil)
|
|
}
|