sshtest

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sshtest is an SSH server in process, for SSHYard's tests and its development server (cmd/devsshd): password and public-key auth (one key, or an authorized_keys file checked as OpenSSH's StrictModes does), a PTY shell (a toy line shell, or a real one through Shell), keepalive replies, the sftp subsystem of github.com/pkg/sftp, exec requests, and TCP forwarding both ways (direct-tcpip channels, tcpip-forward requests).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LineShell

func LineShell(ch ssh.Channel, pty PTY, resize <-chan PTY) int

LineShell is a toy shell: it echoes what is typed and answers lines. "exit" ends it, "size" prints the PTY size, "color" prints SGR colors, and "fail" exits with status 3.

func NewHostKey

func NewHostKey() ssh.Signer

NewHostKey returns a fresh ed25519 host key.

func ShellExec added in v0.0.9

func ShellExec(dir string) func(ch ssh.Channel, cmd string) int

ShellExec is an Exec that runs each command with /bin/sh in dir, on this computer: a server's shell, for code that runs commands remotely.

Types

type FakeEntry

type FakeEntry struct {
	Name string
	Dir  bool
	Size int64
}

FakeEntry is a file or folder a FakeFS lists, under any name at all.

type FakeFS

type FakeFS struct {
	Dirs   map[string][]FakeEntry
	Files  map[string][]byte
	Writes map[string][]byte
	// List, when set, lists the folders Dirs does not have.
	List func(p string) ([]FakeEntry, bool)
	// Real, when set, answers realpath requests.
	Real func(p string) string
	// Lists counts listing requests.
	Lists int
	// Listing, when set, is told of each folder whose names are about to
	// be sent, each time (after the folder was opened).
	Listing func(p string)
	// Many, when set, gives folders of n files ("f0000000" on) the others
	// do not have, made as they are listed.
	Many func(p string) (n int, ok bool)
	// Served counts the entries listings have sent.
	Served atomic.Int64
	// contains filtered or unexported fields
}

FakeFS is an sftp server (Server.SFTPHandlers) that lists what it is told to, names included, as a hostile or broken server may: Dirs maps a folder's path to its listing, Files a file's path to what reading it gives. Writes keep what was written, by path.

func (*FakeFS) Conn

Conn is the server's side of a session (Server.SFTPConn): while the server is stopped, no reply goes out, whatever the request.

func (*FakeFS) File

func (f *FakeFS) File(p string) []byte

File is what the file p holds, nil when there is none.

func (*FakeFS) Filecmd

func (f *FakeFS) Filecmd(r *sftp.Request) error

Filecmd renames and removes files; the rest is accepted and ignored.

func (*FakeFS) Filelist

func (f *FakeFS) Filelist(r *sftp.Request) (sftp.ListerAt, error)

Filelist lists, stats or reads links.

func (*FakeFS) Fileread

func (f *FakeFS) Fileread(r *sftp.Request) (io.ReaderAt, error)

Fileread reads a file.

func (*FakeFS) Filewrite

func (f *FakeFS) Filewrite(r *sftp.Request) (io.WriterAt, error)

Filewrite writes a file.

func (*FakeFS) Handlers

func (f *FakeFS) Handlers() sftp.Handlers

Handlers serves fs.

func (*FakeFS) Listed

func (f *FakeFS) Listed() int

Listed counts the listing requests served so far.

func (*FakeFS) Names

func (f *FakeFS) Names() []string

Names lists the files there are, sorted.

func (*FakeFS) RealPath

func (f *FakeFS) RealPath(p string) (string, error)

RealPath resolves a path (sftp.RealPathFileLister).

func (*FakeFS) Resume

func (f *FakeFS) Resume()

Resume answers again, the requests that waited first.

func (*FakeFS) Seen

func (f *FakeFS) Seen() []string

Seen lists the requests served so far ("Method path"), in order.

func (*FakeFS) Stop

func (f *FakeFS) Stop()

Stop makes the server stop answering: requests from then on (and the rest of listings under way) wait until Resume.

type PTY

type PTY struct {
	Term       string
	Cols, Rows int
}

PTY is the terminal a client asked for.

type Server

type Server struct {
	Addr     string
	User     string
	Password string
	// AuthorizedKey, when set, may log in too.
	AuthorizedKey ssh.PublicKey
	// AuthorizedKeysFile, when set, is read at each public-key attempt;
	// any key in it may log in. As with OpenSSH's StrictModes, it is
	// ignored when it or its directory is writable by group or others.
	AuthorizedKeysFile string
	// TrustedCA, when set, signs the user certificates that may log in,
	// as OpenSSH's TrustedUserCAKeys: valid now, naming User.
	TrustedCA ssh.PublicKey
	// DirectTCPIP lets clients open direct-tcpip channels (ssh -L, -D,
	// jump hosts); TCPIPForward lets them ask for remote listeners
	// (ssh -R).
	DirectTCPIP, TCPIPForward bool
	// Exec runs "exec" requests; nil refuses them.
	Exec func(ch ssh.Channel, cmd string) int
	// RefuseEnv refuses "env" requests, as a server whose AcceptEnv does
	// not name the variable.
	RefuseEnv bool
	// ExecPTY runs "exec" requests on a channel that asked for a PTY
	// first (ssh -t host cmd); nil leaves them to Exec.
	ExecPTY func(ch ssh.Channel, cmd string, pty PTY, resize <-chan PTY) int
	HostKey ssh.Signer
	// SFTPRoot is the working directory of the sftp subsystem.
	SFTPRoot string
	// NoSFTP refuses the sftp subsystem, as a server without one.
	NoSFTP bool
	// SFTPExit, when set, takes the sftp subsystem and ends it at once
	// with that status, as a server whose sftp-server is missing (127).
	SFTPExit int
	// SFTPHandlers, when set, serves the sftp subsystem instead, with
	// handlers it makes for each session (a fake or hostile server).
	SFTPHandlers func() sftp.Handlers
	// SFTPConn, when set, stands between SFTPHandlers' server and its
	// channel (FakeFS.Conn: a server that stops answering).
	SFTPConn func(io.ReadWriteCloser) io.ReadWriteCloser
	// Shell runs sessions; nil is the toy line shell.
	Shell Shell
	// Mute stops replies to keepalives, as a server that hangs does.
	Mute atomic.Bool
	// contains filtered or unexported fields
}

Server is an SSH server on a loopback port.

func Start

func Start(addr, user, password string) (*Server, error)

Start listens on addr ("127.0.0.1:0" for any port) and serves.

func StartWithKey

func StartWithKey(addr, user, password string, hostKey ssh.Signer) (*Server, error)

StartWithKey is Start with a given host key.

func (*Server) Close

func (s *Server) Close()

Close stops the server.

func (*Server) Drop

func (s *Server) Drop()

Drop cuts every connection, as a network failure does.

func (*Server) LastPTY

func (s *Server) LastPTY() PTY

LastPTY is the size of the last PTY, after window changes.

func (*Server) Logins

func (s *Server) Logins() int

Logins counts successful logins.

func (*Server) Resizes

func (s *Server) Resizes() <-chan PTY

Resizes receives every window change.

func (*Server) SetKeyboardInteractive added in v1.1.0

func (s *Server) SetKeyboardInteractive(fn func(m ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge) (*ssh.Permissions, error))

SetKeyboardInteractive logs in by keyboard-interactive questions with fn, as a server asking a one-time password does.

func (*Server) Tunnels

func (s *Server) Tunnels() int

Tunnels counts the direct-tcpip channels opened so far.

type Shell

type Shell func(ch ssh.Channel, pty PTY, resize <-chan PTY) int

Shell runs a shell on a channel: what the client types arrives on ch, and what is written to ch reaches it. resize receives window changes. It returns the exit status.

Jump to

Keyboard shortcuts

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