forked from nanomsg/mangos-v1
/
ws.go
502 lines (432 loc) · 11.7 KB
/
ws.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright 2018 The Mangos Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package ws implements a simple WebSocket transport for mangos.
// This transport is considered EXPERIMENTAL.
package ws
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"strings"
"sync"
"github.com/gorilla/websocket"
"nanomsg.org/go-mangos"
)
// Some special options
const (
// OptionWebSocketMux is a retrieve-only property used to obtain
// the *http.ServeMux instance associated with the server. This
// can be used to subsequently register additional handlers for
// different URIs. This option is only valid on a Listener.
// Generally you use this option when you want to use the standard
// mangos Listen() method to start up the server.
OptionWebSocketMux = "WEBSOCKET-MUX"
// OptionWebSocketHandler is used to obtain the underlying
// http.Handler (websocket.Server) object, so you can use this
// on your own http.Server instances. It is a gross error to use
// the value returned by this method on an http server if the
// server is also started with mangos Listen(). This means that you
// will use at most either this option, or OptionWebSocketMux, but
// never both. This option is only valid on a listener.
OptionWebSocketHandler = "WEBSOCKET-HANDLER"
// OptionWebSocketCheckOrigin controls the check of the origin of the
// underlying Listener (websocket.Upgrader).
// Excerpt from https://godoc.org/github.com/gorilla/websocket:
// Web browsers allow Javascript applications to open a WebSocket
// connection to any host. It's up to the server to enforce an origin
// policy using the Origin request header sent by the browser. The
// Upgrader calls the function specified in the CheckOrigin field to
// check the origin. If the CheckOrigin function returns false, then
// the Upgrade method fails the WebSocket handshake with HTTP status
// 403. If the CheckOrigin field is nil, then the Upgrader uses a safe
// default: fail the handshake if the Origin request header is present
// and not equal to the Host request header. An application can allow
// connections from any origin by specifying a function that always
// returns true:
//
// var upgrader = websocket.Upgrader{
// CheckOrigin: func(r *http.Request) bool { return true },
// }
//
// The deprecated Upgrade function does not enforce an origin policy.
// It's the application's responsibility to check the Origin header
// before calling Upgrade.
OptionWebSocketCheckOrigin = "WEBSOCKET-CHECKORIGIN"
)
type options map[string]interface{}
// GetOption retrieves an option value.
func (o options) get(name string) (interface{}, error) {
if o == nil {
return nil, mangos.ErrBadOption
}
v, ok := o[name]
if !ok {
return nil, mangos.ErrBadOption
}
return v, nil
}
// SetOption sets an option. We have none, so just ErrBadOption.
func (o options) set(name string, val interface{}) error {
switch name {
case mangos.OptionNoDelay:
fallthrough
case mangos.OptionKeepAlive:
switch v := val.(type) {
case bool:
o[name] = v
return nil
default:
return mangos.ErrBadValue
}
case mangos.OptionTLSConfig:
switch v := val.(type) {
case *tls.Config:
o[name] = v
return nil
default:
return mangos.ErrBadValue
}
case OptionWebSocketCheckOrigin:
switch v := val.(type) {
case bool:
o[name] = v
return nil
default:
return mangos.ErrBadValue
}
}
return mangos.ErrBadOption
}
// wsPipe implements the Pipe interface on a websocket
type wsPipe struct {
ws *websocket.Conn
proto mangos.Protocol
addr string
open bool
wg sync.WaitGroup
props map[string]interface{}
iswss bool
dtype int
sync.Mutex
}
type wsTran int
func (w *wsPipe) Recv() (*mangos.Message, error) {
// We ignore the message type for receive.
_, body, err := w.ws.ReadMessage()
if err != nil {
return nil, err
}
msg := mangos.NewMessage(0)
msg.Body = body
return msg, nil
}
func (w *wsPipe) Send(m *mangos.Message) error {
var buf []byte
if m.Expired() {
m.Free()
return nil
}
if len(m.Header) > 0 {
buf = make([]byte, 0, len(m.Header)+len(m.Body))
buf = append(buf, m.Header...)
buf = append(buf, m.Body...)
} else {
buf = m.Body
}
if err := w.ws.WriteMessage(w.dtype, buf); err != nil {
return err
}
m.Free()
return nil
}
func (w *wsPipe) LocalProtocol() uint16 {
return w.proto.Number()
}
func (w *wsPipe) RemoteProtocol() uint16 {
return w.proto.PeerNumber()
}
func (w *wsPipe) Close() error {
w.Lock()
defer w.Unlock()
if w.IsOpen() {
w.open = false
w.ws.Close()
w.wg.Done()
}
return nil
}
func (w *wsPipe) IsOpen() bool {
return w.open
}
func (w *wsPipe) GetProp(name string) (interface{}, error) {
if v, ok := w.props[name]; ok {
return v, nil
}
return nil, mangos.ErrBadProperty
}
type dialer struct {
addr string // url
proto mangos.Protocol
opts options
iswss bool
maxrx int
}
func (d *dialer) Dial() (mangos.Pipe, error) {
var w *wsPipe
wd := &websocket.Dialer{}
wd.Subprotocols = []string{d.proto.PeerName() + ".sp.nanomsg.org"}
if v, ok := d.opts[mangos.OptionTLSConfig]; ok {
wd.TLSClientConfig = v.(*tls.Config)
}
w = &wsPipe{proto: d.proto, addr: d.addr, open: true}
w.dtype = websocket.BinaryMessage
w.props = make(map[string]interface{})
var err error
if w.ws, _, err = wd.Dial(d.addr, nil); err != nil {
return nil, err
}
w.ws.SetReadLimit(int64(d.maxrx))
w.props[mangos.PropLocalAddr] = w.ws.LocalAddr()
w.props[mangos.PropRemoteAddr] = w.ws.RemoteAddr()
if tlsConn, ok := w.ws.UnderlyingConn().(*tls.Conn); ok {
w.props[mangos.PropTLSConnState] = tlsConn.ConnectionState()
}
w.wg.Add(1)
return w, nil
}
func (d *dialer) SetOption(n string, v interface{}) error {
return d.opts.set(n, v)
}
func (d *dialer) GetOption(n string) (interface{}, error) {
return d.opts.get(n)
}
type listener struct {
pending []*wsPipe
lock sync.Mutex
cv sync.Cond
running bool
noserve bool
addr string
ug websocket.Upgrader
htsvr *http.Server
mux *http.ServeMux
url *url.URL
listener net.Listener
proto mangos.Protocol
opts options
iswss bool
maxrx int
}
func (l *listener) SetOption(n string, v interface{}) error {
switch n {
case OptionWebSocketCheckOrigin:
if v, ok := v.(bool); ok {
if !v {
l.ug.CheckOrigin = func(r *http.Request) bool { return true }
}
}
}
return l.opts.set(n, v)
}
func (l *listener) GetOption(n string) (interface{}, error) {
switch n {
case OptionWebSocketMux:
return l.mux, nil
case OptionWebSocketHandler:
// Caller intends to use use in his own server, so mark
// us running. If he didn't mean this, the side effect is
// that Accept() will appear to hang, even though Listen()
// is not called yet.
l.running = true
l.noserve = true
return l, nil
case OptionWebSocketCheckOrigin:
if v, err := l.opts.get(n); err == nil {
if v, ok := v.(bool); ok {
return v, nil
}
}
return true, nil
}
return l.opts.get(n)
}
func (l *listener) Listen() error {
var taddr *net.TCPAddr
var err error
var tcfg *tls.Config
if l.noserve {
// The HTTP framework is going to call us, so we use that rather than
// listening on our own. We just fake this out.
return nil
}
if l.iswss {
v, ok := l.opts[mangos.OptionTLSConfig]
if !ok || v == nil {
return mangos.ErrTLSNoConfig
}
tcfg = v.(*tls.Config)
if tcfg.Certificates == nil || len(tcfg.Certificates) == 0 {
return mangos.ErrTLSNoCert
}
}
// We listen separately, that way we can catch and deal with the
// case of a port already in use. This also lets us configure
// properties of the underlying TCP connection.
if taddr, err = mangos.ResolveTCPAddr(l.url.Host); err != nil {
return err
}
if tlist, err := net.ListenTCP("tcp", taddr); err != nil {
return err
} else if l.iswss {
l.listener = tls.NewListener(tlist, tcfg)
} else {
l.listener = tlist
}
l.pending = nil
l.running = true
l.htsvr = &http.Server{Addr: l.url.Host, Handler: l.mux}
go l.htsvr.Serve(l.listener)
return nil
}
func (l *listener) Accept() (mangos.Pipe, error) {
var w *wsPipe
l.lock.Lock()
defer l.lock.Unlock()
for {
if !l.running {
return nil, mangos.ErrClosed
}
if len(l.pending) == 0 {
l.cv.Wait()
continue
}
w = l.pending[len(l.pending)-1]
l.pending = l.pending[:len(l.pending)-1]
break
}
return w, nil
}
func (l *listener) handler(ws *websocket.Conn, req *http.Request) {
l.lock.Lock()
if !l.running {
ws.Close()
l.lock.Unlock()
return
}
if ws.Subprotocol() != l.proto.Name()+".sp.nanomsg.org" {
ws.Close()
l.lock.Unlock()
return
}
w := &wsPipe{ws: ws, addr: l.addr, proto: l.proto, open: true}
w.dtype = websocket.BinaryMessage
w.iswss = l.iswss
w.ws.SetReadLimit(int64(l.maxrx))
w.props = make(map[string]interface{})
w.props[mangos.PropLocalAddr] = ws.LocalAddr()
w.props[mangos.PropRemoteAddr] = ws.RemoteAddr()
if req.TLS != nil {
w.props[mangos.PropTLSConnState] = *req.TLS
}
w.wg.Add(1)
l.pending = append(l.pending, w)
l.cv.Broadcast()
l.lock.Unlock()
// We must not return before the socket is closed, because
// our caller will close the websocket on our return.
w.wg.Wait()
}
func (l *listener) Handle(pattern string, handler http.Handler) {
l.mux.Handle(pattern, handler)
}
func (l *listener) HandleFunc(pattern string, handler http.HandlerFunc) {
l.mux.HandleFunc(pattern, handler)
}
func (l *listener) Close() error {
l.lock.Lock()
defer l.lock.Unlock()
if !l.running {
return mangos.ErrClosed
}
if l.listener != nil {
l.listener.Close()
}
l.running = false
l.cv.Broadcast()
for _, ws := range l.pending {
ws.Close()
}
l.pending = nil
return nil
}
func (l *listener) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ws, err := l.ug.Upgrade(w, r, nil)
if err != nil {
return
}
l.handler(ws, r)
}
func (l *listener) Address() string {
return l.url.String()
}
func (wsTran) Scheme() string {
return "ws"
}
func (wsTran) NewDialer(addr string, sock mangos.Socket) (mangos.PipeDialer, error) {
iswss := strings.HasPrefix(addr, "wss://")
opts := make(map[string]interface{})
opts[mangos.OptionNoDelay] = true
opts[mangos.OptionKeepAlive] = true
proto := sock.GetProtocol()
maxrx := 0
if v, e := sock.GetOption(mangos.OptionMaxRecvSize); e == nil {
maxrx = v.(int)
}
return &dialer{addr: addr, proto: proto, iswss: iswss, opts: opts, maxrx: maxrx}, nil
}
func (t wsTran) NewListener(addr string, sock mangos.Socket) (mangos.PipeListener, error) {
proto := sock.GetProtocol()
l, e := t.listener(addr, proto)
if e == nil {
if v, e := sock.GetOption(mangos.OptionMaxRecvSize); e == nil {
l.maxrx = v.(int)
}
l.mux.Handle(l.url.Path, l)
}
return l, e
}
func (wsTran) listener(addr string, proto mangos.Protocol) (*listener, error) {
var err error
l := &listener{proto: proto, opts: make(map[string]interface{})}
l.cv.L = &l.lock
l.ug.Subprotocols = []string{proto.Name() + ".sp.nanomsg.org"}
if strings.HasPrefix(addr, "wss://") {
l.iswss = true
}
l.url, err = url.ParseRequestURI(addr)
if err != nil {
return nil, err
}
if len(l.url.Path) == 0 {
l.url.Path = "/"
}
l.mux = http.NewServeMux()
l.htsvr = &http.Server{Addr: l.url.Host, Handler: l.mux}
return l, nil
}
// NewTransport allocates a new ws:// transport.
func NewTransport() mangos.Transport {
return wsTran(0)
}