@@ -201,6 +201,7 @@ export async function createComboServer(
201
201
analyzeAndMaybePassThroughTls (
202
202
tlsServer ,
203
203
options . https . tlsPassthrough ?? [ ] ,
204
+ options . https . tlsIntercept ?? [ ] ,
204
205
tlsPassthroughListener
205
206
) ;
206
207
@@ -369,9 +370,14 @@ function copyTimingDetails<T extends SocketIsh<'__timingInfo'>>(
369
370
function analyzeAndMaybePassThroughTls (
370
371
server : tls . Server ,
371
372
passthroughList : Required < MockttpHttpsOptions > [ 'tlsPassthrough' ] ,
373
+ interceptList : Required < MockttpHttpsOptions > [ 'tlsIntercept' ] ,
372
374
passthroughListener : ( socket : net . Socket , address : string , port ?: number ) => void
373
375
) {
374
- const hostnames = passthroughList . map ( ( { hostname } ) => hostname ) ;
376
+ if ( passthroughList . length > 0 && interceptList . length > 0 ) {
377
+ throw new Error ( 'Cannot use both tlsPassthrough and tlsIntercept at the same time.' ) ;
378
+ }
379
+ const passThroughHostnames = passthroughList . map ( ( { hostname } ) => hostname ) ;
380
+ const interceptHostnames = interceptList . map ( ( { hostname } ) => hostname ) ;
375
381
376
382
const tlsConnectionListener = server . listeners ( 'connection' ) [ 0 ] as ( socket : net . Socket ) => { } ;
377
383
server . removeListener ( 'connection' , tlsConnectionListener ) ;
@@ -389,12 +395,21 @@ function analyzeAndMaybePassThroughTls(
389
395
clientAlpn : helloData . alpnProtocols ,
390
396
ja3Fingerprint : calculateJa3FromFingerprintData ( helloData . fingerprintData )
391
397
} ;
398
+
399
+ if ( interceptHostnames . length > 0 && connectHostname && ! interceptHostnames . includes ( connectHostname ) ) {
400
+ const upstreamPort = connectPort ? parseInt ( connectPort , 10 ) : undefined ;
401
+ passthroughListener ( socket , connectHostname , upstreamPort ) ;
402
+ return ; // Do not continue with TLS
403
+ } else if ( interceptHostnames . length > 0 && sniHostname && ! interceptHostnames . includes ( sniHostname ) ) {
404
+ passthroughListener ( socket , sniHostname ) ; // Can't guess the port - not included in SNI
405
+ return ; // Do not continue with TLS
406
+ }
392
407
393
- if ( connectHostname && hostnames . includes ( connectHostname ) ) {
408
+ if ( connectHostname && passThroughHostnames . includes ( connectHostname ) ) {
394
409
const upstreamPort = connectPort ? parseInt ( connectPort , 10 ) : undefined ;
395
410
passthroughListener ( socket , connectHostname , upstreamPort ) ;
396
411
return ; // Do not continue with TLS
397
- } else if ( sniHostname && hostnames . includes ( sniHostname ) ) {
412
+ } else if ( sniHostname && passThroughHostnames . includes ( sniHostname ) ) {
398
413
passthroughListener ( socket , sniHostname ) ; // Can't guess the port - not included in SNI
399
414
return ; // Do not continue with TLS
400
415
}
0 commit comments