@@ -51,6 +51,8 @@ export class ManagerSingleton {
5151 private constructor ( ) {
5252 // private constructor prevents direct instantiation
5353 const port : any = process . env . DROIDGROUND_ADB_PORT ?? "" ;
54+ const serialEnv : any = process . env . DROIDGROUND_ADB_SERIAL ?? "" ;
55+ const adbSerial = serialEnv . trim ( ) . length === 0 ? undefined : serialEnv . trim ( ) ;
5456 const exploitAppDuration : any = process . env . DROIDGROUND_EXPLOIT_APP_DURATION ?? "" ;
5557 const exploitAppmaxSizeEnv : any = process . env . DROIDGROUND_EXPLOIT_APP_MAX_SIZE ?? "" ;
5658 const exploitAppMaxSize : number =
@@ -77,6 +79,7 @@ export class ManagerSingleton {
7779 adb : {
7880 host : process . env . DROIDGROUND_ADB_HOST ?? "localhost" ,
7981 port : isNaN ( port ) || port . trim ( ) . length === 0 ? 5037 : parseInt ( port ) ,
82+ serial : adbSerial ,
8083 } ,
8184 features : {
8285 basePath : process . env . DROIDGROUND_BASE_PATH ?? "" ,
@@ -147,6 +150,15 @@ export class ManagerSingleton {
147150 return ;
148151 }
149152
153+ const requestedSerial = this . config . adb . serial ;
154+ if ( requestedSerial ) {
155+ const addedSerials = _devices . map ( device => device . serial ) ;
156+ if ( ! addedSerials . includes ( requestedSerial ) ) {
157+ Logger . debug ( `Ignoring added devices because serial '${ requestedSerial } ' was not included.` ) ;
158+ return ;
159+ }
160+ }
161+
150162 await this . setupAdb ( ) ;
151163 await this . setCtf ( ) ;
152164
@@ -204,9 +216,21 @@ export class ManagerSingleton {
204216 throw new Error ( "No device connected" ) ;
205217 }
206218
207- Logger . debug ( "Listing devices (and using the first one)" ) ;
208- Logger . debug ( devices ) ;
209- const device = devices [ 0 ] ;
219+ const serial = this . config . adb . serial ;
220+ let device : AdbServerClient . Device | undefined ;
221+ if ( serial ) {
222+ Logger . debug ( `Looking for device with serial '${ serial } '` ) ;
223+ device = devices . find ( d => d . serial === serial ) ;
224+ if ( ! device ) {
225+ Logger . error ( `ADB device with serial '${ serial } ' not found` ) ;
226+ Logger . debug ( `Connected devices: ${ JSON . stringify ( devices ) } ` ) ;
227+ throw new Error ( `ADB device with serial '${ serial } ' not found` ) ;
228+ }
229+ } else {
230+ Logger . debug ( "Listing devices (and using the first one)" ) ;
231+ Logger . debug ( devices ) ;
232+ device = devices [ 0 ] ;
233+ }
210234
211235 this . device = device ;
212236
@@ -337,7 +361,8 @@ export class ManagerSingleton {
337361 }
338362
339363 Logger . info ( "Running setup.sh script..." ) ;
340- const scriptOutput = execFileSync ( setupScript , {
364+ const setupArgs = this . config . adb . serial ? [ this . config . adb . serial ] : [ ] ;
365+ const scriptOutput = execFileSync ( setupScript , setupArgs , {
341366 cwd : process . env . DROIDGROUND_INIT_SCRIPTS_FOLDER ,
342367 stdio : "pipe" ,
343368 encoding : "utf-8" ,
@@ -366,7 +391,8 @@ export class ManagerSingleton {
366391 const initDFolder = process . env . DROIDGROUND_INIT_SCRIPTS_FOLDER ?? "/init.d" ;
367392 const resetScript = path . resolve ( initDFolder , "reset.sh" ) ;
368393 if ( safeFileExists ( resetScript ) ) {
369- const scriptOutput = execFileSync ( resetScript , {
394+ const resetArgs = this . config . adb . serial ? [ this . config . adb . serial ] : [ ] ;
395+ const scriptOutput = execFileSync ( resetScript , resetArgs , {
370396 cwd : process . env . DROIDGROUND_INIT_SCRIPTS_FOLDER ,
371397 stdio : "pipe" ,
372398 encoding : "utf-8" ,
0 commit comments