Skip to content

Commit d4663fa

Browse files
committed
fixup! feat: add endpoint to get listening ports in agent
1 parent ed21d4a commit d4663fa

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

agent/statsendpoint.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func (lp *listeningPortsHandler) getListeningPorts() ([]codersdk.ListeningPort,
6161

6262
ports := []codersdk.ListeningPort{}
6363
for _, tab := range tabs {
64+
if tab.LocalAddr.Port < uint16(codersdk.MinimumListeningPort) {
65+
continue
66+
}
67+
6468
ports = append(ports, codersdk.ListeningPort{
6569
ProcessName: tab.Process.Name,
6670
Network: codersdk.ListeningPortNetworkTCP,

coderd/workspaceapps.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,14 @@ func (api *API) proxyWorkspaceApplication(proxyApp proxyApplication, rw http.Res
466466
return
467467
}
468468

469-
// Verify that the port is allowed. See `codersdk.MinimumListeningPort` for
470-
// more details.
469+
// Verify that the port is allowed. See the docs above
470+
// `codersdk.MinimumListeningPort` for more details.
471471
port := appURL.Port()
472472
if port != "" {
473473
portInt, err := strconv.Atoi(port)
474474
if err != nil {
475-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
476-
Message: fmt.Sprintf("App URL %q has an invalid port %q. Named ports are currently not supported.", internalURL, port),
475+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
476+
Message: fmt.Sprintf("App URL %q has an invalid port %q.", internalURL, port),
477477
Detail: err.Error(),
478478
})
479479
return

coderd/workspaceapps_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,4 +695,23 @@ func TestWorkspaceAppsProxySubdomain(t *testing.T) {
695695
defer resp.Body.Close()
696696
require.Equal(t, http.StatusBadGateway, resp.StatusCode)
697697
})
698+
699+
t.Run("ProxyPortMinimumError", func(t *testing.T) {
700+
t.Parallel()
701+
702+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
703+
defer cancel()
704+
705+
port := uint16(codersdk.MinimumListeningPort - 1)
706+
resp, err := client.Request(ctx, http.MethodGet, proxyURL(t, port, "/", proxyTestAppQuery), nil)
707+
require.NoError(t, err)
708+
defer resp.Body.Close()
709+
710+
// Should have an error response.
711+
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
712+
var resBody codersdk.Response
713+
err = json.NewDecoder(resp.Body).Decode(&resBody)
714+
require.NoError(t, err)
715+
require.Contains(t, resBody.Message, "Coder reserves ports less than")
716+
})
698717
}

codersdk/agentconn.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ var (
3636
// by the proxy applications endpoint. Coder consumes ports 1-4 at the
3737
// moment, and we reserve some extra ports for future use. Port 9 and up are
3838
// available for the user.
39+
//
40+
// This is not enforced in the CLI intentionally as we don't really care
41+
// *that* much. The user could bypass this in the CLI by using SSH instead
42+
// anyways.
3943
MinimumListeningPort = 9
4044
)
4145

0 commit comments

Comments
 (0)