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,182 @@
|
||||
// Package executor is the node-side execution surface. It intentionally has
|
||||
// no import of store/service/httpapi and cannot schedule control-plane work.
|
||||
package executor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"sea-vhost/internal/controlwire"
|
||||
"sea-vhost/internal/nodefiles"
|
||||
"sea-vhost/internal/panel"
|
||||
)
|
||||
|
||||
type Gateway struct {
|
||||
NodeID uint64
|
||||
TenantRoot string
|
||||
BrokerSocket string
|
||||
AgentSocket string
|
||||
InternalToken string
|
||||
// Direct is reserved for in-process tests; deployment always uses the
|
||||
// independent, unprivileged credential broker over a Unix socket.
|
||||
Direct panel.Interface
|
||||
mu sync.Mutex
|
||||
fileGate sync.Mutex
|
||||
}
|
||||
|
||||
var nodeHostPattern = regexp.MustCompile(`^sv[a-f0-9]{10}$`)
|
||||
var agentRoutes = map[string]bool{
|
||||
"/v1/database/phpmyadmin":true,
|
||||
"/v1/scheduler/configure":true,
|
||||
"/v1/schedule/execute":true,
|
||||
"/v1/ftp/disconnect":true,
|
||||
"/v1/site/startup": true,
|
||||
"/v1/site/settings": true, "/v1/site/log": true,
|
||||
"/v1/runtime/backups/prune": true,
|
||||
"/v1/prepare": true, "/v1/quarantine": true, "/v1/web-protection/apply": true, "/v1/web-protection/remove": true,
|
||||
"/v1/runtime/executable": true, "/v1/runtime/panel-state": true, "/v1/runtime/python/install": true,
|
||||
"/v1/resources/runtime/terminate": true, "/v1/run-path/release": true, "/v1/trash/prepare": true,
|
||||
"/v1/ftp/identity": true, "/v1/php/identity": true, "/v1/php/identity/start": true, "/v1/php/identity/stop": true, "/v1/php/identity/remove": true,
|
||||
"/v1/resources/usage": true, "/v1/resources/processes/terminate": true, "/v1/project/freeze-writes": true, "/v1/ftp/tls": true, "/v1/nginx/network": true,
|
||||
"/v1/lifecycle/backup": true, "/v1/lifecycle/backup/delete": true, "/v1/lifecycle/project/purge": true, "/v1/import/project": true,
|
||||
"/v1/database/access": true,
|
||||
"/v1/credentials/disconnect": true,
|
||||
"/v1/resources/limits": true,
|
||||
"/v1/lifecycle/backup/verify": true,
|
||||
"/v1/runtime/probe": true, "/v1/runtime/php/maintenance": true, "/v1/runtime/python/dependencies": true,
|
||||
}
|
||||
|
||||
func (g *Gateway) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /v1/status", func(w http.ResponseWriter, r *http.Request) {
|
||||
controlwire.Encode(w, 200, map[string]any{"nodeId": g.NodeID, "protocol": controlwire.Version, "businessDatabase": false, "taskScheduler": false})
|
||||
})
|
||||
mux.HandleFunc("POST /v1/panel", g.panel)
|
||||
mux.HandleFunc("POST /v1/files", g.files)
|
||||
mux.HandleFunc("POST /v1/isolation/", g.isolation)
|
||||
return controlwire.Authenticated(g.NodeID, mux)
|
||||
}
|
||||
|
||||
func (g *Gateway) files(w http.ResponseWriter, r *http.Request) {
|
||||
if !g.fileGate.TryLock() {
|
||||
http.Error(w, "node file transfer busy", 429)
|
||||
return
|
||||
}
|
||||
defer g.fileGate.Unlock()
|
||||
var q nodefiles.Request
|
||||
r.Body = http.MaxBytesReader(w, r.Body, nodefiles.MaxMessageBytes)
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(&q) != nil || decoder.Decode(&struct{}{}) != io.EOF || !nodeHostPattern.MatchString(q.HostName) {
|
||||
http.Error(w, "invalid host file request", 400)
|
||||
return
|
||||
}
|
||||
controlwire.Encode(w, 200, nodefiles.Execute(filepath.Join(g.TenantRoot, q.HostName), q))
|
||||
}
|
||||
func (g *Gateway) panel(w http.ResponseWriter, r *http.Request) {
|
||||
var input panel.ScopedExecution
|
||||
if controlwire.Decode(w, r, &input) != nil {
|
||||
http.Error(w, "invalid scoped panel request", 400)
|
||||
return
|
||||
}
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
if g.Direct != nil {
|
||||
result, err := panel.ExecuteScoped(r.Context(), g.Direct, g.TenantRoot, input)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 422)
|
||||
return
|
||||
}
|
||||
controlwire.Encode(w, 200, result)
|
||||
return
|
||||
}
|
||||
body, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid request", 400)
|
||||
return
|
||||
}
|
||||
g.forward(w, r, g.BrokerSocket, "/v1/scoped-operations", body)
|
||||
}
|
||||
func (g *Gateway) isolation(w http.ResponseWriter, r *http.Request) {
|
||||
route := strings.TrimPrefix(r.URL.Path, "/v1/isolation")
|
||||
if !agentRoutes[route] {
|
||||
http.Error(w, "operation is not in the node allowlist", 403)
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, controlwire.MaxEnvelopeBytes+1))
|
||||
if err != nil || len(body) > controlwire.MaxEnvelopeBytes {
|
||||
http.Error(w, "invalid node payload", 413)
|
||||
return
|
||||
}
|
||||
var value any
|
||||
if json.Unmarshal(body, &value) != nil || !validHostIdentities(value) {
|
||||
http.Error(w, "invalid node host scope", 400)
|
||||
return
|
||||
}
|
||||
g.forward(w, r, g.AgentSocket, route, body)
|
||||
}
|
||||
func validHostIdentities(value any) bool {
|
||||
switch v := value.(type) {
|
||||
case map[string]any:
|
||||
for key, item := range v {
|
||||
if strings.EqualFold(key, "hostName") {
|
||||
name, ok := item.(string)
|
||||
if !ok || !nodeHostPattern.MatchString(name) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if !validHostIdentities(item) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
case []any:
|
||||
for _, item := range v {
|
||||
if !validHostIdentities(item) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (g *Gateway) forward(w http.ResponseWriter, r *http.Request, socket, route string, body []byte) {
|
||||
if socket == "" || g.InternalToken == "" {
|
||||
http.Error(w, "node execution service is not configured", 503)
|
||||
return
|
||||
}
|
||||
transport := &http.Transport{DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
return (&net.Dialer{Timeout: 5 * time.Second}).DialContext(ctx, "unix", socket)
|
||||
}}
|
||||
defer transport.CloseIdleConnections()
|
||||
req, err := http.NewRequestWithContext(r.Context(), "POST", "http://unix"+route, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
http.Error(w, "invalid local operation", 400)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+g.InternalToken)
|
||||
response, err := (&http.Client{Transport: transport, Timeout: 2 * time.Hour, CheckRedirect: func(*http.Request, []*http.Request) error { return errors.New("redirect denied") }}).Do(req)
|
||||
if err != nil {
|
||||
http.Error(w, "node local execution service unavailable", 502)
|
||||
return
|
||||
}
|
||||
defer response.Body.Close()
|
||||
output, err := io.ReadAll(io.LimitReader(response.Body, controlwire.MaxEnvelopeBytes+1))
|
||||
if err != nil || len(output) > controlwire.MaxEnvelopeBytes {
|
||||
http.Error(w, "node execution response exceeded limit", 502)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.WriteHeader(response.StatusCode)
|
||||
_, _ = w.Write(output)
|
||||
}
|
||||
Reference in new issue
Block a user