221 lines
6.6 KiB
Go
221 lines
6.6 KiB
Go
package httpapi
|
||
|
||
import (
|
||
"errors"
|
||
"net/http"
|
||
"net/mail"
|
||
"sea-vhost/internal/store"
|
||
"strconv"
|
||
"strings"
|
||
)
|
||
|
||
func (s *Server) resourceAudit(r *http.Request, action, kind string, id uint64, err error, detail string) {
|
||
s.store.Log(r.Context(), nil, currentUser(r).ID, action, kind, strconv.FormatUint(id, 10), requestClientIP(r), err == nil, detail)
|
||
}
|
||
func (s *Server) adminAccount(w http.ResponseWriter, r *http.Request) {
|
||
id, err := strconv.ParseUint(r.PathValue("id"), 10, 64)
|
||
if err != nil || id == 0 {
|
||
writeError(w, 400, "用户编号无效")
|
||
return
|
||
}
|
||
p, err := s.store.AccountProfile(r.Context(), id)
|
||
if err != nil {
|
||
writeError(w, 404, "用户不存在或已删除")
|
||
return
|
||
}
|
||
if r.Method == http.MethodGet {
|
||
writeJSON(w, 200, p)
|
||
return
|
||
}
|
||
if r.Method == http.MethodDelete {
|
||
var input struct {
|
||
Confirmation string `json:"confirmation"`
|
||
}
|
||
if decodeJSON(r, &input) != nil || input.Confirmation != p.Slug {
|
||
writeError(w, 422, "请输入用户标识确认删除")
|
||
return
|
||
}
|
||
err = s.store.DeleteAccount(r.Context(), id)
|
||
s.resourceAudit(r, "delete", "account", id, err, "删除用户 "+p.Slug+",保留审计记录")
|
||
if err != nil {
|
||
writeError(w, 409, "无法删除用户:请先解绑或转移全部主机,并确认没有正在执行的归属变更")
|
||
return
|
||
}
|
||
w.WriteHeader(204)
|
||
return
|
||
}
|
||
var input store.AccountProfile
|
||
if decodeJSON(r, &input) != nil {
|
||
writeError(w, 400, "请求格式错误")
|
||
return
|
||
}
|
||
input.Name = strings.TrimSpace(input.Name)
|
||
input.DisplayName = strings.TrimSpace(input.DisplayName)
|
||
input.Email = strings.ToLower(strings.TrimSpace(input.Email))
|
||
address, e := mail.ParseAddress(input.Email)
|
||
if input.Name == "" || len([]rune(input.Name)) > 120 || len([]rune(input.DisplayName)) < 2 || len([]rune(input.DisplayName)) > 80 || e != nil || address.Address != input.Email || len(input.Email) > 190 {
|
||
writeError(w, 422, "请检查显示名称和登录邮箱")
|
||
return
|
||
}
|
||
err = s.store.UpdateAccount(r.Context(), id, input)
|
||
s.resourceAudit(r, "update", "account", id, err, "更新用户资料 "+p.Slug)
|
||
if err != nil {
|
||
writeError(w, 422, "无法保存用户资料,请检查登录邮箱是否重复")
|
||
return
|
||
}
|
||
w.WriteHeader(204)
|
||
}
|
||
func (s *Server) adminHostRecord(w http.ResponseWriter, r *http.Request) {
|
||
id, err := projectID(r)
|
||
if err != nil {
|
||
writeError(w, 400, "主机编号无效")
|
||
return
|
||
}
|
||
hosts, err := s.store.ListAdminProjects(r.Context())
|
||
if err != nil {
|
||
writeError(w, 500, "无法读取主机")
|
||
return
|
||
}
|
||
var host *store.AdminProject
|
||
for i := range hosts {
|
||
if hosts[i].ID == id {
|
||
host = &hosts[i]
|
||
break
|
||
}
|
||
}
|
||
if host == nil {
|
||
writeError(w, 404, "主机不存在")
|
||
return
|
||
}
|
||
if r.Method == http.MethodGet {
|
||
writeJSON(w, 200, host)
|
||
return
|
||
}
|
||
if r.Method == http.MethodDelete {
|
||
var input struct {
|
||
Confirmation string `json:"confirmation"`
|
||
}
|
||
if decodeJSON(r, &input) != nil || input.Confirmation != host.HostName {
|
||
writeError(w, 422, "请输入主机名确认回收")
|
||
return
|
||
}
|
||
err = s.projects.RecycleManagedHost(r.Context(), id)
|
||
s.resourceAudit(r, "recycle", "project", id, err, "手动回收主机 "+host.HostName+";7 天后清理,备份保留 14 天")
|
||
if err != nil {
|
||
writeError(w, 409, err.Error())
|
||
return
|
||
}
|
||
w.WriteHeader(204)
|
||
return
|
||
}
|
||
var input struct {
|
||
Name string `json:"name"`
|
||
AllowRuntimeChange *bool `json:"allowRuntimeChange"`
|
||
}
|
||
if decodeJSON(r, &input) != nil {
|
||
writeError(w, 400, "请求格式错误")
|
||
return
|
||
}
|
||
input.Name = strings.TrimSpace(input.Name)
|
||
if len([]rune(input.Name)) < 1 || len([]rune(input.Name)) > 50 || strings.ContainsAny(input.Name, "\x00\r\n") {
|
||
writeError(w, 422, "主机显示名称应为 1–50 个字符")
|
||
return
|
||
}
|
||
err = s.store.RenameHost(r.Context(), id, input.Name)
|
||
if err == nil && input.AllowRuntimeChange != nil {
|
||
err = s.store.SetRuntimePolicy(r.Context(), id, *input.AllowRuntimeChange)
|
||
}
|
||
s.resourceAudit(r, "update", "project", id, err, "修改主机显示名称 "+host.HostName)
|
||
if err != nil {
|
||
writeError(w, 409, "无法修改名称,请检查主机是否正在回收或处理归属变更")
|
||
return
|
||
}
|
||
w.WriteHeader(204)
|
||
}
|
||
func (s *Server) adminRestoreHost(w http.ResponseWriter, r *http.Request) {
|
||
id, err := projectID(r)
|
||
if err != nil {
|
||
writeError(w, 400, "主机编号无效")
|
||
return
|
||
}
|
||
err = s.projects.RestoreManagedHost(r.Context(), id)
|
||
s.resourceAudit(r, "restore", "project", id, err, "恢复手动回收主机;网站和 FTP 保持停止,计划任务保持暂停")
|
||
if err != nil {
|
||
writeError(w, 409, err.Error())
|
||
return
|
||
}
|
||
w.WriteHeader(204)
|
||
}
|
||
func (s *Server) adminNodeRecord(w http.ResponseWriter, r *http.Request) {
|
||
id, err := nodeID(r)
|
||
if err != nil || id == 0 {
|
||
writeError(w, 400, "节点编号无效")
|
||
return
|
||
}
|
||
existing, err := s.store.Node(r.Context(), id)
|
||
if err != nil {
|
||
writeError(w, 404, "节点不存在或已删除")
|
||
return
|
||
}
|
||
if r.Method == http.MethodGet {
|
||
writeJSON(w, 200, existing)
|
||
return
|
||
}
|
||
if r.Method == http.MethodDelete {
|
||
var input struct {
|
||
Confirmation string `json:"confirmation"`
|
||
}
|
||
if decodeJSON(r, &input) != nil || input.Confirmation != existing.Name {
|
||
writeError(w, 422, "请输入节点名称确认删除")
|
||
return
|
||
}
|
||
err = s.projects.DeleteManagedNode(r.Context(), id)
|
||
s.resourceAudit(r, "delete", "node", id, err, "删除节点 "+existing.Name+",撤销主控保存的连接凭据")
|
||
} else {
|
||
var input store.Node
|
||
if decodeJSON(r, &input) != nil {
|
||
writeError(w, 400, "请求格式错误")
|
||
return
|
||
}
|
||
normalizeNodeMetadata(&input)
|
||
if err = validateNodeMetadata(input, existing.Transport == "local"); err != nil {
|
||
writeError(w, 422, err.Error())
|
||
return
|
||
}
|
||
err = s.projects.UpdateManagedNode(r.Context(), id, input)
|
||
s.resourceAudit(r, "update", "node", id, err, "更新节点配置 "+existing.Name)
|
||
}
|
||
if err != nil {
|
||
if errors.Is(err, store.ErrNotFound) {
|
||
writeError(w, 404, "节点不存在或已删除")
|
||
} else {
|
||
writeError(w, 409, err.Error())
|
||
}
|
||
return
|
||
}
|
||
w.WriteHeader(204)
|
||
}
|
||
|
||
func (s *Server) adminDetachRecycledHost(w http.ResponseWriter, r *http.Request) {
|
||
id, err := projectID(r)
|
||
if err != nil {
|
||
writeError(w, 400, "主机编号无效")
|
||
return
|
||
}
|
||
var input struct {
|
||
Version uint64 `json:"ownershipVersion"`
|
||
Confirmation string `json:"confirmation"`
|
||
}
|
||
if decodeJSON(r, &input) != nil {
|
||
writeError(w, 400, "请求格式错误")
|
||
return
|
||
}
|
||
err = s.store.DetachRecycledHost(r.Context(), id, input.Version, input.Confirmation)
|
||
s.resourceAudit(r, "unbind", "project", id, err, "手动解绑回收主机的用户,保持回收状态与保留期限")
|
||
if err != nil {
|
||
writeError(w, 409, err.Error())
|
||
return
|
||
}
|
||
w.WriteHeader(204)
|
||
}
|