feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,81 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"sea-vhost/internal/service"
|
||||
"sea-vhost/internal/store"
|
||||
)
|
||||
|
||||
func (s *Server) requestAutoCertificate(w http.ResponseWriter, r *http.Request) {
|
||||
projectID, projectErr := projectID(r)
|
||||
siteID, siteErr := siteID(r)
|
||||
if projectErr != nil || siteErr != nil {
|
||||
writeError(w, http.StatusBadRequest, "网站编号无效")
|
||||
return
|
||||
}
|
||||
user := currentUser(r)
|
||||
site, err := s.projects.RequestAutoCertificate(r.Context(), user, projectID, siteID)
|
||||
if err != nil {
|
||||
s.store.Log(r.Context(), user.TenantID, user.ID, "issue", "automatic_certificate", strconv.FormatUint(siteID, 10), requestClientIP(r), false, "自动证书签发或部署失败")
|
||||
switch {
|
||||
case errors.Is(err, service.ErrQuotaSuspended):
|
||||
writeProjectMutationError(w, http.StatusUnprocessableEntity, err)
|
||||
case errors.Is(err, store.ErrNotFound):
|
||||
writeError(w, http.StatusNotFound, "虚拟主机或网站不存在")
|
||||
default:
|
||||
writeError(w, http.StatusUnprocessableEntity, "自动证书申请失败,请确认全部已启用域名均已解析到本服务器,且公网可访问 80 端口")
|
||||
}
|
||||
return
|
||||
}
|
||||
s.store.Log(r.Context(), user.TenantID, user.ID, "issue", "automatic_certificate", strconv.FormatUint(siteID, 10), requestClientIP(r), true, "自动证书已签发并部署到网站")
|
||||
writeJSON(w, http.StatusOK, map[string]any{"site": site})
|
||||
}
|
||||
|
||||
func (s *Server) installManualCertificate(w http.ResponseWriter, r *http.Request) {
|
||||
projectID, projectErr := projectID(r)
|
||||
siteID, siteErr := siteID(r)
|
||||
if projectErr != nil || siteErr != nil {
|
||||
writeError(w, http.StatusBadRequest, "网站编号无效")
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Certificate string `json:"certificate"`
|
||||
PrivateKey string `json:"privateKey"`
|
||||
}
|
||||
if err := decodeJSON(r, &input); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
user := currentUser(r)
|
||||
site, err := s.projects.InstallManualCertificate(r.Context(), user, projectID, siteID, input.Certificate, input.PrivateKey)
|
||||
if err != nil {
|
||||
// Deliberately record no certificate or private-key material. The resource
|
||||
// id is enough to correlate this operation with broker/system logs.
|
||||
s.store.Log(r.Context(), user.TenantID, user.ID, "install", "manual_certificate", strconv.FormatUint(siteID, 10), requestClientIP(r), false, "手动证书校验或安装失败")
|
||||
writeProjectMutationError(w, http.StatusUnprocessableEntity, err)
|
||||
return
|
||||
}
|
||||
s.store.Log(r.Context(), user.TenantID, user.ID, "install", "manual_certificate", strconv.FormatUint(siteID, 10), requestClientIP(r), true, "安装用户提供的网站证书并关闭自动续签")
|
||||
writeJSON(w, http.StatusOK, map[string]any{"site": site})
|
||||
}
|
||||
|
||||
func (s *Server) disableCertificate(w http.ResponseWriter, r *http.Request) {
|
||||
projectID, projectErr := projectID(r)
|
||||
siteID, siteErr := siteID(r)
|
||||
if projectErr != nil || siteErr != nil {
|
||||
writeError(w, http.StatusBadRequest, "网站编号无效")
|
||||
return
|
||||
}
|
||||
user := currentUser(r)
|
||||
site, err := s.projects.DisableCertificate(r.Context(), user, projectID, siteID)
|
||||
if err != nil {
|
||||
s.store.Log(r.Context(), user.TenantID, user.ID, "disable", "certificate", strconv.FormatUint(siteID, 10), requestClientIP(r), false, "关闭网站证书失败")
|
||||
writeProjectMutationError(w, http.StatusUnprocessableEntity, err)
|
||||
return
|
||||
}
|
||||
s.store.Log(r.Context(), user.TenantID, user.ID, "disable", "certificate", strconv.FormatUint(siteID, 10), requestClientIP(r), true, "关闭网站 SSL 与自动续签")
|
||||
writeJSON(w, http.StatusOK, map[string]any{"site": site})
|
||||
}
|
||||
Reference in new issue
Block a user