telnet

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2024 License: MIT Imports: 8 Imported by: 1

README

Telnet

GitHub Build Coverage Go Report Card GoDoc

7 Days to Die remote access to game Command Console. This is not full TELNET protocol implementation.

Supported Games

Open pull request if you have successfully used a package with another game with telnet support and add it to the list.

Install

go get github.com/gorcon/telnet

See Changelog for release details.

Usage

Execute single command
package main

import (
	"log"
	"fmt"

	"github.com/gorcon/telnet"
)

func main() {
	conn, err := telnet.Dial("127.0.0.1:8081", "password")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	response, err := conn.Execute("help")
	if err != nil {
		log.Fatal(err)
	}
	
	fmt.Println(response)	
}
Interactive CLI mode
package main

import (
	"log"
	"os"

	"github.com/gorcon/telnet"
)

func main() {
	err := telnet.DialInteractive(os.Stdin, os.Stdout, "127.0.0.1:8081", "")
	if err != nil {
		log.Println(err)
	}
}

Requirements

Go 1.15 or higher

Contribute

Contributions are more than welcome!

If you think that you have found a bug, create an issue and publish the minimum amount of code triggering the bug so it can be reproduced.

If you want to fix the bug then you can create a pull request. If possible, write a test that will cover this bug.

License

MIT License, see LICENSE

Documentation

Index

Constants

View Source
const (
	ResponseEnterPassword         = "Please enter password"
	ResponseAuthSuccess           = "Logon successful."
	ResponseAuthIncorrectPassword = "Password incorrect, please enter password:"
	ResponseAuthTooManyFails      = "Too many failed login attempts!"
	ResponseWelcome               = "Press 'help' to get a list of all commands. Press 'exit' to end session."

	// ResponseINFLayout is the template for the logline about the command
	// received by the server.
	ResponseINFLayout = "INF Executing command '%s' by Telnet from %s"
)

Remote server response messages.

View Source
const CRLF = "\r\n"

CRLF moves the cursor to the next line and then moves it to the beginning.

View Source
const DefaultDialTimeout = 5 * time.Second

DefaultDialTimeout provides default auth timeout to remote server.

View Source
const DefaultExitCommand = "exit"

DefaultExitCommand provides default TELNET exit command.

View Source
const ExecuteTickTimeout = 1 * time.Second

ExecuteTickTimeout is execute read timeout.

View Source
const ForcedExitCommand = ":q"

ForcedExitCommand provides forced TELNET exit command.

View Source
const MaxCommandLen = 1000

MaxCommandLen is an artificial restriction, but it will help in case of random large queries.

View Source
const NullString = "\x00"

NullString is a null byte in string format.

View Source
const ReceiveWaitPeriod = 3 * time.Millisecond

ReceiveWaitPeriod is a delay to receive data from the server.

Variables

View Source
var (
	// ErrAuthFailed is returned when 7 Days to Die server rejected
	// sent password.
	ErrAuthFailed = errors.New("authentication failed")

	// ErrAuthUnexpectedMessage is returned when 7 Days to Die server responses
	// without ResponseAuthSuccess or ResponseAuthIncorrectPassword
	// on auth request.
	ErrAuthUnexpectedMessage = errors.New("unexpected authentication response")

	// ErrCommandTooLong is returned when executed command length is bigger
	// than MaxCommandLen characters.
	ErrCommandTooLong = errors.New("command too long")

	// ErrCommandEmpty is returned when executed command length equal 0.
	ErrCommandEmpty = errors.New("command too small")

	// ErrMultiErrorOccurred is returned when close connection failed with
	// error after auth failed.
	ErrMultiErrorOccurred = errors.New("an error occurred while handling another error")
)
View Source
var DefaultSettings = Settings{
	// contains filtered or unexported fields
}

DefaultSettings provides default deadline settings to Conn.

Functions

func DialInteractive

func DialInteractive(r io.Reader, w io.Writer, address string, password string, options ...Option) error

DialInteractive parses commands from input reader, executes them on remote server and writes responses to output writer. Password can be empty string. In this case password will be prompted in an interactive window.

Types

type Conn

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

Conn is TELNET connection.

func Dial

func Dial(address string, password string, options ...Option) (*Conn, error)

Dial creates a new authorized TELNET connection.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the client connection.

func (*Conn) Execute

func (c *Conn) Execute(command string) (string, error)

Execute sends command string to execute to the remote TELNET server.

func (*Conn) LocalAddr

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

LocalAddr returns the local network address.

func (*Conn) RemoteAddr

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

RemoteAddr returns the remote network address.

func (*Conn) Status added in v1.1.0

func (c *Conn) Status() string

Status returns server info status after auth request.

type Option

type Option func(s *Settings)

Option allows to inject settings to Settings.

func SetClearResponse added in v1.1.0

func SetClearResponse(clear bool) Option

SetClearResponse injects clearResponse option to telnet client.

func SetDialTimeout

func SetDialTimeout(timeout time.Duration) Option

SetDialTimeout injects dial Timeout to Settings.

func SetExitCommand

func SetExitCommand(command string) Option

SetExitCommand injects telnet exit command.

type Settings

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

Settings contains option to Conn.

Directories

Path Synopsis
Package telnettest contains TELNET server for TELNET client testing.
Package telnettest contains TELNET server for TELNET client testing.

Jump to

Keyboard shortcuts

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