11import { Buffer } from 'buffer' ;
2+ import * as stream from 'stream' ;
3+ import * as fs from 'fs' ;
24import * as net from "net" ;
35import * as tls from "tls" ;
46import * as http from "http" ;
@@ -122,6 +124,8 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
122124 private socksOptions : boolean | SocksServerOptions ;
123125 private passthroughUnknownProtocols : boolean ;
124126 private maxBodySize : number ;
127+ private keyLogFilePath : string | undefined ;
128+ private keyLogStream : fs . WriteStream | undefined ;
125129
126130 private app : connect . Server ;
127131 private server : DestroyableServer < net . Server > | undefined ;
@@ -140,6 +144,8 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
140144 this . socksOptions = options . socks ?? false ;
141145 this . passthroughUnknownProtocols = options . passthrough ?. includes ( 'unknown-protocol' ) ?? false ;
142146 this . maxBodySize = options . maxBodySize ?? Infinity ;
147+ this . keyLogFilePath = options . https ?. keyLogFile ;
148+
143149 this . eventEmitter = new EventEmitter ( ) ;
144150
145151 this . app = connect ( ) ;
@@ -158,12 +164,20 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
158164 }
159165
160166 async start ( portParam : number | PortRange = { startPort : 8000 , endPort : 65535 } ) : Promise < void > {
167+ if ( this . keyLogFilePath ) {
168+ this . keyLogStream = fs . createWriteStream ( this . keyLogFilePath , { flags : 'a' } ) ;
169+ this . keyLogStream . on ( 'error' , ( err ) => {
170+ console . warn ( `Error writing TLS key log file ${ this . keyLogFilePath } :` , err ) ;
171+ } ) ;
172+ }
173+
161174 this . server = await createComboServer ( {
162175 debug : this . debug ,
163176 https : this . httpsOptions ,
164177 http2 : this . isHttp2Enabled ,
165178 socks : this . socksOptions ,
166179 passthroughUnknownProtocols : this . passthroughUnknownProtocols ,
180+ keyLogStream : this . keyLogStream ,
167181
168182 requestListener : this . app ,
169183 tlsClientErrorListener : this . announceTlsErrorAsync . bind ( this ) ,
@@ -222,6 +236,8 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
222236
223237 if ( this . server ) await this . server . destroy ( ) ;
224238
239+ if ( this . keyLogStream ) this . keyLogStream . end ( ) ;
240+
225241 this . reset ( ) ;
226242 }
227243
@@ -837,6 +853,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
837853 await nextRule . handle ( request , response , {
838854 record : this . recordTraffic ,
839855 debug : this . debug ,
856+ keyLogStream : this . keyLogStream ,
840857 emitEventCallback : ( this . eventEmitter . listenerCount ( 'rule-event' ) !== 0 )
841858 ? ( type , event ) => this . announceRuleEventAsync ( request . id , nextRule ! . id , type , event )
842859 : undefined
@@ -912,6 +929,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
912929 await nextRule . handle ( request , socket , head , {
913930 record : this . recordTraffic ,
914931 debug : this . debug ,
932+ keyLogStream : this . keyLogStream ,
915933 emitEventCallback : ( this . eventEmitter . listenerCount ( 'rule-event' ) !== 0 )
916934 ? ( type , event ) => this . announceRuleEventAsync ( request . id , nextRule ! . id , type , event )
917935 : undefined
0 commit comments