StikDebug/StikJIT/idevice/profiles.m
Stephen f3d2dec2e5
Semi-Rewrite – Major Refactor & Codebase Cleanup (#328)
* Change default UPLOAD_IPA value to 'false'

* Part 1 of the great debloat

* Remove Mini Tools, adopt native alerts in StikJITApp, and update HomeView UI

* Refactor views to use native SwiftUI alerts instead of CustomErrorView

* Update ProfileView to use minimal List UI style with native alerts

* Fix up homeview

* Squash: Redesign HomeView, remove Feature Flags, enable all tabs, refactor alerts, and updates

- Redesign HomeView to a simpler style with 'Enable JIT' action.
- Remove 'Help & Info' and 'Quick Launch' from Home.
- Fix DDI false positive by starting heartbeat on HomeView appear.
- Restore robust DDI checks and update app name to 'StikDebug'.
- Remove FeatureFlags and Beta tags; enable 'profiles', 'processes', 'location' tabs by default.
- Remove 'Devices' tab; add 'Target Device IP' field in Settings.
- Hardcode 'Settings' tab to 4th position.
- Refactor 'SettingsView' tab customization to be reorder-only.
- Remove 'Appearance' theme section from Settings.
- Refactor custom error cards to use native SwiftUI/UIKit alerts across the app (HomeView, ConsoleLogsView, ProfileView, etc).
- Restore JS callback support and script execution logic in HomeView.
- Add necessary Color extensions.
- Update project version to 3.0.0.

* Clean up: Remove In-App Purchases, update checker, and unused IPA installer code

* delete more bloat

* fix build issue

* Remove widget to save on App IDs

* Attempt heartbeat restart when Enable JIT button is pressed

* Get rid of PIP

* Update Tab Naming

* remove more bloat

* final changes for today

* Remove unused packages

* UX Improvements

* revert bundle id

* Update README.md

* remove old idevice

* add updated idevice

* Fix ddi false positive errors and add templates for GitHub stuff

* Remove more unused stuff.

* Fix links and update README content

* Update compatibility table in README.md

* Improve performance of console logs

* Remove old code

* Change default UPLOAD_IPA value to 'true'

* remove the welcome sheet

* import support for .mobiledevicepair

* Add back LC JIT stuff

* Fix heartbeat issues with LC JIT

* update the build yml for nightly stuff

* Update README.md

* Update README.md

* Update GitHub Actions workflow for IPA build

* Update GitHub Actions workflow for nightly builds

* Add URL scheme to kill tasks stikjit://kill-process?pid=1234

* fix build issues

* Added App Intents

* Unify the error handling style across the app
2026-02-25 13:44:00 -05:00

170 lines
5.4 KiB
Objective-C

//
// profiles.m
// StikDebug
//
// Created by s s on 2025/11/29.
//
#include "profiles.h"
#import "JITEnableContext.h"
#import "JITEnableContextInternal.h"
@import Foundation;
NSError* makeError(int code, NSString* msg) {
return [NSError errorWithDomain:@"profiles" code:code userInfo:@{NSLocalizedDescriptionKey: msg}];
}
NSArray<NSData*>* fetchAppProfiles(IdeviceProviderHandle* provider, NSError** error) {
MisagentClientHandle *misagentHandle = NULL;
IdeviceFfiError *err = misagent_connect(provider, &misagentHandle);
if (err) {
*error = makeError(err->code, @(err->message));
idevice_error_free(err);
return nil;
}
uint8_t **profileArr = NULL;
size_t profileCount = 0;
size_t *profileLengthArr = NULL;
err = misagent_copy_all(misagentHandle, &profileArr, &profileLengthArr, &profileCount);
if (err) {
*error = makeError((err)->code, @((err)->message));
misagent_client_free(misagentHandle);
idevice_error_free(err);
return nil;
}
NSMutableArray* ans = [NSMutableArray array];
for(int i = 0; i < profileCount; ++i) {
size_t len = profileLengthArr[i];
uint8_t* profile = profileArr[i];
NSData* profileData = [NSData dataWithBytes:profile length:len];
[ans addObject:profileData];
}
misagent_free_profiles(profileArr, profileLengthArr, profileCount);
misagent_client_free(misagentHandle);
return ans;
}
bool removeProfile(IdeviceProviderHandle* provider, NSString* uuid, NSError** error) {
MisagentClientHandle *misagentHandle = NULL;
IdeviceFfiError * err = misagent_connect(provider, &misagentHandle);
if (err) {
*error = makeError(err->code, @(err->message));
idevice_error_free(err);
return false;
}
err = misagent_remove(misagentHandle, [uuid UTF8String]);
if (err) {
*error = makeError((err)->code, @((err)->message));
misagent_client_free(misagentHandle);
idevice_error_free(err);
return false;
}
misagent_client_free(misagentHandle);
return true;
}
bool addProfile(IdeviceProviderHandle* provider, NSData* profile, NSError** error) {
MisagentClientHandle *misagentHandle = NULL;
IdeviceFfiError * err = misagent_connect(provider, &misagentHandle);
if (err) {
*error = makeError(err->code, @(err->message));
idevice_error_free(err);
return false;
}
err = misagent_install(misagentHandle, [profile bytes], [profile length]);
if (err) {
*error = makeError((err)->code, @((err)->message));
misagent_client_free(misagentHandle);
idevice_error_free(err);
return false;
}
misagent_client_free(misagentHandle);
return true;
}
@implementation CMSDecoderHelper
+ (NSData*)decodeCMSData:(NSData *)cmsData
// outCerts:(NSArray<id> * _Nullable * _Nullable)outCerts
error:(NSError * _Nullable * _Nullable)error
{
if (!cmsData || cmsData.length == 0) {
if (error) {
*error = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSURLErrorBadURL
userInfo:@{NSLocalizedDescriptionKey: @"Invalid or empty CMS payload"}];
}
return nil;
}
NSData *xmlStart = [@"<?xml" dataUsingEncoding:NSASCIIStringEncoding];
NSData *plistEnd = [@"</plist>" dataUsingEncoding:NSASCIIStringEncoding];
NSData *binaryMagic = [@"bplist00" dataUsingEncoding:NSASCIIStringEncoding];
NSRange startRange = [cmsData rangeOfData:xmlStart options:0 range:NSMakeRange(0, cmsData.length)];
if (startRange.location != NSNotFound) {
NSRange endSearchRange = NSMakeRange(startRange.location, cmsData.length - startRange.location);
NSRange endRange = [cmsData rangeOfData:plistEnd options:0 range:endSearchRange];
if (endRange.location != NSNotFound) {
NSUInteger plistEndIndex = NSMaxRange(endRange);
if (plistEndIndex > startRange.location && plistEndIndex <= cmsData.length) {
return [cmsData subdataWithRange:NSMakeRange(startRange.location, plistEndIndex - startRange.location)];
}
}
}
NSRange binaryRange = [cmsData rangeOfData:binaryMagic options:0 range:NSMakeRange(0, cmsData.length)];
if (binaryRange.location != NSNotFound) {
return [cmsData subdataWithRange:NSMakeRange(binaryRange.location, cmsData.length - binaryRange.location)];
}
if (error) {
*error = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSFileReadUnknownError
userInfo:@{NSLocalizedDescriptionKey: @"Unable to extract plist from CMS payload"}];
}
return nil;
}
@end
@implementation JITEnableContext(Profile)
- (NSArray<NSData*>*)fetchAllProfiles:(NSError **)error {
[self ensureHeartbeatWithError:error];
if(*error) {
return nil;
}
return fetchAppProfiles(provider, error);
}
- (BOOL)removeProfileWithUUID:(NSString*)uuid error:(NSError **)error {
[self ensureHeartbeatWithError:error];
if(*error) {
return NO;
}
return removeProfile(provider, uuid, error);
}
- (BOOL)addProfile:(NSData*)profile error:(NSError **)error {
[self ensureHeartbeatWithError:error];
if(*error) {
return NO;
}
return addProfile(provider, profile, error);
}
@end