-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
LCSharedUtils.m
334 lines (285 loc) · 13 KB
/
LCSharedUtils.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#import "LCSharedUtils.h"
#import "UIKitPrivate.h"
extern NSUserDefaults *lcUserDefaults;
extern NSString *lcAppUrlScheme;
@implementation LCSharedUtils
+ (NSString *)appGroupID {
static dispatch_once_t once;
static NSString *appGroupID = @"group.com.SideStore.SideStore";
dispatch_once(&once, ^{
for (NSString *group in NSBundle.mainBundle.infoDictionary[@"ALTAppGroups"]) {
NSURL *path = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:group];
NSURL *bundlePath = [path URLByAppendingPathComponent:@"Apps/com.kdt.livecontainer/App.app"];
if ([NSFileManager.defaultManager fileExistsAtPath:bundlePath.path]) {
// This will fail if LiveContainer is installed in both stores, but it should never be the case
appGroupID = group;
return;
}
}
});
return appGroupID;
}
+ (NSString *)certificatePassword {
NSString* ans = [[[NSUserDefaults alloc] initWithSuiteName:[self appGroupID]] objectForKey:@"LCCertificatePassword"];
if(ans) {
return ans;
} else {
return [lcUserDefaults objectForKey:@"LCCertificatePassword"];
}
}
+ (BOOL)launchToGuestApp {
NSString *urlScheme;
NSString *tsPath = [NSString stringWithFormat:@"%@/../_TrollStore", NSBundle.mainBundle.bundlePath];
int tries = 1;
if (!access(tsPath.UTF8String, F_OK)) {
urlScheme = @"apple-magnifier://enable-jit?bundle-id=%@";
} else if (self.certificatePassword) {
tries = 2;
urlScheme = [NSString stringWithFormat:@"%@://livecontainer-relaunch", lcAppUrlScheme];
} else {
urlScheme = @"sidestore://sidejit-enable?bid=%@";
}
NSURL *launchURL = [NSURL URLWithString:[NSString stringWithFormat:urlScheme, NSBundle.mainBundle.bundleIdentifier]];
if ([UIApplication.sharedApplication canOpenURL:launchURL]) {
//[UIApplication.sharedApplication suspend];
for (int i = 0; i < tries; i++) {
[UIApplication.sharedApplication openURL:launchURL options:@{} completionHandler:^(BOOL b) {
exit(0);
}];
}
return YES;
}
return NO;
}
+ (BOOL)askForJIT {
NSString *urlScheme;
NSString *tsPath = [NSString stringWithFormat:@"%@/../_TrollStore", NSBundle.mainBundle.bundlePath];
if (!access(tsPath.UTF8String, F_OK)) {
urlScheme = @"apple-magnifier://enable-jit?bundle-id=%@";
NSURL *launchURL = [NSURL URLWithString:[NSString stringWithFormat:urlScheme, NSBundle.mainBundle.bundleIdentifier]];
if ([UIApplication.sharedApplication canOpenURL:launchURL]) {
[UIApplication.sharedApplication openURL:launchURL options:@{} completionHandler:nil];
[LCSharedUtils launchToGuestApp];
return YES;
}
} else {
NSUserDefaults* groupUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:[self appGroupID]];
NSString* sideJITServerAddress = [groupUserDefaults objectForKey:@"LCSideJITServerAddress"];
NSString* deviceUDID = [groupUserDefaults objectForKey:@"LCDeviceUDID"];
if (!sideJITServerAddress || !deviceUDID) {
return NO;
}
NSString* launchJITUrlStr = [NSString stringWithFormat: @"%@/%@/%@", sideJITServerAddress, deviceUDID, NSBundle.mainBundle.bundleIdentifier];
NSURLSession* session = [NSURLSession sharedSession];
NSURL* launchJITUrl = [NSURL URLWithString:launchJITUrlStr];
NSURLRequest* req = [[NSURLRequest alloc] initWithURL:launchJITUrl];
NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if(error) {
NSLog(@"[LC] failed to contact SideJITServer: %@", error);
}
}];
[task resume];
}
return NO;
}
+ (BOOL)launchToGuestAppWithURL:(NSURL *)url {
NSURLComponents* components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
if(![components.host isEqualToString:@"livecontainer-launch"]) return NO;
NSString* launchBundleId = nil;
NSString* openUrl = nil;
NSString* containerFolderName = nil;
for (NSURLQueryItem* queryItem in components.queryItems) {
if ([queryItem.name isEqualToString:@"bundle-name"]) {
launchBundleId = queryItem.value;
} else if ([queryItem.name isEqualToString:@"open-url"]){
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:queryItem.value options:0];
openUrl = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
} else if ([queryItem.name isEqualToString:@"container-folder-name"]) {
containerFolderName = queryItem.value;
}
}
if(launchBundleId) {
if (openUrl) {
[lcUserDefaults setObject:openUrl forKey:@"launchAppUrlScheme"];
}
// Attempt to restart LiveContainer with the selected guest app
[lcUserDefaults setObject:launchBundleId forKey:@"selected"];
[lcUserDefaults setObject:containerFolderName forKey:@"selectedContainer"];
return [self launchToGuestApp];
}
return NO;
}
+ (void)setWebPageUrlForNextLaunch:(NSString*) urlString {
[lcUserDefaults setObject:urlString forKey:@"webPageToOpen"];
}
+ (NSURL*)appLockPath {
static dispatch_once_t once;
static NSURL *infoPath;
dispatch_once(&once, ^{
NSURL *appGroupPath = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:[LCSharedUtils appGroupID]];
infoPath = [appGroupPath URLByAppendingPathComponent:@"LiveContainer/appLock.plist"];
});
return infoPath;
}
+ (NSURL*)containerLockPath {
static dispatch_once_t once;
static NSURL *infoPath;
dispatch_once(&once, ^{
NSURL *appGroupPath = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:[LCSharedUtils appGroupID]];
infoPath = [appGroupPath URLByAppendingPathComponent:@"LiveContainer/containerLock.plist"];
});
return infoPath;
}
+ (NSString*)getAppRunningLCSchemeWithBundleId:(NSString*)bundleId {
NSURL* infoPath = [self appLockPath];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:infoPath.path];
if (!info) {
return nil;
}
for (NSString* key in info) {
if([bundleId isEqualToString:info[key]]) {
if([key isEqualToString:lcAppUrlScheme]) {
return nil;
}
return key;
}
}
return nil;
}
+ (NSString*)getContainerUsingLCSchemeWithFolderName:(NSString*)folderName {
NSURL* infoPath = [self containerLockPath];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:infoPath.path];
if (!info) {
return nil;
}
for (NSString* key in info) {
if([folderName isEqualToString:info[key]]) {
if([key isEqualToString:lcAppUrlScheme]) {
return nil;
}
return key;
}
}
return nil;
}
// if you pass null then remove this lc from appLock
+ (void)setAppRunningByThisLC:(NSString*)bundleId {
NSURL* infoPath = [self appLockPath];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:infoPath.path];
if (!info) {
info = [NSMutableDictionary new];
}
if(bundleId == nil) {
[info removeObjectForKey:lcAppUrlScheme];
} else {
info[lcAppUrlScheme] = bundleId;
}
[info writeToFile:infoPath.path atomically:YES];
}
+ (void)setContainerUsingByThisLC:(NSString*)folderName {
NSURL* infoPath = [self containerLockPath];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:infoPath.path];
if (!info) {
info = [NSMutableDictionary new];
}
if(folderName == nil) {
[info removeObjectForKey:lcAppUrlScheme];
} else {
info[lcAppUrlScheme] = folderName;
}
[info writeToFile:infoPath.path atomically:YES];
}
+ (void)removeAppRunningByLC:(NSString*)LCScheme {
NSURL* infoPath = [self appLockPath];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:infoPath.path];
if (!info) {
return;
}
[info removeObjectForKey:LCScheme];
[info writeToFile:infoPath.path atomically:YES];
}
+ (void)removeContainerUsingByLC:(NSString*)LCScheme {
NSURL* infoPath = [self containerLockPath];
NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:infoPath.path];
if (!info) {
return;
}
[info removeObjectForKey:LCScheme];
[info writeToFile:infoPath.path atomically:YES];
}
// move app data to private folder to prevent 0xdead10cc https://forums.developer.apple.com/forums/thread/126438
+ (void)moveSharedAppFolderBack {
NSFileManager *fm = NSFileManager.defaultManager;
NSURL *libraryPathUrl = [fm URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]
.lastObject;
NSURL *docPathUrl = [fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]
.lastObject;
NSURL *appGroupPath = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:[LCSharedUtils appGroupID]];
NSURL *appGroupFolder = [appGroupPath URLByAppendingPathComponent:@"LiveContainer"];
NSError *error;
NSString *sharedAppDataFolderPath = [libraryPathUrl.path stringByAppendingPathComponent:@"SharedDocuments"];
if(![fm fileExistsAtPath:sharedAppDataFolderPath]){
[fm createDirectoryAtPath:sharedAppDataFolderPath withIntermediateDirectories:YES attributes:@{} error:&error];
}
// move all apps in shared folder back
NSArray<NSString *> * sharedDataFoldersToMove = [fm contentsOfDirectoryAtPath:sharedAppDataFolderPath error:&error];
for(int i = 0; i < [sharedDataFoldersToMove count]; ++i) {
NSString* destPath = [appGroupFolder.path stringByAppendingPathComponent:[NSString stringWithFormat:@"Data/Application/%@", sharedDataFoldersToMove[i]]];
if([fm fileExistsAtPath:destPath]) {
[fm
moveItemAtPath:[sharedAppDataFolderPath stringByAppendingPathComponent:sharedDataFoldersToMove[i]]
toPath:[docPathUrl.path stringByAppendingPathComponent:[NSString stringWithFormat:@"FOLDER_EXISTS_AT_APP_GROUP_%@", sharedDataFoldersToMove[i]]]
error:&error
];
} else {
[fm
moveItemAtPath:[sharedAppDataFolderPath stringByAppendingPathComponent:sharedDataFoldersToMove[i]]
toPath:destPath
error:&error
];
}
}
}
+ (NSBundle*)findBundleWithBundleId:(NSString*)bundleId {
NSString *docPath = [NSString stringWithFormat:@"%s/Documents", getenv("LC_HOME_PATH")];
NSURL *appGroupFolder = nil;
NSString *bundlePath = [NSString stringWithFormat:@"%@/Applications/%@", docPath, bundleId];
NSBundle *appBundle = [[NSBundle alloc] initWithPath:bundlePath];
// not found locally, let's look for the app in shared folder
if (!appBundle) {
NSURL *appGroupPath = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:[LCSharedUtils appGroupID]];
appGroupFolder = [appGroupPath URLByAppendingPathComponent:@"LiveContainer"];
bundlePath = [NSString stringWithFormat:@"%@/Applications/%@", appGroupFolder.path, bundleId];
appBundle = [[NSBundle alloc] initWithPath:bundlePath];
}
return appBundle;
}
+ (void)dumpPreferenceToPath:(NSString*)plistLocationTo dataUUID:(NSString*)dataUUID {
NSFileManager* fm = [[NSFileManager alloc] init];
NSError* error1;
NSDictionary* preferences = [lcUserDefaults objectForKey:dataUUID];
if(!preferences) {
return;
}
[fm createDirectoryAtPath:plistLocationTo withIntermediateDirectories:YES attributes:@{} error:&error1];
for(NSString* identifier in preferences) {
NSDictionary* preference = preferences[identifier];
NSString *itemPath = [plistLocationTo stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", identifier]];
if([preference count] == 0) {
// Attempt to delete the file
[fm removeItemAtPath:itemPath error:&error1];
continue;
}
[preference writeToFile:itemPath atomically:YES];
}
[lcUserDefaults removeObjectForKey:dataUUID];
}
+ (NSString*)findDefaultContainerWithBundleId:(NSString*)bundleId {
// find app's default container
NSURL *appGroupPath = [NSFileManager.defaultManager containerURLForSecurityApplicationGroupIdentifier:[LCSharedUtils appGroupID]];
NSURL* appGroupFolder = [appGroupPath URLByAppendingPathComponent:@"LiveContainer"];
NSString* bundleInfoPath = [NSString stringWithFormat:@"%@/Applications/%@/Info.plist", appGroupFolder.path, bundleId];
NSDictionary* infoDict = [NSDictionary dictionaryWithContentsOfFile:bundleInfoPath];
return infoDict[@"LCDataUUID"];
}
@end