-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweak.m
More file actions
279 lines (217 loc) · 11.9 KB
/
Copy pathTweak.m
File metadata and controls
279 lines (217 loc) · 11.9 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
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
static NSString * const kTargetBundleID = @"com.mfcloudcalculate.123networkdisk";
static BOOL (*orig_NSURL_startAccessing)(id self, SEL _cmd) = NULL;
static void (*orig_NSURL_stopAccessing)(id self, SEL _cmd) = NULL;
static void (*orig_documentPicker_didPickDocumentAtURL)(id self, SEL _cmd, UIDocumentPickerViewController *controller, NSURL *url) = NULL;
static BOOL (*orig_application_openURL_options)(id self, SEL _cmd, UIApplication *application, NSURL *url, NSDictionary *options) = NULL;
static BOOL (*orig_application_openURL_source_annotation)(id self, SEL _cmd, UIApplication *application, NSURL *url, NSString *sourceApplication, id annotation) = NULL;
static BOOL (*orig_application_handleOpenURL)(id self, SEL _cmd, UIApplication *application, NSURL *url) = NULL;
static BOOL (*orig_application_didFinishLaunching)(id self, SEL _cmd, UIApplication *application, NSDictionary *launchOptions) = NULL;
static BOOL (*orig_application_willFinishLaunching)(id self, SEL _cmd, UIApplication *application, NSDictionary *launchOptions) = NULL;
static id (*orig_picker_initWithDocumentTypes_inMode)(id self, SEL _cmd, id documentTypes, NSInteger mode) = NULL;
static id (*orig_picker_initForOpeningContentTypes_asCopy)(id self, SEL _cmd, id contentTypes, BOOL asCopy) = NULL;
#pragma mark - Helpers
static BOOL FPCFIsTargetApp(void) {
return [[[NSBundle mainBundle] bundleIdentifier] isEqualToString:kTargetBundleID];
}
static NSURL *FPCFDocumentsURL(void) {
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
if (!url) {
url = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] isDirectory:YES];
}
[[NSFileManager defaultManager] createDirectoryAtURL:url withIntermediateDirectories:YES attributes:nil error:nil];
return url;
}
static void FPCFWriteStatus(NSString *line) {
@try {
NSURL *url = [FPCFDocumentsURL() URLByAppendingPathComponent:@"FilePickerCopyFixStatus.txt" isDirectory:NO];
NSString *old = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
if (!old) old = @"";
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *newLine = [NSString stringWithFormat:@"%@ %@\n", [formatter stringFromDate:[NSDate date]], line ?: @""];
NSString *content = [old stringByAppendingString:newLine];
if (content.length > 30000) {
content = [content substringFromIndex:content.length - 22000];
}
[content writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
} @catch (__unused NSException *exception) {
}
}
static NSString *FPCFURLInfo(NSURL *url) {
if (![url isKindOfClass:[NSURL class]]) return @"<not NSURL>";
return url.isFileURL ? (url.path ?: @"<nil path>") : (url.absoluteString ?: @"<nil url>");
}
static void FPCFCleanOldCopies(void) {
@try {
NSURL *oldInbox = [FPCFDocumentsURL() URLByAppendingPathComponent:@"FilePickerCopyFixInbox" isDirectory:YES];
if ([[NSFileManager defaultManager] fileExistsAtPath:oldInbox.path]) {
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtURL:oldInbox error:&error];
FPCFWriteStatus([NSString stringWithFormat:@"cleanup old FilePickerCopyFixInbox: %@", error ?: @"OK"]);
}
} @catch (NSException *exception) {
FPCFWriteStatus([NSString stringWithFormat:@"cleanup exception: %@", exception]);
}
}
#pragma mark - NSURL security scope fix
static BOOL fixed_NSURL_startAccessing(id self, SEL _cmd) {
BOOL result = NO;
if (orig_NSURL_startAccessing) {
result = orig_NSURL_startAccessing(self, _cmd);
}
if (FPCFIsTargetApp() && [self isKindOfClass:[NSURL class]]) {
NSURL *url = (NSURL *)self;
if (url.isFileURL) {
/*
123云盘原 Swift 逻辑会在 startAccessingSecurityScopedResource 返回 false 时直接 return。
侧载/重签后,本地 tmp Inbox 文件经常返回 false。
这里强制返回 YES,但不复制文件,避免占用 Documents 存储。
*/
FPCFWriteStatus([NSString stringWithFormat:@"startAccess original=%d forced=1 path=%@", result ? 1 : 0, url.path]);
return YES;
}
}
return result;
}
static void fixed_NSURL_stopAccessing(id self, SEL _cmd) {
if (orig_NSURL_stopAccessing) {
orig_NSURL_stopAccessing(self, _cmd);
}
}
#pragma mark - AppDelegate exact hooks, no-copy
static void fixed_documentPicker_didPickDocumentAtURL(id self, SEL _cmd, UIDocumentPickerViewController *controller, NSURL *url) {
FPCFWriteStatus([NSString stringWithFormat:@"documentPicker passthrough: %@", FPCFURLInfo(url)]);
if (orig_documentPicker_didPickDocumentAtURL) {
orig_documentPicker_didPickDocumentAtURL(self, _cmd, controller, url);
}
}
static BOOL fixed_application_openURL_options(id self, SEL _cmd, UIApplication *application, NSURL *url, NSDictionary *options) {
FPCFWriteStatus([NSString stringWithFormat:@"openURL options passthrough: %@", FPCFURLInfo(url)]);
if (orig_application_openURL_options) {
return orig_application_openURL_options(self, _cmd, application, url, options);
}
return NO;
}
static BOOL fixed_application_openURL_source_annotation(id self, SEL _cmd, UIApplication *application, NSURL *url, NSString *sourceApplication, id annotation) {
FPCFWriteStatus([NSString stringWithFormat:@"openURL source passthrough: %@ source=%@", FPCFURLInfo(url), sourceApplication ?: @"<nil>"]);
if (orig_application_openURL_source_annotation) {
return orig_application_openURL_source_annotation(self, _cmd, application, url, sourceApplication, annotation);
}
return NO;
}
static BOOL fixed_application_handleOpenURL(id self, SEL _cmd, UIApplication *application, NSURL *url) {
FPCFWriteStatus([NSString stringWithFormat:@"handleOpenURL passthrough: %@", FPCFURLInfo(url)]);
if (orig_application_handleOpenURL) {
return orig_application_handleOpenURL(self, _cmd, application, url);
}
return NO;
}
static BOOL fixed_application_didFinishLaunching(id self, SEL _cmd, UIApplication *application, NSDictionary *launchOptions) {
NSURL *url = [launchOptions isKindOfClass:[NSDictionary class]] ? launchOptions[UIApplicationLaunchOptionsURLKey] : nil;
if (url) {
FPCFWriteStatus([NSString stringWithFormat:@"didFinish launch URL passthrough: %@", FPCFURLInfo(url)]);
} else {
FPCFWriteStatus(@"didFinish no launch URL");
}
if (orig_application_didFinishLaunching) {
return orig_application_didFinishLaunching(self, _cmd, application, launchOptions);
}
return YES;
}
static BOOL fixed_application_willFinishLaunching(id self, SEL _cmd, UIApplication *application, NSDictionary *launchOptions) {
NSURL *url = [launchOptions isKindOfClass:[NSDictionary class]] ? launchOptions[UIApplicationLaunchOptionsURLKey] : nil;
if (url) {
FPCFWriteStatus([NSString stringWithFormat:@"willFinish launch URL passthrough: %@", FPCFURLInfo(url)]);
}
if (orig_application_willFinishLaunching) {
return orig_application_willFinishLaunching(self, _cmd, application, launchOptions);
}
return YES;
}
#pragma mark - Keep document picker import/copy mode
static id fixed_picker_initWithDocumentTypes_inMode(id self, SEL _cmd, id documentTypes, NSInteger mode) {
if (orig_picker_initWithDocumentTypes_inMode) {
return orig_picker_initWithDocumentTypes_inMode(self, _cmd, documentTypes, 0); // UIDocumentPickerModeImport
}
return self;
}
static id fixed_picker_initForOpeningContentTypes_asCopy(id self, SEL _cmd, id contentTypes, BOOL asCopy) {
if (orig_picker_initForOpeningContentTypes_asCopy) {
return orig_picker_initForOpeningContentTypes_asCopy(self, _cmd, contentTypes, YES);
}
return self;
}
#pragma mark - Swizzle helpers
static Class FPCFAppDelegateClass(void) {
Class cls = objc_getClass("_TtC6Runner11AppDelegate");
if (!cls) cls = NSClassFromString(@"Runner.AppDelegate");
return cls;
}
static void replaceInstanceMethod(Class cls, SEL sel, IMP newImp, IMP *oldImp) {
if (!cls || !sel || !newImp || !oldImp) {
FPCFWriteStatus([NSString stringWithFormat:@"hook skipped: %@", NSStringFromSelector(sel)]);
return;
}
Method method = class_getInstanceMethod(cls, sel);
if (!method) {
FPCFWriteStatus([NSString stringWithFormat:@"method not found: %@ on %@", NSStringFromSelector(sel), cls]);
return;
}
IMP current = method_getImplementation(method);
if (current == newImp) return;
*oldImp = method_setImplementation(method, newImp);
FPCFWriteStatus([NSString stringWithFormat:@"hooked: %@ on %@", NSStringFromSelector(sel), cls]);
}
__attribute__((constructor))
static void FilePickerCopyFixInit(void) {
@autoreleasepool {
if (!FPCFIsTargetApp()) return;
FPCFWriteStatus(@"AccessNoCopy loaded");
FPCFCleanOldCopies();
replaceInstanceMethod([NSURL class],
@selector(startAccessingSecurityScopedResource),
(IMP)fixed_NSURL_startAccessing,
(IMP *)&orig_NSURL_startAccessing);
replaceInstanceMethod([NSURL class],
@selector(stopAccessingSecurityScopedResource),
(IMP)fixed_NSURL_stopAccessing,
(IMP *)&orig_NSURL_stopAccessing);
Class appDelegateClass = FPCFAppDelegateClass();
replaceInstanceMethod(appDelegateClass,
@selector(documentPicker:didPickDocumentAtURL:),
(IMP)fixed_documentPicker_didPickDocumentAtURL,
(IMP *)&orig_documentPicker_didPickDocumentAtURL);
replaceInstanceMethod(appDelegateClass,
@selector(application:openURL:options:),
(IMP)fixed_application_openURL_options,
(IMP *)&orig_application_openURL_options);
replaceInstanceMethod(appDelegateClass,
@selector(application:openURL:sourceApplication:annotation:),
(IMP)fixed_application_openURL_source_annotation,
(IMP *)&orig_application_openURL_source_annotation);
replaceInstanceMethod(appDelegateClass,
@selector(application:handleOpenURL:),
(IMP)fixed_application_handleOpenURL,
(IMP *)&orig_application_handleOpenURL);
replaceInstanceMethod(appDelegateClass,
@selector(application:didFinishLaunchingWithOptions:),
(IMP)fixed_application_didFinishLaunching,
(IMP *)&orig_application_didFinishLaunching);
replaceInstanceMethod(appDelegateClass,
@selector(application:willFinishLaunchingWithOptions:),
(IMP)fixed_application_willFinishLaunching,
(IMP *)&orig_application_willFinishLaunching);
Class pickerClass = objc_getClass("UIDocumentPickerViewController");
replaceInstanceMethod(pickerClass,
@selector(initWithDocumentTypes:inMode:),
(IMP)fixed_picker_initWithDocumentTypes_inMode,
(IMP *)&orig_picker_initWithDocumentTypes_inMode);
replaceInstanceMethod(pickerClass,
@selector(initForOpeningContentTypes:asCopy:),
(IMP)fixed_picker_initForOpeningContentTypes_asCopy,
(IMP *)&orig_picker_initForOpeningContentTypes_asCopy);
}
}