Summary: We were using an old unmaintained/abandonned library for communicating with adb server https://github.com/openstf/adbkit This was giving me issues i couldnt figure out when running flipper server. There is a popular fork written in typescript here https://github.com/DeviceFarmer/adbkit but it uses blue bird for promises. There is a fork of the fork here which i have chosen to use which is the same api as above but with es6 promises, https://github.com/UrielCh/adbkit. Both forks have a slightly different api to the original. In the original library there was a single client and any command directed at a particular device had a serial as the first argument In the new libraries you create a DeviceClient where the serial is baked in and you don't need to supply this argument every time allow-large-files Reviewed By: lblasa Differential Revision: D45569652 fbshipit-source-id: 2a23c0eaa12feaebdccadb3d343e087c0d5708d5
139 lines
3.9 KiB
TypeScript
139 lines
3.9 KiB
TypeScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
*/
|
|
|
|
import CertificateProvider from '../../utils/CertificateProvider';
|
|
import {Client} from '@u4/adbkit';
|
|
import * as androidUtil from './androidContainerUtility';
|
|
import {csrFileName, extractAppNameFromCSR} from '../../utils/certificateUtils';
|
|
|
|
const logTag = 'AndroidCertificateProvider';
|
|
|
|
export default class AndroidCertificateProvider extends CertificateProvider {
|
|
name = 'AndroidCertificateProvider';
|
|
medium = 'FS_ACCESS' as const;
|
|
|
|
constructor(private adb: Client) {
|
|
super();
|
|
}
|
|
|
|
async getTargetDeviceId(
|
|
appName: string,
|
|
appDirectory: string,
|
|
csr: string,
|
|
): Promise<string> {
|
|
console.debug(
|
|
'AndroidCertificateProvider.getTargetDeviceId',
|
|
appName,
|
|
appDirectory,
|
|
csr,
|
|
);
|
|
const devicesInAdb = await this.adb.listDevices();
|
|
if (devicesInAdb.length === 0) {
|
|
throw new Error('No Android devices found');
|
|
}
|
|
const deviceMatchList = devicesInAdb.map(async (device) => {
|
|
try {
|
|
console.debug(
|
|
'AndroidCertificateProvider.getTargetDeviceId -> matching device',
|
|
device.id,
|
|
appName,
|
|
appDirectory,
|
|
);
|
|
const result = await this.androidDeviceHasMatchingCSR(
|
|
appDirectory,
|
|
device.id,
|
|
appName,
|
|
csr,
|
|
);
|
|
return {id: device.id, ...result, error: null};
|
|
} catch (e) {
|
|
console.warn(
|
|
`Unable to check for matching CSR in ${device.id}:${appName}`,
|
|
logTag,
|
|
e,
|
|
);
|
|
return {id: device.id, isMatch: false, foundCsr: null, error: e};
|
|
}
|
|
});
|
|
const devices = await Promise.all(deviceMatchList);
|
|
const matchingIds = devices.filter((m) => m.isMatch).map((m) => m.id);
|
|
if (matchingIds.length == 0) {
|
|
const erroredDevice = devices.find((d) => d.error);
|
|
if (erroredDevice) {
|
|
throw erroredDevice.error;
|
|
}
|
|
const foundCsrs = devices
|
|
.filter((d) => d.foundCsr !== null)
|
|
.map((d) => (d.foundCsr ? encodeURI(d.foundCsr) : 'null'));
|
|
console.warn(`Looking for CSR (url encoded):
|
|
|
|
${encodeURI(this.santitizeString(csr))}
|
|
|
|
Found these:
|
|
|
|
${foundCsrs.join('\n\n')}`);
|
|
throw new Error(`No matching device found for app: ${appName}`);
|
|
}
|
|
if (matchingIds.length > 1) {
|
|
console.warn(
|
|
new Error('[conn] More than one matching device found for CSR'),
|
|
csr,
|
|
);
|
|
}
|
|
return matchingIds[0];
|
|
}
|
|
|
|
protected async deployOrStageFileForDevice(
|
|
destination: string,
|
|
filename: string,
|
|
contents: string,
|
|
csr: string,
|
|
) {
|
|
const appName = await extractAppNameFromCSR(csr);
|
|
const deviceId = await this.getTargetDeviceId(appName, destination, csr);
|
|
await androidUtil.push(
|
|
this.adb.getDevice(deviceId),
|
|
deviceId,
|
|
appName,
|
|
destination + filename,
|
|
contents,
|
|
);
|
|
}
|
|
|
|
private async androidDeviceHasMatchingCSR(
|
|
directory: string,
|
|
deviceId: string,
|
|
processName: string,
|
|
csr: string,
|
|
): Promise<{isMatch: boolean; foundCsr: string}> {
|
|
const deviceCsr = await androidUtil.pull(
|
|
this.adb.getDevice(deviceId),
|
|
deviceId,
|
|
processName,
|
|
directory + csrFileName,
|
|
);
|
|
// Santitize both of the string before comparation
|
|
// The csr string extraction on client side return string in both way
|
|
const [sanitizedDeviceCsr, sanitizedClientCsr] = [
|
|
deviceCsr.toString(),
|
|
csr,
|
|
].map((s) => this.santitizeString(s));
|
|
console.debug(
|
|
'AndroidCertificateProvider.androidDeviceHasMatchingCSR',
|
|
directory,
|
|
deviceId,
|
|
processName,
|
|
sanitizedDeviceCsr,
|
|
sanitizedClientCsr,
|
|
);
|
|
const isMatch = sanitizedDeviceCsr === sanitizedClientCsr;
|
|
return {isMatch: isMatch, foundCsr: sanitizedDeviceCsr};
|
|
}
|
|
}
|