forked from LiveContainer/LiveContainer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCSharedUtils.m
More file actions
300 lines (256 loc) · 12.1 KB
/
Copy pathLCSharedUtils.m
File metadata and controls
300 lines (256 loc) · 12.1 KB
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
#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;
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];
}
}
if(launchBundleId) {
if (openUrl) {
[lcUserDefaults setObject:openUrl forKey:@"launchAppUrlScheme"];
}
// Attempt to restart LiveContainer with the selected guest app
[lcUserDefaults setObject:launchBundleId forKey:@"selected"];
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;
}
+ (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;
}
// 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)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];
}
// move all plists file from fromPath to toPath
+ (void)movePreferencesFromPath:(NSString*) plistLocationFrom toPath:(NSString*)plistLocationTo {
NSFileManager* fm = [[NSFileManager alloc] init];
NSError* error1;
NSArray<NSString *> * plists = [fm contentsOfDirectoryAtPath:plistLocationFrom error:&error1];
// remove all plists in toPath first
NSArray *directoryContents = [fm contentsOfDirectoryAtPath:plistLocationTo error:&error1];
for (NSString *item in directoryContents) {
// Check if the item is a plist and does not contain "LiveContainer"
if(![item hasSuffix:@".plist"] || [item containsString:@"livecontainer"]) {
continue;
}
NSString *itemPath = [plistLocationTo stringByAppendingPathComponent:item];
// Attempt to delete the file
[fm removeItemAtPath:itemPath error:&error1];
}
[fm createDirectoryAtPath:plistLocationTo withIntermediateDirectories:YES attributes:@{} error:&error1];
// move all plists in fromPath to toPath
for (NSString* item in plists) {
if(![item hasSuffix:@".plist"] || [item containsString:@"livecontainer"]) {
continue;
}
NSString* toPlistPath = [NSString stringWithFormat:@"%@/%@", plistLocationTo, item];
NSString* fromPlistPath = [NSString stringWithFormat:@"%@/%@", plistLocationFrom, item];
[fm moveItemAtPath:fromPlistPath toPath:toPlistPath error:&error1];
if(error1) {
NSLog(@"[LC] error1 = %@", error1.description);
}
}
}
// to make apple happy and prevent, we have to load all preferences into NSUserDefault so that guest app can read them
+ (void)loadPreferencesFromPath:(NSString*) plistLocationFrom {
NSFileManager* fm = [[NSFileManager alloc] init];
NSError* error1;
NSArray<NSString *> * plists = [fm contentsOfDirectoryAtPath:plistLocationFrom error:&error1];
// move all plists in fromPath to toPath
for (NSString* item in plists) {
if(![item hasSuffix:@".plist"] || [item containsString:@"livecontainer"]) {
continue;
}
NSString* fromPlistPath = [NSString stringWithFormat:@"%@/%@", plistLocationFrom, item];
// load, the file and sync
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithContentsOfFile:fromPlistPath];
NSUserDefaults* nud = [[NSUserDefaults alloc] initWithSuiteName: [item substringToIndex:[item length]-6]];
for(NSString* key in dict) {
[nud setObject:dict[key] forKey:key];
}
[nud synchronize];
}
}
// 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;
}
@end