-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_websocket.go
429 lines (419 loc) · 11 KB
/
server_websocket.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package comet
import (
"context"
"crypto/tls"
"io"
"net"
"strings"
"time"
"github.com/youminxue/euler/api/comet/grpc"
"github.com/youminxue/euler/internal/comet/conf"
"github.com/youminxue/euler/pkg/bytes"
xtime "github.com/youminxue/euler/pkg/time"
"github.com/youminxue/euler/pkg/websocket"
log "github.com/golang/glog"
)
// InitWebsocket listen all tcp.bind and start accept connections.
func InitWebsocket(server *Server, addrs []string, accept int) (err error) {
var (
bind string
listener *net.TCPListener
addr *net.TCPAddr
)
for _, bind = range addrs {
if addr, err = net.ResolveTCPAddr("tcp", bind); err != nil {
log.Errorf("net.ResolveTCPAddr(tcp, %s) error(%v)", bind, err)
return
}
if listener, err = net.ListenTCP("tcp", addr); err != nil {
log.Errorf("net.ListenTCP(tcp, %s) error(%v)", bind, err)
return
}
log.Infof("start ws listen: %s", bind)
// split N core accept
for i := 0; i < accept; i++ {
go acceptWebsocket(server, listener)
}
}
return
}
// InitWebsocketWithTLS init websocket with tls.
func InitWebsocketWithTLS(server *Server, addrs []string, certFile, privateFile string, accept int) (err error) {
var (
bind string
listener net.Listener
cert tls.Certificate
certs []tls.Certificate
)
certFiles := strings.Split(certFile, ",")
privateFiles := strings.Split(privateFile, ",")
for i := range certFiles {
cert, err = tls.LoadX509KeyPair(certFiles[i], privateFiles[i])
if err != nil {
log.Errorf("Error loading certificate. error(%v)", err)
return
}
certs = append(certs, cert)
}
tlsCfg := &tls.Config{Certificates: certs}
tlsCfg.BuildNameToCertificate()
for _, bind = range addrs {
if listener, err = tls.Listen("tcp", bind, tlsCfg); err != nil {
log.Errorf("net.ListenTCP(tcp, %s) error(%v)", bind, err)
return
}
log.Infof("start wss listen: %s", bind)
// split N core accept
for i := 0; i < accept; i++ {
go acceptWebsocketWithTLS(server, listener)
}
}
return
}
// Accept accepts connections on the listener and serves requests
// for each incoming connection. Accept blocks; the caller typically
// invokes it in a go statement.
func acceptWebsocket(server *Server, lis *net.TCPListener) {
var (
conn *net.TCPConn
err error
r int
)
for {
if conn, err = lis.AcceptTCP(); err != nil {
// if listener close then return
log.Errorf("listener.Accept(%s) error(%v)", lis.Addr().String(), err)
return
}
if err = conn.SetKeepAlive(server.c.TCP.KeepAlive); err != nil {
log.Errorf("conn.SetKeepAlive() error(%v)", err)
return
}
if err = conn.SetReadBuffer(server.c.TCP.Rcvbuf); err != nil {
log.Errorf("conn.SetReadBuffer() error(%v)", err)
return
}
if err = conn.SetWriteBuffer(server.c.TCP.Sndbuf); err != nil {
log.Errorf("conn.SetWriteBuffer() error(%v)", err)
return
}
go serveWebsocket(server, conn, r)
if r++; r == maxInt {
r = 0
}
}
}
// Accept accepts connections on the listener and serves requests
// for each incoming connection. Accept blocks; the caller typically
// invokes it in a go statement.
func acceptWebsocketWithTLS(server *Server, lis net.Listener) {
var (
conn net.Conn
err error
r int
)
for {
if conn, err = lis.Accept(); err != nil {
// if listener close then return
log.Errorf("listener.Accept(\"%s\") error(%v)", lis.Addr().String(), err)
return
}
go serveWebsocket(server, conn, r)
if r++; r == maxInt {
r = 0
}
}
}
func serveWebsocket(s *Server, conn net.Conn, r int) {
var (
// timer
tr = s.round.Timer(r)
rp = s.round.Reader(r)
wp = s.round.Writer(r)
)
if conf.Conf.Debug {
// ip addr
lAddr := conn.LocalAddr().String()
rAddr := conn.RemoteAddr().String()
log.Infof("start tcp serve \"%s\" with \"%s\"", lAddr, rAddr)
}
s.ServeWebsocket(conn, rp, wp, tr)
}
// ServeWebsocket serve a websocket connection.
func (s *Server) ServeWebsocket(conn net.Conn, rp, wp *bytes.Pool, tr *xtime.Timer) {
var (
err error
rid string
accepts []int32
hb time.Duration
white bool
p *grpc.Proto
b *Bucket
trd *xtime.TimerData
lastHB = time.Now()
rb = rp.Get()
ch = NewChannel(s.c.Protocol.CliProto, s.c.Protocol.SvrProto)
rr = &ch.Reader
wr = &ch.Writer
ws *websocket.Conn // websocket
req *websocket.Request
)
// reader
ch.Reader.ResetBuffer(conn, rb.Bytes())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// handshake
step := 0
trd = tr.Add(time.Duration(s.c.Protocol.HandshakeTimeout), func() {
// NOTE: fix close block for tls
_ = conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
_ = conn.Close()
log.Errorf("key: %s remoteIP: %s step: %d ws handshake timeout", ch.Key, conn.RemoteAddr().String(), step)
})
// websocket
ch.IP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
step = 1
if req, err = websocket.ReadRequest(rr); err != nil || req.RequestURI != "/sub" {
conn.Close()
tr.Del(trd)
rp.Put(rb)
if err != io.EOF {
log.Errorf("http.ReadRequest(rr) error(%v)", err)
}
return
}
// writer
wb := wp.Get()
ch.Writer.ResetBuffer(conn, wb.Bytes())
step = 2
if ws, err = websocket.Upgrade(conn, rr, wr, req); err != nil {
conn.Close()
tr.Del(trd)
rp.Put(rb)
wp.Put(wb)
if err != io.EOF {
log.Errorf("websocket.NewServerConn error(%v)", err)
}
return
}
// must not setadv, only used in auth
step = 3
if p, err = ch.CliProto.Set(); err == nil {
if ch.Mid, ch.Key, rid, accepts, hb, err = s.authWebsocket(ctx, ws, p, req.Header.Get("Cookie")); err == nil {
ch.Watch(accepts...)
b = s.Bucket(ch.Key)
err = b.Put(rid, ch)
if conf.Conf.Debug {
log.Infof("websocket connnected key:%s mid:%d proto:%+v", ch.Key, ch.Mid, p)
}
}
}
step = 4
if err != nil {
ws.Close()
rp.Put(rb)
wp.Put(wb)
tr.Del(trd)
if err != io.EOF && err != websocket.ErrMessageClose {
log.Errorf("key: %s remoteIP: %s step: %d ws handshake failed error(%v)", ch.Key, conn.RemoteAddr().String(), step, err)
}
return
}
trd.Key = ch.Key
tr.Set(trd, hb)
white = whitelist.Contains(ch.Mid)
if white {
whitelist.Printf("key: %s[%s] auth\n", ch.Key, rid)
}
// hanshake ok start dispatch goroutine
step = 5
go s.dispatchWebsocket(ws, wp, wb, ch)
serverHeartbeat := s.RandServerHearbeat()
for {
if p, err = ch.CliProto.Set(); err != nil {
break
}
if white {
whitelist.Printf("key: %s start read proto\n", ch.Key)
}
if err = p.ReadWebsocket(ws); err != nil {
break
}
if white {
whitelist.Printf("key: %s read proto:%v\n", ch.Key, p)
}
if p.Op == grpc.OpHeartbeat {
tr.Set(trd, hb)
p.Op = grpc.OpHeartbeatReply
p.Body = nil
// NOTE: send server heartbeat for a long time
if now := time.Now(); now.Sub(lastHB) > serverHeartbeat {
if err1 := s.Heartbeat(ctx, ch.Mid, ch.Key); err1 == nil {
lastHB = now
}
}
if conf.Conf.Debug {
log.Infof("websocket heartbeat receive key:%s, mid:%d", ch.Key, ch.Mid)
}
step++
} else {
if err = s.Operate(ctx, p, ch, b); err != nil {
break
}
}
if white {
whitelist.Printf("key: %s process proto:%v\n", ch.Key, p)
}
ch.CliProto.SetAdv()
ch.Signal()
if white {
whitelist.Printf("key: %s signal\n", ch.Key)
}
}
if white {
whitelist.Printf("key: %s server tcp error(%v)\n", ch.Key, err)
}
if err != nil && err != io.EOF && err != websocket.ErrMessageClose && !strings.Contains(err.Error(), "closed") {
log.Errorf("key: %s server ws failed error(%v)", ch.Key, err)
}
b.Del(ch)
tr.Del(trd)
ws.Close()
ch.Close()
rp.Put(rb)
if err = s.Disconnect(ctx, ch.Mid, ch.Key); err != nil {
log.Errorf("key: %s operator do disconnect error(%v)", ch.Key, err)
}
if white {
whitelist.Printf("key: %s disconnect error(%v)\n", ch.Key, err)
}
if conf.Conf.Debug {
log.Infof("websocket disconnected key: %s mid:%d", ch.Key, ch.Mid)
}
}
// dispatch accepts connections on the listener and serves requests
// for each incoming connection. dispatch blocks; the caller typically
// invokes it in a go statement.
func (s *Server) dispatchWebsocket(ws *websocket.Conn, wp *bytes.Pool, wb *bytes.Buffer, ch *Channel) {
var (
err error
finish bool
online int32
white = whitelist.Contains(ch.Mid)
)
if conf.Conf.Debug {
log.Infof("key: %s start dispatch tcp goroutine", ch.Key)
}
for {
if white {
whitelist.Printf("key: %s wait proto ready\n", ch.Key)
}
var p = ch.Ready()
if white {
whitelist.Printf("key: %s proto ready\n", ch.Key)
}
if conf.Conf.Debug {
log.Infof("key:%s dispatch msg:%s", ch.Key, p.Body)
}
switch p {
case grpc.ProtoFinish:
if white {
whitelist.Printf("key: %s receive proto finish\n", ch.Key)
}
if conf.Conf.Debug {
log.Infof("key: %s wakeup exit dispatch goroutine", ch.Key)
}
finish = true
goto failed
case grpc.ProtoReady:
// fetch message from svrbox(client send)
for {
if p, err = ch.CliProto.Get(); err != nil {
break
}
if white {
whitelist.Printf("key: %s start write client proto%v\n", ch.Key, p)
}
if p.Op == grpc.OpHeartbeatReply {
if ch.Room != nil {
online = ch.Room.OnlineNum()
}
if err = p.WriteWebsocketHeart(ws, online); err != nil {
goto failed
}
} else {
if err = p.WriteWebsocket(ws); err != nil {
goto failed
}
}
if white {
whitelist.Printf("key: %s write client proto%v\n", ch.Key, p)
}
p.Body = nil // avoid memory leak
ch.CliProto.GetAdv()
}
default:
if white {
whitelist.Printf("key: %s start write server proto%v\n", ch.Key, p)
}
if err = p.WriteWebsocket(ws); err != nil {
goto failed
}
if white {
whitelist.Printf("key: %s write server proto%v\n", ch.Key, p)
}
if conf.Conf.Debug {
log.Infof("websocket sent a message key:%s mid:%d proto:%+v", ch.Key, ch.Mid, p)
}
}
if white {
whitelist.Printf("key: %s start flush \n", ch.Key)
}
// only hungry flush response
if err = ws.Flush(); err != nil {
break
}
if white {
whitelist.Printf("key: %s flush\n", ch.Key)
}
}
failed:
if white {
whitelist.Printf("key: %s dispatch tcp error(%v)\n", ch.Key, err)
}
if err != nil && err != io.EOF && err != websocket.ErrMessageClose {
log.Errorf("key: %s dispatch ws error(%v)", ch.Key, err)
}
ws.Close()
wp.Put(wb)
// must ensure all channel message discard, for reader won't blocking Signal
for !finish {
finish = (ch.Ready() == grpc.ProtoFinish)
}
if conf.Conf.Debug {
log.Infof("key: %s dispatch goroutine exit", ch.Key)
}
}
// auth for goim handshake with client, use rsa & aes.
func (s *Server) authWebsocket(ctx context.Context, ws *websocket.Conn, p *grpc.Proto, cookie string) (mid int64, key, rid string, accepts []int32, hb time.Duration, err error) {
for {
if err = p.ReadWebsocket(ws); err != nil {
return
}
if p.Op == grpc.OpAuth {
break
} else {
log.Errorf("ws request operation(%d) not auth", p.Op)
}
}
if mid, key, rid, accepts, hb, err = s.Connect(ctx, p, cookie); err != nil {
return
}
p.Op = grpc.OpAuthReply
p.Body = nil
if err = p.WriteWebsocket(ws); err != nil {
return
}
err = ws.Flush()
return
}