feat: Let port-forwarding support custom http(s) port (#5084)

This commit is contained in:
Tao Yang
2022-12-02 04:39:19 +08:00
committed by GitHub
parent 2ec3b09ca7
commit 5457dd0c65
3 changed files with 17 additions and 4 deletions

View File

@ -124,6 +124,9 @@ func (api *API) workspaceAgentMetadata(rw http.ResponseWriter, r *http.Request)
workspace.Name, workspace.Name,
owner.Username, owner.Username,
)) ))
if api.AccessURL.Port() != "" {
vscodeProxyURI += fmt.Sprintf(":%s", api.AccessURL.Port())
}
httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceAgentMetadata{ httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceAgentMetadata{
Apps: convertApps(dbApps), Apps: convertApps(dbApps),

View File

@ -40,8 +40,13 @@ const (
) )
func (api *API) appHost(rw http.ResponseWriter, r *http.Request) { func (api *API) appHost(rw http.ResponseWriter, r *http.Request) {
host := api.AppHostname
if api.AccessURL.Port() != "" {
host += fmt.Sprintf(":%s", api.AccessURL.Port())
}
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.GetAppHostResponse{ httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.GetAppHostResponse{
Host: api.AppHostname, Host: host,
}) })
} }

View File

@ -69,7 +69,8 @@ func TestGetAppHost(t *testing.T) {
_ = coderdtest.CreateFirstUser(t, client) _ = coderdtest.CreateFirstUser(t, client)
host, err = client.GetAppHost(ctx) host, err = client.GetAppHost(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, c, host.Host) domain := strings.Split(host.Host, ":")[0]
require.Equal(t, c, domain)
}) })
} }
} }
@ -204,13 +205,17 @@ func createWorkspaceWithApps(t *testing.T, client *codersdk.Client, orgID uuid.U
if appHost != "" { if appHost != "" {
metadata, err := agentClient.WorkspaceAgentMetadata(context.Background()) metadata, err := agentClient.WorkspaceAgentMetadata(context.Background())
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, fmt.Sprintf( proxyURL := fmt.Sprintf(
"http://{{port}}--%s--%s--%s%s", "http://{{port}}--%s--%s--%s%s",
proxyTestAgentName, proxyTestAgentName,
workspace.Name, workspace.Name,
"testuser", "testuser",
strings.ReplaceAll(appHost, "*", ""), strings.ReplaceAll(appHost, "*", ""),
), metadata.VSCodePortProxyURI) )
if client.URL.Port() != "" {
proxyURL += fmt.Sprintf(":%s", client.URL.Port())
}
require.Equal(t, proxyURL, metadata.VSCodePortProxyURI)
} }
agentCloser := agent.New(agent.Options{ agentCloser := agent.New(agent.Options{
Client: agentClient, Client: agentClient,