@@ -3,6 +3,7 @@ import { Writable } from 'stream';
33import * as url from 'url' ;
44import type * as dns from 'dns' ;
55import * as net from 'net' ;
6+ import * as tls from 'tls' ;
67import * as http from 'http' ;
78import * as https from 'https' ;
89
@@ -401,6 +402,9 @@ const mapOmitToUndefined = <T extends { [key: string]: any }>(
401402 : v
402403 ) ;
403404
405+ // Only used if key logging is enabled, meaning we need to hook into http2-wrapper's TLS setup:
406+ const h2ProtocolQueue = new Map ( ) ;
407+
404408export class PassThroughStepImpl extends PassThroughStep {
405409
406410 private _trustedCACertificates : MaybePromise < Array < string > | undefined > ;
@@ -697,8 +701,21 @@ export class PassThroughStepImpl extends PassThroughStep {
697701
698702 let makeRequest = (
699703 shouldTryH2Upstream
700- ? ( options : any , cb : any ) =>
701- h2Client . auto ( options , cb ) . catch ( ( e ) => {
704+ ? ( reqOpts : any , cb : any ) =>
705+ h2Client . auto ( {
706+ ...reqOpts ,
707+ resolveProtocol : options . keyLogStream
708+ // Wrap TLS setup in key logging:
709+ ? h2Client . auto . createResolveProtocol (
710+ h2Client . auto . protocolCache as any ,
711+ h2ProtocolQueue ,
712+ function ( ...args ) {
713+ const socket = tls . connect ( ...args ) ;
714+ socket . on ( 'keylog' , ( line ) => options . keyLogStream ! . write ( line ) ) ;
715+ return socket ;
716+ }
717+ ) : undefined ,
718+ } , cb ) . catch ( ( e ) => {
702719 // If an error occurs during auto detection via ALPN, that's an
703720 // TypeError implies it's an invalid HTTP/2 request that was rejected.
704721 // Anything else implies an upstream HTTP/2 issue.
@@ -1067,9 +1084,7 @@ export class PassThroughStepImpl extends PassThroughStep {
10671084 if ( this . outgoingSockets . has ( socket ) ) return ;
10681085
10691086 if ( options . keyLogStream ) {
1070- socket . on ( 'keylog' , ( line ) => {
1071- options . keyLogStream ! . write ( line ) ;
1072- } ) ;
1087+ socket . on ( 'keylog' , ( line ) => options . keyLogStream ! . write ( line ) ) ;
10731088 }
10741089
10751090 // Add this port to our list of active ports, once it's connected (before then it has no port)
0 commit comments