telnet

package module
v0.0.0-...-7feb8d5 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2016 License: MIT Imports: 18 Imported by: 0

README

telnet

Package telnet implements the telnet protocol.

Acknowledgements

Credits, 💖, 🍺 & 🌻 to Jeff Quast since this package is derived from telnetlib3.

Documentation

Overview

Package telnet implements the telnet protocol.

Index

Constants

View Source
const (
	LFLOW_OFF byte = iota
	LFLOW_ON
	LFLOW_RESTART_ANY
	LFLOW_RESTART_XON
)
View Source
const (
	BINARY              byte = iota // 8-bit data path
	ECHO                            // Echo
	RCP                             // Prepare to reconnect
	SGA                             // Suppress Go Ahead
	NAMS                            // Approximate Message Size
	STATUS                          // Status
	TM                              // Timing Mark
	RCTE                            // Remote Controlled Transmission and Echo
	NAOL                            // Negotiate About Output Line (width)
	NAOP                            // Negotiate About Output Page (size)
	NAOCRD                          // Negotiate About CR Disposition
	NAOHTS                          // Negotiate About Horizontal Tabstops
	NAOHTD                          // Negotiate About Horizontal Tab Disposition
	NAOFFD                          // Negotiate About Formfeed Disposition
	NAOVTS                          // Negotiate About Vertical Tabstops
	NAOVTD                          // Negotiate About Vertial Tab Disposition
	NAOLFD                          // Negotiate About Output LF Disposition
	XASCII                          // Extended ASCII character set
	LOGOUT                          // Force logout
	BM                              // Byte Macro
	DET                             // Data Entry Terminal
	SUPDUP                          // Supdup protocol
	SUPDUPOUTPUT                    // Supdup output
	SNDLOC                          // Send Location
	TTYPE                           // Terminal Type
	EOR                             // End Of Record
	TUID                            // TACAS User Identification
	VT3270REGIME                    // vt3270 regime
	X3PAD                           // X.3 Pad
	NAWS                            // Window Size
	TSPEED                          // Terminal Speed
	LFLOW                           // Remote Flow Control
	LINEMODE                        // Linemode option
	XDISPLOC                        // X Display Location
	ENVIRON                         // Environment Variables
	AUTHENTICATION                  // Authenticate
	ENCRYPT                         // Encrypt
	NEW_ENVIRON                     // Environment Variables (new)
	TN3270E                         // TN3270E
	XAUTH                           // XAuth
	CHARSET                         // Character set
	RSP                             // Remote Serial Port
	COM_PORT_OPTION                 // COM Port Control Option
	SUPPRESS_LOCAL_ECHO             // Telnet Suppress Local Echo
	TLS                             // Telnet StartTLS
	KERMIT                          // Kermit
	SEND_URL                        // Send URL
	FORWARD_X                       // Forward X
	MSDP                byte = 69
	MSSP                byte = 70
	COMPRESS            byte = 85
	COMPRESS2           byte = 86
	MSP                 byte = 90
	MXP                 byte = 91
	MSP2                byte = 92
	ZMP                 byte = 93
)

Telnet protocol options (arpa/telnet.h)

View Source
const (
	PRAGMA_LOGON     byte = iota + 138 // Telopt Pragma Logon
	SSPI_LOGON                         // Telopt SSPI Logon
	PRAGMA_HEARTBEAT                   // Telopt Pragma Heartbeat
)
View Source
const (
	EOF     byte = iota + 236 // End Of File
	SUSP                      // Suspend
	ABORT                     // Abort
	CMD_EOR                   // End Of Record
	SE                        // Sub-negotiation End
	NOP                       // No-Operation
	DM                        // Data Mark
	BRK                       // Break
	IP                        // Interrupt Process
	AO                        // Abort Output
	AYT                       // Are You There?
	EC                        // Erase Character
	EL                        // Erase Line
	GA                        // Go Ahead
	SB                        // Sub-negotiation Begin
	WILL                      // Will
	WONT                      // Wont
	DO                        // Do
	DONT                      // Dont
	IAC                       // Interpret as Command
)
View Source
const (
	TTYPE_IS byte = iota
	TTYPE_SEND
)

TTYPE bytes

View Source
const (
	MTTS_ANSI byte = 1 << iota
	MTTS_VT100
	MTTS_UTF8
	MTTS_256_COLORS
	MTTS_MOUSE_TRACKING
	MTTS_OSC_COLOR_PALETTE
	MTTS_SCREEN_READER
	MTTS_PROXY
)

MTTS bytes

View Source
const (
	MSSP_VAR byte = 1 + iota
	MSSP_VAL
)

MSSP bytes

View Source
const (
	MSDP_VAR byte = 1 + iota
	MSDP_VAL
	MSDP_TABLE_OPEN
	MSDP_TABLE_CLOSE
	MSDP_ARRAY_OPEN
	MSDP_ARRAY_CLOSE
)

MSDP values

View Source
const (
	IS byte = iota
	SEND
	INFO
)

Environment commands (rfc1572)

View Source
const (
	VAR byte = iota
	VALUE
	ESC
	USERVAR
)

Environment values (rfc1572)

View Source
const (
	REQUEST byte = 1 + iota
	ACCEPTED
	REJECTED
	TTABLE_IS
	TTABLE_REJECTED
	TTABLE_ACK
	TTABLE_NAK
)

Charset commands ()

View Source
const NULL byte = 0

NULL is the NULL byte.

Variables

View Source
var (
	// DefaultServerOption is a map of telnet options negotiated by a default server.
	DefaultServerOption = map[byte]bool{
		SGA:         true,
		TSPEED:      true,
		TTYPE:       true,
		NAWS:        true,
		NEW_ENVIRON: true,
	}
	DefaultNegotiationTimeout = time.Second * 10
)
View Source
var (
	ErrTimeout      = errors.New("telnet: timeout")
	ErrNotConnected = errors.New("telnet: not connected")
	ErrNotSupported = notSupported(true)
)

Common errors

View Source
var (
	// DefaultEnvironmentRequest are the default environment variable being requested.
	DefaultEnvironmentRequest = []string{
		"USER", "HOSTNAME", "UID", "TERM", "COLUMNS", "LINES", "DISPLAY", "LANG", "SYSTEMTYPE",
		"ACCT", "JOB", "PRINTER", "SFUTLNTVER", "SFUTLNTMODE", "LC_ALL", "VISUAL", "EDITOR",
		"LC_COLLATE", "LC_CTYPE", "LC_MESSAGES", "LC_MONETARY", "LC_NUMERIC", "LC_TIME",
	}
)

Functions

func IsNotSupported

func IsNotSupported(err error) bool

IsNotSupported checks if the error is a not-supported error.

func Prompt

func Prompt(term Terminal, format string) string

Prompt resolves a prompt with format.

'%%'       A single '%'.
'%#'       Prompt character.
'%u'       Username.
'%h'       Hostname.
'%H'       Full hostname.
'%i'       Terminal type.
'%?'       Return code last command processed.
'%E'       Encoding of session.
'%s'       Name of shell.
'%v'       Version of shell.
'%t'       Time of day in 12-hour AM/PM format.
'%T'       Time of day in 24-hour format.
'%p'       Time of day in 12-hour format with seconds, AM/PM format.
'%P'       Time of day in 24-hour format with seconds.
'%d        The weekday in `Day' format.
'%D'       The day in `dd' format.
'%w'       The month in `Mon' format.
'%W'       The month in `mm' format.
'%y'       The year in `yy' format.
'%Y'       The year in `yyyy' format.
'%z'       The timezone in `[-+]NNNN' format.
'%Z'       The timezone name in `TZNAME' format.
'%-'       Reset formatting.
'%b'       Bold formatting.
'%0'..'%7' Foreground color.

Types

type Client

type Client struct {
	net.Conn

	// TermFunc allocates a new terminal for the incoming connection. If left
	// empty a new Virtual terminal will be created.
	TermFunc func(Stream) Terminal

	// StreamFunc allocates a new stream for the incoming connection. If left
	// empty a new telnet Protocol stream.
	StreamFunc func(Conn) Stream
}

Client implements a telnet client.

func Dial

func Dial(network, address string) (*Client, error)

Dial opens a new telnet client connection. Output is sent to standard output and input is read from standard input.

func (*Client) Dial

func (c *Client) Dial(network, address string) error

Dial starts connecting over the network.

func (*Client) Interact

func (c *Client) Interact() (err error)

type Conn

type Conn interface {
	// Conn is the (raw) network connection.
	net.Conn

	// IsClient indicates that the connection is a client connection.
	IsClient() bool

	// IsServer indicates that the connection is a server connection.
	IsServer() bool

	// Terminal is the connected terminal.
	Terminal() Terminal
}

Conn is a telnet connection.

type Console

type Console struct {
	// contains filtered or unexported fields
}

Console implements the Terminal interface using standard output.

func NewConsole

func NewConsole(stream Stream, out, in *os.File) *Console

NewConsole creates a new Terminal tied to a local file descriptor, such as os.Stdout.

func (*Console) FQDN

func (c *Console) FQDN() string

func (*Console) Hostname

func (c *Console) Hostname() string

func (*Console) Info

func (c *Console) Info() *terminfo.Terminfo

func (*Console) Prompt

func (c *Console) Prompt() error

Prompt ...

func (*Console) Read

func (c *Console) Read(p []byte) (n int, err error)

func (*Console) SetSize

func (c *Console) SetSize(_, _ int) error

SetSize is not supported.

func (*Console) SetSpeed

func (c *Console) SetSpeed(_, _ int) error

SetSpeed is not supported.

func (*Console) SetType

func (c *Console) SetType(_ string) error

SetType is not supported.

func (*Console) SetXDisplayLocation

func (c *Console) SetXDisplayLocation(_ string) error

SetXDisplayLocation is not supported.

func (*Console) Size

func (c *Console) Size() (int, int)

Size returns the current terminal size. If the operation is not supported by the operating system, it will default to 80x24.

func (*Console) Speed

func (c *Console) Speed() (int, int)

Speed returns the receive and transmit baudrates of the current output device.

func (*Console) Type

func (c *Console) Type() string

Type returns the current terminal type.

func (*Console) Write

func (c *Console) Write(p []byte) (n int, err error)

func (*Console) XDisplayLocation

func (c *Console) XDisplayLocation() string

XDisplayLocation returns the current X-Display location.

type Protocol

type Protocol struct {
	// Events
	Events chan event.Event

	// IsServer interprets telnet commands in server or client mode.
	IsServer bool
	// contains filtered or unexported fields
}

Protocol implements the telnet protocol and Stream interface.

func NewProtocol

func NewProtocol(conn Conn, isServer bool) *Protocol

NewProtocol starts a new telnet stream on the connection.

func (*Protocol) Conn

func (p *Protocol) Conn() net.Conn

Conn returns the underlying transport.

func (*Protocol) Feed

func (p *Protocol) Feed(buf []byte) error

Feed interprets bytes from remote.

func (*Protocol) Negotiate

func (p *Protocol) Negotiate(timeout time.Duration) (err error)

Negotiate telnet options

func (*Protocol) Next

func (p *Protocol) Next() event.Event

Next event in the channel.

func (*Protocol) Option

func (p *Protocol) Option(opt byte) bool

Option returns the local status.

func (*Protocol) Read

func (p *Protocol) Read(b []byte) (int, error)

Read reads bytes from the internal buffer, it will block if no data is available.

func (*Protocol) ReadByte

func (p *Protocol) ReadByte() (byte, error)

ReadByte reads one byte from the internal buffer, it will block if no data is available.

func (*Protocol) RemoteOption

func (p *Protocol) RemoteOption(opt byte) bool

RemoteOption returns the remote status.

func (*Protocol) SetOption

func (p *Protocol) SetOption(opt byte, enabled bool)

SetOption sets the local status.

func (*Protocol) SubnegotiateMSSP

func (p *Protocol) SubnegotiateMSSP(b []byte) error

SubnegotiateMSSP parses a MUD Server Status Protocol sub-negotiation

func (*Protocol) Write

func (p *Protocol) Write(b []byte) (int, error)

Write transmits in-band data.

func (*Protocol) WriteOOB

func (p *Protocol) WriteOOB(b []byte) (int, error)

WriteOOB transmits out-of-band data.

func (*Protocol) WriteRaw

func (p *Protocol) WriteRaw(b []byte) (int, error)

WriteRaw transmits unescaped, raw data.

type ProtocolState

type ProtocolState uint8

ProtocolState is the current state of the telnet state machine.

func (ProtocolState) String

func (s ProtocolState) String() string

type Server

type Server struct {
	// Server listen address.
	Addr string

	// Banner message which gets sent on-connect. Some formatting strings are
	// allowed, resolved via the Prompt function.
	Banner string

	// Debug messages.
	Debug bool

	// Option is a map of telnet protocol options negotiated with new clients.
	Option map[byte]bool

	// TermFunc allocates a new terminal for the incoming connection. If left
	// empty a new Virtual terminal will be created.
	TermFunc func(Stream) Terminal

	// StreamFunc allocates a new stream for the incoming connection. If left
	// empty a new telnet Protocol stream.
	StreamFunc func(Conn) Stream

	// NegotiationTimeout is the time we allow for the client and server to
	// negotiate telnet options.
	NegotiationTimeout time.Duration
}

func NewServer

func NewServer(addr string) *Server

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

type ServerTerminal

type ServerTerminal struct {
	// contains filtered or unexported fields
}

ServerTerminal implements the Terminal interface for a Server connection.

func NewServerTerminal

func NewServerTerminal(stream Stream) *ServerTerminal

func (ServerTerminal) Charset

func (s ServerTerminal) Charset() string

func (ServerTerminal) Environment

func (s ServerTerminal) Environment() map[string]string

func (ServerTerminal) Info

func (s ServerTerminal) Info() *terminfo.Terminfo

func (ServerTerminal) Name

func (s ServerTerminal) Name() string

func (*ServerTerminal) Prompt

func (s *ServerTerminal) Prompt() error

func (ServerTerminal) Read

func (s ServerTerminal) Read(p []byte) (int, error)

func (*ServerTerminal) SetCharset

func (s *ServerTerminal) SetCharset(charsets []string) error

func (*ServerTerminal) SetEnvironment

func (s *ServerTerminal) SetEnvironment(env map[string]string) error

func (*ServerTerminal) SetName

func (s *ServerTerminal) SetName(name string)

func (*ServerTerminal) SetPrompt

func (s *ServerTerminal) SetPrompt(prompt string)

func (ServerTerminal) SetSize

func (s ServerTerminal) SetSize(w, h int) error

func (*ServerTerminal) SetSpeed

func (s *ServerTerminal) SetSpeed(rx, tx int) error

func (*ServerTerminal) SetVersion

func (s *ServerTerminal) SetVersion(version string)

func (*ServerTerminal) Size

func (s *ServerTerminal) Size() (int, int)

func (ServerTerminal) Speed

func (s ServerTerminal) Speed() (int, int)

func (ServerTerminal) Version

func (s ServerTerminal) Version() string

func (ServerTerminal) Write

func (s ServerTerminal) Write(p []byte) (int, error)

type Stream

type Stream interface {
	// Reader implements reading data bytes from the stream.
	io.Reader
	io.ByteReader

	// Writer implements writing to the stream.
	io.Writer

	// WriteRaw writes unescaped bytes.
	WriteRaw([]byte) (int, error)

	// WriteOOB writes out-of-band bytes.
	WriteOOB([]byte) (int, error)

	// Feed raw bytes.
	Feed([]byte) error

	// Option gets a telopt status for local.
	Option(byte) bool

	// SetOption sets a telopt status for local.
	SetOption(byte, bool)

	// RemoteOption gets a telopt status for remote.
	RemoteOption(byte) bool

	// Negotiate telnet options.
	Negotiate(time.Duration) error

	// Next event sent by remote.
	Next() event.Event

	// Conn is the underlying system transport.
	Conn() net.Conn
}

Stream handles raw bytes

type Terminal

type Terminal interface {
	// Writer implements writing to the terminal.
	io.Writer

	// Reader implements reading from the connected stream.
	io.Reader

	// Info returns the terminfo entry (can be nil).
	Info() *terminfo.Terminfo

	// Prompt starts interacting with remote end.
	Prompt() error
}

Terminal implements a Reader/Writer.

type TerminalWithCharset

type TerminalWithCharset interface {
	Charset() string
	SetCharset([]string) error
}

TerminalWithCharset implements a terminal with charset support (rfc2066)

type TerminalWithEnvironment

type TerminalWithEnvironment interface {
	Environment() map[string]string
	SetEnvironment(map[string]string) error
}

TerminalWithEnvironment implements a termina with environment support

type TerminalWithHostname

type TerminalWithHostname interface {
	Hostname() string
	FQDN() string
}

TerminalWithHostname implements a terminal with hostname and FQDN.

type TerminalWithLocation

type TerminalWithLocation interface {
	Location() string
	SetLocation(string) error
}

TerminalWithLocation implements a terminal with send-location support (rfc779)

type TerminalWithLogout

type TerminalWithLogout interface {
	Logout()
}

TerminalWithLogout implements a terminal with logout support

type TerminalWithReturnCode

type TerminalWithReturnCode interface {
	ReturnCode() int
}

TerminalWithReturnCode implements a terminal with return code.

type TerminalWithSize

type TerminalWithSize interface {
	Size() (int, int)
	SetSize(int, int) error
}

TerminalWithSize implements a terminal with window size support (rfc1073)

type TerminalWithSpeed

type TerminalWithSpeed interface {
	Speed() (int, int)
	SetSpeed(int, int) error
}

TerminalWithSpeed implements a terminal with speed support (rfc1079)

type TerminalWithType

type TerminalWithType interface {
	Type() string
	SetType(string) error
}

TerminalWithType implements a terminal with terminal type support (rfc1091)

type TerminalWithUsername

type TerminalWithUsername interface {
	Username() string
}

TerminalWithUsername implements a terminal with username.

type TerminalWithVersion

type TerminalWithVersion interface {
	Name() string
	Version() string
}

TerminalWithVersion implements a terminal with name and version.

type TerminalWithXDisplayLocation

type TerminalWithXDisplayLocation interface {
	XDisplayLocation() string
	SetXDisplayLocation(string) error
}

TerminalWithXDisplayLocation implements a terminal with X-Display location support (rfc1096)

Directories

Path Synopsis
cmd
telnet
Command telnet implements a telnet client.
Command telnet implements a telnet client.
Package event implements types and structures for the telnet out-of-band events.
Package event implements types and structures for the telnet out-of-band events.

Jump to

Keyboard shortcuts

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