Skip to content

Commit 94e6630

Browse files
committed
Don't inject certs via ADB if already present
1 parent 09675c6 commit 94e6630

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

custom-typings/adbkit.d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ declare module 'adbkit' {
77
type: string;
88
}
99

10+
export interface File {
11+
name: string;
12+
}
13+
1014
export interface AdbClient {
1115
listDevices(): Promise<Device[]>;
1216
isInstalled(id: string, pkg: string): Promise<boolean>;
@@ -20,12 +24,17 @@ declare module 'adbkit' {
2024
data?: string;
2125
}
2226
): Promise<true>;
27+
readdir(id: string, path: string): Promise<Array<File>>;
2328
push(
2429
id: string,
2530
contents: string | stream.Readable,
2631
path: string,
2732
mode?: number
28-
): Promise<events.EventEmitter>
33+
): Promise<events.EventEmitter>;
34+
pull(
35+
id: string,
36+
path: string
37+
): Promise<events.EventEmitter & { cancel: () => void }>
2938
shell(id: string, cmd: string | string[]): Promise<stream.Readable>;
3039
root(id: string): Promise<true>;
3140
}

src/interceptors/android/adb-commands.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ export async function getRootCommand(adbClient: adb.AdbClient, deviceId: string)
8282
return validRootCommands[0];
8383
}
8484

85+
export async function hasCertInstalled(
86+
adbClient: adb.AdbClient,
87+
deviceId: string,
88+
certHash: string
89+
) {
90+
try {
91+
const certPath = `/system/etc/security/cacerts/${certHash}.0`;
92+
const certStream = await adbClient.pull(deviceId, certPath);
93+
94+
// Wait until it's clear that the read is successful
95+
await new Promise((resolve, reject) => {
96+
certStream.on('progress', resolve);
97+
certStream.on('end', resolve);
98+
99+
certStream.on('error', reject);
100+
});
101+
102+
certStream.cancel();
103+
return true;
104+
} catch (e) {
105+
// If we fail to read the cert somehow, it's probably not there
106+
return false;
107+
}
108+
}
109+
85110
export async function injectSystemCertificate(
86111
adbClient: adb.AdbClient,
87112
deviceId: string,

src/interceptors/android/android-adb-interceptor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
getRootCommand,
1616
pushFile,
1717
injectSystemCertificate,
18-
stringAsStream
18+
stringAsStream,
19+
hasCertInstalled
1920
} from './adb-commands';
2021
import { streamLatestApk } from './fetch-apk';
2122

@@ -141,8 +142,14 @@ export class AndroidAdbInterceptor implements Interceptor {
141142
const cert = forge.pki.certificateFromPem(certContent);
142143

143144
try {
144-
const certName = getCertificateHash(cert);
145-
const certPath = `${ANDROID_TEMP}/${certName}.0`;
145+
const certHash = getCertificateHash(cert);
146+
147+
if (await hasCertInstalled(this.adbClient, deviceId, certHash)) {
148+
console.log("Cert already installed, nothing to do");
149+
return;
150+
}
151+
152+
const certPath = `${ANDROID_TEMP}/${certHash}.0`;
146153
console.log(`Adding cert file as ${certPath}`);
147154

148155
await pushFile(

0 commit comments

Comments
 (0)