telnetserver

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package telnetserver accepts raw telnet connections and presents each one to the BBS as a gliderlabs/ssh.Session, so the same session handler serves both SSH and telnet callers. Server listens and negotiates options, TelnetConn handles the IAC protocol layer (NAWS, TERM_TYPE, 0xFF escaping), and TelnetSessionAdapter wraps the connection in the ssh.Session interface.

Index

Constants

View Source
const (
	IAC  byte = 255 // Interpret As Command
	DONT byte = 254
	DO   byte = 253
	WONT byte = 252
	WILL byte = 251
	SB   byte = 250 // Subnegotiation Begin
	SE   byte = 240 // Subnegotiation End

	OptEcho     byte = 1  // Echo option
	OptSGA      byte = 3  // Suppress Go Ahead
	OptTermType byte = 24 // Terminal Type (RFC 1091)
	OptNAWS     byte = 31 // Negotiate About Window Size
	OptLinemode byte = 34 // Linemode

	TermTypeIs   byte = 0 // IS sub-command: client sends its terminal type
	TermTypeSend byte = 1 // SEND sub-command: server requests terminal type
)

Telnet protocol constants

Variables

View Source
var ErrWoken = errors.New("telnet: read woken")

ErrWoken is returned by Read when Wake cut a blocked read short. No data was consumed; the caller may read again.

Functions

This section is empty.

Types

type Config

type Config struct {
	Port           int
	Host           string
	SessionHandler SessionHandler
}

Config holds telnet server configuration.

type Server

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

Server is a telnet server that listens for TCP connections and wraps them with telnet protocol handling.

func NewServer

func NewServer(cfg Config) (*Server, error)

NewServer creates a new telnet server instance.

func (*Server) Close

func (s *Server) Close() error

Close shuts down the telnet server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts listening for telnet connections and blocks.

type SessionHandler

type SessionHandler func(*TelnetSessionAdapter)

SessionHandler is called when a new telnet session is established.

type TelnetConn

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

TelnetConn wraps a net.Conn with telnet protocol awareness. Read() strips IAC commands transparently; Write() escapes 0xFF bytes.

func NewTelnetConn

func NewTelnetConn(conn net.Conn) *TelnetConn

NewTelnetConn wraps an existing net.Conn with telnet protocol handling.

func (*TelnetConn) Close

func (tc *TelnetConn) Close() error

Close closes the telnet connection.

func (*TelnetConn) DetectTerminalSize

func (tc *TelnetConn) DetectTerminalSize() (width, height int, method string)

DetectTerminalSize performs terminal size detection using ANSI cursor position reporting (CPR) as the primary method, with NAWS fallback and 80x25 defaults. CPR is the most reliable method because it detects the actual usable area, accounting for status bars (e.g., SyncTerm's status row steals one row from NAWS).

func (*TelnetConn) LocalAddr

func (tc *TelnetConn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*TelnetConn) Negotiate

func (tc *TelnetConn) Negotiate() error

Negotiate sends telnet option negotiations and waits for client responses. Phase 1: sends DO NAWS + DO TERM_TYPE and reads until the client has answered TERM_TYPE (WILL or WONT). Phase 2: if client responded WILL TERM_TYPE, sends SB TERM_TYPE SEND and reads until the IS <string> subnegotiation arrives. Each phase gives up after negotiationTimeout.

func (*TelnetConn) Read

func (tc *TelnetConn) Read(p []byte) (int, error)

Read reads data from the telnet connection, stripping IAC commands transparently.

func (*TelnetConn) RemoteAddr

func (tc *TelnetConn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*TelnetConn) SetReadInterrupt

func (tc *TelnetConn) SetReadInterrupt(ch <-chan struct{})

SetReadInterrupt sets a channel that, when closed, causes any blocked Read() to return io.EOF without consuming data. This works by setting a past read deadline on the underlying connection to unblock the blocked read syscall. Pass nil to clear the interrupt and reset the deadline.

func (*TelnetConn) TermType

func (tc *TelnetConn) TermType() string

TermType returns the terminal type string reported by the client via TERM_TYPE negotiation (RFC 1091). Returns "ansi" if no type was negotiated.

func (*TelnetConn) Wake added in v0.9.5

func (tc *TelnetConn) Wake()

Wake makes a blocked Read return ErrWoken, using the same past-deadline mechanism as SetReadInterrupt.

func (*TelnetConn) WindowSize

func (tc *TelnetConn) WindowSize() (width, height int)

WindowSize returns the current terminal dimensions.

func (*TelnetConn) Write

func (tc *TelnetConn) Write(p []byte) (int, error)

Write writes data to the telnet connection, escaping any 0xFF bytes as IAC IAC.

type TelnetSessionAdapter

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

TelnetSessionAdapter adapts a telnet connection to the gliderlabs/ssh.Session interface so that the existing sessionHandler() works unchanged.

func NewTelnetSessionAdapter

func NewTelnetSessionAdapter(tc *TelnetConn) *TelnetSessionAdapter

NewTelnetSessionAdapter creates an adapter that implements ssh.Session for a telnet connection.

func (*TelnetSessionAdapter) Break

func (a *TelnetSessionAdapter) Break(c chan<- bool)

Break is a no-op for telnet.

func (*TelnetSessionAdapter) Close

func (a *TelnetSessionAdapter) Close() error

Close closes the telnet session.

func (*TelnetSessionAdapter) CloseWrite

func (a *TelnetSessionAdapter) CloseWrite() error

CloseWrite is a no-op for telnet (no half-close support).

func (*TelnetSessionAdapter) Command

func (a *TelnetSessionAdapter) Command() []string

Command returns an empty command for telnet (shell session).

func (*TelnetSessionAdapter) Context

func (a *TelnetSessionAdapter) Context() ssh.Context

Context returns the session context.

func (*TelnetSessionAdapter) Environ

func (a *TelnetSessionAdapter) Environ() []string

Environ returns an empty environment for telnet.

func (*TelnetSessionAdapter) Exit

func (a *TelnetSessionAdapter) Exit(code int) error

Exit closes the session.

func (*TelnetSessionAdapter) IsTransferActive

func (a *TelnetSessionAdapter) IsTransferActive() bool

IsTransferActive returns true if a binary transfer is in progress.

func (*TelnetSessionAdapter) LocalAddr

func (a *TelnetSessionAdapter) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*TelnetSessionAdapter) Permissions

func (a *TelnetSessionAdapter) Permissions() ssh.Permissions

Permissions returns empty permissions.

func (*TelnetSessionAdapter) Pty

func (a *TelnetSessionAdapter) Pty() (ssh.Pty, <-chan ssh.Window, bool)

Pty returns the synthesized PTY info with Term="ansi" and NAWS-detected window size.

func (*TelnetSessionAdapter) PublicKey

func (a *TelnetSessionAdapter) PublicKey() ssh.PublicKey

PublicKey returns nil (telnet has no public key auth).

func (*TelnetSessionAdapter) RawCommand

func (a *TelnetSessionAdapter) RawCommand() string

RawCommand returns an empty raw command.

func (*TelnetSessionAdapter) RawWrite

func (a *TelnetSessionAdapter) RawWrite(p []byte) (int, error)

RawWrite provides a binary-safe write path for transfer protocols (ZMODEM etc.). For telnet, IAC escaping is still required by the protocol, so this is functionally identical to Write. It exists so TelnetSessionAdapter satisfies the rawBinaryWriter interface used by RunCommandDirect, avoiding the fallback warning and keeping the code path consistent with BBSSession.

func (*TelnetSessionAdapter) Read

func (a *TelnetSessionAdapter) Read(p []byte) (int, error)

Read reads from the telnet connection (IAC-filtered). With a tap attached, queued sysop bytes are returned before the socket is read again; a Read already blocked on the socket is woken when sysop bytes arrive. Neither source drops bytes and each keeps its own order. Read must not be called from two goroutines at once: tapPending and TelnetConn's parser state assume a single reader.

func (*TelnetSessionAdapter) RemoteAddr

func (a *TelnetSessionAdapter) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*TelnetSessionAdapter) SendRequest

func (a *TelnetSessionAdapter) SendRequest(name string, wantReply bool, payload []byte) (bool, error)

SendRequest is not supported on telnet.

func (*TelnetSessionAdapter) SessionID

func (a *TelnetSessionAdapter) SessionID() string

SessionID returns the unique session identifier.

func (*TelnetSessionAdapter) SetReadInterrupt

func (a *TelnetSessionAdapter) SetReadInterrupt(ch <-chan struct{})

SetReadInterrupt sets a channel that, when closed, causes any blocked Read() to return without consuming data. Used to cleanly stop door I/O goroutines.

func (*TelnetSessionAdapter) SetTap added in v0.9.5

func (a *TelnetSessionAdapter) SetTap(t *snoop.Tap)

SetTap attaches the node's snoop tap. While a Read blocks on the socket, a watcher goroutine wakes it whenever the sysop types.

func (*TelnetSessionAdapter) SetTransferActive

func (a *TelnetSessionAdapter) SetTransferActive(active bool)

SetTransferActive marks/unmarks the session as being in a binary transfer.

func (*TelnetSessionAdapter) Signals

func (a *TelnetSessionAdapter) Signals(c chan<- ssh.Signal)

Signals is a no-op for telnet.

func (*TelnetSessionAdapter) Stderr

func (a *TelnetSessionAdapter) Stderr() io.ReadWriter

Stderr returns a writer for stderr (same as stdout for BBS).

func (*TelnetSessionAdapter) Subsystem

func (a *TelnetSessionAdapter) Subsystem() string

Subsystem returns an empty subsystem.

func (*TelnetSessionAdapter) Tap added in v0.9.5

func (a *TelnetSessionAdapter) Tap() *snoop.Tap

Tap returns the attached snoop tap, or nil.

func (*TelnetSessionAdapter) User

func (a *TelnetSessionAdapter) User() string

User returns empty string for telnet (triggers manual login flow).

func (*TelnetSessionAdapter) Write

func (a *TelnetSessionAdapter) Write(p []byte) (int, error)

Write writes to the telnet connection (IAC-escaped) and copies what was sent to the snoop tap. During a binary transfer the tap gets a marker instead of the data.

type TelnetSessionContext

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

TelnetSessionContext implements context.Context and provides session metadata.

func (*TelnetSessionContext) ClientVersion

func (c *TelnetSessionContext) ClientVersion() string

ClientVersion implements ssh.Context; it always returns "telnet".

func (*TelnetSessionContext) Deadline

func (c *TelnetSessionContext) Deadline() (deadline time.Time, ok bool)

Deadline implements context.Context; telnet sessions have no deadline, so ok is always false.

func (*TelnetSessionContext) Done

func (c *TelnetSessionContext) Done() <-chan struct{}

Done implements context.Context. The channel is closed when the owning TelnetSessionAdapter is closed.

func (*TelnetSessionContext) Err

func (c *TelnetSessionContext) Err() error

Err implements context.Context; it returns context.Canceled once the session has been closed and nil before that.

func (*TelnetSessionContext) LocalAddr

func (c *TelnetSessionContext) LocalAddr() net.Addr

LocalAddr implements ssh.Context, returning the listener-side address captured when the session was created.

func (*TelnetSessionContext) Lock

func (c *TelnetSessionContext) Lock()

Lock implements the sync.Locker part of ssh.Context. It takes the same mutex that guards Value and SetValue, so do not call those while holding it.

func (*TelnetSessionContext) Permissions

func (c *TelnetSessionContext) Permissions() *ssh.Permissions

Permissions implements ssh.Context; it always returns nil since telnet has no SSH authentication step.

func (*TelnetSessionContext) RemoteAddr

func (c *TelnetSessionContext) RemoteAddr() net.Addr

RemoteAddr implements ssh.Context, returning the caller's address captured when the session was created.

func (*TelnetSessionContext) ServerVersion

func (c *TelnetSessionContext) ServerVersion() string

ServerVersion implements ssh.Context; it always returns "vision3-telnet".

func (*TelnetSessionContext) SessionID

func (c *TelnetSessionContext) SessionID() string

SessionID implements ssh.Context, returning the "telnet-<nanos>-<n>" identifier assigned by NewTelnetSessionAdapter.

func (*TelnetSessionContext) SetValue

func (c *TelnetSessionContext) SetValue(key, value interface{})

SetValue implements ssh.Context, storing a session-scoped value that Value returns in preference to the parent context. It is safe for concurrent use.

func (*TelnetSessionContext) Unlock

func (c *TelnetSessionContext) Unlock()

Unlock implements the sync.Locker part of ssh.Context, releasing the lock taken by Lock.

func (*TelnetSessionContext) User

func (c *TelnetSessionContext) User() string

User implements ssh.Context. It always returns "" because telnet carries no username, which sends the caller through the manual login flow.

func (*TelnetSessionContext) Value

func (c *TelnetSessionContext) Value(key interface{}) interface{}

Value implements context.Context. Values stored with SetValue take precedence over those of the parent context.

Jump to

Keyboard shortcuts

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