refactor: PTY & SSH (#7100)

* Add ssh tests for longoutput, orphan

Signed-off-by: Spike Curtis <spike@coder.com>

* PTY/SSH tests & improvements

Signed-off-by: Spike Curtis <spike@coder.com>

* Fix some tests

Signed-off-by: Spike Curtis <spike@coder.com>

* Fix linting

Signed-off-by: Spike Curtis <spike@coder.com>

* fmt

Signed-off-by: Spike Curtis <spike@coder.com>

* Fix windows test

Signed-off-by: Spike Curtis <spike@coder.com>

* Windows copy test

Signed-off-by: Spike Curtis <spike@coder.com>

* WIP Windows pty handling

Signed-off-by: Spike Curtis <spike@coder.com>

* Fix truncation tests

Signed-off-by: Spike Curtis <spike@coder.com>

* Appease linter/fmt

Signed-off-by: Spike Curtis <spike@coder.com>

* Fix typo

Signed-off-by: Spike Curtis <spike@coder.com>

* Rework truncation test to not assume OS buffers

Signed-off-by: Spike Curtis <spike@coder.com>

* Disable orphan test on Windows --- uses sh

Signed-off-by: Spike Curtis <spike@coder.com>

* agent_test running SSH in pty use ptytest.Start

Signed-off-by: Spike Curtis <spike@coder.com>

* More detail about closing pseudoconsole on windows

Signed-off-by: Spike Curtis <spike@coder.com>

* Code review fixes

Signed-off-by: Spike Curtis <spike@coder.com>

* Rearrange ptytest method order

Signed-off-by: Spike Curtis <spike@coder.com>

* Protect pty.Resize on windows from races

Signed-off-by: Spike Curtis <spike@coder.com>

* Fix windows bugs

Signed-off-by: Spike Curtis <spike@coder.com>

* PTY doesn't extend PTYCmd

Signed-off-by: Spike Curtis <spike@coder.com>

* Fix windows types

Signed-off-by: Spike Curtis <spike@coder.com>

---------

Signed-off-by: Spike Curtis <spike@coder.com>
This commit is contained in:
Spike Curtis
2023-04-24 14:53:57 +04:00
committed by GitHub
parent c000f2ec28
commit daee91c6dc
16 changed files with 803 additions and 288 deletions
+26 -12
View File
@@ -3,7 +3,6 @@ package pty
import (
"io"
"log"
"os"
"github.com/gliderlabs/ssh"
"golang.org/x/xerrors"
@@ -12,10 +11,33 @@ import (
// ErrClosed is returned when a PTY is used after it has been closed.
var ErrClosed = xerrors.New("pty: closed")
// PTY is a minimal interface for interacting with a TTY.
// PTYCmd is an interface for interacting with a pseudo-TTY where we control
// only one end, and the other end has been passed to a running os.Process.
// nolint:revive
type PTYCmd interface {
io.Closer
// Resize sets the size of the PTY.
Resize(height uint16, width uint16) error
// OutputReader returns an io.Reader for reading the output from the process
// controlled by the pseudo-TTY
OutputReader() io.Reader
// InputWriter returns an io.Writer for writing into to the process
// controlled by the pseudo-TTY
InputWriter() io.Writer
}
// PTY is a minimal interface for interacting with pseudo-TTY where this
// process retains access to _both_ ends of the pseudo-TTY (i.e. `ptm` & `pts`
// on Linux).
type PTY interface {
io.Closer
// Resize sets the size of the PTY.
Resize(height uint16, width uint16) error
// Name of the TTY. Example on Linux would be "/dev/pts/1".
Name() string
@@ -34,14 +56,6 @@ type PTY interface {
//
// The same stream would be used to provide user input: pty.Input().Write(...)
Input() ReadWriter
// Dup returns a new file descriptor for the PTY.
//
// This is useful for closing stdin and stdout separately.
Dup() (*os.File, error)
// Resize sets the size of the PTY.
Resize(height uint16, width uint16) error
}
// Process represents a process running in a PTY. We need to trigger special processing on the PTY
@@ -108,8 +122,8 @@ func New(opts ...Option) (PTY, error) {
// underlying file descriptors, one for reading and one for writing, and allows
// them to be accessed separately.
type ReadWriter struct {
Reader *os.File
Writer *os.File
Reader io.Reader
Writer io.Writer
}
func (rw ReadWriter) Read(p []byte) (int, error) {