Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Connection method with timeout support #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 63 additions & 13 deletions conn.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package telnet


import (
"crypto/tls"
"net"
"time"
)


type Conn struct {
conn interface {
Read(b []byte) (n int, err error)
Expand All @@ -19,7 +18,6 @@ type Conn struct {
dataWriter *internalDataWriter
}


// Dial makes a (un-secure) TELNET client connection to the system's 'loopback address'
// (also known as "localhost" or 127.0.0.1).
//
Expand Down Expand Up @@ -49,14 +47,43 @@ func DialTo(addr string) (*Conn, error) {
dataWriter := newDataWriter(conn)

clientConn := Conn{
conn:conn,
dataReader:dataReader,
dataWriter:dataWriter,
conn: conn,
dataReader: dataReader,
dataWriter: dataWriter,
}

return &clientConn, nil
}

// DialToTimeout makes a (un-secure) TELNET client connection to the the address specified by
// 'addr'.
// Also a timeout could be specified.
//
// If a secure connection is desired, use `DialToTLS` instead.
func DialToTimeout(addr string, timeout time.Duration) (*Conn, error) {

const network = "tcp"

if "" == addr {
addr = "127.0.0.1:telnet"
}

conn, err := net.DialTimeout(network, addr, timeout)
if nil != err {
return nil, err
}

dataReader := newDataReader(conn)
dataWriter := newDataWriter(conn)

clientConn := Conn{
conn: conn,
dataReader: dataReader,
dataWriter: dataWriter,
}

return &clientConn, nil
}

// DialTLS makes a (secure) TELNETS client connection to the system's 'loopback address'
// (also known as "localhost" or 127.0.0.1).
Expand All @@ -83,15 +110,42 @@ func DialToTLS(addr string, tlsConfig *tls.Config) (*Conn, error) {
dataWriter := newDataWriter(conn)

clientConn := Conn{
conn:conn,
dataReader:dataReader,
dataWriter:dataWriter,
conn: conn,
dataReader: dataReader,
dataWriter: dataWriter,
}

return &clientConn, nil
}

// DialToTLS makes a (secure) TELNETS client connection to the the address specified by
// 'addr'.
// Also a timeout could be specified.
func DialToTLSTimeout(addr string, timeout time.Duration, tlsConfig *tls.Config) (*Conn, error) {

const network = "tcp"

if "" == addr {
addr = "127.0.0.1:telnets"
}

d := net.Dialer{Timeout: timeout}
conn, err := tls.DialWithDialer(&d, network, addr, tlsConfig)
if nil != err {
return nil, err
}

dataReader := newDataReader(conn)
dataWriter := newDataWriter(conn)

clientConn := Conn{
conn: conn,
dataReader: dataReader,
dataWriter: dataWriter,
}

return &clientConn, nil
}

// Close closes the client connection.
//
Expand All @@ -107,7 +161,6 @@ func (clientConn *Conn) Close() error {
return clientConn.conn.Close()
}


// Read receives `n` bytes sent from the server to the client,
// and "returns" into `p`.
//
Expand All @@ -122,7 +175,6 @@ func (clientConn *Conn) Read(p []byte) (n int, err error) {
return clientConn.dataReader.Read(p)
}


// Write sends `n` bytes from 'p' to the server.
//
// Note that Write can only be used for sending TELNET (and TELNETS) data to the server.
Expand All @@ -135,13 +187,11 @@ func (clientConn *Conn) Write(p []byte) (n int, err error) {
return clientConn.dataWriter.Write(p)
}


// LocalAddr returns the local network address.
func (clientConn *Conn) LocalAddr() net.Addr {
return clientConn.conn.LocalAddr()
}


// RemoteAddr returns the remote network address.
func (clientConn *Conn) RemoteAddr() net.Addr {
return clientConn.conn.RemoteAddr()
Expand Down