forked from nanomsg/mangos-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wss.go
347 lines (289 loc) · 7.35 KB
/
wss.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
// Copyright 2015 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 an simple websocket transport for mangos.
// This transport is considered EXPERIMENTAL.
package wss
import (
"crypto/tls"
"errors"
"golang.org/x/net/websocket"
"net"
"net/http"
"net/url"
"github.com/gdamore/mangos"
"sync"
)
var (
ErrWSSNoConfig = errors.New("missing WSS/TLS config")
ErrWSSNoCert = errors.New("missing WSS/TLS certificate")
)
type options map[string]interface{}
func (o options) get(name string) (interface{}, error) {
if v, ok := o[name]; ok {
return v, nil
}
return nil, mangos.ErrBadOption
}
func (o options) set(name string, val interface{}) error {
switch name {
case mangos.OptionTLSConfig:
switch v := val.(type) {
case *tls.Config:
// Make a private copy.
cfg := *v
// TLS versions prior to 1.2 were *insecure*
cfg.MinVersion = tls.VersionTLS12
cfg.MaxVersion = tls.VersionTLS12
o[name] = &cfg
return nil
default:
return mangos.ErrBadValue
}
}
return mangos.ErrBadOption
}
// wsPipe implements the Pipe interface on a websocket
type wssPipe struct {
ws *websocket.Conn
rlock sync.Mutex
wlock sync.Mutex
proto mangos.Protocol
addr string
open bool
wg sync.WaitGroup
props map[string]interface{}
}
type wssTran int
func (w *wssPipe) Recv() (*mangos.Message, error) {
var buf []byte
// prevent interleaved reads
w.rlock.Lock()
defer w.rlock.Unlock()
if err := websocket.Message.Receive(w.ws, &buf); err != nil {
return nil, err
}
msg := mangos.NewMessage(len(buf))
// This is kind of suboptimal copying...
msg.Body = append(msg.Body, buf...)
return msg, nil
}
func (w *wssPipe) Send(m *mangos.Message) error {
var buf []byte
w.wlock.Lock()
defer w.wlock.Unlock()
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 := websocket.Message.Send(w.ws, buf); err != nil {
return err
}
m.Free()
return nil
}
func (w *wssPipe) LocalProtocol() uint16 {
return w.proto.Number()
}
func (w *wssPipe) RemoteProtocol() uint16 {
return w.proto.PeerNumber()
}
func (w *wssPipe) Close() error {
w.open = false
w.ws.Close()
w.wg.Done()
return nil
}
func (w *wssPipe) IsOpen() bool {
return w.open
}
func (w *wssPipe) 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
origin string
opts options
}
func (d *dialer) GetOption(n string) (interface{}, error) {
return d.opts.get(n)
}
func (d *dialer) SetOption(n string, v interface{}) error {
return d.opts.set(n, v)
}
func (d *dialer) Dial() (mangos.Pipe, error) {
pname := d.proto.PeerName() + ".sp.nanomsg.org"
// We have to supply an origin because Go's websocket
// implementation seems to require it. We fake a garbage one.
// Perhaps we should allow applications to fake this out.
d.origin = "x://"
config, err := websocket.NewConfig(d.addr, d.origin)
if err != nil {
return nil, err
}
if v, ok := d.opts[mangos.OptionTLSConfig]; ok {
config.TlsConfig = v.(*tls.Config)
}
config.Protocol = append([]string{}, pname)
ws, err := websocket.DialConfig(config)
if err != nil {
return nil, err
}
w := &wssPipe{ws: ws, proto: d.proto, addr: d.addr, open: true, props: make(map[string]interface{})}
w.wg.Add(1)
return w, nil
}
type listener struct {
pending []*wssPipe
lock sync.Mutex
cv sync.Cond
running bool
addr string
wssvr websocket.Server
htsvr *http.Server
url_ *url.URL
listener net.Listener
proto mangos.Protocol
opts options
}
func (l *listener) GetOption(n string) (interface{}, error) {
return l.opts.get(n)
}
func (l *listener) SetOption(n string, v interface{}) error {
return l.opts.set(n, v)
}
func (l *listener) Listen() error {
// We listen separately, that way we can catch and deal with the
// case of a port already in use.
v, ok := l.opts[mangos.OptionTLSConfig]
if !ok || v == nil {
return ErrWSSNoConfig
}
tcfg := v.(*tls.Config)
if tcfg.Certificates == nil || len(tcfg.Certificates) == 0 {
return ErrWSSNoCert
}
var taddr *net.TCPAddr
var err error
if taddr, err = net.ResolveTCPAddr("tcp", l.url_.Host); err != nil {
return err
}
if tlist, err := net.ListenTCP("tcp", taddr); err != nil {
return err
} else {
l.listener = tls.NewListener(tlist, tcfg)
}
l.pending = nil
l.running = true
l.wssvr.Config.TlsConfig = tcfg
l.wssvr.Handler = l.handler
l.wssvr.Handshake = l.handshake
l.htsvr = &http.Server{Addr: l.url_.Host, Handler: l.wssvr}
go l.htsvr.Serve(l.listener)
return nil
}
func (l *listener) Accept() (mangos.Pipe, error) {
var w *wssPipe
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) {
l.lock.Lock()
if !l.running {
ws.Close()
l.lock.Unlock()
return
}
w := &wssPipe{ws: ws, addr: l.addr, proto: l.proto, open: true, props: make(map[string]interface{})}
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) handshake(c *websocket.Config, _ *http.Request) error {
pname := l.proto.Name() + ".sp.nanomsg.org"
for _, p := range c.Protocol {
if p == pname {
c.Protocol = append([]string{}, p)
return nil
}
}
return websocket.ErrBadWebSocketProtocol
}
func (l *listener) Close() error {
l.lock.Lock()
defer l.lock.Unlock()
if !l.running {
return mangos.ErrClosed
}
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) {
l.wssvr.ServeHTTP(w, r)
}
func (wssTran) Scheme() string {
return "wss"
}
func (wssTran) NewDialer(addr string, proto mangos.Protocol) (mangos.PipeDialer, error) {
return &dialer{addr: addr, proto: proto, opts: make(map[string]interface{})}, nil
}
func (wssTran) NewListener(addr string, proto mangos.Protocol) (mangos.PipeListener, error) {
var err error
l := &listener{addr: addr, proto: proto, opts: make(map[string]interface{})}
l.cv.L = &l.lock
l.url_, err = url.ParseRequestURI(addr)
if err != nil {
return nil, err
}
return l, nil
}
func (wssTran) SetOption(name string, val interface{}) error {
return mangos.ErrBadOption
}
func (wssTran) GetOption(name string) (interface{}, error) {
return nil, mangos.ErrBadOption
}
// NewTransport allocates a new wss:// transport.
func NewTransport() mangos.Transport {
return wssTran(0)
}