forked from nange/easyss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_state.go
278 lines (235 loc) · 5.54 KB
/
conn_state.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package easyss
import (
"io"
"net"
"sync"
"time"
"github.com/nange/easyss/cipherstream"
"github.com/nange/easyss/util"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type state int
const (
ESTABLISHED state = iota
FIN_WAIT1
FIN_WAIT2
LAST_ACK
CLOSING
CLOSE_WAIT
TIME_WAIT
CLOSED
)
var headerPool = sync.Pool{
New: func() interface{} {
header := make([]byte, 9)
return header
},
}
var stateMap = map[state]string{
ESTABLISHED: "state: ESTABLISHED",
FIN_WAIT1: "state: FIN_WAIT1",
FIN_WAIT2: "state: FIN_WAIT2",
LAST_ACK: "state: LAST_ACK",
CLOSING: "state: CLOSING",
CLOSE_WAIT: "state: CLOSE_WAIT",
TIME_WAIT: "state: TIME_WAIT",
CLOSED: "state: CLOSED",
}
func (s state) String() string {
if _, ok := stateMap[s]; ok {
return stateMap[s]
}
return "unknown state"
}
type ConnStateFn func(conn io.ReadWriteCloser) *ConnState
type ConnState struct {
fn ConnStateFn
state state
err error
buf []byte
}
func NewConnState(s state, buf []byte) *ConnState {
cs := &ConnState{
state: s,
buf: buf,
}
statefnMap := map[state]ConnStateFn{
FIN_WAIT1: cs.FINWait1,
FIN_WAIT2: cs.FINWait2,
LAST_ACK: cs.LastACK,
CLOSING: cs.Closing,
CLOSE_WAIT: cs.CloseWait,
TIME_WAIT: cs.TimeWait,
CLOSED: cs.Closed,
}
if statefn, ok := statefnMap[s]; ok {
cs.fn = statefn
}
return cs
}
func (cs *ConnState) FINWait1(conn io.ReadWriteCloser) *ConnState {
log.Info("start FINWait1 state")
defer log.Info("end FINWait1 state")
cs.state = FIN_WAIT1
header := headerPool.Get().([]byte)
defer headerPool.Put(header)
fin := util.EncodeFINRstStreamHeader(header)
_, err := conn.Write(fin)
if err != nil {
log.Errorf("conn.Write FIN err:%+v", errors.WithStack(err))
cs.err = err
cs.fn = nil
return cs
}
for {
_, err = conn.Read(cs.buf)
if err != nil {
break
}
}
if cipherstream.FINRSTStreamErr(err) {
cs.fn = cs.Closing
return cs
}
if cipherstream.ACKRSTStreamErr(err) {
cs.fn = cs.FINWait2
return cs
}
log.Errorf("except get ErrFINRSTStream or ErrACKRSTStream, but get: %v", err)
cs.err = err
cs.fn = nil
return cs
}
func (cs *ConnState) FINWait2(conn io.ReadWriteCloser) *ConnState {
log.Info("start FINWait2 state")
defer log.Info("end FINWait2 state")
cs.state = FIN_WAIT2
var err error
for {
_, err = conn.Read(cs.buf)
if err != nil {
break
}
}
if !cipherstream.FINRSTStreamErr(err) {
log.Errorf("except get ErrFINRSTStream, but get: %+v", err)
cs.err = err
cs.fn = nil
return cs
}
header := headerPool.Get().([]byte)
defer headerPool.Put(header)
ack := util.EncodeACKRstStreamHeader(header)
_, err = conn.Write(ack)
if err != nil {
log.Errorf("conn.Write ACK err:%+v", errors.WithStack(err))
cs.err = err
cs.fn = nil
return cs
}
cs.fn = cs.TimeWait
return cs
}
func (cs *ConnState) LastACK(conn io.ReadWriteCloser) *ConnState {
log.Info("start LastACK state")
defer log.Info("end LastACK state")
cs.state = LAST_ACK
header := headerPool.Get().([]byte)
defer headerPool.Put(header)
fin := util.EncodeFINRstStreamHeader(header)
_, err := conn.Write(fin)
if err != nil {
log.Errorf("conn.Write FIN err:%+v", errors.WithStack(err))
cs.err = err
cs.fn = nil
return cs
}
cs.fn = cs.Closed
return cs
}
func (cs *ConnState) Closing(conn io.ReadWriteCloser) *ConnState {
log.Info("start Closing state")
defer log.Info("end Closing state")
cs.state = CLOSING
header := headerPool.Get().([]byte)
defer headerPool.Put(header)
ack := util.EncodeACKRstStreamHeader(header)
_, err := conn.Write(ack)
if err != nil {
log.Errorf("conn.Write ACK err:%+v", errors.WithStack(err))
cs.err = err
cs.fn = nil
return cs
}
cs.fn = cs.Closed
return cs
}
func (cs *ConnState) CloseWait(conn io.ReadWriteCloser) *ConnState {
log.Info("start CloseWait state")
defer log.Info("end CloseWait state")
cs.state = CLOSE_WAIT
header := headerPool.Get().([]byte)
defer headerPool.Put(header)
ack := util.EncodeACKRstStreamHeader(header)
_, err := conn.Write(ack)
if err != nil {
log.Errorf("conn.Write ack err:%+v", errors.WithStack(err))
cs.err = err
cs.fn = nil
return cs
}
cs.fn = cs.LastACK
return cs
}
func (cs *ConnState) TimeWait(conn io.ReadWriteCloser) *ConnState {
log.Info("start TimeWait state")
defer log.Info("end TimeWait state")
cs.state = TIME_WAIT
log.Info("in our TimeWait state, we should end our state machine immediately")
// if conn is tcp connection, set the deadline to default
var err error
if cs, ok := conn.(*cipherstream.CipherStream); ok {
if c, ok := cs.ReadWriteCloser.(net.Conn); ok {
err = c.SetDeadline(time.Time{})
log.Info("set tcp connection deadline to default")
}
}
if err != nil {
log.Errorf("conn.SetDeadline to default err:%+v", errors.WithStack(err))
cs.err = err
}
cs.fn = nil
return cs
}
func (cs *ConnState) Closed(conn io.ReadWriteCloser) *ConnState {
log.Info("start Closed state")
defer log.Info("end Closed state")
cs.state = CLOSED
var err error
for {
_, err = conn.Read(cs.buf)
if err != nil {
break
}
}
if !cipherstream.ACKRSTStreamErr(err) {
log.Errorf("except get ErrACKRSTStream, but get: %+v", err)
cs.err = err
cs.fn = nil
return cs
}
// if conn is tcp connection, set the deadline to default
if cs, ok := conn.(*cipherstream.CipherStream); ok {
if c, ok := cs.ReadWriteCloser.(net.Conn); ok {
err = c.SetDeadline(time.Time{})
log.Info("set tcp connection deadline to default")
}
}
if err != nil {
log.Errorf("conn.SetDeadline to default err:%+v", errors.WithStack(err))
cs.err = err
}
cs.fn = nil
return cs
}