/
client.go
597 lines (491 loc) · 13.5 KB
/
client.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Package signalr provides a simple interface for sending and receiving messages via the Azure SignalR service.
// For more information about the Azure SignalR service see https://aka.ms/signalr-go.
package signalr
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/google/uuid"
"nhooyr.io/websocket"
)
type (
// Client represents a bidirectional connection to Azure SignalR
Client struct {
name string
hubName string
audType audienceType
parsedConnStr *ParsedConnString
nMutex sync.RWMutex
negotiateRes *negotiateResponse
}
// ClientOption provides a way to configure a client at time of construction
ClientOption func(*Client) error
// NegotiateResponse is the structure to respond to a client for access to a hub resource
negotiateResponse struct {
ConnectionID string `json:"connectionId,omitempty"`
AvailableTransports []transport `json:"availableTransports"`
}
transport struct {
Name string `json:"transport,omitempty"`
Formats []string `json:"transportFormats,omitempty"`
}
audienceType string
transportTypes string
signalrCliams struct {
jwt.StandardClaims
NameID string `json:"nameid,omitempty"`
}
handshakeRequest struct {
Protocol string `json:"protocol"`
Version int `json:"version"`
}
handshakeResponse struct {
Error string `json:"error,omitempty"`
}
messageType int
// InvocationMessage is the structure expected for sending and receiving in the SignalR protocol
InvocationMessage struct {
Type messageType `json:"type,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
InvocationID string `json:"invocationId,omitempty"`
Target string `json:"target"`
Arguments []json.RawMessage `json:"arguments"`
Error string `json:"error,omitempty"`
}
)
const (
messageTerminator byte = 0x1E
invocationMessageType messageType = iota
streamItemMessageType
completionMessageType
streamInvocationMessageType
cancelInvocationMessageType
pingMessageType
closeMessageType
)
var (
serverAudienceType audienceType = "server"
clientAudienceType audienceType = "client"
websocketTransportType transportTypes = "WebSockets"
)
// ClientWithName configures a SignalR client to use a specific name for addressing the client individually
//
// If a client name is provided, a random name will be assigned to the client and can be accessed via `client.GetName()`
func ClientWithName(name string) ClientOption {
return func(client *Client) error {
client.name = name
return nil
}
}
// NewClient constructs a new client given a set of construction options
func NewClient(connStr string, hubName string, opts ...ClientOption) (*Client, error) {
parsed, err := ParseConnectionString(connStr)
if err != nil {
return nil, err
}
client := &Client{
hubName: hubName,
parsedConnStr: parsed,
audType: clientAudienceType,
name: uuid.Must(uuid.NewRandom()).String(),
}
for _, opt := range opts {
if err := opt(client); err != nil {
return client, err
}
}
return client, nil
}
// Listen will start the WebSocket connection for the client
func (c *Client) Listen(ctx context.Context, handler Handler) error {
err := c.negotiateOnce(ctx)
if err != nil {
return err
}
audience := c.getWssAudience()
token, err := c.generateToken(audience, 2*time.Hour)
if err != nil {
return err
}
conn, resp, err := websocket.Dial(ctx, c.getWssURI(), websocket.DialOptions{
HTTPHeader: http.Header{
"Authorization": []string{"Bearer " + token},
},
HTTPClient: newHTTPClient(),
})
if resp != nil {
defer func() {
_ = resp.Body.Close()
}()
}
if err != nil {
return err
}
err = c.handshake(ctx, conn)
if err != nil {
return err
}
select {
case <-ctx.Done():
default:
if h, ok := handler.(NotifiedHandler); ok {
h.OnStart()
}
for {
bits, err := readConn(ctx, conn)
if err != nil {
if err.Error() == "failed to get reader: context canceled" {
return nil
}
return err
}
var msg InvocationMessage
err = json.Unmarshal(bits, &msg)
if err != nil {
return err
}
//fmt.Println(string(bits))
switch msg.Type {
case pingMessageType:
// nop
case invocationMessageType:
return dispatch(ctx, handler, &msg)
case streamInvocationMessageType, streamItemMessageType, cancelInvocationMessageType, completionMessageType:
return errors.New("unhandled InvocationMessage type: " + string(msg.Type))
case closeMessageType:
return conn.Close(websocket.StatusNormalClosure, "received close message from SignalR service")
}
}
}
return nil
}
// BroadcastAll will send a broadcast `InvocationMessage` to all listening to the hub
func (c *Client) BroadcastAll(ctx context.Context, msg *InvocationMessage) error {
return c.SendInvocation(ctx, c.getBroadcastURI(), msg)
}
// BroadcastGroup will send a broadcast `InvocationMessage` to all listening to the hub group
func (c *Client) BroadcastGroup(ctx context.Context, msg *InvocationMessage, groupName string) error {
return c.SendInvocation(ctx, c.getSendToGroupURI(groupName), msg)
}
// SendToUser will send a `InvocationMessage` to a particular user
func (c *Client) SendToUser(ctx context.Context, msg *InvocationMessage, userID string) error {
return c.SendInvocation(ctx, c.getSendToUserURI(userID), msg)
}
// AddUserToGroup will add a userID to a SignalR group
func (c *Client) AddUserToGroup(ctx context.Context, groupName string, userID string) error {
req, err := http.NewRequest(http.MethodPut, c.getGroupUserURI(groupName, userID), nil)
if err != nil {
return err
}
res, err := c.do(ctx, req)
defer closeRes(res)
if err != nil {
return err
}
bodyBits, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode > 399 {
return SendFailureError{
StatusCode: res.StatusCode,
Body: string(bodyBits),
}
}
return nil
}
// RemoveUserFromGroup will remove a userID from a SignalR group
func (c *Client) RemoveUserFromGroup(ctx context.Context, groupName string, userID string) error {
req, err := http.NewRequest(http.MethodDelete, c.getGroupUserURI(groupName, userID), nil)
if err != nil {
return err
}
res, err := c.do(ctx, req)
defer closeRes(res)
if err != nil {
return err
}
bodyBits, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode > 399 {
return SendFailureError{
StatusCode: res.StatusCode,
Body: string(bodyBits),
}
}
return nil
}
// RemoveUserFromAllGroups will remove a user from all groups
func (c *Client) RemoveUserFromAllGroups(ctx context.Context, userID string) error {
req, err := http.NewRequest(http.MethodDelete, c.getUsersGroupsURI(userID), nil)
if err != nil {
return err
}
res, err := c.do(ctx, req)
defer closeRes(res)
if err != nil {
return err
}
bodyBits, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode > 399 {
return SendFailureError{
StatusCode: res.StatusCode,
Body: string(bodyBits),
}
}
return nil
}
// SendInvocation will send an `InvocationMessage` to the hub
func (c *Client) SendInvocation(ctx context.Context, uri string, msg *InvocationMessage) error {
bits, err := json.Marshal(msg)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, uri, bytes.NewReader(bits))
if err != nil {
return err
}
token, err := c.generateToken(uri, 2*time.Hour)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
req.WithContext(ctx)
client := newHTTPClient()
res, err := client.Do(req)
defer closeRes(res)
if err != nil {
fmt.Println(err)
}
bodyBits, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode > 399 {
return SendFailureError{
StatusCode: res.StatusCode,
Body: string(bodyBits),
}
}
return nil
}
// GetName returns the name of the client
func (c *Client) GetName() string {
return c.name
}
// GetHub returns the name of the SignalR hub the client is targeting
func (c *Client) GetHub() string {
return c.hubName
}
func (c *Client) do(ctx context.Context, req *http.Request) (*http.Response, error) {
token, err := c.generateToken(req.URL.String(), 2*time.Hour)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
req.WithContext(ctx)
client := newHTTPClient()
return client.Do(req)
}
func readConn(ctx context.Context, conn *websocket.Conn) ([]byte, error) {
readerCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
_, reader, err := conn.Reader(readerCtx)
if err != nil {
return nil, err
}
bits, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
if bits[len(bits)-1] == messageTerminator {
bits = bits[0 : len(bits)-1]
}
return bits, nil
}
func (c *Client) handshake(ctx context.Context, conn *websocket.Conn) error {
hsReq := handshakeRequest{
Protocol: "json",
Version: 1,
}
bits, err := json.Marshal(hsReq)
if err != nil {
return err
}
wrCloser, err := conn.Writer(ctx, websocket.MessageText)
if err != nil {
return err
}
_, err = wrCloser.Write(append(bits, messageTerminator))
if err != nil {
return err
}
if err := wrCloser.Close(); err != nil {
return err
}
_, resp, err := conn.Reader(ctx)
if err != nil {
return err
}
bits, err = ioutil.ReadAll(resp)
if err != nil {
return err
}
if bits[len(bits)-1] == messageTerminator {
bits = bits[0 : len(bits)-1]
}
var hsRes handshakeResponse
if err := json.Unmarshal(bits, &hsRes); err != nil {
return err
}
if hsRes.Error != "" {
return errors.New(hsRes.Error)
}
return nil
}
func (c *Client) generateToken(audience string, expiresAfter time.Duration) (string, error) {
now := time.Now().UTC()
claims := signalrCliams{
StandardClaims: jwt.StandardClaims{
IssuedAt: now.Unix(),
NotBefore: now.Unix(),
Audience: audience,
ExpiresAt: now.Add(expiresAfter).Unix(),
},
NameID: c.name,
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(c.parsedConnStr.Key))
}
func (c *Client) getWssURI() string {
wssBaseURI := strings.Replace(c.getWssAudience(), "https://", "wss://", 1)
return wssBaseURI + "&id=" + c.negotiateRes.ConnectionID
}
func (c *Client) getWssAudience() string {
return fmt.Sprintf("%s/%s/?hub=%s", c.parsedConnStr.Endpoint.String(), c.audType, strings.ToLower(c.hubName))
}
func (c *Client) getBroadcastURI() string {
return c.getBaseURI()
}
func (c *Client) getSendToUserURI(userID string) string {
return fmt.Sprintf("%s/users/%s", c.getBaseURI(), userID)
}
func (c *Client) getSendToGroupURI(groupName string) string {
return fmt.Sprintf("%s/groups/%s", c.getBaseURI(), groupName)
}
func (c *Client) getGroupUserURI(groupName, userID string) string {
return fmt.Sprintf("%s/groups/%s/users/%s", c.getBaseURI(), groupName, userID)
}
func (c *Client) getUsersGroupsURI(userID string) string {
return fmt.Sprintf("%s/users/%s/groups", c.getBaseURI(), userID)
}
func (c *Client) getBaseURI() string {
return fmt.Sprintf("%s/api/v1/hubs/%s", c.parsedConnStr.Endpoint, strings.ToLower(c.hubName))
}
func newHTTPClient() *http.Client {
tr := &http.Transport{
MaxIdleConnsPerHost: 10,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}
return &http.Client{
Transport: tr,
}
}
func (c *Client) negotiateOnce(ctx context.Context) error {
c.nMutex.Lock()
defer c.nMutex.Unlock()
if c.negotiateRes == nil {
res, err := c.negotiate(ctx)
if err != nil {
return err
}
found := false
for _, txport := range res.AvailableTransports {
if txport.Name == string(websocketTransportType) {
found = true
}
}
if !found {
return errors.New("WebSockets transport is not supported by the service")
}
c.negotiateRes = res
}
return nil
}
func (c *Client) negotiate(ctx context.Context) (*negotiateResponse, error) {
endpoint := c.parsedConnStr.Endpoint
negotiateURI := fmt.Sprintf("%s/%s/%s", endpoint, c.audType, "negotiate?hub="+c.hubName)
req, err := http.NewRequest(http.MethodPost, negotiateURI, nil)
if err != nil {
return nil, err
}
audience := c.getWssAudience()
token, err := c.generateToken(audience, 2*time.Hour)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
req.WithContext(ctx)
client := newHTTPClient()
res, err := client.Do(req)
defer closeRes(res)
if err != nil {
fmt.Println(err)
}
bodyBits, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode > 399 {
return nil, &SendFailureError{
StatusCode: res.StatusCode,
Body: string(bodyBits),
}
}
var negRes negotiateResponse
err = json.Unmarshal(bodyBits, &negRes)
if err != nil {
return nil, err
}
return &negRes, nil
}
// NewInvocationMessage creates a new `InvocationMessage` from a target method name and arguments
func NewInvocationMessage(target string, args ...interface{}) (*InvocationMessage, error) {
jsonArgs := make([]json.RawMessage, len(args))
for i := 0; i < len(args); i++ {
bits, err := json.Marshal(args[i])
if err != nil {
return nil, err
}
jsonArgs[i] = json.RawMessage(bits)
}
return &InvocationMessage{
Type: invocationMessageType,
Target: target,
Arguments: jsonArgs,
}, nil
}
func closeRes(res *http.Response) {
if res != nil {
_ = res.Body.Close()
}
}