@@ -20,7 +20,10 @@ import {
2020 getFridaServer ,
2121 killProcess ,
2222 launchScript ,
23- testAndSelectProxyAddress
23+ testAndSelectProxyAddress ,
24+ createFridaSessionCache ,
25+ clearFridaSessionCache ,
26+ getOrCreateFridaSession
2427} from './frida-integration' ;
2528
2629const ANDROID_DEVICE_HTK_PATH = '/data/local/tmp/.httptoolkit' ;
@@ -181,47 +184,15 @@ const getFridaStream = (hostId: string, deviceClient: DeviceClient) =>
181184 } ) ;
182185 } ) ;
183186
184- const fridaSessionCache : Record < string , {
185- fridaSession : FridaJs . FridaSession ,
186- cleanup : ( ) => void ,
187- timeout : NodeJS . Timeout
188- } > = { } ;
187+ // Frida session cache for Android hosts
188+ const fridaSessionCache = createFridaSessionCache ( ) ;
189189
190- const FRIDA_SESSION_IDLE_TIMEOUT = 30_000 ;
191-
192- function clearFridaSessionCache ( hostId : string ) {
193- const cached = fridaSessionCache [ hostId ] ;
194- if ( cached ) {
195- clearTimeout ( cached . timeout ) ;
196- cached . cleanup ( ) ;
197- delete fridaSessionCache [ hostId ] ;
198- }
199- }
200-
201- async function getOrCreateFridaSession ( hostId : string , deviceClient : DeviceClient ) {
202- let cached = fridaSessionCache [ hostId ] ;
203- if ( cached ) {
204- clearTimeout ( cached . timeout ) ;
205- cached . timeout = setTimeout ( ( ) => clearFridaSessionCache ( hostId ) , FRIDA_SESSION_IDLE_TIMEOUT ) ;
206- return { fridaSession : cached . fridaSession , wasCached : true } ;
207- }
208-
209- const fridaStream = await getFridaStream ( hostId , deviceClient ) ;
210- const fridaSession = await FridaJs . connect ( { stream : fridaStream } ) ;
211-
212- const timeout = setTimeout ( ( ) => clearFridaSessionCache ( hostId ) , FRIDA_SESSION_IDLE_TIMEOUT ) ;
213- const cleanup = ( ) => fridaSession . disconnect ( )
214- . catch ( ( ) => { } )
215- . finally ( ( ) => fridaStream . destroy ( ) ) ;
216-
217- fridaSessionCache [ hostId ] = {
218- fridaSession,
219- cleanup,
220- timeout
221- } ;
222-
223- fridaStream . on ( 'error' , cleanup ) ;
224- return { fridaSession, wasCached : false } ;
190+ async function getOrCreateAndroidFridaSession ( hostId : string , deviceClient : DeviceClient ) {
191+ return getOrCreateFridaSession (
192+ fridaSessionCache ,
193+ hostId ,
194+ ( ) => getFridaStream ( hostId , deviceClient )
195+ ) ;
225196}
226197
227198export async function getAndroidFridaTargets ( adbClient : AdbClient , hostId : string ) : Promise < Array < {
@@ -235,11 +206,10 @@ export async function getAndroidFridaTargets(adbClient: AdbClient, hostId: strin
235206 let wasCached : boolean = false ;
236207
237208 try {
238- ( { fridaSession, wasCached } = await getOrCreateFridaSession ( hostId , deviceClient ) ) ;
239- const apps = await fridaSession . enumerateApplications ( ) ;
240- return apps ;
209+ ( { fridaSession, wasCached } = await getOrCreateAndroidFridaSession ( hostId , deviceClient ) ) ;
210+ return await fridaSession . enumerateApplications ( ) ;
241211 } catch ( e ) {
242- clearFridaSessionCache ( hostId ) ;
212+ clearFridaSessionCache ( fridaSessionCache , hostId ) ;
243213 if ( wasCached ) {
244214 // When a cached session fails, we retry with a fresh one:
245215 return getAndroidFridaTargets ( adbClient , hostId ) ;
0 commit comments