Skip to content

Commit 6794a73

Browse files
committed
fix: Add command to reconnecting PTY (#1860)
This fixes #1708 and opens the door for PTYs to execute non-shell commands!
1 parent 61cdea3 commit 6794a73

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

agent/agent.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne
456456

457457
// The ID format is referenced in conn.go.
458458
// <uuid>:<height>:<width>
459-
idParts := strings.Split(rawID, ":")
460-
if len(idParts) != 3 {
459+
idParts := strings.SplitN(rawID, ":", 4)
460+
if len(idParts) != 4 {
461461
a.logger.Warn(ctx, "client sent invalid id format", slog.F("raw-id", rawID))
462462
return
463463
}
@@ -489,7 +489,7 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne
489489
}
490490
} else {
491491
// Empty command will default to the users shell!
492-
cmd, err := a.createCommand(ctx, "", nil)
492+
cmd, err := a.createCommand(ctx, idParts[3], nil)
493493
if err != nil {
494494
a.logger.Warn(ctx, "create reconnecting pty command", slog.Error(err))
495495
return

agent/agent_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func TestAgent(t *testing.T) {
221221

222222
conn := setupAgent(t, agent.Metadata{}, 0)
223223
id := uuid.NewString()
224-
netConn, err := conn.ReconnectingPTY(id, 100, 100)
224+
netConn, err := conn.ReconnectingPTY(id, 100, 100, "/bin/bash")
225225
require.NoError(t, err)
226226
bufRead := bufio.NewReader(netConn)
227227

@@ -259,7 +259,7 @@ func TestAgent(t *testing.T) {
259259
expectLine(matchEchoOutput)
260260

261261
_ = netConn.Close()
262-
netConn, err = conn.ReconnectingPTY(id, 100, 100)
262+
netConn, err = conn.ReconnectingPTY(id, 100, 100, "/bin/bash")
263263
require.NoError(t, err)
264264
bufRead = bufio.NewReader(netConn)
265265

agent/conn.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ type Conn struct {
3434

3535
// ReconnectingPTY returns a connection serving a TTY that can
3636
// be reconnected to via ID.
37-
func (c *Conn) ReconnectingPTY(id string, height, width uint16) (net.Conn, error) {
38-
channel, err := c.CreateChannel(context.Background(), fmt.Sprintf("%s:%d:%d", id, height, width), &peer.ChannelOptions{
37+
//
38+
// The command is optional and defaults to start a shell.
39+
func (c *Conn) ReconnectingPTY(id string, height, width uint16, command string) (net.Conn, error) {
40+
channel, err := c.CreateChannel(context.Background(), fmt.Sprintf("%s:%d:%d:%s", id, height, width, command), &peer.ChannelOptions{
3941
Protocol: ProtocolReconnectingPTY,
4042
})
4143
if err != nil {

coderd/workspaceagents.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func (api *API) workspaceAgentPTY(rw http.ResponseWriter, r *http.Request) {
381381
return
382382
}
383383
defer agentConn.Close()
384-
ptNetConn, err := agentConn.ReconnectingPTY(reconnect.String(), uint16(height), uint16(width))
384+
ptNetConn, err := agentConn.ReconnectingPTY(reconnect.String(), uint16(height), uint16(width), "")
385385
if err != nil {
386386
_ = conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("dial: %s", err))
387387
return

0 commit comments

Comments
 (0)