Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v1.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package session carries a session between the host and the VM over SSH, on a terminal or without one. The guest serves the session, the host attaches to it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serve

func Serve(conn net.Conn, start Starter) error

Serve runs an SSH server on the connection, serves the first session with a command from start and returns when that session is over.

Types

type Client

type Client struct {
	In      io.Reader
	Out     io.Writer
	Term    string
	Size    Size
	Resized <-chan Size
	// Env are variables for the command, as NAME=value.
	Env []string
	// OnPaste is called each time the person presses Ctrl+V, or is nil.
	OnPaste func()
}

Client is the terminal on the host that attaches to a session. Resized delivers the new size whenever the terminal changes size, and may be nil.

func NewClient

func NewClient(stdin *os.File, out io.Writer) (Client, func(), error)

NewClient prepares the terminal on stdin for a session: raw mode, its size and size changes. When stdin is not a terminal the client gets a default size and no size changes, and a nil stdin types nothing. The returned function puts the terminal back.

func (Client) Attach

func (c Client) Attach(conn net.Conn) (int, error)

Attach opens a session on the connection, runs it on the client's terminal and returns the exit code of the command.

type Exec added in v1.2.0

type Exec struct {
	In     io.Reader
	Out    io.Writer
	Errors io.Writer
	// Env are variables for the command, as NAME=value.
	Env []string
}

Exec is a session without a terminal. The command reads In until it ends, a nil In sends nothing, and what the command prints goes to Out and, for standard error, to Errors. In may still be read after Run returned, until it ends or a read fails, so it must not be used for anything else.

func (Exec) Run added in v1.2.0

func (e Exec) Run(conn net.Conn) (int, error)

Run opens a session on the connection, runs the command on pipes and returns its exit code once its output has arrived. A command may end without reading all of In, which is no error.

type PTY

type PTY struct {
	Master *os.File
	Slave  *os.File
}

PTY is a pseudo terminal. Master is the side a session reads and writes, Slave is the terminal of the command.

func OpenPTY

func OpenPTY(size Size) (*PTY, error)

OpenPTY opens a pseudo terminal of the size.

func (*PTY) Close

func (p *PTY) Close() error

Close closes both sides.

func (*PTY) Resize

func (p *PTY) Resize(size Size) error

Resize sets the size of the terminal.

type Piped added in v1.2.0

type Piped interface {
	Process
	Stderr() io.Reader
	CloseInput() error
}

Piped is a Process without a terminal. Stderr is what it prints to standard error, which the session keeps apart from the rest. CloseInput ends what it reads once the client has sent everything.

type Process

type Process interface {
	io.ReadWriteCloser
	Resize(size Size) error
	Wait() (exitCode int, err error)
}

Process is a command running on a terminal or on pipes. Reads return what it prints and writes type into it. Close releases the terminal or the pipes after Wait.

type Request

type Request struct {
	Terminal bool
	Term     string
	Size     Size
	Env      []string
}

Request is what a client asked for: whether it wants a terminal, the TERM and the size of that terminal, and the variables it sent, as NAME=value.

type Size

type Size struct {
	Rows uint16
	Cols uint16
}

Size is the size of a terminal in characters.

type Starter

type Starter func(request Request) (Process, error)

Starter runs the command of a session as the client asked for it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL