Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 194 additions & 0 deletions desktop/internal/core/remote_terminal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package core

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"unicode/utf8"
)

// RemoteTerminalOpen is how a host terminal starts.
type RemoteTerminalOpen struct {
// Argv is the program and its arguments; empty runs the user's login shell as a login
// shell, which reads the same profile files as an SSH login.
Argv []string `json:"argv,omitempty"`
// Cwd is the absolute host working directory; empty starts in the user's home.
Cwd string `json:"cwd,omitempty"`
// Env adds variables to the server's environment. TERM is xterm-256color unless Env sets
// it.
Env map[string]string `json:"env,omitempty"`
// Columns and Rows are the size in character cells; both must be at least 1.
Columns uint16 `json:"columns"`
Rows uint16 `json:"rows"`
// Label is what server.state reports for the terminal, so the window that closes the
// session can name what still runs. Nil reports none.
Label *RemoteResourceLabel `json:"label,omitempty"`
}

// RemoteResourceLabel names a host resource in server.state.
type RemoteResourceLabel struct {
// Kind is the client's category, such as "terminal"; at most 64 characters.
Kind string `json:"kind"`
// Name is shown to the user; at most 256 characters, and NewRemoteResourceLabel cuts a
// longer one.
Name string `json:"name"`
}

// Core's limits on a resource label, in characters.
const (
remoteLabelKindChars = 64
remoteLabelNameChars = 256
)

// NewRemoteResourceLabel is a label of kind and name cut to the lengths Core accepts, so a
// long tab title cannot fail the request.
func NewRemoteResourceLabel(kind, name string) *RemoteResourceLabel {
return &RemoteResourceLabel{Kind: cutRunes(kind, remoteLabelKindChars), Name: cutRunes(name, remoteLabelNameChars)}
}

// cutRunes is text cut to at most limit characters.
func cutRunes(text string, limit int) string {
if utf8.RuneCountInString(text) <= limit {
return text
}
return string([]rune(text)[:limit])
}

// RemoteTerminalEvents receive a host terminal's events. Both run on the request's goroutine
// and must return at once: they must not wait for a UI thread or for the terminal's input.
// Core holds 1024 undelivered events per terminal and ends the terminal, and with it the
// request, when a subscriber stops reading.
type RemoteTerminalEvents struct {
// Started is called once, before any output, with the id terminal.write, terminal.resize
// and terminal.close take.
Started func(terminalID uint64)
// Output receives the bytes the program wrote, in order. Output owns data.
Output func(data []byte)
}

// RemoteTerminalExit is how a host terminal's program ended: ExitCode when it exited, Signal
// when a signal ended it.
type RemoteTerminalExit struct {
ExitCode *int `json:"exitCode"`
Signal *int `json:"signal"`
}

// remoteTerminalEvent is one remote event of remote.runTerminal.
type remoteTerminalEvent struct {
Type string `json:"type"`
Event string `json:"event"`
Params struct {
TerminalID uint64 `json:"terminalId"`
Data string `json:"data"`
} `json:"params"`
}

// errRemoteTerminalEvent is an event of remote.runTerminal the client cannot read.
var errRemoteTerminalEvent = errors.New("core: unreadable remote terminal event")

// RemoteRunTerminal runs a terminal on the host of sessionID until its program exits, and
// returns how it ended. It blocks the calling goroutine for the terminal's whole life, so run
// it off the UI thread. Ending ctx closes the terminal: Core sends terminal.close, which hangs
// the program up and kills it two seconds later, and returns the context's error. A broken or
// unknown session fails with CodeConnectionClosed, and a program that cannot start with
// CodeProcessFailed. An event the client cannot read ends the terminal too and is returned as
// the error, since its output would be incomplete.
func (c *Client) RemoteRunTerminal(ctx context.Context, sessionID string, open RemoteTerminalOpen, events RemoteTerminalEvents) (RemoteTerminalExit, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var unreadable error
observer := func(raw json.RawMessage) {
if unreadable != nil {
return
}
if err := deliverRemoteTerminalEvent(raw, events); err != nil {
// Cancelling ends the terminal rather than letting it run unseen.
unreadable = err
cancel()
}
}
var exit RemoteTerminalExit
observationErr, err := c.ExecuteWithEvents(ctx, "remote.runTerminal", struct {
SessionID string `json:"sessionId"`
Params RemoteTerminalOpen `json:"params"`
}{sessionID, open}, &exit, observer)
switch {
case unreadable != nil:
return RemoteTerminalExit{}, unreadable
case err != nil:
return RemoteTerminalExit{}, err
case observationErr != nil:
return RemoteTerminalExit{}, observationErr
}
return exit, nil
}

// deliverRemoteTerminalEvent hands one event of remote.runTerminal to events. Events of other
// kinds are not the terminal's and are ignored.
func deliverRemoteTerminalEvent(raw json.RawMessage, events RemoteTerminalEvents) error {
if !bytes.Contains(raw, []byte(`"remote"`)) {
return nil
}
var event remoteTerminalEvent
if err := json.Unmarshal(raw, &event); err != nil || event.Type != "remote" {
return nil
}
switch event.Event {
case "terminal.started":
if events.Started != nil {
events.Started(event.Params.TerminalID)
}
case "terminal.output":
data, err := base64.StdEncoding.DecodeString(event.Params.Data)
if err != nil {
return errRemoteTerminalEvent
}
if events.Output != nil && len(data) > 0 {
events.Output(data)
}
}
return nil
}

// RemoteTerminalWrite types data into the host terminal terminalID of sessionID. busy is true
// when the program has not read what it was given before and the host refused data without
// taking any of it: sending the same bytes again later is safe. A terminal that already ended
// fails with CodeProcessFailed, a broken or unknown session with CodeConnectionClosed.
func (c *Client) RemoteTerminalWrite(ctx context.Context, sessionID string, terminalID uint64, data []byte) (busy bool, err error) {
err = c.RemoteHostRequest(ctx, sessionID, "terminal.write", struct {
TerminalID uint64 `json:"terminalId"`
Data string `json:"data"`
}{terminalID, base64.StdEncoding.EncodeToString(data)}, nil)
if remoteHostCode(err) == "BUSY" {
return true, nil
}
return false, err
}

// RemoteTerminalResize tells the host terminal terminalID of sessionID its size in cells; both
// must be at least 1.
func (c *Client) RemoteTerminalResize(ctx context.Context, sessionID string, terminalID uint64, columns, rows uint16) error {
return c.RemoteHostRequest(ctx, sessionID, "terminal.resize", struct {
TerminalID uint64 `json:"terminalId"`
Columns uint16 `json:"columns"`
Rows uint16 `json:"rows"`
}{terminalID, columns, rows}, nil)
}

// remoteHostCode is the host protocol's code of a host method that failed, such as "BUSY" or
// "UNKNOWN_RESOURCE"; empty when err is not one. Core reports it in the error's details.
func remoteHostCode(err error) string {
var coreErr *Error
if !errors.As(err, &coreErr) || coreErr.Details == "" {
return ""
}
var details struct {
RemoteCode string `json:"remoteCode"`
}
if json.Unmarshal([]byte(coreErr.Details), &details) != nil {
return ""
}
return details.RemoteCode
}
91 changes: 91 additions & 0 deletions desktop/internal/core/remote_terminal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package core

import (
"encoding/json"
"errors"
"strconv"
"strings"
"testing"
"unicode/utf8"
)

// The terminal's events reach their callbacks in order: started with its id, output decoded,
// an empty output dropped, and events that are not the terminal's ignored. Output that is not
// base64 is an error, since what the terminal shows would be incomplete.
func TestRemoteTerminalEventsReachTheirCallbacks(t *testing.T) {
var got []string
events := RemoteTerminalEvents{
Started: func(id uint64) { got = append(got, "started "+strconv.FormatUint(id, 10)) },
Output: func(data []byte) { got = append(got, "output "+string(data)) },
}
for _, raw := range []string{
`{"type":"progress","message":"remote"}`,
`{"type":"remote","event":"terminal.started","params":{"terminalId":7}}`,
`{"type":"remote","event":"terminal.output","params":{"terminalId":7,"data":"aGk="}}`,
`{"type":"remote","event":"terminal.output","params":{"terminalId":7,"data":""}}`,
`{"type":"remote","event":"terminal.exit","params":{"terminalId":7,"exitCode":0}}`,
`{"type":"remote","event":"watch.changed","params":{}}`,
} {
if err := deliverRemoteTerminalEvent(json.RawMessage(raw), events); err != nil {
t.Fatalf("event %s: %v", raw, err)
}
}
if want := []string{"started 7", "output hi"}; strings.Join(got, "|") != strings.Join(want, "|") {
t.Fatalf("delivered %q, want %q", got, want)
}
bad := `{"type":"remote","event":"terminal.output","params":{"terminalId":7,"data":"not base64!"}}`
if err := deliverRemoteTerminalEvent(json.RawMessage(bad), events); !errors.Is(err, errRemoteTerminalEvent) {
t.Fatalf("unreadable output = %v, want errRemoteTerminalEvent", err)
}
}

// The host protocol's code is read from a Core error's details; anything else has none.
func TestRemoteHostCodeReadsTheDetails(t *testing.T) {
busy := &Error{Command: "remote.hostRequest", Code: CodeProcessFailed, Details: `{"remoteCode":"BUSY"}`}
if got := remoteHostCode(busy); got != "BUSY" {
t.Fatalf("code = %q, want BUSY", got)
}
for _, err := range []error{
nil,
errors.New("plain"),
&Error{Code: CodeProcessFailed},
&Error{Code: CodeProcessFailed, Details: "not json"},
&Error{Code: CodeProcessFailed, Details: `{"other":"BUSY"}`},
} {
if got := remoteHostCode(err); got != "" {
t.Errorf("code of %v = %q, want none", err, got)
}
}
}

// A label longer than Core accepts is cut by characters, not bytes.
func TestNewRemoteResourceLabelCutsToCoreLimits(t *testing.T) {
label := NewRemoteResourceLabel(strings.Repeat("k", 70), strings.Repeat("终", 300))
if utf8.RuneCountInString(label.Kind) != remoteLabelKindChars || utf8.RuneCountInString(label.Name) != remoteLabelNameChars || !utf8.ValidString(label.Name) {
t.Fatalf("label kind %d, name %d characters", utf8.RuneCountInString(label.Kind), utf8.RuneCountInString(label.Name))
}
if short := NewRemoteResourceLabel("terminal", "devbox"); short.Kind != "terminal" || short.Name != "devbox" {
t.Fatalf("short label = %+v", short)
}
}

// With the real kernel, a terminal, its input and its size over a session Core does not know
// fail with connection_closed, and the run never reports a start.
func TestRemoteTerminalOfAnUnknownSession(t *testing.T) {
client := NewClient()
ctx := testContext(t)
started := false
_, err := client.RemoteRunTerminal(ctx, "no-such-session", RemoteTerminalOpen{Columns: 80, Rows: 24}, RemoteTerminalEvents{
Started: func(uint64) { started = true },
})
var coreErr *Error
if !errors.As(err, &coreErr) || coreErr.Code != CodeConnectionClosed || started {
t.Fatalf("runTerminal = %v, started %v, want connection_closed", err, started)
}
if busy, err := client.RemoteTerminalWrite(ctx, "no-such-session", 1, []byte("x")); busy || !errors.As(err, &coreErr) || coreErr.Code != CodeConnectionClosed {
t.Fatalf("write = %v, %v, want connection_closed", busy, err)
}
if err := client.RemoteTerminalResize(ctx, "no-such-session", 1, 80, 24); !errors.As(err, &coreErr) || coreErr.Code != CodeConnectionClosed {
t.Fatalf("resize = %v, want connection_closed", err)
}
}
14 changes: 10 additions & 4 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions desktop/internal/termview/open_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package termview

import (
"errors"
"strings"
"testing"
)

// A program Config.Open starts elsewhere is given the screen's size and nothing of the local
// shell's start: its output is shown and what is typed is written to it. A failure to open is
// shown with its reason, logged and ends the view, and Enter opens the program again.
func TestOpenStartsTheProgramElsewhere(t *testing.T) {
var sizes [][2]int
refusal := errors.New("the host is not connected")
refuse := true
shell := &fakeShell{done: make(chan struct{}), output: []byte("host$ ")}
var failures []Failure
view := New(Config{
Shell: "ignored", Dir: "/ignored",
Messages: Messages{
SessionEnded: "ended",
SessionEndedStatus: func(int) string { return "ended with status" },
RestartHint: "press enter",
StartFailed: func(reason string) string { return "could not start: " + reason },
},
Failed: func(f Failure) { failures = append(failures, f) },
Open: func(cols, rows int) (Shell, error) {
sizes = append(sizes, [2]int{cols, rows})
if refuse {
refuse = false
return nil, refusal
}
return shell, nil
},
})
view.ensureStarted()
cols, rows := view.screen.Size()
if len(sizes) != 1 || sizes[0] != [2]int{cols, rows} {
t.Fatalf("opened with %v, want the screen's %dx%d", sizes, cols, rows)
}
if !view.Ended() || !strings.Contains(view.RetainedText(1<<16), "could not start: the host is not connected") {
t.Fatalf("a failed open shows %q and ended = %v", view.RetainedText(1<<16), view.Ended())
}
if len(failures) != 1 || failures[0].Event != eventStartFailed || !errors.Is(failures[0].Err, refusal) {
t.Fatalf("failures = %+v, want the start failure", failures)
}

if !view.Restart() {
t.Fatal("Enter did not open the program again")
}
view.Pump()
if len(sizes) != 2 || view.Ended() || !strings.Contains(view.RetainedText(1<<16), "host$") {
t.Fatalf("opened %d times; the view shows %q", len(sizes), view.RetainedText(1<<16))
}
if !view.SendLine("ls") || !strings.Contains(string(shell.input), "ls") {
t.Fatalf("the program was given %q", shell.input)
}
}
Loading
Loading