2022-10-06 22:38:22 +10:00
|
|
|
package agent
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2023-08-17 14:46:56 +02:00
|
|
|
"github.com/go-chi/chi/v5"
|
2025-07-08 11:05:30 +01:00
|
|
|
"github.com/google/uuid"
|
2022-10-06 22:38:22 +10:00
|
|
|
|
2026-05-08 13:25:30 +10:00
|
|
|
"github.com/coder/coder/v2/agent/agentchat"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
2025-11-25 14:59:20 -09:00
|
|
|
"github.com/coder/coder/v2/coderd/httpmw/loggermw"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/tracing"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2025-11-25 14:25:24 +04:00
|
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
2025-11-25 14:59:20 -09:00
|
|
|
"github.com/coder/coder/v2/httpmw"
|
2022-10-06 22:38:22 +10:00
|
|
|
)
|
|
|
|
|
|
2025-06-25 11:52:50 +01:00
|
|
|
func (a *agent) apiHandler() http.Handler {
|
2022-10-06 22:38:22 +10:00
|
|
|
r := chi.NewRouter()
|
2025-11-25 14:59:20 -09:00
|
|
|
r.Use(
|
|
|
|
|
httpmw.Recover(a.logger),
|
|
|
|
|
tracing.StatusWriterMiddleware,
|
2026-08-27 13:12:41 -07:00
|
|
|
// Reuse the coderd tracing middleware with a noop tracer (nil provider):
|
|
|
|
|
// it emits no spans or telemetry on the agent and only enriches the log
|
|
|
|
|
// context with client_session_id. Tracks the API, debug (for
|
|
|
|
|
// support-bundle correlation), and root routes.
|
|
|
|
|
tracing.Middleware(nil, []string{"/", "/api", "/api/**", "/debug/**"}, "agent"),
|
2026-06-11 10:55:00 -07:00
|
|
|
loggermw.Logger(a.logger, nil),
|
2026-05-08 13:25:30 +10:00
|
|
|
agentchat.Middleware,
|
2025-11-25 14:59:20 -09:00
|
|
|
)
|
2022-10-06 22:38:22 +10:00
|
|
|
r.Get("/", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
|
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.Response{
|
|
|
|
|
Message: "Hello from the agent!",
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-01-16 18:03:17 -08:00
|
|
|
r.Mount("/api/v0", a.filesAPI.Routes())
|
2026-03-06 10:47:55 +01:00
|
|
|
r.Mount("/api/v0/git", a.gitAPI.Routes())
|
2026-02-28 12:33:52 -05:00
|
|
|
r.Mount("/api/v0/processes", a.processAPI.Routes())
|
2026-03-13 19:49:34 +01:00
|
|
|
r.Mount("/api/v0/desktop", a.desktopAPI.Routes())
|
2026-03-26 15:57:02 -04:00
|
|
|
r.Mount("/api/v0/mcp", a.mcpAPI.Routes())
|
2026-04-01 12:28:47 -04:00
|
|
|
r.Mount("/api/v0/context-config", a.contextConfigAPI.Routes())
|
2026-06-08 12:08:40 -07:00
|
|
|
if a.contextAPI != nil {
|
|
|
|
|
r.Mount("/api/v0/context", a.contextAPI.Routes())
|
|
|
|
|
}
|
2026-01-16 18:03:17 -08:00
|
|
|
|
2025-07-08 11:05:30 +01:00
|
|
|
if a.devcontainers {
|
2025-06-25 11:52:50 +01:00
|
|
|
r.Mount("/api/v0/containers", a.containerAPI.Routes())
|
2025-07-08 11:05:30 +01:00
|
|
|
} else if manifest := a.manifest.Load(); manifest != nil && manifest.ParentID != uuid.Nil {
|
|
|
|
|
r.HandleFunc("/api/v0/containers", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
httpapi.Write(r.Context(), w, http.StatusForbidden, codersdk.Response{
|
|
|
|
|
Message: "Dev Container feature not supported.",
|
|
|
|
|
Detail: "Dev Container integration inside other Dev Containers is explicitly not supported.",
|
|
|
|
|
})
|
|
|
|
|
})
|
2025-04-29 17:53:10 +03:00
|
|
|
} else {
|
|
|
|
|
r.HandleFunc("/api/v0/containers", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
httpapi.Write(r.Context(), w, http.StatusForbidden, codersdk.Response{
|
2025-07-08 11:05:30 +01:00
|
|
|
Message: "Dev Container feature not enabled.",
|
2025-04-29 17:53:10 +03:00
|
|
|
Detail: "To enable this feature, set CODER_AGENT_DEVCONTAINERS_ENABLE=true in your template.",
|
|
|
|
|
})
|
|
|
|
|
})
|
2025-04-15 17:53:37 +03:00
|
|
|
}
|
2025-04-11 11:41:13 +03:00
|
|
|
|
2024-03-15 15:33:49 +00:00
|
|
|
promHandler := PrometheusMetricsHandler(a.prometheusRegistry, a.logger)
|
2025-04-11 11:41:13 +03:00
|
|
|
|
2025-11-25 14:25:24 +04:00
|
|
|
r.Get("/api/v0/listening-ports", a.listeningPortsHandler.handler)
|
2024-08-28 15:39:01 +10:00
|
|
|
r.Get("/api/v0/netcheck", a.HandleNetcheck)
|
2024-03-14 15:36:12 +00:00
|
|
|
r.Get("/debug/logs", a.HandleHTTPDebugLogs)
|
2024-03-14 10:02:01 +00:00
|
|
|
r.Get("/debug/magicsock", a.HandleHTTPDebugMagicsock)
|
|
|
|
|
r.Get("/debug/magicsock/debug-logging/{state}", a.HandleHTTPMagicsockDebugLoggingState)
|
2024-03-14 15:36:12 +00:00
|
|
|
r.Get("/debug/manifest", a.HandleHTTPDebugManifest)
|
2024-03-15 15:33:49 +00:00
|
|
|
r.Get("/debug/prometheus", promHandler.ServeHTTP)
|
2022-10-06 22:38:22 +10:00
|
|
|
|
2025-06-25 11:52:50 +01:00
|
|
|
return r
|
2022-10-06 22:38:22 +10:00
|
|
|
}
|
|
|
|
|
|
2025-11-25 14:25:24 +04:00
|
|
|
type ListeningPortsGetter interface {
|
|
|
|
|
GetListeningPorts() ([]codersdk.WorkspaceAgentListeningPort, error)
|
|
|
|
|
}
|
2023-11-22 19:30:51 +10:00
|
|
|
|
2025-11-25 14:25:24 +04:00
|
|
|
type listeningPortsHandler struct {
|
|
|
|
|
// In production code, this is set to an osListeningPortsGetter, but it can be overridden for
|
|
|
|
|
// testing.
|
|
|
|
|
getter ListeningPortsGetter
|
|
|
|
|
ignorePorts map[int]string
|
2022-10-06 22:38:22 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handler returns a list of listening ports. This is tested by coderd's
|
|
|
|
|
// TestWorkspaceAgentListeningPorts test.
|
|
|
|
|
func (lp *listeningPortsHandler) handler(rw http.ResponseWriter, r *http.Request) {
|
2025-11-25 14:25:24 +04:00
|
|
|
ports, err := lp.getter.GetListeningPorts()
|
2022-10-06 22:38:22 +10:00
|
|
|
if err != nil {
|
|
|
|
|
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
|
|
|
|
|
Message: "Could not scan for listening ports.",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 14:25:24 +04:00
|
|
|
filteredPorts := make([]codersdk.WorkspaceAgentListeningPort, 0, len(ports))
|
|
|
|
|
for _, port := range ports {
|
|
|
|
|
if port.Port < workspacesdk.AgentMinimumListeningPort {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ignore ports that we've been told to ignore.
|
|
|
|
|
if _, ok := lp.ignorePorts[int(port.Port)]; ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
filteredPorts = append(filteredPorts, port)
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-29 15:47:24 -06:00
|
|
|
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.WorkspaceAgentListeningPortsResponse{
|
2025-11-25 14:25:24 +04:00
|
|
|
Ports: filteredPorts,
|
2022-10-06 22:38:22 +10:00
|
|
|
})
|
|
|
|
|
}
|