1- import MitmSocket , { IGoTlsSocketConnectOpts } from '@secret-agent/mitm-socket' ;
1+ import MitmSocket from '@secret-agent/mitm-socket' ;
22import * as http2 from 'http2' ;
33import { ClientHttp2Session , Http2ServerRequest } from 'http2' ;
44import Log from '@secret-agent/commons/Logger' ;
@@ -10,6 +10,7 @@ import ITcpSettings from '@secret-agent/interfaces/ITcpSettings';
1010import ITlsSettings from '@secret-agent/interfaces/ITlsSettings' ;
1111import Resolvable from '@secret-agent/commons/Resolvable' ;
1212import IHttp2ConnectSettings from '@secret-agent/interfaces/IHttp2ConnectSettings' ;
13+ import IHttpSocketConnectOptions from '@secret-agent/interfaces/IHttpSocketConnectOptions' ;
1314import IMitmRequestContext from '../interfaces/IMitmRequestContext' ;
1415import MitmRequestContext from './MitmRequestContext' ;
1516import RequestSession from '../handlers/RequestSession' ;
@@ -135,27 +136,7 @@ export default class MitmRequestAgent {
135136 return await pool . isHttp2 ( false , ( ) => this . createSocketConnection ( options ) ) ;
136137 }
137138
138- private async assignSocket (
139- ctx : IMitmRequestContext ,
140- options : IGoTlsSocketConnectOpts & { headers : IResourceHeaders } ,
141- ) : Promise < MitmSocket > {
142- ctx . setState ( ResourceState . GetSocket ) ;
143- const pool = this . getSocketPoolByOrigin ( ctx . url . origin ) ;
144-
145- options . isSsl = ctx . isSSL ;
146- options . keepAlive = ! (
147- ( options . headers . connection ?? options . headers . Connection ) as string
148- ) ?. match ( / c l o s e / i) ;
149- options . isWebsocket = ctx . isUpgrade ;
150-
151- const mitmSocket = await pool . getSocket ( options . isWebsocket , ( ) =>
152- this . createSocketConnection ( options ) ,
153- ) ;
154- MitmRequestContext . assignMitmSocket ( ctx , mitmSocket ) ;
155- return mitmSocket ;
156- }
157-
158- private async createSocketConnection ( options : IGoTlsSocketConnectOpts ) : Promise < MitmSocket > {
139+ public async createSocketConnection ( options : IHttpSocketConnectOptions ) : Promise < MitmSocket > {
159140 const session = this . session ;
160141
161142 const dnsLookupTime = new Date ( ) ;
@@ -187,6 +168,25 @@ export default class MitmRequestAgent {
187168 return mitmSocket ;
188169 }
189170
171+ private async assignSocket (
172+ ctx : IMitmRequestContext ,
173+ options : IHttpSocketConnectOptions & { headers : IResourceHeaders } ,
174+ ) : Promise < MitmSocket > {
175+ ctx . setState ( ResourceState . GetSocket ) ;
176+ const pool = this . getSocketPoolByOrigin ( ctx . url . origin ) ;
177+
178+ options . isSsl = ctx . isSSL ;
179+ options . keepAlive = ! ( ( options . headers . connection ??
180+ options . headers . Connection ) as string ) ?. match ( / c l o s e / i) ;
181+ options . isWebsocket = ctx . isUpgrade ;
182+
183+ const mitmSocket = await pool . getSocket ( options . isWebsocket , ( ) =>
184+ this . createSocketConnection ( options ) ,
185+ ) ;
186+ MitmRequestContext . assignMitmSocket ( ctx , mitmSocket ) ;
187+ return mitmSocket ;
188+ }
189+
190190 private getSocketPoolByOrigin ( origin : string ) : SocketPool {
191191 let lookup = origin . split ( '://' ) . pop ( ) ;
192192 if ( ! lookup . includes ( ':' ) && origin . includes ( '://' ) ) {
0 commit comments