-
Notifications
You must be signed in to change notification settings - Fork 10
/
AbstractPeer.ts
794 lines (671 loc) · 19.8 KB
/
AbstractPeer.ts
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
import { FailureDetector } from 'adaptive-accrual-failure-detector';
import { Event } from 'atvik';
import { debug } from 'debug';
import {
AuthProvider,
AuthClientFlow,
AuthServerFlow,
AuthServerReplyType,
AuthServerReply,
AuthClientReplyType,
AuthClientReply
} from './auth';
import { DisconnectReason } from './DisconnectReason';
import { noId, encodeId } from './ids';
import {
PeerMessageType,
PeerMessage,
HelloMessage,
SelectMessage,
AuthMessage,
AuthDataMessage
} from './messages';
import { Peer } from './Peer';
import { TransportOptions } from './TransportOptions';
/**
* The interval at which pings are sent.
*/
const pingInterval = 30000;
/**
* The interval at which pings are checked.
*/
const pingCheckInterval = 5000;
/**
* The maximum time before a ping must have been received.
*/
const pingMaxTime = pingInterval * 3;
const enum State {
Initial,
WaitingForHello,
WaitingForSelectAck,
WaitingForAuth,
WaitingForAuthData,
WaitingForBegin,
WaitingForSelect,
WaitingForAuthAck,
Active
}
/**
* Abstract implementation of `Peer`. Used as the basis that negotiates
* protocol versions and requested features.
*/
export abstract class AbstractPeer implements Peer {
protected readonly transportOptions: TransportOptions;
protected debug: debug.Debugger;
private failureDetector: FailureDetector;
public id: ArrayBuffer;
protected version?: number;
private state: State;
private readonly connectEvent: Event<this>;
private readonly disconnectEvent: Event<this>;
private readonly dataEvent: Event<this, [ PeerMessageType, any ]>;
private lastLatencyTime: number;
private latencyValues: number[];
private helloTimeout: any;
private pingSender: any;
private pingChecker: any;
private lastPing: number;
private readonly authProviders: ReadonlyArray<AuthProvider>;
private remainingAuthProviders?: AuthProvider[];
private authClientFlow?: AuthClientFlow;
private authServerFlow?: AuthServerFlow;
/**
* Create a new peer over the given transport.
*
* @param transportOptions -
* the transport this belongs to
* @param authProviders -
* `AuthProvider` instances to use to authenticate with the other side
*/
public constructor(
transportOptions: TransportOptions,
authProviders: ReadonlyArray<AuthProvider>
) {
this.transportOptions = transportOptions;
this.debug = debug(transportOptions.debugNamespace + ':peer:pending');
this.authProviders = authProviders;
this.state = State.Initial;
this.connectEvent = new Event(this);
this.disconnectEvent = new Event(this);
this.dataEvent = new Event(this);
this.id = noId();
this.failureDetector = new FailureDetector();
this.lastLatencyTime = Date.now();
this.latencyValues = [];
this.lastPing = 0;
}
public get onConnect() {
return this.connectEvent.subscribable;
}
public get onDisconnect() {
return this.disconnectEvent.subscribable;
}
public get onData() {
return this.dataEvent.subscribable;
}
public get connected() {
return this.state === State.Active;
}
/**
* Get a buffer representing a publicly known security challenge for the
* local side of the peer.
*
* @returns
* buffer with challenge or undefined
*/
protected localPublicSecurity(): ArrayBuffer | undefined {
return undefined;
}
/**
* Get a buffer representing a publicly known security challenge for the
* remote side of the peer.
*
* @returns
* buffer with challenge or undefined
*/
protected remotePublicSecurity(): ArrayBuffer | undefined {
return undefined;
}
/**
* Manually disconnect this peer.
*/
public disconnect() {
this.debug('Requesting disconnect from peer');
}
/**
* Request that this peer disconnects.
*
* @param reason -
* the reason why this disconnect is happening
* @param err -
* optional error recorded
*/
protected abstract requestDisconnect(reason: DisconnectReason, err?: Error): void;
/**
* Handle disconnect event. This implementation will log info about the
* disconnect and then mark the peer as disconnected.
*
* Transports may override this to provide reconnection behavior.
*
* @param reason -
* the reason why this disconnect happened
* @param err -
* optional error recorded
*/
protected handleDisconnect(reason: DisconnectReason, err?: Error) {
this.debug('Disconnected', 'reason=', DisconnectReason[reason], 'error=', err);
clearTimeout(this.helloTimeout);
clearInterval(this.pingSender);
clearInterval(this.pingChecker);
const wasActive = this.state === State.Active;
this.state = State.Initial;
if(wasActive) {
this.disconnectEvent.emit();
}
}
protected queueNegotiationTimeout() {
// Wait a few seconds for the hello from the other side
if(this.helloTimeout) {
clearTimeout(this.helloTimeout);
}
this.helloTimeout = setTimeout(
() => this.abort('Timeout during negotiation'),
5000
);
}
/**
* Abort a connection.
*
* @param message -
* message for logging purposes
* @param error -
* optional error recording
* @param reason -
* reason for disconnect
*/
protected abort(message: string, error?: unknown, reason: DisconnectReason = DisconnectReason.NegotiationFailed) {
clearTimeout(this.helloTimeout);
this.debug(message, 'reason=', DisconnectReason[reason], 'error=', error);
this.requestDisconnect(reason, error instanceof Error ? error : new Error(String(error)));
}
/**
* Initiate negotiation as the server. This will send the initial Hello
* to the client and this peer will start waiting for a reply.
*/
protected negotiateAsServer() {
this.debug = debug(this.transportOptions.debugNamespace + ':peer:pending:server');
this.state = State.WaitingForSelect;
// Write the hello message
const message: HelloMessage = {
id: this.transportOptions.networkId,
capabilities: new Set()
};
this.send(PeerMessageType.Hello, message)
.catch(err => this.abort('Unable to send HELLO to client', err));
this.queueNegotiationTimeout();
}
/**
* Initiate negotiation as the client. This will switch the peer into a
* client mode and wait for the initial Hello from the server.
*/
protected negotiateAsClient() {
this.debug = debug(this.transportOptions.debugNamespace + ':peer:pending:client');
this.registerLatencySend();
this.state = State.WaitingForHello;
this.queueNegotiationTimeout();
}
/**
* Receive a message from the peer. This will method is responsible for
* checking the state of the peer and routing messages to their correct
* locations.
*
* @param type -
* type of message received
* @param payload -
* data of message received
*/
protected receiveData(type: PeerMessageType, payload: any) {
this.debug('Incoming', PeerMessageType[type], 'with payload', payload);
switch(type) {
case PeerMessageType.Bye:
this.requestDisconnect(DisconnectReason.Manual);
break;
case PeerMessageType.Ping:
this.receivePing();
break;
case PeerMessageType.Pong:
this.receivePong();
break;
case PeerMessageType.Hello:
if(this.state === State.WaitingForHello) {
this.receiveHello(payload as HelloMessage);
} else {
this.abort('Received unexpected HELLO');
}
break;
case PeerMessageType.Select:
if(this.state === State.WaitingForSelect) {
this.receiveSelect(payload as SelectMessage);
} else {
this.abort('Received unexpected SELECT');
}
break;
case PeerMessageType.Auth:
if(this.state === State.WaitingForAuth) {
this.receiveAuth(payload as AuthMessage);
} else {
this.abort('Received unexpected AUTH');
}
break;
case PeerMessageType.AuthData:
if(this.state === State.WaitingForAuthData) {
this.receiveClientAuthData(payload as AuthDataMessage);
} else if(this.state === State.WaitingForAuthAck) {
this.receiveServerAuthData(payload as AuthDataMessage);
} else {
this.abort('Received unexpected AUTHDATA');
}
break;
case PeerMessageType.Begin:
if(this.state === State.WaitingForBegin) {
this.registerLatencyReply();
this.switchToActive();
} else {
this.abort('Received unexpected BEGIN');
}
break;
case PeerMessageType.Ok:
if(this.state === State.WaitingForSelectAck) {
this.receiveSelectOK();
} else if(this.state === State.WaitingForAuthAck) {
this.receiveAuthOk();
} else {
this.abort('Received unexpected OK');
}
break;
case PeerMessageType.Reject:
if(this.state === State.WaitingForSelectAck) {
this.abort('SELECT was rejected by server');
} else if(this.state === State.WaitingForAuthAck) {
this.receiveAuthReject();
} else {
this.abort('Received unexpected REJECT');
}
break;
default:
this.dataEvent.emit(type, payload);
}
}
/**
* Client flow: HELLO received. A HELLO with information about the server
* has been received. Process and send back a reply.
*
* @param message -
*/
private receiveHello(message: HelloMessage) {
// Set the identifier and the version of the protocol to use
this.id = message.id;
// Update debugging with the identifier of the peer
this.debug = debug(this.transportOptions.debugNamespace + ':peer:' + encodeId(this.id) + ':client');
// TODO: Select capabilities wanted
const capabilities = new Set<string>();
// Switch state to waiting for the OK from the server
this.state = State.WaitingForSelectAck;
// Send our reply
const reply: SelectMessage = {
id: this.transportOptions.networkId,
capabilities: capabilities
};
// Measure the latency between the sending of SELECT and OK from server
this.registerLatencySend();
this.send(PeerMessageType.Select, reply)
.catch(err => this.abort('Unable to send SELECT reply', err));
// Requeue a timeout for the negotiation
this.queueNegotiationTimeout();
}
/**
* Server flow: SELECT received. The client as picked the capabilities it
* wants and is ready to proceed.
*
* @param message -
*/
private receiveSelect(message: SelectMessage) {
// Update the id of this peer with the client one
this.id = message.id;
// Update debugging with the identifier of the peer
this.debug = debug(this.transportOptions.debugNamespace + ':peer:' + encodeId(this.id) + ':server');
// TODO: Handle incoming capabilities
// Next step is to wait for the client to request authentication
this.state = State.WaitingForAuth;
// Simple OK reply expected by the client
this.send(PeerMessageType.Ok, undefined)
.catch(err => this.abort('Unable to send OK', err));
// Requeue a timeout for the negotiation
this.queueNegotiationTimeout();
}
/**
* Client flow: OK after SELECT. The server has received our SELECT and
* replied with an OK.
*/
private receiveSelectOK() {
// Register the time it took for the server to ok
this.registerLatencyReply();
this.state = State.WaitingForAuthAck;
// Assign the initial providers and send our initial auth
this.remainingAuthProviders = this.authProviders.slice();
this.sendInitialAuth();
}
/**
* Pick the next provider to use for authentication.
*
* @returns
* the next provider to try or `null`
*/
private pickNextAuthProvider() {
if(! this.remainingAuthProviders) {
return null;
}
// eslint-disable-next-line no-constant-condition
while(true) {
if(this.remainingAuthProviders.length === 0) return null;
const provider = this.remainingAuthProviders[0];
this.remainingAuthProviders.splice(0, 1);
if(provider.createClientFlow) {
return provider;
}
}
}
private sendInitialAuth() {
const provider = this.pickNextAuthProvider();
if(provider && provider.createClientFlow) {
const authClientFlow = provider.createClientFlow({
localPublicSecurity: this.localPublicSecurity(),
remotePublicSecurity: this.remotePublicSecurity()
});
this.authClientFlow = authClientFlow;
// Get the initial message and send the request to the server
(async () => {
let msg;
try {
msg = await authClientFlow.initialMessage();
} catch(err) {
this.abort('Initial auth message failed', err);
return;
}
try {
await this.send(PeerMessageType.Auth, {
method: provider.id,
data: msg
});
} catch(err) {
this.abort('Could not get or send initial auth message', err);
}
})().catch(err => this.abort('Unexpected error', err));
// Queue a negotiation timeout
this.queueNegotiationTimeout();
} else {
this.abort('Could not authenticate with any activate provider', undefined, DisconnectReason.AuthReject);
}
}
/**
* Client flow: Server accepted our authentication.
*/
private receiveAuthOk() {
this.send(PeerMessageType.Begin, undefined)
.then(() => this.switchToActive())
.catch(err => this.abort('Sending BEGIN failed', err));
}
/**
* Client flow: Server rejected our authentication. Try another one.
*/
private receiveAuthReject() {
this.sendInitialAuth();
}
/**
* Client flow: AUTHDATA has been received from the server.
*
* @param message -
*/
private receiveServerAuthData(message: AuthDataMessage) {
(async () => {
if(! this.authClientFlow) {
this.abort('No client flow available and server sent AUTHDATA');
return;
}
let reply: AuthClientReply;
try {
reply = await this.authClientFlow.receiveData(message.data);
} catch(err) {
this.abort('Error while handling auth data', err);
return;
}
if(reply.type === AuthClientReplyType.Data) {
try {
await this.send(PeerMessageType.AuthData, {
data: reply.data
});
} catch(err) {
this.abort('Error while sending auth reply', err);
}
} else if(reply.type === AuthClientReplyType.Reject) {
// Retry the next authentication method
this.sendInitialAuth();
}
})().catch(err => this.abort('Unexpected error', err));
this.queueNegotiationTimeout();
}
/**
* Server flow: AUTH has been received from client.
*
* @param message -
*/
private receiveAuth(message: AuthMessage) {
(async () => {
if(this.authServerFlow) {
try {
await this.authServerFlow.destroy();
} catch(err) {
this.debug('Error while releasing server auth flow', err);
}
}
let provider: AuthProvider | undefined;
for(const p of this.authProviders) {
if(p.id === message.method) {
provider = p;
break;
}
}
if(! provider || ! provider.createServerFlow) {
// This provider does not exist, reject the authentication attempt
this.send(PeerMessageType.Reject, undefined)
.catch(err => this.abort('Could not send REJECT', err));
this.queueNegotiationTimeout();
return;
}
const authServerFlow = provider.createServerFlow({
localPublicSecurity: this.localPublicSecurity(),
remotePublicSecurity: this.remotePublicSecurity()
});
this.authServerFlow = authServerFlow;
let reply;
try {
reply = await authServerFlow.receiveInitial(message.data);
} catch(err) {
this.abort('Error while handling initial auth', err);
return;
}
try {
await this.handleSendingAuthReply(reply);
} catch(err) {
this.abort('Error while sending auth reply', err);
}
})().catch(err => this.abort('Unexpected error', err));
this.queueNegotiationTimeout();
}
private handleSendingAuthReply(reply: AuthServerReply): Promise<void> {
switch(reply.type) {
case AuthServerReplyType.Ok:
// Authentication passed
this.registerLatencySend();
this.state = State.WaitingForBegin;
return this.send(PeerMessageType.Ok, undefined);
case AuthServerReplyType.Reject:
// Authentication was rejected, switch back to waiting for another auth
this.state = State.WaitingForAuth;
return this.send(PeerMessageType.Reject, undefined);
case AuthServerReplyType.Data:
// Extra data to pass back to the client
this.state = State.WaitingForAuthData;
if(! reply.data) {
this.abort('Auth provider returned data reply without any data');
return Promise.reject();
}
return this.send(PeerMessageType.AuthData, {
data: reply.data
});
}
throw new Error('Unknown type of reply');
}
/**
* Server flow: AUTHDATA has been received from client.
*
* @param message -
*/
private receiveClientAuthData(message: AuthDataMessage) {
(async () => {
if(! this.authServerFlow) {
this.abort('No server flow active and client sent AUTHDATA');
return;
}
let reply;
try {
reply = await this.authServerFlow.receiveData(message.data);
} catch(err) {
this.abort('Error while handling auth data', err);
return;
}
try {
await this.handleSendingAuthReply(reply);
} catch(err) {
this.abort('Error while sending auth reply', err);
}
})().catch(err => this.abort('Unexpected error', err));
this.queueNegotiationTimeout();
}
private switchToActive() {
this.debug('Switching to active');
// Make sure the timeout won't be triggered
clearTimeout(this.helloTimeout);
this.helloTimeout = undefined;
// Notify the peer that it has been connected
this.didConnect();
// Switch to active state
this.state = State.Active;
if(this.authClientFlow) {
this.authClientFlow.destroy()
.catch(err => this.debug('Error while releasing client auth flow', err));
}
if(this.authServerFlow) {
this.authServerFlow.destroy()
.catch(err => this.debug('Error while releasing server auth flow', err));
}
// Setup ping sending
this.failureDetector.registerHeartbeat();
this.lastPing = Date.now();
this.pingChecker = setInterval(this.checkFailure.bind(this), pingCheckInterval);
this.pingSender = setInterval(this.sendPing.bind(this), pingInterval);
// Emit the connect event
this.connectEvent.emit();
}
/**
* Callback for when a peer has connected.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
protected didConnect(): void {
}
/**
* Force a connection without performing negotiation.
*
* @param id -
* identifier of the peer
*/
protected forceConnect(id: ArrayBuffer) {
this.id = id;
this.debug = debug(this.transportOptions.debugNamespace + ':peer:' + encodeId(this.id) + ':client');
this.latencyValues.push(0);
this.switchToActive();
}
/**
* Send a ping to the peer.
*/
private sendPing() {
this.registerLatencySend();
this.send(PeerMessageType.Ping, undefined)
.catch(err => this.debug('Caught error while sending ping', err));
}
/**
* Receive a ping and send a pong response.
*/
private receivePing() {
this.lastPing = Date.now();
this.failureDetector.registerHeartbeat();
this.send(PeerMessageType.Pong, undefined)
.catch(err => this.debug('Caught error while sending pong', err));
}
/**
* Receive a pong from a sent ping.
*/
private receivePong() {
this.registerLatencyReply();
}
/**
* Check if this peer can be considered failed and request us to be
* disconnected from it.
*/
private checkFailure() {
if(this.failureDetector.checkFailure() || this.lastPing + pingMaxTime < Date.now()) {
this.requestDisconnect(DisconnectReason.PingTimeout, new Error('Timeout due to no ping'));
}
}
/**
* Register that a latency request has been sent, such as a ping.
*/
private registerLatencySend() {
this.lastLatencyTime = Date.now();
}
/**
* Register that a latency reply has been received, such as a pong.
*/
private registerLatencyReply() {
const time = Date.now();
if(this.latencyValues.length > 5) {
this.latencyValues.splice(0, 1);
}
this.latencyValues.push(time - this.lastLatencyTime);
}
/**
* Get the current latency.
*
* @returns
* estimated latency to the peer in milliseconds
*/
public get latency() {
if(this.latencyValues.length === 0) {
throw new Error('Latency unknown');
}
let sum = 0;
for(const v of this.latencyValues) {
sum += v;
}
return Math.floor(sum / this.latencyValues.length);
}
/**
* Send data to this peer.
*
* @param type -
* type of message being sent
* @param payload -
* data of message
*/
public abstract send<T extends PeerMessageType>(type: T, payload: PeerMessage<T>): Promise<void>;
}