11import * as _ from 'lodash' ;
22import net = require( 'net' ) ;
33import * as url from 'url' ;
4- import * as tls from 'tls' ;
54import * as http from 'http' ;
6- import * as fs from 'fs/promises' ;
75import * as WebSocket from 'ws' ;
86
97import {
@@ -12,10 +10,11 @@ import {
1210 deserializeProxyConfig
1311} from "../../serialization/serialization" ;
1412
15- import { OngoingRequest , RawHeaders } from "../../types" ;
13+ import { Headers , OngoingRequest , RawHeaders } from "../../types" ;
1614
1715import {
1816 CloseConnectionHandler ,
17+ RequestHandlerOptions ,
1918 ResetConnectionHandler ,
2019 TimeoutHandler
2120} from '../requests/request-handlers' ;
@@ -60,7 +59,9 @@ export interface WebSocketHandler extends WebSocketHandlerDefinition {
6059 // The raw socket on which we'll be communicating
6160 socket : net . Socket ,
6261 // Initial data received
63- head : Buffer
62+ head : Buffer ,
63+ // Other general handler options
64+ options : RequestHandlerOptions
6465 ) : Promise < void > ;
6566}
6667
@@ -219,7 +220,7 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
219220 return this . _trustedCACertificates ;
220221 }
221222
222- async handle ( req : OngoingRequest , socket : net . Socket , head : Buffer ) {
223+ async handle ( req : OngoingRequest , socket : net . Socket , head : Buffer , options : RequestHandlerOptions ) {
223224 this . initializeWsServer ( ) ;
224225
225226 let { protocol, hostname, port, path } = url . parse ( req . url ! ) ;
@@ -266,7 +267,7 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
266267 hostHeader [ 1 ] = updateHostHeader ;
267268 } // Otherwise: falsey means don't touch it.
268269
269- await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head ) ;
270+ await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head , options ) ;
270271 } else if ( ! hostname ) { // No hostname in URL means transparent proxy, so use Host header
271272 const hostHeader = req . headers [ hostHeaderName ] ;
272273 [ hostname , port ] = hostHeader ! . split ( ':' ) ;
@@ -280,14 +281,14 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
280281 }
281282
282283 const wsUrl = `${ protocol } ://${ hostname } ${ port ? ':' + port : '' } ${ path } ` ;
283- await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head ) ;
284+ await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head , options ) ;
284285 } else {
285286 // Connect directly according to the specified URL
286287 const wsUrl = `${
287288 protocol ! . replace ( 'http' , 'ws' )
288289 } //${ hostname } ${ port ? ':' + port : '' } ${ path } `;
289290
290- await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head ) ;
291+ await this . connectUpstream ( wsUrl , reqMessage , rawHeaders , socket , head , options ) ;
291292 }
292293 }
293294
@@ -296,7 +297,8 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
296297 req : http . IncomingMessage ,
297298 rawHeaders : RawHeaders ,
298299 incomingSocket : net . Socket ,
299- head : Buffer
300+ head : Buffer ,
301+ options : RequestHandlerOptions
300302 ) {
301303 const parsedUrl = url . parse ( wsUrl ) ;
302304
@@ -370,6 +372,19 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
370372 ...caConfig
371373 } as WebSocket . ClientOptions & { lookup : any , maxPayload : number } ) ;
372374
375+ if ( options . emitEventCallback ) {
376+ const upstreamReq = ( upstreamWebSocket as any as { _req : http . ClientRequest } ) . _req ;
377+ options . emitEventCallback ( 'passthrough-websocket-connect' , {
378+ method : upstreamReq . method ,
379+ protocol : upstreamReq . protocol . replace ( / : $ / , '' ) ,
380+ hostname : upstreamReq . host ,
381+ port : effectivePort . toString ( ) ,
382+ path : upstreamReq . path ,
383+ rawHeaders : objectHeadersToRaw ( upstreamReq . getHeaders ( ) as Headers ) ,
384+ subprotocols : filteredSubprotocols
385+ } ) ;
386+ }
387+
373388 upstreamWebSocket . once ( 'open' , ( ) => {
374389 // Used in the subprotocol selection handler during the upgrade:
375390 ( req as InterceptedWebSocketRequest ) . upstreamWebSocketProtocol = upstreamWebSocket . protocol || false ;
0 commit comments