Skip to content

Commit

Permalink
feat: Add MCInstall service (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
YueChen-C authored Mar 2, 2022
1 parent 219ac22 commit e764891
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ More information can be found at the links below:
- `services.startNotificationProxyService`
- `services.startHouseArrestService`
- `services.startInstrumentService`
- `services.startMCInstallService`

### Usage

Expand Down
89 changes: 89 additions & 0 deletions lib/mcinstall/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {BaseServicePlist} from '../base-service';
import {fs, plist} from '@appium/support';


const MC_INSTALL_SERVICE_NAME = 'com.apple.mobile.MCInstall';
const ACKNOWLEDGED = 'Acknowledged';

function checkACK (res, name) {
if (res.Status !== ACKNOWLEDGED) {
throw new Error(`${name} error: ${JSON.stringify(res)}`);
}
return res;
}

class MCInstallProxyService extends BaseServicePlist {
constructor (socketClient) {
super(socketClient);
}

/**
* @typedef {Object} ProfileList
* @property {Array} OrderedIdentifiers list of all profile ident
* @property {Object} ProfileManifest
* @property {Object} ProfileMetadata
* @property {String} Status
*/

/**
* Get all profiles of iOS devices
* @returns {ProfileList}
* e.g.
* {
* OrderedIdentifiers: [ '2fac1c2b3d684843189b2981c718b0132854a847a' ],
* ProfileManifest: {
* '2fac1c2b3d684843189b2981c718b0132854a847a': {
* Description: 'Charles Proxy CA (7 Dec 2020, MacBook-Pro.local)',
* IsActive: true
* }
* },
* ProfileMetadata: {
* '2fac1c2b3d684843189b2981c718b0132854a847a': {
* PayloadDisplayName: 'Charles Proxy CA (7 Dec 2020, MacBook-Pro.local)',
* PayloadRemovalDisallowed: false,
* PayloadUUID: 'B30005CC-BC73-4E42-8545-8DA6C44A8A71',
* PayloadVersion: 1
* }
* },
* Status: 'Acknowledged'
* }
*/
async getProfileList () {
const res = await this._plistService.sendPlistAndReceive({RequestType: 'GetProfileList'});
return checkACK(res, 'getProfileList');
}

/**
* Install profile to iOS device
* @param {String} path must be a certificate file .PEM .CER and more formats
* e.g: /Downloads/charles-certificate.pem
* @returns {Object} e.g. {Status: 'Acknowledged'}
*/
async installProfile (path) {
const payload = await fs.readFile(path);
const res = await this._plistService.sendPlistAndReceive({'RequestType': 'InstallProfile', 'Payload': payload});
return checkACK(res, 'installProfile');
}

/**
* Remove profile from iOS device
* @param {String} ident Query identifier list through getProfileList method
* @returns {Object} e.g. {Status: 'Acknowledged'}
*/
async removeProfile (ident) {
const profiles = await this.getProfileList();
const meta = profiles.ProfileMetadata[ident];
if (!meta) {
throw new Error(`not find installed profile ident:${ident}`);
}
const data = plist.createBinaryPlist({'PayloadType': 'Configuration',
'PayloadIdentifier': ident,
'PayloadUUID': meta.PayloadUUID,
'PayloadVersion': meta.PayloadVersion});
const res = await this._plistService.sendPlistAndReceive({'RequestType': 'RemoveProfile', 'ProfileIdentifier': data});
return checkACK(res, 'removeProfile');
}
}

export { MCInstallProxyService, MC_INSTALL_SERVICE_NAME };
export default MCInstallProxyService;
8 changes: 7 additions & 1 deletion lib/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AfcService, AFC_SERVICE_NAME } from './afc';
import { NotificationProxyService, NOTIFICATION_PROXY_SERVICE_NAME } from './notification-proxy';
import { HouseArrestService, HOUSE_ARREST_SERVICE_NAME } from './house-arrest';
import { InstrumentService, INSTRUMENT_SERVICE_NAME_VERSION_14, INSTRUMENT_SERVICE_NAME} from './instrument';
import { MCInstallProxyService, MC_INSTALL_SERVICE_NAME} from './mcinstall';
import PlistService from './plist-service';
import semver from 'semver';

Expand Down Expand Up @@ -91,6 +92,11 @@ async function startInstrumentService (udid, opts = {}) {
);
}

async function startMCInstallService (udid, opts = {}) {
const socket = await startService(udid, MC_INSTALL_SERVICE_NAME, opts.socket);
return new MCInstallProxyService(new PlistService(socket));
}

async function startService (udid, serviceName, socket, handshakeOnly = false) {
const lockdown = await startLockdownSession(udid, socket);
try {
Expand All @@ -109,5 +115,5 @@ export {
startSyslogService, startWebInspectorService,
startInstallationProxyService, startSimulateLocationService,
startAfcService, startCrashLogService, startNotificationProxyService,
startHouseArrestService, startInstrumentService
startHouseArrestService, startInstrumentService, startMCInstallService
};

0 comments on commit e764891

Please sign in to comment.