Skip to content

Modern Docker Management, Designed for Everyone

2.0 KB
Raw Blame
package edge

import (
	"context"
	"fmt"
	"io"
	"log/slog"
	"net"
	"net/http"
	"time"

	"github.com/hashicorp/yamux"

	"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
)

// serveMuxSession serves manager requests over a multiplexed session with the agent's own router until the session ends.
func (c *TunnelClient) serveMuxSession(ctx context.Context, rw io.ReadWriteCloser) error {
	session, err := yamux.Client(rw, newMuxConfig(true))
	if err != nil {
		_ = rw.Close()
		return fmt.Errorf("start edge tunnel session: %w", err)
	}

	srv := &http.Server{
		// The edge command allowlist stays the security boundary for what the manager may call.
		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			stream := httpx.IsWebSocketUpgradeRequest(r)
			if ResolveEdgeCommandName(r.Method, r.URL.EscapedPath(), stream).IsAbsent() {
				slog.WarnContext(r.Context(), "Rejected edge tunnel request outside the command allowlist", "method", r.Method, "path", r.URL.Path, "websocket", stream)
				w.Header().Set("Content-Type", "application/json")
				w.WriteHeader(http.StatusForbidden)
				_, _ = w.Write([]byte(`{"success":false,"error":"unsupported edge command"}`))
				return
			}
			c.handler.ServeHTTP(w, r)
		}),
		ReadHeaderTimeout: 30 * time.Second,
		IdleTimeout:       120 * time.Second,
		// Manager requests are logged by the agent's request logger, like any other API call.
		BaseContext: func(net.Listener) context.Context { return ctx },
		ConnContext: httpx.WithConn,
		ErrorLog:    newMuxLogger(),
	}
	// Closing the server closes the session it listens on and every stream.
	defer func() { _ = srv.Close() }()

	defer setActiveAgentEvents(&agentEventSink{client: newMuxHTTPClient(func(dialCtx context.Context) (net.Conn, error) { return openMuxStream(dialCtx, session) })})()

	serveDone := make(chan error, 1)
	go func() { serveDone <- srv.Serve(session) }()
	select {
	case <-ctx.Done():
		return ctx.Err()
	case <-session.CloseChan():
		return ErrTunnelConnectionClosed
	case serveErr := <-serveDone:
		return serveErr
	}
}