mirror of
https://github.com/StikDebug/StikDebug.git
synced 2026-10-15 20:10:30 +00:00
204 lines
6 KiB
Objective-C
204 lines
6 KiB
Objective-C
//
|
|
// JITEnableContext.m
|
|
// StikJIT
|
|
//
|
|
// Created by s s on 2025/3/28.
|
|
//
|
|
#include "idevice.h"
|
|
#include <arpa/inet.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "heartbeat.h"
|
|
#include "jit.h"
|
|
#include "applist.h"
|
|
|
|
#include "JITEnableContext.h"
|
|
#import "StikDebug-Swift.h"
|
|
|
|
JITEnableContext* sharedJITContext = nil;
|
|
|
|
@implementation JITEnableContext {
|
|
bool heartbeatRunning;
|
|
IdeviceProviderHandle* provider;
|
|
}
|
|
|
|
+ (instancetype)shared {
|
|
if (!sharedJITContext) {
|
|
sharedJITContext = [[JITEnableContext alloc] init];
|
|
}
|
|
return sharedJITContext;
|
|
}
|
|
|
|
- (instancetype)init {
|
|
NSFileManager* fm = [NSFileManager defaultManager];
|
|
NSURL* docPathUrl = [fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
|
|
NSURL* logURL = [docPathUrl URLByAppendingPathComponent:@"idevice_log.txt"];
|
|
idevice_init_logger(Info, Debug, (char*)logURL.path.UTF8String);
|
|
return self;
|
|
}
|
|
|
|
- (NSError*)errorWithStr:(NSString*)str code:(int)code {
|
|
return [NSError errorWithDomain:@"StikJIT"
|
|
code:code
|
|
userInfo:@{ NSLocalizedDescriptionKey: str }];
|
|
}
|
|
|
|
- (LogFuncC)createCLogger:(LogFunc)logger {
|
|
return ^(const char* format, ...) {
|
|
va_list args;
|
|
va_start(args, format);
|
|
NSString* fmt = [NSString stringWithCString:format encoding:NSASCIIStringEncoding];
|
|
NSString* message = [[NSString alloc] initWithFormat:fmt arguments:args];
|
|
NSLog(@"%@", message);
|
|
|
|
if ([message containsString:@"ERROR"] || [message containsString:@"Error"]) {
|
|
[[LogManagerBridge shared] addErrorLog:message];
|
|
} else if ([message containsString:@"WARNING"] || [message containsString:@"Warning"]) {
|
|
[[LogManagerBridge shared] addWarningLog:message];
|
|
} else if ([message containsString:@"DEBUG"]) {
|
|
[[LogManagerBridge shared] addDebugLog:message];
|
|
} else {
|
|
[[LogManagerBridge shared] addInfoLog:message];
|
|
}
|
|
|
|
if (logger) {
|
|
logger(message);
|
|
}
|
|
va_end(args);
|
|
};
|
|
}
|
|
|
|
- (IdevicePairingFile*)getPairingFileWithError:(NSError**)error {
|
|
NSFileManager* fm = [NSFileManager defaultManager];
|
|
NSURL* docPathUrl = [fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
|
|
NSURL* pairingFileURL = [docPathUrl URLByAppendingPathComponent:@"pairingFile.plist"];
|
|
|
|
if (![fm fileExistsAtPath:pairingFileURL.path]) {
|
|
NSLog(@"Pairing file not found!");
|
|
*error = [self errorWithStr:@"Pairing file not found!" code:-17];
|
|
return nil;
|
|
}
|
|
|
|
IdevicePairingFile* pairingFile = NULL;
|
|
IdeviceFfiError* err = idevice_pairing_file_read(pairingFileURL.fileSystemRepresentation, &pairingFile);
|
|
if (err) {
|
|
*error = [self errorWithStr:@"Failed to read pairing file!" code:err->code];
|
|
return nil;
|
|
}
|
|
return pairingFile;
|
|
}
|
|
|
|
- (void)startHeartbeatWithCompletionHandler:(HeartbeatCompletionHandler)completionHandler
|
|
logger:(LogFunc)logger
|
|
{
|
|
NSError* err = nil;
|
|
IdevicePairingFile* pairingFile = [self getPairingFileWithError:&err];
|
|
if (err) {
|
|
// silently swallow “pairing file not found” (-17)
|
|
if (err.code == -17) {
|
|
return;
|
|
}
|
|
// for all other errors, log and forward
|
|
if (logger) {
|
|
logger(err.localizedDescription);
|
|
}
|
|
completionHandler(err.code, err.localizedDescription);
|
|
return;
|
|
}
|
|
|
|
if(heartbeatRunning) {
|
|
return;
|
|
}
|
|
startHeartbeat(
|
|
pairingFile,
|
|
&provider,
|
|
&heartbeatRunning,
|
|
^(int result, const char *message) {
|
|
completionHandler(result,
|
|
[NSString stringWithCString:message
|
|
encoding:NSASCIIStringEncoding]);
|
|
},
|
|
[self createCLogger:logger]
|
|
);
|
|
}
|
|
|
|
- (void)ensureHeartbeat {
|
|
// wait a bit until heartbeat finish. wait at most 10s
|
|
int deadline = 50;
|
|
while((!lastHeartbeatDate || [[NSDate now] timeIntervalSinceDate:lastHeartbeatDate] > 15) && deadline) {
|
|
--deadline;
|
|
usleep(200);
|
|
}
|
|
}
|
|
|
|
- (BOOL)debugAppWithBundleID:(NSString*)bundleID logger:(LogFunc)logger jsCallback:(DebugAppCallback)jsCallback {
|
|
if (!provider) {
|
|
if (logger) {
|
|
logger(@"Provider not initialized!");
|
|
}
|
|
NSLog(@"Provider not initialized!");
|
|
return NO;
|
|
}
|
|
|
|
[self ensureHeartbeat];
|
|
|
|
return debug_app(provider,
|
|
[bundleID UTF8String],
|
|
[self createCLogger:logger], jsCallback) == 0;
|
|
}
|
|
|
|
- (BOOL)debugAppWithPID:(int)pid logger:(LogFunc)logger jsCallback:(DebugAppCallback)jsCallback {
|
|
if (!provider) {
|
|
if (logger) {
|
|
logger(@"Provider not initialized!");
|
|
}
|
|
NSLog(@"Provider not initialized!");
|
|
return NO;
|
|
}
|
|
|
|
[self ensureHeartbeat];
|
|
|
|
return debug_app_pid(provider,
|
|
pid,
|
|
[self createCLogger:logger], jsCallback) == 0;
|
|
}
|
|
|
|
- (NSDictionary<NSString*, NSString*>*)getAppListWithError:(NSError**)error {
|
|
if (!provider) {
|
|
NSLog(@"Provider not initialized!");
|
|
*error = [self errorWithStr:@"Provider not initialized!" code:-1];
|
|
return nil;
|
|
}
|
|
|
|
NSString* errorStr = nil;
|
|
NSDictionary<NSString*, NSString*>* apps = list_installed_apps(provider, &errorStr);
|
|
if (errorStr) {
|
|
*error = [self errorWithStr:errorStr code:-17];
|
|
return nil;
|
|
}
|
|
return apps;
|
|
}
|
|
|
|
- (UIImage*)getAppIconWithBundleId:(NSString*)bundleId error:(NSError**)error {
|
|
if (!provider) {
|
|
NSLog(@"Provider not initialized!");
|
|
*error = [self errorWithStr:@"Provider not initialized!" code:-1];
|
|
return nil;
|
|
}
|
|
|
|
NSString* errorStr = nil;
|
|
UIImage* icon = getAppIcon(provider, bundleId, &errorStr);
|
|
if (errorStr) {
|
|
*error = [self errorWithStr:errorStr code:-17];
|
|
return nil;
|
|
}
|
|
return icon;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
if (provider) {
|
|
idevice_provider_free(provider);
|
|
}
|
|
}
|
|
|
|
@end
|