1
1
import { Buffer } from 'buffer' ;
2
+ import * as stream from 'stream' ;
3
+ import * as fs from 'fs' ;
2
4
import * as net from "net" ;
3
5
import * as tls from "tls" ;
4
6
import * as http from "http" ;
@@ -122,6 +124,8 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
122
124
private socksOptions : boolean | SocksServerOptions ;
123
125
private passthroughUnknownProtocols : boolean ;
124
126
private maxBodySize : number ;
127
+ private keyLogFilePath : string | undefined ;
128
+ private keyLogStream : fs . WriteStream | undefined ;
125
129
126
130
private app : connect . Server ;
127
131
private server : DestroyableServer < net . Server > | undefined ;
@@ -140,6 +144,8 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
140
144
this . socksOptions = options . socks ?? false ;
141
145
this . passthroughUnknownProtocols = options . passthrough ?. includes ( 'unknown-protocol' ) ?? false ;
142
146
this . maxBodySize = options . maxBodySize ?? Infinity ;
147
+ this . keyLogFilePath = options . https ?. keyLogFile ;
148
+
143
149
this . eventEmitter = new EventEmitter ( ) ;
144
150
145
151
this . app = connect ( ) ;
@@ -158,12 +164,20 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
158
164
}
159
165
160
166
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
+
161
174
this . server = await createComboServer ( {
162
175
debug : this . debug ,
163
176
https : this . httpsOptions ,
164
177
http2 : this . isHttp2Enabled ,
165
178
socks : this . socksOptions ,
166
179
passthroughUnknownProtocols : this . passthroughUnknownProtocols ,
180
+ keyLogStream : this . keyLogStream ,
167
181
168
182
requestListener : this . app ,
169
183
tlsClientErrorListener : this . announceTlsErrorAsync . bind ( this ) ,
@@ -222,6 +236,8 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
222
236
223
237
if ( this . server ) await this . server . destroy ( ) ;
224
238
239
+ if ( this . keyLogStream ) this . keyLogStream . end ( ) ;
240
+
225
241
this . reset ( ) ;
226
242
}
227
243
@@ -837,6 +853,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
837
853
await nextRule . handle ( request , response , {
838
854
record : this . recordTraffic ,
839
855
debug : this . debug ,
856
+ keyLogStream : this . keyLogStream ,
840
857
emitEventCallback : ( this . eventEmitter . listenerCount ( 'rule-event' ) !== 0 )
841
858
? ( type , event ) => this . announceRuleEventAsync ( request . id , nextRule ! . id , type , event )
842
859
: undefined
@@ -912,6 +929,7 @@ export class MockttpServer extends AbstractMockttp implements Mockttp {
912
929
await nextRule . handle ( request , socket , head , {
913
930
record : this . recordTraffic ,
914
931
debug : this . debug ,
932
+ keyLogStream : this . keyLogStream ,
915
933
emitEventCallback : ( this . eventEmitter . listenerCount ( 'rule-event' ) !== 0 )
916
934
? ( type , event ) => this . announceRuleEventAsync ( request . id , nextRule ! . id , type , event )
917
935
: undefined
0 commit comments