forked from reiver/go-telnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
148 lines (115 loc) · 3.36 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package telnet
import (
"crypto/tls"
"net"
)
type Conn struct {
conn interface {
Read(b []byte) (n int, err error)
Write(b []byte) (n int, err error)
Close() error
LocalAddr() net.Addr
RemoteAddr() net.Addr
}
dataReader *internalDataReader
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).
//
// If a secure connection is desired, use `DialTLS` instead.
func Dial() (*Conn, error) {
return DialTo("")
}
// DialTo makes a (un-secure) TELNET client connection to the the address specified by
// 'addr'.
//
// If a secure connection is desired, use `DialToTLS` instead.
func DialTo(addr string) (*Conn, error) {
const network = "tcp"
if "" == addr {
addr = "127.0.0.1:telnet"
}
conn, err := net.Dial(network, addr)
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).
func DialTLS(tlsConfig *tls.Config) (*Conn, error) {
return DialToTLS("", tlsConfig)
}
// DialToTLS makes a (secure) TELNETS client connection to the the address specified by
// 'addr'.
func DialToTLS(addr string, tlsConfig *tls.Config) (*Conn, error) {
const network = "tcp"
if "" == addr {
addr = "127.0.0.1:telnets"
}
conn, err := tls.Dial(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.
//
// Typical usage might look like:
//
// telnetsClient, err = telnet.DialToTLS(addr, tlsConfig)
// if nil != err {
// //@TODO: Handle error.
// return err
// }
// defer telnetsClient.Close()
func (clientConn *Conn) Close() error {
return clientConn.conn.Close()
}
// Read receives `n` bytes sent from the server to the client,
// and "returns" into `p`.
//
// Note that Read can only be used for receiving TELNET (and TELNETS) data from the server.
//
// TELNET (and TELNETS) command codes cannot be received using this method, as Read deals
// with TELNET (and TELNETS) "unescaping", and (when appropriate) filters out TELNET (and TELNETS)
// command codes.
//
// Read makes Client fit the io.Reader interface.
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.
//
// TELNET (and TELNETS) command codes cannot be sent using this method, as Write deals with
// TELNET (and TELNETS) "escaping", and will properly "escape" anything written with it.
//
// Write makes Conn fit the io.Writer interface.
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()
}