telnet

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2020 License: MIT Imports: 7 Imported by: 7

README

Telnet

Go package for creating a Telnet protocol ReadWriter, which would typically be used to create a TCP telnet server.

Correctness is the primary focus and performance is secondary.

Options included:

  • Echo us
  • Suppress Go Ahead (SGA)
  • Terminal-Type

Refer to the Option interface in USAGE for information about implementing other options.

Specifications:

Document Description
RFC854 Telnet Protocol Specification
RFC855 Telnet Option Specifications
RFC857 Telnet Echo Option
RFC858 Telnet Suppress Go Ahead Option
RFC1091 Telnet Terminal-Type Option
RFC1143 The Q Method of Implementing TELNET Option Negotiation

Installation

$ go get github.com/ebarkie/telnet{,/option}

Usage

See USAGE.

Example

package main

import (
	"io"
	"log"
	"net"

	"github.com/ebarkie/telnet"
)

func serve(conn net.Conn) {
	defer conn.Close()
	defer log.Printf("Connection from %s closed", conn.RemoteAddr())

	// Create telnet ReadWriter with no options.
	tn := telnet.NewReadWriter(conn)

	// Welcome banner.
	tn.Write([]byte("Welcome to a test telnet server!\r\n\r\n"))

	// Process input until connection is closed.
	buf := make([]byte, 1024)
	for {
		tn.Write([]byte("> "))
		n, err := tn.Read(buf)
		if err == io.EOF {
			return
		}
		log.Printf("Read '%s' {% [1]x} n=%d", buf[:n], n)
	}
}

func main() {
	// Create TCP listener.
	addr := net.JoinHostPort("127.0.0.1", "8023")
	log.Printf("Listening on %s", addr)
	l, err := net.Listen("tcp", addr)
	if err != nil {
		panic(err)
	}

	for {
		conn, _ := l.Accept()
		log.Printf("Accepted connection from %s", conn.RemoteAddr())
		go serve(conn)
	}
}

License

Copyright (c) 2018-2019 Eric Barkie. All rights reserved.
Use of this source code is governed by the MIT license that can be found in the LICENSE file.

Documentation

Overview

Package telnet implements the RFC854 Telnet Protocol Specification, as well as:

RFC855  Telnet Option Specifications
RFC1143 The Q Method of Implementing TELNET Option Negotiation

Index

Constants

This section is empty.

Variables

Loggers.

View Source
var (
	ErrNegAskDenied = errors.New("Ask violates let")
)

Errors.

Functions

This section is empty.

Types

type Command

type Command byte

Command is a telnet command.

const (
	EOF Command = 236 + iota // End of File character
	SP                       // Suspend Process
	AP                       // Abort Process
	EOR                      // End Of Record

	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

)

RFC854 telnet commands.

func (Command) String

func (i Command) String() string

type Ctx

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

Ctx is a telnet context.

func NewReadWriter

func NewReadWriter(rw io.ReadWriter, opts ...Option) *Ctx

NewReadWriter allocates a new ReadWriter that intercepts and handles telnet negotiations and dispatches remaining data to rw. Any options that are provided will be available for negotiation.

func (Ctx) AskHim

func (t Ctx) AskHim(opt Option, enable bool) error

AskHim asks him to enable or disable an option.

func (Ctx) AskUs

func (t Ctx) AskUs(opt Option, enable bool) error

AskUs asks if we can enable or disable an option.

func (*Ctx) Read

func (t *Ctx) Read(b []byte) (n int, err error)

Read is a telnet Reader.

The Reader provided by the client is read and command sequences are parsed and executed. If a non-empty buffer is passed then data will be passed as-is.

func (Ctx) SendCmd

func (t Ctx) SendCmd(cmd Command)

SendCmd sends a line mode command signal.

func (Ctx) SendParams

func (t Ctx) SendParams(opt Option, params []byte)

SendParams sends option subnegotiation parameters.

func (Ctx) Write

func (t Ctx) Write(b []byte) (int, error)

Write is a telnet Writer.

Any Interpret as Command bytes are escaped and the result is written using the Writer provided by the client.

type Option

type Option interface {
	// Byte returns the byte code of the option.
	Byte() byte
	// String is the text name of the option (for debugging).
	String() string

	// LetHim indicates if he is allowed to enable the option.
	LetHim() bool
	// LetUs indicates if we are willing to enable the option.
	LetUs() bool

	// Params is called when subnegotiation parameters are received.
	Params(tn *Ctx, params []byte)

	// SetHim sets the option state for him.
	SetHim(tn *Ctx, enabled bool)
	// SetUs sets the option state for us.
	SetUs(tn *Ctx, enabled bool)
}

Option is an interface for implementing telnet options.

Directories

Path Synopsis
Package option implements several RFC855 Telnet Option Specifications, including: RFC857 Telnet Echo Option RFC858 Telnet Suppress Go Ahead Option RFC1091 Telnet Terminal-Type Option
Package option implements several RFC855 Telnet Option Specifications, including: RFC857 Telnet Echo Option RFC858 Telnet Suppress Go Ahead Option RFC1091 Telnet Terminal-Type Option

Jump to

Keyboard shortcuts

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