- Notifications
You must be signed in to change notification settings - Fork929
feat: Add web terminal with reconnecting TTYs#1186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
31f27bc
feat: Add web terminal with reconnecting TTYs
kylecarbs15d843e
Add xstate service
kylecarbscb5ae98
Add the webpage for accessing a web terminal
kylecarbs229c7e4
Add terminal page tests
kylecarbs621aeb1
Merge branch 'main' into webterm
kylecarbs3e1a0a4
Use Ticker instead of Timer
kylecarbs4ef7106
Active Windows mode on Windows
kylecarbs19c7b54
Merge branch 'main' into webterm
kylecarbsFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Add the webpage for accessing a web terminal
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commitcb5ae98b47bd53cf07e7073d4bddcc2ab02f1007
There are no files selected for viewing
3 changes: 3 additions & 0 deletions.vscode/settings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
91 changes: 70 additions & 21 deletionsagent/agent.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -18,8 +18,8 @@ import ( | ||
"sync" | ||
"time" | ||
"github.com/armon/circbuf" | ||
"github.com/google/uuid" | ||
gsyslog "github.com/hashicorp/go-syslog" | ||
"go.uber.org/atomic" | ||
@@ -417,6 +417,8 @@ func (a *agent) handleSSHSession(session ssh.Session) error { | ||
func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn net.Conn) { | ||
defer conn.Close() | ||
// The ID format is referenced in conn.go. | ||
// <uuid>:<height>:<width> | ||
idParts := strings.Split(rawID, ":") | ||
if len(idParts) != 3 { | ||
a.logger.Warn(ctx, "client sent invalid id format", slog.F("raw-id", rawID)) | ||
@@ -429,6 +431,7 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne | ||
a.logger.Warn(ctx, "client sent reconnection token that isn't a uuid", slog.F("id", id), slog.Error(err)) | ||
return | ||
} | ||
// Parse the initial terminal dimensions. | ||
height, err := strconv.Atoi(idParts[1]) | ||
if err != nil { | ||
a.logger.Warn(ctx, "client sent invalid height", slog.F("id", id), slog.F("height", idParts[1])) | ||
@@ -454,41 +457,55 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne | ||
a.logger.Warn(ctx, "create reconnecting pty command", slog.Error(err)) | ||
return | ||
} | ||
cmd.Env = append(cmd.Env, "TERM=xterm-256color") | ||
ptty, process, err := pty.Start(cmd) | ||
if err != nil { | ||
a.logger.Warn(ctx, "start reconnecting pty command", slog.F("id", id)) | ||
} | ||
// Default to buffer 64KB. | ||
circularBuffer, err := circbuf.NewBuffer(64 * 1024) | ||
if err != nil { | ||
a.logger.Warn(ctx, "create circular buffer", slog.Error(err)) | ||
return | ||
} | ||
a.closeMutex.Lock() | ||
a.connCloseWait.Add(1) | ||
a.closeMutex.Unlock() | ||
ctx, cancelFunc := context.WithCancel(ctx) | ||
rpty = &reconnectingPTY{ | ||
activeConns: make(map[string]net.Conn), | ||
ptty: ptty, | ||
// Timeouts created with an after func can be reset! | ||
timeout: time.AfterFunc(a.reconnectingPTYTimeout, cancelFunc), | ||
circularBuffer: circularBuffer, | ||
} | ||
a.reconnectingPTYs.Store(id, rpty) | ||
go func() { | ||
// When the context has been completed either: | ||
// 1. The timeout completed. | ||
// 2. The parent context was cancelled. | ||
<-ctx.Done() | ||
_ = process.Kill() | ||
}() | ||
go func() { | ||
// If the process dies randomly, we should | ||
// close the pty. | ||
_, _ = process.Wait() | ||
rpty.Close() | ||
}() | ||
go func() { | ||
buffer := make([]byte, 1024) | ||
for { | ||
read, err := rpty.ptty.Output().Read(buffer) | ||
if err != nil { | ||
// When the PTY is closed, this is triggered. | ||
break | ||
} | ||
part := buffer[:read] | ||
_, err = rpty.circularBuffer.Write(part) | ||
if err != nil { | ||
a.logger.Error(ctx, "reconnecting pty write buffer", slog.Error(err), slog.F("id", id)) | ||
break | ||
@@ -499,27 +516,56 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, rawID string, conn ne | ||
} | ||
rpty.activeConnsMutex.Unlock() | ||
} | ||
// Cleanup the process, PTY, and delete it's | ||
// ID from memory. | ||
_ = process.Kill() | ||
rpty.Close() | ||
a.reconnectingPTYs.Delete(id) | ||
a.connCloseWait.Done() | ||
}() | ||
} | ||
// Resize the PTY to initial height + width. | ||
err = rpty.ptty.Resize(uint16(height), uint16(width)) | ||
if err != nil { | ||
// We can continue after this, it's not fatal! | ||
a.logger.Error(ctx, "resize reconnecting pty", slog.F("id", id), slog.Error(err)) | ||
} | ||
// Write any previously stored data for the TTY. | ||
_, err = conn.Write(rpty.circularBuffer.Bytes()) | ||
if err != nil { | ||
a.logger.Warn(ctx, "write reconnecting pty buffer", slog.F("id", id), slog.Error(err)) | ||
return | ||
} | ||
connectionID := uuid.NewString() | ||
// Multiple connections to the same TTY are permitted. | ||
// This could easily be used for terminal sharing, but | ||
// we do it because it's a nice user experience to | ||
// copy/paste a terminal URL and have it _just work_. | ||
rpty.activeConnsMutex.Lock() | ||
rpty.activeConns[connectionID] = conn | ||
rpty.activeConnsMutex.Unlock() | ||
// Resetting this timeout prevents the PTY from exiting. | ||
rpty.timeout.Reset(a.reconnectingPTYTimeout) | ||
heartbeat := time.NewTimer(a.reconnectingPTYTimeout / 2) | ||
defer heartbeat.Stop() | ||
go func() { | ||
// Keep updating the activity while this | ||
// connection is alive! | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-heartbeat.C: | ||
} | ||
rpty.timeout.Reset(a.reconnectingPTYTimeout) | ||
} | ||
}() | ||
defer func() { | ||
// After this connection ends, remove it from | ||
// the PTYs active connections. If it isn't | ||
// removed, all PTY data will be sent to it. | ||
rpty.activeConnsMutex.Lock() | ||
delete(rpty.activeConns, connectionID) | ||
rpty.activeConnsMutex.Unlock() | ||
@@ -579,17 +625,20 @@ type reconnectingPTY struct { | ||
activeConnsMutex sync.Mutex | ||
activeConns map[string]net.Conn | ||
circularBuffer *circbuf.Buffer | ||
timeout*time.Timer | ||
pttypty.PTY | ||
} | ||
// Close ends all connections to the reconnecting | ||
// PTY and clear the circular buffer. | ||
func (r *reconnectingPTY) Close() { | ||
r.activeConnsMutex.Lock() | ||
defer r.activeConnsMutex.Unlock() | ||
code-asher marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
for _, conn := range r.activeConns { | ||
_ = conn.Close() | ||
} | ||
_ = r.ptty.Close() | ||
r.circularBuffer.Reset() | ||
r.timeout.Stop() | ||
} |
21 changes: 14 additions & 7 deletionsagent/agent_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioncoderd/workspaceagents.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -375,7 +375,7 @@ func (api *api) workspaceAgentPTY(rw http.ResponseWriter, r *http.Request) { | ||
_ = conn.Close(websocket.StatusNormalClosure, "ended") | ||
}() | ||
// Accept text connections, because it's more developer friendly. | ||
wsNetConn := websocket.NetConn(r.Context(), conn, websocket.MessageBinary) | ||
agentConn, err := api.dialWorkspaceAgent(r, workspaceAgent.ID) | ||
if err != nil { | ||
return | ||
kylecarbs marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
2 changes: 1 addition & 1 deletioncodersdk/workspaceagents.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletiongo.mod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionsgo.sum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionspty/pty_other.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletionssite/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletionssite/src/AppRouter.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletionsite/src/api/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletionssite/src/api/types.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.