|
1 | 1 | package coderd
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "database/sql" |
| 5 | + "errors" |
| 6 | + "fmt" |
4 | 7 | "net/http"
|
| 8 | + "net/http/httputil" |
| 9 | + "net/url" |
| 10 | + "strings" |
5 | 11 |
|
6 |
| - "github.com/coder/coder/coderd/database" |
| 12 | + "github.com/go-chi/chi/v5" |
7 | 13 | "github.com/google/uuid"
|
8 |
| -) |
9 | 14 |
|
10 |
| -// workspaceAppsAuthWildcard authenticates the wildcard domain. |
11 |
| -func (api *API) workspaceAppsAuthWildcard(rw http.ResponseWriter, r *http.Request) { |
12 |
| - // r.URL.Query().Get("redirect") |
| 15 | + "github.com/coder/coder/coderd/database" |
| 16 | + "github.com/coder/coder/coderd/httpapi" |
| 17 | + "github.com/coder/coder/coderd/httpmw" |
| 18 | +) |
13 | 19 |
|
14 |
| -} |
| 20 | +func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request) { |
| 21 | + user := httpmw.UserParam(r) |
| 22 | + // This can be in the form of: "<workspace-name>.[workspace-agent]" or "<workspace-name>" |
| 23 | + workspaceWithAgent := chi.URLParam(r, "workspaceagent") |
| 24 | + workspaceParts := strings.Split(workspaceWithAgent, ".") |
15 | 25 |
|
16 |
| -func (api *API) workspaceAppsProxyWildcard(rw http.ResponseWriter, r *http.Request) { |
| 26 | + workspace, err := api.Database.GetWorkspaceByOwnerIDAndName(r.Context(), database.GetWorkspaceByOwnerIDAndNameParams{ |
| 27 | + OwnerID: user.ID, |
| 28 | + Name: workspaceParts[0], |
| 29 | + }) |
| 30 | + if errors.Is(err, sql.ErrNoRows) { |
| 31 | + httpapi.Write(rw, http.StatusNotFound, httpapi.Response{ |
| 32 | + Message: "workspace not found", |
| 33 | + }) |
| 34 | + return |
| 35 | + } |
| 36 | + if err != nil { |
| 37 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 38 | + Message: fmt.Sprintf("get workspace: %s", err), |
| 39 | + }) |
| 40 | + return |
| 41 | + } |
17 | 42 |
|
18 |
| -} |
| 43 | + build, err := api.Database.GetLatestWorkspaceBuildByWorkspaceID(r.Context(), workspace.ID) |
| 44 | + if err != nil { |
| 45 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 46 | + Message: fmt.Sprintf("get workspace build: %s", err), |
| 47 | + }) |
| 48 | + return |
| 49 | + } |
19 | 50 |
|
20 |
| -func (api *API) workspaceAppsProxyPath(rw http.ResponseWriter, r *http.Request) { |
21 |
| - conn, err := api.dialWorkspaceAgent(r, uuid.Nil) |
| 51 | + resources, err := api.Database.GetWorkspaceResourcesByJobID(r.Context(), build.JobID) |
22 | 52 | if err != nil {
|
| 53 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 54 | + Message: fmt.Sprintf("get workspace resources: %s", err), |
| 55 | + }) |
23 | 56 | return
|
24 | 57 | }
|
| 58 | + resourceIDs := make([]uuid.UUID, 0) |
| 59 | + for _, resource := range resources { |
| 60 | + resourceIDs = append(resourceIDs, resource.ID) |
| 61 | + } |
| 62 | + agents, err := api.Database.GetWorkspaceAgentsByResourceIDs(r.Context(), resourceIDs) |
| 63 | + if err != nil { |
| 64 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 65 | + Message: fmt.Sprintf("get workspace agents: %s", err), |
| 66 | + }) |
| 67 | + return |
| 68 | + } |
| 69 | + if len(agents) == 0 { |
| 70 | + httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{ |
| 71 | + Message: "no agents exist", |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + agent := agents[0] |
| 76 | + if len(workspaceParts) > 1 { |
| 77 | + for _, otherAgent := range agents { |
| 78 | + if otherAgent.Name == workspaceParts[1] { |
| 79 | + agent = otherAgent |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
25 | 85 | app, err := api.Database.GetWorkspaceAppByAgentIDAndName(r.Context(), database.GetWorkspaceAppByAgentIDAndNameParams{
|
26 |
| - AgentID: uuid.Nil, |
27 |
| - Name: "something", |
| 86 | + AgentID: agent.ID, |
| 87 | + Name: chi.URLParam(r, "application"), |
28 | 88 | })
|
| 89 | + if errors.Is(err, sql.ErrNoRows) { |
| 90 | + httpapi.Write(rw, http.StatusNotFound, httpapi.Response{ |
| 91 | + Message: "application not found", |
| 92 | + }) |
| 93 | + return |
| 94 | + } |
29 | 95 | if err != nil {
|
| 96 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 97 | + Message: fmt.Sprintf("get workspace app: %s", err), |
| 98 | + }) |
30 | 99 | return
|
31 | 100 | }
|
32 |
| - conn.DialContext(r.Context(), "tcp", "localhost:3000") |
| 101 | + if !app.Url.Valid { |
| 102 | + httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{ |
| 103 | + Message: fmt.Sprintf("application does not have a url: %s", err), |
| 104 | + }) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + appURL, err := url.Parse(app.Url.String) |
| 109 | + if err != nil { |
| 110 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 111 | + Message: fmt.Sprintf("parse app url: %s", err), |
| 112 | + }) |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + conn, err := api.dialWorkspaceAgent(r, agent.ID) |
| 117 | + if err != nil { |
| 118 | + httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ |
| 119 | + Message: fmt.Sprintf("dial workspace agent: %s", err), |
| 120 | + }) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + proxy := httputil.NewSingleHostReverseProxy(appURL) |
| 125 | + defaultTransport, valid := http.DefaultTransport.(*http.Transport) |
| 126 | + if !valid { |
| 127 | + panic("dev error: default transport isn't a transport") |
| 128 | + } |
| 129 | + transport := defaultTransport.Clone() |
| 130 | + transport.DialContext = conn.DialContext |
| 131 | + proxy.Transport = transport |
| 132 | + proxy.ServeHTTP(rw, r) |
33 | 133 | }
|
0 commit comments