Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
refactor: split logic for stats ws
Kyle Mendelld43a25650692
diff --git a/backend/internal/api/environment_handler.go b/backend/internal/api/environment_handler.goindex 5bb2f5b68..f0d20fa56 100644--- a/backend/internal/api/environment_handler.go+++ b/backend/internal/api/environment_handler.go@@ -984,83 +984,26 @@ func (h *EnvironmentHandler) GetContainerLogsWS(c *gin.Context) {func (h *EnvironmentHandler) GetStatsWS(c *gin.Context) {envID := c.Param("id")-if envID == LOCAL_DOCKER_ENVIRONMENT_ID {- u := &url.URL{}-- xfp := strings.TrimSpace(c.GetHeader("X-Forwarded-Proto"))- if xfp != "" {- if idx := strings.Index(xfp, ","); idx != -1 {- xfp = xfp[:idx]- }- if strings.HasPrefix(strings.ToLower(xfp), "https") {- u.Scheme = "wss"- } else {- u.Scheme = "ws"- }- } else {-- if c.Request.TLS != nil {- u.Scheme = "wss"- } else {- u.Scheme = "ws"- }- }-- u.Host = c.Request.Host- u.Path = "/api/system/stats/ws"- u.RawQuery = c.Request.URL.RawQuery-- hdr := http.Header{}- // Forward auth if present (or promote cookie)- if auth := c.GetHeader("Authorization"); auth != "" {- hdr.Set("Authorization", auth)- } else if cookieToken, err := c.Cookie("token"); err == nil && cookieToken != "" {- hdr.Set("Authorization", "Bearer "+cookieToken)- }-+ agentToken := ""if h.cfg != nil && h.cfg.AgentMode && h.cfg.AgentToken != "" {- hdr.Set("X-Arcane-Agent-Token", h.cfg.AgentToken)+ agentToken = h.cfg.AgentToken}-- _ = wsutil.ProxyHTTP(c.Writer, c.Request, u.String(), hdr)+ target, hdr := h.environmentService.BuildLocalWSTarget(c.Request, "/api/system/stats/ws", agentToken)+ _ = wsutil.ProxyHTTP(c.Writer, c.Request, target, hdr)return}-environment, err := h.environmentService.GetEnvironmentByID(c.Request.Context(), envID)if err != nil || environment == nil || !environment.Enabled {c.JSON(http.StatusNotFound, gin.H{"success": false, "data": gin.H{"error": "Environment not found or disabled"}})return}-- u, err := url.Parse(strings.TrimRight(environment.ApiUrl, "/"))+ target, hdr, err := h.environmentService.BuildRemoteWSTarget(environment, "/api/environments/0/stats/ws", c.Request)if err != nil {- c.JSON(http.StatusBadRequest, gin.H{"success": false, "data": gin.H{"error": "Invalid environment URL"}})+ c.JSON(http.StatusBadRequest, gin.H{"success": false, "data": gin.H{"error": err.Error()}})return}-- // websocket scheme- if u.Scheme == "https" {- u.Scheme = "wss"- } else {- u.Scheme = "ws"- }- u.Path = path.Join(u.Path, "/api/environments/"+LOCAL_DOCKER_ENVIRONMENT_ID+"/stats/ws")- u.RawQuery = c.Request.URL.RawQuery-- hdr := http.Header{}- // Forward auth if present- if auth := c.GetHeader("Authorization"); auth != "" {- hdr.Set("Authorization", auth)- } else if cookieToken, err := c.Cookie("token"); err == nil && cookieToken != "" {- hdr.Set("Authorization", "Bearer "+cookieToken)- }- // Agent token- if environment.AccessToken != nil && *environment.AccessToken != "" {- hdr.Set("X-Arcane-Agent-Token", *environment.AccessToken)- }-- _ = wsutil.ProxyHTTP(c.Writer, c.Request, u.String(), hdr)+ _ = wsutil.ProxyHTTP(c.Writer, c.Request, target, hdr)}// New Project Handlersdiff --git a/backend/internal/services/environment_service.go b/backend/internal/services/environment_service.goindex 74f13e8bd..5193da515 100644--- a/backend/internal/services/environment_service.go+++ b/backend/internal/services/environment_service.go@@ -7,6 +7,8 @@ import ("fmt""io""net/http"+ "net/url"+ "path""strings""time"@@ -274,3 +276,68 @@ func (s *EnvironmentService) PairAndPersistAgentToken(ctx context.Context, envir}return token, nil}++func (s *EnvironmentService) BuildWSAuthHeadersFromRequest(req *http.Request, agentToken string) http.Header {+ h := http.Header{}+ if auth := req.Header.Get("Authorization"); auth != "" {+ h.Set("Authorization", auth)+ } else if c, err := req.Cookie("token"); err == nil && c != nil && c.Value != "" {+ h.Set("Authorization", "Bearer "+c.Value)+ }+ if agentToken != "" {+ h.Set("X-Arcane-Agent-Token", agentToken)+ }+ return h+}++func wsScheme(req *http.Request) string {+ xfp := strings.TrimSpace(req.Header.Get("X-Forwarded-Proto"))+ if xfp != "" {+ if idx := strings.Index(xfp, ","); idx != -1 {+ xfp = strings.TrimSpace(xfp[:idx])+ }+ if strings.HasPrefix(strings.ToLower(xfp), "https") {+ return "wss"+ }+ return "ws"+ }+ if req.TLS != nil {+ return "wss"+ }+ return "ws"+}++// BuildLocalWSTarget returns ws URL and headers to proxy to this server.+func (s *EnvironmentService) BuildLocalWSTarget(req *http.Request, absolutePath string, agentToken string) (string, http.Header) {+ u := &url.URL{+ Scheme: wsScheme(req),+ Host: req.Host,+ Path: absolutePath,+ RawQuery: req.URL.RawQuery,+ }+ h := s.BuildWSAuthHeadersFromRequest(req, agentToken)+ return u.String(), h+}++// BuildRemoteWSTarget returns ws URL and headers for a remote environment.+// absolutePath should be the full API path (e.g. "/api/environments/0/stats/ws").+func (s *EnvironmentService) BuildRemoteWSTarget(environment *models.Environment, absolutePath string, req *http.Request) (string, http.Header, error) {+ base, err := url.Parse(strings.TrimRight(environment.ApiUrl, "/"))+ if err != nil {+ return "", nil, fmt.Errorf("invalid environment url: %w", err)+ }+ if base.Scheme == "https" {+ base.Scheme = "wss"+ } else {+ base.Scheme = "ws"+ }+ base.Path = path.Join(base.Path, absolutePath)+ base.RawQuery = req.URL.RawQuery++ agentToken := ""+ if environment.AccessToken != nil {+ agentToken = *environment.AccessToken+ }+ h := s.BuildWSAuthHeadersFromRequest(req, agentToken)+ return base.String(), h, nil+}