-
Notifications
You must be signed in to change notification settings - Fork 7
/
conn.go
142 lines (122 loc) · 2.65 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
package es
import (
"bytes"
"encoding/binary"
"errors"
"io"
"github.com/ooclab/es/ecrypt"
)
const (
maxMessageLength = 1024*64 - 1
)
// common error define
var (
ErrBufferIsShort = errors.New("buffer is short")
ErrMaxLengthLimit = errors.New("max length limit")
)
// Conn is a interface a Conn
type Conn interface {
Recv() (message []byte, err error)
Send(message []byte) error
Close() error
}
// BaseConn is the basic connection type
type BaseConn struct {
conn io.ReadWriteCloser
}
// NewBaseConn create a base Conn object
func NewBaseConn(conn io.ReadWriteCloser) Conn {
return &BaseConn{
conn: conn,
}
}
// Recv read a message from this Conn
func (c *BaseConn) Recv() (message []byte, err error) {
head, err := c.mustRecv(2)
if err != nil {
return
}
return c.mustRecv(binary.BigEndian.Uint16(head))
}
// Send send a message to this Conn
func (c *BaseConn) Send(message []byte) error {
dlen := uint16(len(message))
if dlen > maxMessageLength {
return ErrMaxLengthLimit
}
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, dlen)
if err != nil {
return err
}
err = binary.Write(buf, binary.BigEndian, message)
if err != nil {
return err
}
// TODO: make sure write exactly data
_, err = c.conn.Write(buf.Bytes())
return err
}
// Close close a Conn
func (c *BaseConn) Close() error {
return c.conn.Close()
}
func (c *BaseConn) mustRecv(dlen uint16) ([]byte, error) {
data := make([]byte, dlen)
for i := 0; i < int(dlen); {
n, err := c.conn.Read(data[i:])
if err != nil {
return nil, err
}
i += n
}
return data, nil
}
// SafeConn ecrypt Conn
type SafeConn struct {
BaseConn
cipher *ecrypt.Cipher
}
// NewSafeConn create a safe Conn
func NewSafeConn(conn io.ReadWriteCloser, cipher *ecrypt.Cipher) Conn {
c := &SafeConn{
cipher: cipher,
}
c.conn = conn
return c
}
// Recv read a message from this Conn
func (c *SafeConn) Recv() (message []byte, err error) {
head, err := c.mustRecv(2)
if err != nil {
return
}
c.cipher.Decrypt(head[0:2], head[0:2])
message, err = c.mustRecv(binary.BigEndian.Uint16(head))
if err == nil {
c.cipher.Decrypt(message, message)
}
return
}
// Send send a message to this Conn
func (c *SafeConn) Send(message []byte) error {
dlen := uint16(len(message))
if dlen > maxMessageLength {
return ErrMaxLengthLimit
}
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, dlen)
if err != nil {
return err
}
err = binary.Write(buf, binary.BigEndian, message)
if err != nil {
return err
}
// TODO: make sure write exactly data
b := buf.Bytes()
c.cipher.Encrypt(b, b)
_, err = c.conn.Write(b)
return err
}
// TODO: LargeMessageConn for send large message