Documentation ¶
Index ¶
- Constants
- func DialAndCall(srvAddr string, caller Caller) error
- func DialAndCallTLS(srvAddr string, caller Caller, tlsConfig *tls.Config) error
- func ListenAndServe(addr string, handler HandlerFunc) error
- func ListenAndServeTLS(addr string, certFile string, keyFile string, handler HandlerFunc) error
- func LongWrite(w io.Writer, p []byte) (int64, error)
- func ReadLine(reader io.Reader) (string, error)
- func Serve(listener net.Listener, handler HandlerFunc) error
- func WriteCommand(writer io.Writer, command byte, option byte, action byte) (n int, err error)
- func WriteLine(writer io.Writer, text ...string) error
- type Caller
- type CallerFunc
- type Client
- type Conn
- type HandlerFunc
- type Server
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func DialAndCall ¶
func DialAndCallTLS ¶
func ListenAndServe ¶
func ListenAndServe(addr string, handler HandlerFunc) error
ListenAndServe listens on the TCP network address 'addr' and then spawns a call to ServeTELNET method on 'handler' to serve each incoming connection.
func ListenAndServeTLS ¶
func ListenAndServeTLS(addr string, certFile string, keyFile string, handler HandlerFunc) error
ListenAndServeTLS functions similarly to ListenAndServe, but supports the TELNET protocol over TLS.
This enables 'secured telnet' (TELNETS), typically on port 992 by default, though this can be overridden using the 'addr' argument.
func LongWrite ¶
LongWrite attempts to write the bytes from 'p' to the writer 'w', handling short writes where w.Write returns io.ErrShortWrite and n < len(p).
func ReadLine ¶
ReadLine is a helper function to read a line from the Telnet client.
This doesn't really work for reading from servers, as servers may not finish a line with a \r or \n (e.g. an auth prompt), causing reader.Read(p) to block indefinitely.
func Serve ¶
func Serve(listener net.Listener, handler HandlerFunc) error
Serve accepts an incoming TELNET or TELNETS client connection on the net.Listener 'listener'.
func WriteCommand ¶
WriteCommand is a dirty workaround to write Telnet commands directly to the client. The internal wrapper satisfies io.Write, preventing us from including custom logic to handle commands (without risking bodging real data). Instead, this submits a signature (IAC x4) the underlying Write function knows to look for, and to treat as a command.
Types ¶
type Caller ¶
A Caller represents the client end of a TELNET (or TELNETS) connection.
Writing data to the Writer passed as an argument to the CallTELNET method will send data to the TELNET (or TELNETS) server.
Reading data from the Reader passed as an argument to the CallTELNET method will receive data from the TELNET server.
The Writer's Write method sends "escaped" TELNET (and TELNETS) data.
The Reader's Read method "un-escapes" TELNET (and TELNETS) data, and filters out TELNET (and TELNETS) command sequences.
var StandardCaller Caller = internalStandardCaller{}
StandardCaller is a simple TELNET client which sends to the server any data it gets from os.Stdin as TELNET (and TELNETS) data, and writes any TELNET (or TELNETS) data it receives from the server to os.Stdout, and writes any error it has to os.Stderr.
type CallerFunc ¶
The CallerFunc type is an adapter to allow the use of ordinary functions as TELNET callers.
var EchoCaller CallerFunc = func(ctx context.Context, w io.Writer, r io.Reader) { for { serverLine, err := ReadLine(r) if err != nil { if err == io.EOF { fmt.Println("Connection closed by foreign host.") return } fmt.Printf("Failed to read from the server: %v\n", err) return } if _, err = os.Stdout.WriteString(serverLine); err != nil { fmt.Printf("Failed to write server response to stdout: %v\n", err) return } clientLine, err := ReadLine(os.Stdin) if err != nil { fmt.Printf("Failed to read client input: %v\n", err) return } if !strings.HasSuffix(clientLine, "\r\n") { clientLine = strings.TrimSuffix(clientLine, "\n") + "\r\n" } if err = WriteLine(w, clientLine); err != nil { fmt.Printf("Failed to write to server: %v\n", err) return } } }
EchoCaller is a simple TELNET client which sends to the server any data it gets from os.Stdin as TELNET data, and writes any TELNET data it receives from the server to os.Stdout.
func (CallerFunc) CallTELNET ¶
CallTELNET calls f(ctx, w, r).
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
func Dial ¶
Dial makes an unsecured TELNET client connection to the specified address. If no address is supplied, it'll default to localhost.
func DialTLS ¶
DialTLS makes a secure TELNETS client connection to the specified address. If no address is supplied, it'll default to localhost.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address.
type HandlerFunc ¶
The HandlerFunc type is an adapter to allow the use of ordinary functions as TELNET handlers.
var EchoHandler HandlerFunc = func(ctx context.Context, w io.Writer, r io.Reader) { // Buffer needs to be small to avoid waiting for it to fill up. var buffer [1]byte p := buffer[:] for { n, err := r.Read(p) if n > 0 { if _, err := w.Write(p[:n]); err != nil { return } } if err != nil { break } } }
EchoHandler is a simple TELNET server which "echos" back to the client any (non-command) data back to the TELNET client, it received from the TELNET client.
func (HandlerFunc) ServeTELNET ¶
ServeTELNET calls f(ctx, w, r).
type Server ¶
type Server struct { Handler HandlerFunc // handler to invoke; default is telnet.EchoHandler if nil TLSConfig *tls.Config // optional TLS configuration; used by ListenAndServeTLS Addr string // TCP address to listen on; ":telnet" or ":telnets" if empty (used with ListenAndServe or ListenAndServeTLS respectively). // contains filtered or unexported fields }
Server defines parameters of a running TELNET server.
func (*Server) ListenAndServe ¶
ListenAndServe listens on the TCP network address 'server.Addr' and then spawns a call to Serve method on 'server.Handler' to serve each incoming connection.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS behaves similarly to ListenAndServe, but operates over the TELNET protocol with TLS encryption.
In the context of the TELNET protocol, it enables 'secured telnet' (TELNETS), typically on port 992.