forked from bunny-mod/BunnyTweak
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSideloading.x
196 lines (160 loc) · 5.95 KB
/
Sideloading.x
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
#import "Logger.h"
#import <Foundation/Foundation.h>
#import <Security/Security.h>
#import <UIKit/UIKit.h>
#import <dlfcn.h>
typedef struct __CMSDecoder *CMSDecoderRef;
extern CFTypeRef SecCMSDecodeGetContent(CFDataRef message);
extern OSStatus CMSDecoderCreate(CMSDecoderRef *cmsDecoder);
extern OSStatus CMSDecoderUpdateMessage(CMSDecoderRef cmsDecoder, const void *content,
size_t contentLength);
extern OSStatus CMSDecoderFinalizeMessage(CMSDecoderRef cmsDecoder);
extern OSStatus CMSDecoderCopyContent(CMSDecoderRef cmsDecoder, CFDataRef *content);
@interface LSApplicationWorkspace : NSObject
- (BOOL)openApplicationWithBundleID:(NSString *)bundleID;
@end
#define DISCORD_BUNDLE_ID @"com.hammerandchisel.discord"
#define DISCORD_NAME @"Discord"
static NSString *getAccessGroupID(void) {
NSDictionary *query = @{
(__bridge NSString *)kSecClass : (__bridge NSString *)kSecClassGenericPassword,
(__bridge NSString *)kSecAttrAccount : @"bundleSeedID",
(__bridge NSString *)kSecAttrService : @"",
(__bridge NSString *)kSecReturnAttributes : @YES
};
CFDictionaryRef result = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef *)&result);
if (status == errSecItemNotFound) {
status = SecItemAdd((__bridge CFDictionaryRef)query, (CFTypeRef *)&result);
}
if (status != errSecSuccess)
return nil;
NSString *accessGroup =
[(__bridge NSDictionary *)result objectForKey:(__bridge NSString *)kSecAttrAccessGroup];
if (result)
CFRelease(result);
return accessGroup;
}
static BOOL isSelfCall(void) {
NSArray *address = [NSThread callStackReturnAddresses];
Dl_info info = {0};
if (dladdr((void *)[address[2] longLongValue], &info) == 0)
return NO;
NSString *path = [NSString stringWithUTF8String:info.dli_fname];
return [path hasPrefix:NSBundle.mainBundle.bundlePath];
}
static NSString *getProvisioningBundleID(void) {
NSString *provisionPath = [NSBundle.mainBundle pathForResource:@"embedded"
ofType:@"mobileprovision"];
if (!provisionPath)
return nil;
NSData *provisionData = [NSData dataWithContentsOfFile:provisionPath];
if (!provisionData)
return nil;
CMSDecoderRef decoder = NULL;
CMSDecoderCreate(&decoder);
CMSDecoderUpdateMessage(decoder, provisionData.bytes, provisionData.length);
CMSDecoderFinalizeMessage(decoder);
CFDataRef dataRef = NULL;
CMSDecoderCopyContent(decoder, &dataRef);
NSData *data = (__bridge_transfer NSData *)dataRef;
if (decoder)
CFRelease(decoder);
NSError *error = nil;
id plist = [NSPropertyListSerialization propertyListWithData:data
options:0
format:NULL
error:&error];
if (!plist || ![plist isKindOfClass:[NSDictionary class]])
return nil;
NSString *appID = plist[@"Entitlements"][@"application-identifier"];
if (!appID)
return nil;
NSArray *components = [appID componentsSeparatedByString:@"."];
if (components.count > 1) {
return [[components subarrayWithRange:NSMakeRange(1, components.count - 1)]
componentsJoinedByString:@"."];
}
return nil;
}
%group Sideloading
%hook NSBundle
- (NSString *)bundleIdentifier {
if (!isSelfCall())
return %orig;
static NSString *provisionID = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ provisionID = getProvisioningBundleID(); });
return provisionID ?: DISCORD_BUNDLE_ID;
}
- (NSDictionary *)infoDictionary {
if (!isSelfCall())
return %orig;
NSMutableDictionary *info = [%orig mutableCopy];
info[@"CFBundleIdentifier"] = DISCORD_BUNDLE_ID;
info[@"CFBundleDisplayName"] = DISCORD_NAME;
info[@"CFBundleName"] = DISCORD_NAME;
return info;
}
- (id)objectForInfoDictionaryKey:(NSString *)key {
if (!isSelfCall())
return %orig;
if ([key isEqualToString:@"CFBundleIdentifier"])
return DISCORD_BUNDLE_ID;
if ([key isEqualToString:@"CFBundleDisplayName"] || [key isEqualToString:@"CFBundleName"])
return DISCORD_NAME;
return %orig;
}
%end
%hook NSFileManager
- (NSURL *)containerURLForSecurityApplicationGroupIdentifier:(NSString *)groupIdentifier {
BunnyLog(@"containerURLForSecurityApplicationGroupIdentifier called! %@",
groupIdentifier ?: @"nil");
NSArray *paths = [self URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *lastPath = [paths lastObject];
return [lastPath URLByAppendingPathComponent:@"AppGroup"];
}
%end
%hook UIPasteboard
- (NSString *)_accessGroup {
return getAccessGroupID();
}
%end
%hook UIApplication
- (BOOL)_canChangeAlternateIconName {
return YES;
}
- (void)setAlternateIconName:(NSString *)iconName
completionHandler:(void (^)(NSError *))completion {
if (completion)
completion(nil);
%orig;
}
%end
%hook LSApplicationWorkspace
- (BOOL)openApplicationWithBundleID:(NSString *)bundleID {
if ([bundleID isEqualToString:DISCORD_BUNDLE_ID]) {
return [self openApplicationWithBundleID:getProvisioningBundleID()];
}
return %orig;
}
%end
%hook UIDocumentPickerViewController
- (id)initForOpeningContentTypes:(NSArray *)contentTypes {
if (isSelfCall()) {
NSBundle *bundle = [NSBundle bundleWithIdentifier:getProvisioningBundleID()];
self = %orig(contentTypes);
[self setValue:bundle forKey:@"_clientBundle"];
return self;
}
return %orig;
}
%end
%end // End of Sideloading group
%ctor {
BOOL isAppStoreApp = [[NSFileManager defaultManager]
fileExistsAtPath:[[NSBundle mainBundle] appStoreReceiptURL].path];
if (!isAppStoreApp) {
%init(Sideloading);
}
}