-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
587 lines (550 loc) · 13.9 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
package client
import (
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/ungive/loon/pkg/pb"
"github.com/ungive/loon/pkg/server"
"google.golang.org/protobuf/proto"
)
type Client interface {
// Run loop for the client.
Run() error
// Registers content with this client and returns a handle for it.
// The content is available until it is unregistered with Unregister.
// Calls to any of the methods of the source are synchronized.
// The source's Data() method is only called
// while no requests are being handled that are using
// a previous return value of it.
// If the source reads from a file e.g.,
// the Data() method can safely seek to the beginning
// without having to worry about corrupting other ongoing requests.
Register(source ContentSource, info *ContentInfo) (ContentHandle, error)
// Unregisters content from this client.
// The client will not respond with the data anymore,
// if the server makes a request for it.
// Content may still be cached on the server or elsewhere.
// The Close() method will be called on the ContentSource instance
// that is associated with the passed ContentHandle.
Unregister(handle ContentHandle) error
// Closes the client's connection to the server.
// Calls Close() on any registered ContentSource objects.
Close()
}
type clientImpl struct {
baseUrl string
conn *websocket.Conn
hello *pb.Hello
content map[server.UUID]*contentHandle
forPath map[string]*contentHandle
send chan []byte
ping chan struct{}
stop chan struct{}
done chan struct{}
lastErr chan error
register chan *contentHandle
unregister chan *contentHandle
}
type contentHandle struct {
client *clientImpl
id server.UUID
url string
src ContentSource
info *ContentInfo
serve chan *pb.Request
cancel chan struct{}
}
func (h *contentHandle) URL() string {
return h.url
}
func (h *contentHandle) responsePump() {
for {
select {
case request := <-h.serve:
h.respond(request)
case <-h.client.done:
return
}
}
}
func (h *contentHandle) respond(request *pb.Request) {
cancel := make(chan struct{})
done := make(chan struct{})
go func() {
select {
case <-h.cancel:
close(cancel)
case <-done:
case <-h.client.done:
}
}()
defer func() {
close(done)
select {
case <-cancel:
h.client.write(&pb.ClientMessage{
Data: &pb.ClientMessage_CloseResponse{
CloseResponse: &pb.CloseResponse{
RequestId: request.Id,
},
},
})
default:
}
}()
reader, err := h.src.Data()
if err != nil {
log.Printf("failed to get reader for source: %v\n", err)
close(cancel)
return
}
err = h.client.write(&pb.ClientMessage{
Data: &pb.ClientMessage_ContentHeader{
ContentHeader: &pb.ContentHeader{
RequestId: request.Id,
ContentType: h.src.ContentType(),
ContentSize: uint64(h.src.Size()),
Filename: h.info.AttachmentFilename,
MaxCacheDuration: &h.info.MaxCacheDuration,
},
},
})
if err != nil {
log.Printf("failed to write response: %v\n", err)
close(cancel)
return
}
chunkSize := h.client.hello.Constraints.ChunkSize
buf := make([]byte, chunkSize)
for sequence := uint64(0); ; sequence++ {
sent := sequence * chunkSize
if sent >= h.src.Size() {
break
}
toRead := min(chunkSize, h.src.Size()-sent)
select {
case <-cancel:
return
default:
}
_, err := io.ReadAtLeast(reader, buf, int(toRead))
if errors.Is(err, io.EOF) {
break
}
if err != nil {
log.Printf("failed to read content data: %v\n", err)
close(cancel)
return
}
data := buf[0:toRead]
err = h.client.write(&pb.ClientMessage{
Data: &pb.ClientMessage_ContentChunk{
ContentChunk: &pb.ContentChunk{
RequestId: request.Id,
Sequence: sequence,
Data: data,
},
},
})
if err != nil {
log.Printf("failed to write response: %v\n", err)
close(cancel)
return
}
}
}
var ErrClientClosed = errors.New("the Client is closed")
var ErrInvalidScheme = errors.New("invalid scheme in URL")
var ErrNoHello = errors.New("server did not send a Hello message")
const (
pingInterval = 20 * time.Second
pongWait = 60 * time.Second
writeWait = 10 * time.Second
websocketPath = "/ws"
requestBuffer = 16
)
func verifyBaseUrl(connectedWith string, helloContains string) error {
a, err := url.Parse(connectedWith)
if err != nil {
return fmt.Errorf("failed to parse URL %v: %v", connectedWith, err)
}
b, err := url.Parse(helloContains)
if err != nil {
return fmt.Errorf("failed to parse URL %v: %v", helloContains, err)
}
aAddrs, err := net.LookupHost(a.Hostname())
if err != nil {
return fmt.Errorf("failed to lookup host %v: %v", a.Hostname(), err)
}
bAddrs, err := net.LookupHost(b.Hostname())
if err != nil {
return fmt.Errorf("failed to lookup host %v: %v", b.Hostname(), err)
}
aMap := make(map[string]struct{})
for _, aAddr := range aAddrs {
aMap[aAddr] = struct{}{}
}
for _, bAddr := range bAddrs {
if _, ok := aMap[bAddr]; ok {
return nil
}
}
return fmt.Errorf("could not verify the server's base URL: "+
"%v and %v do not point to the same host", connectedWith, helloContains)
}
func NewClient(address string, httpBasicAuth *string) (Client, error) {
var headers http.Header
if httpBasicAuth != nil {
auth := base64.StdEncoding.EncodeToString([]byte(*httpBasicAuth))
headers = http.Header{"Authorization": {"Basic " + auth}}
}
// For now, accept all certificates.
// TODO: check the certificate, let the user supply one.
websocket.DefaultDialer.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
VerifyConnection: func(cs tls.ConnectionState) error {
// opts := x509.VerifyOptions{
// Roots: x509.NewCertPool(),
// }
// log.Println("VerifyConnection")
// for _, cert := range cs.PeerCertificates {
// log.Printf("pubkey: %#v\n", cert.PublicKey)
// log.Printf("pubkey-algo: %#v\n", cert.PublicKeyAlgorithm)
// publicKeyDer, _ := x509.MarshalPKIXPublicKey(cert.PublicKey)
// publicKeyBlock := pem.Block{
// Type: "PUBLIC KEY",
// Bytes: publicKeyDer,
// }
// publicKeyPem := string(pem.EncodeToMemory(&publicKeyBlock))
// log.Printf("%v\n", publicKeyPem)
// }
return nil
},
}
log.Println(websocket.DefaultDialer.TLSClientConfig)
conn, _, err := websocket.DefaultDialer.Dial(address, headers)
if err != nil {
log.Fatalf("failed to dial websocket at %v: %v", address, err)
}
conn.SetPongHandler(func(string) error {
conn.SetReadDeadline(time.Now().Add(pongWait))
return nil
})
client := &clientImpl{
baseUrl: "",
conn: conn,
hello: nil,
content: make(map[server.UUID]*contentHandle),
forPath: make(map[string]*contentHandle),
send: make(chan []byte),
ping: make(chan struct{}),
stop: make(chan struct{}),
done: make(chan struct{}),
lastErr: make(chan error, 1),
register: make(chan *contentHandle),
unregister: make(chan *contentHandle),
}
message, err := client.read()
if err != nil {
return nil, err
}
switch m := message.Data.(type) {
case *pb.ServerMessage_Hello:
// Make sure to verify that the base URL points to the same server.
err := verifyBaseUrl(address, m.Hello.BaseUrl)
if err != nil {
return nil, err
}
client.baseUrl = m.Hello.BaseUrl
client.hello = m.Hello
default:
return nil, ErrNoHello
}
return client, nil
}
func (c *clientImpl) Run() error {
var wg sync.WaitGroup
wg.Add(2)
go func() { c.writePump(); wg.Done() }()
go func() { c.readPump(); wg.Done() }()
wg.Wait()
return <-c.lastErr
}
func (c *clientImpl) Close() {
select {
case c.lastErr <- ErrClientClosed:
default:
}
select {
case c.stop <- struct{}{}:
case <-c.done:
return
}
}
func (c *clientImpl) Register(
source ContentSource,
info *ContentInfo,
) (ContentHandle, error) {
uuid, err := server.NewUUID()
if err != nil {
return nil, err
}
handle := &contentHandle{
client: c,
id: uuid,
url: c.buildUrl(info.Path),
src: source,
info: info,
serve: make(chan *pb.Request, requestBuffer),
cancel: make(chan struct{}),
}
select {
case c.register <- handle:
case <-c.done:
return nil, ErrClientClosed
}
go handle.responsePump()
return handle, nil
}
func (c *clientImpl) Unregister(handle ContentHandle) error {
select {
case c.unregister <- handle.(*contentHandle):
case <-c.done:
return ErrClientClosed
}
return nil
}
func (c *clientImpl) report(err error) {
select {
case c.lastErr <- err:
default:
}
}
func (c *clientImpl) writePump() {
ticker := time.NewTicker(pingInterval)
defer func() {
close(c.done)
for _, content := range c.content {
content.src.Close()
}
ticker.Stop()
c.conn.WriteMessage(websocket.CloseMessage, []byte{})
c.conn.Close()
}()
for {
select {
case data := <-c.send:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
// switch m := message.Data.(type) {
// case *pb.ClientMessage_ContentChunk:
// hash := sha256.New()
// hash.Write(m.ContentChunk.Data)
// bs := hash.Sum(nil)
// log.Println(len(m.ContentChunk.Data), hex.EncodeToString(bs))
// }
err := c.conn.WriteMessage(websocket.BinaryMessage, data)
if err != nil {
c.report(err)
return
}
case <-ticker.C:
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
err := c.conn.WriteMessage(websocket.PingMessage, []byte{})
if err != nil {
c.report(err)
return
}
case content := <-c.register:
if _, ok := c.content[content.id]; ok {
panic("content ID already registered")
}
c.content[content.id] = content
c.forPath[content.info.Path] = content
case content := <-c.unregister:
if _, ok := c.content[content.id]; ok {
delete(c.content, content.id)
delete(c.forPath, content.info.Path)
content.src.Close()
}
case <-c.stop:
return
}
}
}
func (c *clientImpl) readPump() {
defer func() {
select {
case c.stop <- struct{}{}:
case <-c.done:
}
}()
requests := make(map[uint64]*pb.Request)
for {
message, err := c.read()
select {
case <-c.done:
return
default:
}
if err != nil {
c.report(err)
return
}
switch m := message.Data.(type) {
case *pb.ServerMessage_Hello:
c.report(errors.New("unexpected Hello message from server"))
return
case *pb.ServerMessage_Request:
requests[m.Request.Id] = m.Request
c.handleRequest(m.Request)
case *pb.ServerMessage_Success:
delete(requests, m.Success.RequestId)
// TODO: report to handle
log.Printf("response for request %v was successfully forwarded\n",
m.Success.RequestId)
case *pb.ServerMessage_RequestClosed:
id := m.RequestClosed.RequestId
log.Printf("request closed (%v): %v\n", id, m.RequestClosed.Message)
request, ok := requests[id]
if !ok {
panic(fmt.Sprintf("request not registered: %v", id))
}
delete(requests, id)
c.cancelResponse(request)
case *pb.ServerMessage_Close:
parts := strings.Split(m.Close.Reason.String(), "_")
c.report(fmt.Errorf("connection closed: %v (%v)",
m.Close.Message, strings.Join(parts[1:], "_")))
return
default:
c.report(fmt.Errorf("unexpected server message: %T %v", m, m))
return
}
select {
case <-c.done:
return
default:
}
}
}
func (c *clientImpl) handleRequest(request *pb.Request) {
handle, ok := c.forPath[request.Path]
if !ok {
c.write(&pb.ClientMessage{
Data: &pb.ClientMessage_EmptyResponse{
EmptyResponse: &pb.EmptyResponse{
RequestId: request.Id,
},
},
})
return
}
select {
case handle.serve <- request:
case <-c.done:
return
}
}
func (c *clientImpl) cancelResponse(request *pb.Request) {
handle, ok := c.forPath[request.Path]
if !ok {
return
}
select {
case handle.cancel <- struct{}{}:
case <-c.done:
}
}
func (c *clientImpl) write(message *pb.ClientMessage) error {
data, err := proto.Marshal(message)
if err != nil {
return fmt.Errorf("failed to marshal client message: %v", err)
}
select {
case c.send <- data:
case <-c.done:
return ErrClientClosed
}
return nil
}
func (c *clientImpl) read() (*pb.ServerMessage, error) {
typ, data, err := c.conn.ReadMessage()
if err != nil {
return nil, err
}
if typ != websocket.BinaryMessage {
return nil, fmt.Errorf("received an unexpected message type: %v", typ)
}
msg := &pb.ServerMessage{}
err = proto.Unmarshal(data, msg)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal server message: %v", err)
}
return msg, nil
}
func (c *clientImpl) buildUrl(path string) string {
mac, err := server.ComputeMac(
c.hello.ClientId, path, c.hello.ConnectionSecret)
if err != nil {
log.Fatalf("failed to compute MAC: %v\n", err)
}
macEncoded := mac.UrlEncode()
result := formatString(
"{base_url}/{client_id}/{mac}/{path}",
"base_url", c.baseUrl,
"client_id", c.hello.ClientId,
"mac", macEncoded,
"path", url.PathEscape(path))
return result
}
// https://stackoverflow.com/a/40811635/6748004
func formatString(format string, args ...string) string {
for i, v := range args {
if i%2 == 0 {
args[i] = "{" + v + "}"
}
}
return strings.NewReplacer(args...).Replace(format)
}
// ---
// func main() {
// file, err := os.Open("image.jpg")
// if err != nil {
// log.Fatalf("failed to open file: %v", err)
// }
// defer file.Close()
// contentType, err := server.NewContentType("image/jpeg")
// if err != nil {
// log.Fatalf("invalid explicit content type: %v", err)
// }
// source, err := NewFileContentSource(file, contentType)
// if err != nil {
// log.Fatalf("failed to create file content source: %v", err)
// }
// client, err := NewClient()
// if err != nil {
// log.Fatalf("failed to create new client: %v", err)
// }
// content, err := client.Register(source, &ContentInfo{
// Path: "P1ml0UXFpCIW",
// AttachmentFilename: nil,
// MaxCacheDuration: 0,
// UploadLimit: 0,
// })
// defer client.Unregister(content)
// if err != nil {
// log.Fatalf("failed to register content: %v", err)
// }
// log.Printf("URL: %v\n", content.URL())
// time.Sleep(30 * time.Second)
// }