-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
83 lines (69 loc) · 1.63 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
package telnet
import (
"crypto/tls"
"net"
)
type Conn struct {
conn net.Conn
reader *reader
writer *writer
}
// TODO: implement timeout for dialing
// Dial makes an unsecured TELNET client connection to the specified address.
// If no address is supplied, it'll default to localhost.
func Dial(protocol, addr string) (*Conn, error) {
if protocol == "" {
protocol = "tcp"
}
if addr == "" {
addr = "127.0.0.1:telnet"
}
conn, err := net.Dial(protocol, addr)
if err != nil {
return nil, err
}
return &Conn{
conn: conn,
reader: newReader(conn),
writer: newWriter(conn),
}, nil
}
// DialTLS makes a secure TELNETS client connection to the specified address.
// If no address is supplied, it'll default to localhost.
func DialTLS(protocol, addr string, tlsConfig *tls.Config) (*Conn, error) {
if protocol == "" {
protocol = "tcp"
}
if addr == "" {
addr = "127.0.0.1:telnets"
}
conn, err := tls.Dial(protocol, addr, tlsConfig)
if err != nil {
return nil, err
}
return &Conn{
conn: conn,
reader: newReader(conn),
writer: newWriter(conn),
}, nil
}
// Close closes the client connection.
func (c *Conn) Close() error {
return c.conn.Close()
}
// Read reads bytes from the server into p.
func (c *Conn) Read(p []byte) (int, error) {
return c.reader.Read(p)
}
// Write writes bytes to the server from p.
func (c *Conn) Write(p []byte) (int, error) {
return c.writer.Write(p)
}
// LocalAddr returns the local network address.
func (c *Conn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (c *Conn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}