@@ -20,6 +20,7 @@ async function main () {
20
20
timeout : NodeJS . Timeout | null ;
21
21
deadline : number ;
22
22
streams : Set < number > ;
23
+ fins : number ;
23
24
}
24
25
const connectionMap : Map < string , ConnectionData > = new Map ( ) ;
25
26
@@ -157,7 +158,7 @@ async function main () {
157
158
return ;
158
159
}
159
160
160
- const dcid : Uint8Array = header . dcid ;
161
+ const dcid : Buffer = Buffer . from ( header . dcid ) ;
161
162
let scid : Uint8Array = new QUICConnectionId (
162
163
await crypto . ops . sign (
163
164
crypto . key ,
@@ -168,7 +169,8 @@ async function main () {
168
169
) ;
169
170
170
171
let client : ConnectionData ;
171
- if ( ! connectionMap . has ( Buffer . from ( dcid ) . toString ( ) ) ) {
172
+ if ( ! connectionMap . has ( dcid . toString ( ) ) ) {
173
+ console . log ( 'Got a new connection!!!!!!!!!!' , dcid . toString ( 'hex' ) )
172
174
if ( header . ty !== quiche . Type . Initial ) {
173
175
console . log ( `QUIC packet must be Initial for new connections` ) ;
174
176
return ;
@@ -192,9 +194,12 @@ async function main () {
192
194
remoteInfo . port ,
193
195
remoteInfo . address ,
194
196
) ;
195
- } catch {
197
+ // console.log('sent bytes', versionDatagramLength);
198
+ } catch ( e ) {
199
+ console . error ( e ) ;
196
200
return ;
197
201
}
202
+ console . log ( `Sent VersionNegotiation packet to ${ remoteInfo . address } "${ remoteInfo . port } ` ) ;
198
203
return ;
199
204
}
200
205
// At this point we are processing an `Initial` packet.
@@ -203,6 +208,7 @@ async function main () {
203
208
const token = header . token ! ;
204
209
// Stateless Retry
205
210
if ( token . byteLength === 0 ) {
211
+ console . log ( 'Doing stateless retry' )
206
212
const token = await mintToken ( dcid , remoteInfo . address , crypto ) ;
207
213
const retryDatagram = Buffer . allocUnsafe ( quiche . MAX_DATAGRAM_SIZE ) ;
208
214
const retryDatagramLength = quiche . retry (
@@ -221,53 +227,76 @@ async function main () {
221
227
remoteInfo . port ,
222
228
remoteInfo . address ,
223
229
) ;
230
+ // console.log('sent bytes ', retryDatagramLength);
224
231
} catch ( e ) {
232
+ console . error ( e ) ;
225
233
return ;
226
234
}
235
+ console . log ( `Sent Retry packet to ${ remoteInfo . address } "${ remoteInfo . port } ` )
227
236
return ;
228
237
}
229
238
// At this point in time, the packet's DCID is the originally-derived DCID.
230
239
// While the DCID embedded in the token is the original DCID that the client first created.
240
+ console . log (
241
+ 'validate' ,
242
+ Buffer . from ( token ) ,
243
+ remoteInfo . address ,
244
+ ) ;
231
245
const dcidOriginal = await validateToken (
232
246
Buffer . from ( token ) ,
233
247
remoteInfo . address ,
234
248
crypto ,
235
249
) ;
250
+ console . log ( dcidOriginal ) ;
236
251
if ( dcidOriginal == null ) {
252
+ console . log (
253
+ `QUIC packet token failed validation due to missing DCID` ,
254
+ ) ;
237
255
return ;
238
256
}
239
257
// Check that the newly-derived DCID (passed in as the SCID) is the same
240
258
// length as the packet DCID.
241
259
// This ensures that the derivation process hasn't changed.
242
260
if ( scid . byteLength !== header . dcid . byteLength ) {
261
+ console . log (
262
+ `QUIC packet token failed validation due to mismatched length` ,
263
+ ) ;
243
264
return ;
244
265
}
245
266
// Here we shall re-use the originally-derived DCID as the SCID
246
267
scid = new QUICConnectionId ( header . dcid ) ;
247
-
248
- console . log ( 'creating new connection' )
268
+ console . log (
269
+ `Accepting new connection from QUIC packet from ${ remoteInfo . address } :${ remoteInfo . port } ` ,
270
+ ) ;
271
+ const localHost = {
272
+ host : host ,
273
+ port : localPort ,
274
+ } ;
275
+ const remoteHost = {
276
+ host : remoteInfo . address ,
277
+ port : remoteInfo . port ,
278
+ } ;
279
+ console . log (
280
+ dcidOriginal ,
281
+ )
249
282
const conn = quiche . Connection . accept (
250
283
scid ,
251
284
dcidOriginal ,
252
- {
253
- host : host ,
254
- port : localPort ,
255
- } ,
256
- {
257
- host : remoteInfo . address ,
258
- port : remoteInfo . port ,
259
- } ,
285
+ localHost ,
286
+ remoteHost ,
260
287
config ,
261
288
)
262
289
client = {
263
290
deadline : Infinity ,
264
291
timeout : null ,
265
292
streams : new Set ( ) ,
266
- conn
293
+ conn,
294
+ fins : 0 ,
267
295
}
296
+ console . log ( 'setting connection ' , Buffer . from ( scid ) . toString ( 'hex' ) ) ;
268
297
connectionMap . set ( Buffer . from ( scid ) . toString ( ) , client ) ;
269
298
} else {
270
- client = connectionMap . get ( Buffer . from ( dcid ) . toString ( ) ) ! ;
299
+ client = connectionMap . get ( dcid . toString ( ) ) ! ;
271
300
}
272
301
273
302
client . conn . recv ( data , recvInfo ) ;
@@ -293,10 +322,17 @@ async function main () {
293
322
const [ , fin ] = client . conn . streamRecv ( streamId , data ) ;
294
323
if ( fin ) {
295
324
client . streams . delete ( streamId )
296
- console . log ( "Stream finished! " , streamId , "left" , client . streams . size ) ;
325
+ client . fins ++ ;
326
+ console . log ( "Stream finished! " , streamId , "left" , client . streams . size , 'fins:' , client . fins ) ;
327
+ break ;
297
328
}
298
329
} catch ( e ) {
299
330
if ( e . message === 'Done' ) break ;
331
+ console . error ( e ) ;
332
+ if ( / ^ I n v a l i d S t r e a m S t a t e .* / . test ( e . message ) ) {
333
+ console . log ( 'got ' , e . message )
334
+ break ;
335
+ }
300
336
throw e ;
301
337
}
302
338
}
@@ -326,12 +362,12 @@ async function main () {
326
362
let sendInfo : SendInfo ;
327
363
try {
328
364
[ write , sendInfo ] = client . conn . send ( out ) ;
329
- console . log ( sendInfo ) ;
330
365
} catch ( e ) {
331
366
if ( e . message == 'Done' ) break ;
332
367
throw e ;
333
368
}
334
369
await socketSend ( out , 0 , write , sendInfo . to . port , sendInfo . to . host ) ;
370
+ // console.log('sent bytes ', write);
335
371
checkTimeout ( client ) ;
336
372
}
337
373
if ( client . conn . isClosed ( ) ) {
@@ -350,7 +386,7 @@ async function mintToken(
350
386
peerHost : string ,
351
387
crypto : any
352
388
) : Promise < Buffer > {
353
- const msgData = { dcid : Buffer . from ( dcid ) . toString ( ) , host : peerHost } ;
389
+ const msgData = { dcid : Buffer . from ( dcid ) . toString ( 'hex' ) , host : peerHost } ;
354
390
const msgJSON = JSON . stringify ( msgData ) ;
355
391
const msgBuffer = Buffer . from ( msgJSON ) ;
356
392
const msgSig = Buffer . from (
@@ -372,9 +408,11 @@ async function validateToken(
372
408
let tokenData ;
373
409
try {
374
410
tokenData = JSON . parse ( tokenBuffer . toString ( ) ) ;
375
- } catch {
411
+ console . log ( tokenData ) ;
412
+ } catch ( e ) {
376
413
return ;
377
414
}
415
+ console . log ( tokenData )
378
416
if ( typeof tokenData !== 'object' || tokenData == null ) {
379
417
return ;
380
418
}
@@ -395,6 +433,7 @@ async function validateToken(
395
433
} catch {
396
434
return ;
397
435
}
436
+ console . log ( 'data' , msgData ) ;
398
437
if ( typeof msgData !== 'object' || msgData == null ) {
399
438
return ;
400
439
}
@@ -404,6 +443,9 @@ async function validateToken(
404
443
if ( msgData . host !== peerHost ) {
405
444
return ;
406
445
}
446
+ console . log ( 'dcid' , msgData . dcid ) ;
447
+ console . log ( 'id' , QUICConnectionId . fromString ( msgData . dcid ) )
448
+ console . log ( 'buf' , Buffer . from ( msgData . dcid , 'hex' ) ) ;
407
449
return QUICConnectionId . fromString ( msgData . dcid ) ;
408
450
}
409
451
0 commit comments