telnetserver

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

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

This section is empty.

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, drains responses (500ms). Phase 2: if client responded WILL TERM_TYPE, sends SB TERM_TYPE SEND and drains again (500ms) to collect the IS <string> subnegotiation.

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) 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) 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) Read

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

Read reads from the telnet connection (IAC-filtered).

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) 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) 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).

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

func (*TelnetSessionContext) Deadline

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

func (*TelnetSessionContext) Done

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

func (*TelnetSessionContext) Err

func (c *TelnetSessionContext) Err() error

func (*TelnetSessionContext) LocalAddr

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

func (*TelnetSessionContext) Lock

func (c *TelnetSessionContext) Lock()

func (*TelnetSessionContext) Permissions

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

func (*TelnetSessionContext) RemoteAddr

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

func (*TelnetSessionContext) ServerVersion

func (c *TelnetSessionContext) ServerVersion() string

func (*TelnetSessionContext) SessionID

func (c *TelnetSessionContext) SessionID() string

func (*TelnetSessionContext) SetValue

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

func (*TelnetSessionContext) Unlock

func (c *TelnetSessionContext) Unlock()

func (*TelnetSessionContext) User

func (c *TelnetSessionContext) User() string

func (*TelnetSessionContext) Value

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

Jump to

Keyboard shortcuts

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