-
Notifications
You must be signed in to change notification settings - Fork 0
/
parcelCollection.ts
346 lines (307 loc) · 10.9 KB
/
parcelCollection.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
import {
Certificate,
DETACHED_SIGNATURE_TYPES,
HandshakeChallenge,
HandshakeResponse,
ParcelDelivery,
} from '@relaycorp/relaynet-core';
import AbortController from 'abort-controller';
import bufferToArray from 'buffer-to-arraybuffer';
import { FastifyInstance } from 'fastify';
import { IncomingHttpHeaders, IncomingMessage, Server as HTTPServer } from 'http';
import pipe from 'it-pipe';
import { Connection } from 'mongoose';
import { Logger } from 'pino';
import { duplex } from 'stream-to-it';
import uuid from 'uuid-random';
import WebSocket, {
createWebSocketStream,
Server as WSServer,
ServerOptions as WSServerOptions,
} from 'ws';
import {
NatsStreamingClient,
NatsStreamingSubscriptionError,
} from '../../backingServices/natsStreaming';
import { ParcelStore, ParcelStreamMessage } from '../../parcelStore';
import { retrieveOwnCertificates } from '../../pki';
import { WebSocketCode } from './websockets';
/**
* Maximum size of each incoming message.
*
* ACK messages are tiny, but HandshakeResponse messages contain digital signatures with their
* respective signers' certificates. Keeping in mind that each certificate takes up around 1.9 kib
* and a private gateway could have 2-3 valid certificates (whilst order certificates are being
* rotated out), we should allow 6 kib.
*/
export const PARCEL_COLLECTION_MAX_PAYLOAD_OCTETS = 6 * 1024;
const WEBSOCKET_PING_INTERVAL_MS = 5_000;
interface PendingACK {
readonly ack: () => Promise<void>;
readonly parcelObjectKey: string;
}
export default async function (
fastify: FastifyInstance,
_options: any,
done: () => void,
): Promise<void> {
const fastifyTypeless = fastify as any;
makeWebSocketServer(
fastifyTypeless.mongo.db,
fastifyTypeless.initialConfig.requestIdHeader,
fastify.log as Logger,
fastify.server,
);
done();
}
export function makeWebSocketServer(
mongooseConnection: Connection,
requestIdHeader: string,
baseLogger: Logger,
httpServer?: HTTPServer,
): WSServer {
const serverOptions: Partial<WSServerOptions> = httpServer
? { server: httpServer }
: { noServer: true };
const wsServer = new WSServer({
clientTracking: false,
maxPayload: PARCEL_COLLECTION_MAX_PAYLOAD_OCTETS,
path: '/v1/parcel-collection',
...serverOptions,
});
wsServer.on('connection', makeConnectionHandler(mongooseConnection, requestIdHeader, baseLogger));
return wsServer;
}
function makeConnectionHandler(
mongooseConnection: Connection,
requestIdHeader: string,
baseLogger: Logger,
): (ws: WebSocket, request: IncomingMessage) => void {
const parcelStore = ParcelStore.initFromEnv();
return async (wsConnection, request) => {
const reqId = (request.headers[requestIdHeader] as string | undefined) ?? uuid();
const requestAwareLogger = baseLogger.child({ reqId });
requestAwareLogger.debug('Starting parcel collection request');
if (request.headers.origin) {
requestAwareLogger.debug('Denying web browser request');
wsConnection.close(
WebSocketCode.VIOLATED_POLICY,
'Web browser requests are disabled for security reasons',
);
return;
}
const pingIntervalId = setInterval(() => {
requestAwareLogger.debug('Sending ping to client');
wsConnection.ping();
}, WEBSOCKET_PING_INTERVAL_MS);
wsConnection.once('close', () => {
clearInterval(pingIntervalId);
});
const abortController = makeAbortController(wsConnection, requestAwareLogger);
const peerGatewayAddress = await doHandshake(
wsConnection,
mongooseConnection,
requestAwareLogger,
);
if (!peerGatewayAddress) {
return;
}
const peerAwareLogger = requestAwareLogger.child({ peerGatewayAddress });
peerAwareLogger.debug('Handshake completed successfully');
const tracker = new CollectionTracker();
await pipe(
streamActiveParcels(
parcelStore,
peerGatewayAddress,
peerAwareLogger,
reqId,
request.headers,
abortController.signal,
tracker,
),
makeDeliveryStream(wsConnection, tracker, peerAwareLogger),
duplex(createWebSocketStream(wsConnection)),
makeACKProcessor(wsConnection, tracker, peerAwareLogger),
);
};
}
function makeAbortController(wsConnection: WebSocket, logger: Logger): AbortController {
const abortController = new AbortController();
wsConnection.once('close', (closeCode, closeReason) => {
logger.info({ closeCode, closeReason }, 'Closing connection');
abortController.abort();
});
wsConnection.once('error', (err) => {
logger.info({ err }, 'Closing connection due to error');
abortController.abort();
});
return abortController;
}
async function doHandshake(
wsConnection: WebSocket,
mongooseConnection: Connection,
logger: Logger,
): Promise<string | null> {
const nonce = bufferToArray(uuid.bin() as Buffer);
return new Promise((resolve) => {
wsConnection.once('message', async (message: Buffer) => {
let handshakeResponse: HandshakeResponse;
try {
handshakeResponse = HandshakeResponse.deserialize(bufferToArray(message));
} catch (err) {
logger.info({ err }, 'Refusing malformed handshake response');
wsConnection.close(WebSocketCode.CANNOT_ACCEPT, 'Invalid handshake response');
return resolve(null);
}
const nonceSignaturesCount = handshakeResponse.nonceSignatures.length;
if (nonceSignaturesCount !== 1) {
logger.info(
{ nonceSignaturesCount },
'Refusing handshake response with invalid number of signatures',
);
wsConnection.close(
WebSocketCode.CANNOT_ACCEPT,
'Handshake response did not include exactly one nonce signature ' +
`(got ${nonceSignaturesCount})`,
);
return resolve(null);
}
const trustedCertificates = await retrieveOwnCertificates(mongooseConnection);
let peerGatewayCertificate: Certificate;
try {
peerGatewayCertificate = await DETACHED_SIGNATURE_TYPES.NONCE.verify(
handshakeResponse.nonceSignatures[0],
nonce,
trustedCertificates,
);
} catch (err) {
logger.info({ err }, 'Refusing handshake response with invalid signature');
wsConnection.close(WebSocketCode.CANNOT_ACCEPT, 'Nonce signature is invalid');
return resolve(null);
}
resolve(await peerGatewayCertificate.calculateSubjectPrivateAddress());
});
logger.debug('Sending handshake challenge');
const challenge = new HandshakeChallenge(nonce);
wsConnection.send(challenge.serialize());
});
}
async function* streamActiveParcels(
parcelStore: ParcelStore,
peerGatewayAddress: string,
logger: Logger,
requestId: string,
requestHeaders: IncomingHttpHeaders,
abortSignal: AbortSignal,
tracker: CollectionTracker,
): AsyncIterable<ParcelStreamMessage> {
// "keep-alive" or any value other than "close-upon-completion" should keep the connection alive
const keepAlive = requestHeaders['x-relaynet-streaming-mode'] !== 'close-upon-completion';
if (keepAlive) {
const natsStreamingClient = NatsStreamingClient.initFromEnv(`parcel-collection-${requestId}`);
try {
yield* await parcelStore.liveStreamActiveParcelsForGateway(
peerGatewayAddress,
natsStreamingClient,
abortSignal,
logger,
);
} catch (err) {
if (err instanceof NatsStreamingSubscriptionError) {
logger.warn({ err }, 'Failed to subscribe to NATS queue to live stream active parcels');
} else {
logger.error({ err }, 'Failed to live stream parcels');
}
tracker.setCloseFrameCode(WebSocketCode.SERVER_ERROR);
}
} else {
yield* await parcelStore.streamActiveParcelsForGateway(peerGatewayAddress, logger);
}
}
function makeDeliveryStream(
wsConnection: WebSocket,
tracker: CollectionTracker,
logger: Logger,
): (parcelMessages: AsyncIterable<ParcelStreamMessage>) => AsyncIterable<Buffer> {
return async function* (
parcelMessages: AsyncIterable<ParcelStreamMessage>,
): AsyncIterable<Buffer> {
for await (const parcelMessage of parcelMessages) {
logger.info({ parcelObjectKey: parcelMessage.parcelObjectKey }, 'Sending parcel');
const parcelDelivery = new ParcelDelivery(
uuid(),
bufferToArray(parcelMessage.parcelSerialized),
);
tracker.addPendingACK(parcelDelivery.deliveryId, {
ack: parcelMessage.ack,
parcelObjectKey: parcelMessage.parcelObjectKey,
});
yield Buffer.from(parcelDelivery.serialize());
}
tracker.markAllParcelsDelivered();
if (tracker.isCollectionComplete) {
logger.info('All parcels were acknowledged shortly after the last one was sent');
wsConnection.close(tracker.closeFrameCode);
}
};
}
function makeACKProcessor(
wsConnection: WebSocket,
tracker: CollectionTracker,
logger: Logger,
): (ackMessages: AsyncIterable<string>) => Promise<void> {
return async (ackMessages: AsyncIterable<string>) => {
for await (const ackMessage of ackMessages) {
const pendingACK = tracker.popPendingACK(ackMessage);
if (!pendingACK) {
logger.info('Closing connection due to unknown acknowledgement');
wsConnection.close(
WebSocketCode.CANNOT_ACCEPT,
'Unknown delivery id sent as acknowledgement',
);
break;
}
logger.info({ parcelObjectKey: pendingACK.parcelObjectKey }, 'Acknowledgement received');
await pendingACK.ack();
if (tracker.isCollectionComplete) {
logger.info('Closing connection after all parcels have been acknowledged');
wsConnection.close(tracker.closeFrameCode);
break;
}
}
};
}
class CollectionTracker {
// tslint:disable-next-line:readonly-keyword
private wereAllParcelsDelivered = false;
// tslint:disable-next-line:readonly-keyword
private pendingACKs: { [key: string]: PendingACK } = {};
// tslint:disable-next-line:readonly-keyword
private _closeFrameCode: WebSocketCode | null = null;
get isCollectionComplete(): boolean {
return this.wereAllParcelsDelivered && Object.keys(this.pendingACKs).length === 0;
}
public markAllParcelsDelivered(): void {
// tslint:disable-next-line:no-object-mutation
this.wereAllParcelsDelivered = true;
}
public addPendingACK(deliveryId: string, pendingACK: PendingACK): void {
// tslint:disable-next-line:no-object-mutation
this.pendingACKs[deliveryId] = pendingACK;
}
public popPendingACK(deliveryId: string): PendingACK | undefined {
const pendingACK = this.pendingACKs[deliveryId];
if (pendingACK) {
// tslint:disable-next-line:no-delete no-object-mutation
delete this.pendingACKs[deliveryId];
}
return pendingACK;
}
public setCloseFrameCode(code: WebSocketCode): void {
// tslint:disable-next-line:no-object-mutation
this._closeFrameCode = code;
}
get closeFrameCode(): WebSocketCode {
return this._closeFrameCode ?? WebSocketCode.NORMAL;
}
}