feat: import Sea vHost single-server testing release

This commit is contained in:
canglan committed 2026-10-30 08:39:10 +08:00
commit 248badd33e
441 files changed
+63221

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
package isolation
import (
"context"
"encoding/json"
"io"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
var nginxListenLine = regexp.MustCompile(`(?m)^(\s*)listen\s+(?:\[[0-9A-Fa-f:]+\]|[0-9.]+)?(?::)?([0-9]{1,5})([^;]*);\s*$`)
func (a Agent) configureNginxNetwork(w http.ResponseWriter, r *http.Request) {
var spec NginxNetworkSpec
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&spec); err != nil || decoder.Decode(&struct{}{}) != io.EOF ||
!managedWebHostPattern.MatchString(spec.Project.HostName) || !webDomainPattern.MatchString(spec.Domain) ||
spec.Project.TenantID == 0 || spec.Project.StorageBytes == 0 || spec.Project.ProcessLimit == 0 ||
spec.HTTPPort < 1 || spec.HTTPPort > 65535 || spec.HTTPSPort < 1 || spec.HTTPSPort > 65535 || spec.HTTPPort == spec.HTTPSPort {
http.Error(w, "invalid request", http.StatusBadRequest)
return
}
if spec.Dedicated && net.ParseIP(spec.IPAddress) == nil {
http.Error(w, "invalid dedicated ip", http.StatusBadRequest)
return
}
if !spec.Dedicated && strings.TrimSpace(spec.IPAddress) != "" {
http.Error(w, "shared mode cannot bind an ip", http.StatusBadRequest)
return
}
vhostRoot, err := filepath.Abs(a.NginxVhostRoot)
if err != nil || !filepath.IsAbs(vhostRoot) || requirePhysicalDirectory(vhostRoot) != nil {
http.Error(w, "nginx vhost root is unavailable", http.StatusPreconditionFailed)
return
}
configPath := filepath.Join(vhostRoot, strings.ToLower(spec.Domain)+".conf")
info, err := os.Lstat(configPath)
if err != nil || !info.Mode().IsRegular() || info.Mode()&os.ModeSymlink != 0 || info.Size() > 2<<20 {
http.Error(w, "unsafe nginx site configuration", http.StatusPreconditionFailed)
return
}
content, err := os.ReadFile(configPath)
if err != nil {
http.Error(w, "cannot read nginx site configuration", http.StatusInternalServerError)
return
}
matched := 0
updated := nginxListenLine.ReplaceAllStringFunc(string(content), func(line string) string {
parts := nginxListenLine.FindStringSubmatch(line)
if len(parts) != 4 {
return line
}
port, parseErr := strconv.Atoi(parts[2])
if parseErr != nil {
return line
}
suffix := parts[3]
isTLS := strings.Contains(strings.ToLower(suffix), "ssl") || port == 443
wanted := spec.HTTPPort
if isTLS {
wanted = spec.HTTPSPort
}
address := ""
if spec.Dedicated {
address = spec.IPAddress
if strings.Contains(address, ":") {
address = "[" + address + "]"
}
address += ":"
}
matched++
return parts[1] + "listen " + address + strconv.Itoa(wanted) + suffix + ";"
})
if matched == 0 {
http.Error(w, "nginx listen directives were not found", http.StatusPreconditionFailed)
return
}
if updated == string(content) {
w.WriteHeader(http.StatusOK)
return
}
restore, err := writeManagedFile(configPath, []byte(updated))
if err != nil {
http.Error(w, "cannot write nginx site configuration", http.StatusInternalServerError)
return
}
ctx, cancel := contextWithNetworkTimeout(r)
defer cancel()
rollback := func() { _ = restore(); _ = a.runNginx(ctx, "-t"); _ = a.runNginx(ctx, "-s", "reload") }
if err = a.runNginx(ctx, "-t"); err != nil {
rollback()
http.Error(w, "nginx rejected network configuration", http.StatusPreconditionFailed)
return
}
if err = a.runNginx(ctx, "-s", "reload"); err != nil {
rollback()
http.Error(w, "cannot reload nginx", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func contextWithNetworkTimeout(r *http.Request) (context.Context, context.CancelFunc) {
return context.WithTimeout(r.Context(), 20*time.Second)
}