From 2fd9d87a4dbf2a3d8c220e161ce0c8874284586a Mon Sep 17 00:00:00 2001 From: Gian Luca Dalla Torre Date: Tue, 9 Apr 2019 22:14:31 +0200 Subject: [PATCH 1/3] Add Connection with Timeout --- conn.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 13 deletions(-) diff --git a/conn.go b/conn.go index 31a9659..07c8e43 100644 --- a/conn.go +++ b/conn.go @@ -1,12 +1,11 @@ package telnet - import ( "crypto/tls" "net" + "time" ) - type Conn struct { conn interface { Read(b []byte) (n int, err error) @@ -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). // @@ -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). @@ -83,15 +110,44 @@ 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. // @@ -107,7 +163,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`. // @@ -122,7 +177,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. @@ -135,13 +189,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() From 7d73a738d8b64405108b46d2bebff98384699df9 Mon Sep 17 00:00:00 2001 From: Gian Luca Dalla Torre Date: Tue, 9 Apr 2019 22:27:55 +0200 Subject: [PATCH 2/3] Add Connection with Timeout (small fix) --- conn.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/conn.go b/conn.go index 07c8e43..ab5149c 100644 --- a/conn.go +++ b/conn.go @@ -129,9 +129,7 @@ func DialToTLSTimeout(addr string, timeout time.Duration, tlsConfig *tls.Config) addr = "127.0.0.1:telnets" } - d:= net.Dialer{ - Timeout: timeout - } + d := net.Dialer{Timeout: timeout} conn, err := tls.DialWithDialer(d, network, addr, tlsConfig) if nil != err { return nil, err From 586aaa0b0ba3c8ec55c96114aa9c1170200da496 Mon Sep 17 00:00:00 2001 From: Gian Luca Dalla Torre Date: Tue, 9 Apr 2019 22:34:07 +0200 Subject: [PATCH 3/3] Small fixes --- conn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conn.go b/conn.go index ab5149c..43a6ad2 100644 --- a/conn.go +++ b/conn.go @@ -130,7 +130,7 @@ func DialToTLSTimeout(addr string, timeout time.Duration, tlsConfig *tls.Config) } d := net.Dialer{Timeout: timeout} - conn, err := tls.DialWithDialer(d, network, addr, tlsConfig) + conn, err := tls.DialWithDialer(&d, network, addr, tlsConfig) if nil != err { return nil, err }