|
| 1 | +package workspacetraffic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/coder/coder/codersdk" |
| 9 | + |
| 10 | + "github.com/google/uuid" |
| 11 | + "github.com/hashicorp/go-multierror" |
| 12 | + gossh "golang.org/x/crypto/ssh" |
| 13 | + "golang.org/x/xerrors" |
| 14 | +) |
| 15 | + |
| 16 | +func connectPTY(ctx context.Context, client *codersdk.Client, agentID, reconnect uuid.UUID) (*countReadWriteCloser, error) { |
| 17 | + conn, err := client.WorkspaceAgentReconnectingPTY(ctx, codersdk.WorkspaceAgentReconnectingPTYOpts{ |
| 18 | + AgentID: agentID, |
| 19 | + Reconnect: reconnect, |
| 20 | + Height: 25, |
| 21 | + Width: 80, |
| 22 | + Command: "/bin/sh", |
| 23 | + }) |
| 24 | + if err != nil { |
| 25 | + return nil, xerrors.Errorf("connect pty: %w", err) |
| 26 | + } |
| 27 | + |
| 28 | + // Wrap the conn in a countReadWriteCloser so we can monitor bytes sent/rcvd. |
| 29 | + crw := countReadWriteCloser{ctx: ctx, rwc: conn} |
| 30 | + return &crw, nil |
| 31 | +} |
| 32 | + |
| 33 | +func connectSSH(ctx context.Context, client *codersdk.Client, agentID uuid.UUID) (*countReadWriteCloser, error) { |
| 34 | + agentConn, err := client.DialWorkspaceAgent(ctx, agentID, &codersdk.DialWorkspaceAgentOptions{}) |
| 35 | + if err != nil { |
| 36 | + return nil, xerrors.Errorf("dial workspace agent: %w", err) |
| 37 | + } |
| 38 | + agentConn.AwaitReachable(ctx) |
| 39 | + sshClient, err := agentConn.SSHClient(ctx) |
| 40 | + if err != nil { |
| 41 | + return nil, xerrors.Errorf("get ssh client: %w", err) |
| 42 | + } |
| 43 | + sshSession, err := sshClient.NewSession() |
| 44 | + if err != nil { |
| 45 | + _ = agentConn.Close() |
| 46 | + return nil, xerrors.Errorf("new ssh session: %w", err) |
| 47 | + } |
| 48 | + wrappedConn := &wrappedSSHConn{ctx: ctx} |
| 49 | + // Do some plumbing to hook up the wrappedConn |
| 50 | + pr1, pw1 := io.Pipe() |
| 51 | + wrappedConn.stdout = pr1 |
| 52 | + sshSession.Stdout = pw1 |
| 53 | + pr2, pw2 := io.Pipe() |
| 54 | + sshSession.Stdin = pr2 |
| 55 | + wrappedConn.stdin = pw2 |
| 56 | + err = sshSession.RequestPty("xterm", 25, 80, gossh.TerminalModes{}) |
| 57 | + if err != nil { |
| 58 | + _ = pr1.Close() |
| 59 | + _ = pr2.Close() |
| 60 | + _ = pw1.Close() |
| 61 | + _ = pw2.Close() |
| 62 | + _ = sshSession.Close() |
| 63 | + _ = agentConn.Close() |
| 64 | + return nil, xerrors.Errorf("request pty: %w", err) |
| 65 | + } |
| 66 | + err = sshSession.Shell() |
| 67 | + if err != nil { |
| 68 | + _ = sshSession.Close() |
| 69 | + _ = agentConn.Close() |
| 70 | + return nil, xerrors.Errorf("shell: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + closeFn := func() error { |
| 74 | + var merr error |
| 75 | + if err := sshSession.Close(); err != nil { |
| 76 | + merr = multierror.Append(merr, err) |
| 77 | + } |
| 78 | + if err := agentConn.Close(); err != nil { |
| 79 | + merr = multierror.Append(merr, err) |
| 80 | + } |
| 81 | + return merr |
| 82 | + } |
| 83 | + wrappedConn.close = closeFn |
| 84 | + |
| 85 | + crw := &countReadWriteCloser{ctx: ctx, rwc: wrappedConn} |
| 86 | + return crw, nil |
| 87 | +} |
| 88 | + |
| 89 | +// wrappedSSHConn wraps an ssh.Session to implement io.ReadWriteCloser. |
| 90 | +type wrappedSSHConn struct { |
| 91 | + ctx context.Context |
| 92 | + stdout io.Reader |
| 93 | + stdin io.Writer |
| 94 | + closeOnce sync.Once |
| 95 | + closeErr error |
| 96 | + close func() error |
| 97 | +} |
| 98 | + |
| 99 | +func (w *wrappedSSHConn) Close() error { |
| 100 | + w.closeOnce.Do(func() { |
| 101 | + _, _ = w.stdin.Write([]byte("exit\n")) |
| 102 | + w.closeErr = w.close() |
| 103 | + }) |
| 104 | + return w.closeErr |
| 105 | +} |
| 106 | + |
| 107 | +func (w *wrappedSSHConn) Read(p []byte) (n int, err error) { |
| 108 | + select { |
| 109 | + case <-w.ctx.Done(): |
| 110 | + return 0, xerrors.Errorf("read: %w", w.ctx.Err()) |
| 111 | + default: |
| 112 | + return w.stdout.Read(p) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func (w *wrappedSSHConn) Write(p []byte) (n int, err error) { |
| 117 | + select { |
| 118 | + case <-w.ctx.Done(): |
| 119 | + return 0, xerrors.Errorf("write: %w", w.ctx.Err()) |
| 120 | + default: |
| 121 | + return w.stdin.Write(p) |
| 122 | + } |
| 123 | +} |
0 commit comments