forked from tien0246/ProtectedApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweak.xm
523 lines (422 loc) · 21.4 KB
/
Tweak.xm
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#import <UIKit/UIKit.h>
@interface ProtectedApp : NSObject
@property (nonatomic, strong) UIWindow *lockWindow;
@property (nonatomic, strong) NSMutableString *enteredCode;
@property (nonatomic, assign) NSInteger failedAttempts;
@property (nonatomic, strong) NSDate *lockoutEndTime;
@property (nonatomic, strong) NSDate *lastBackgroundTime;
@property (nonatomic, assign) BOOL isLockScreenPresented;
@property (nonatomic, assign) NSTimeInterval timeoutInterval;
+ (instancetype)sharedInstance;
- (void)presentLockScreenIfNeeded;
- (void)setupLockWindow;
- (void)handleTripleTap;
- (void)showLockScreen;
- (void)numberButtonTapped:(UIButton *)sender;
- (void)checkPasscode;
- (void)resetPasscode;
- (BOOL)isLockedOut;
- (void)applyLockout;
- (void)showLockoutMessage;
- (void)initializePasscodeFileIfNeeded;
- (NSString *)readPasscodeFromDefaults;
- (void)savePasscodeToDefaults:(NSString *)passcode;
- (void)loadLockoutState;
- (void)saveLockoutState;
- (BOOL)isCurrentPasscodeValid:(NSString *)currentPasscode;
- (BOOL)areNewPasscodesValid:(NSString *)newPasscode confirmPasscode:(NSString *)confirmPasscode;
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message;
- (void)handleWillResignActive;
- (void)handleWillEnterForeground;
@end
@implementation ProtectedApp
+ (instancetype)sharedInstance {
static ProtectedApp *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance
selector:@selector(handleWillResignActive)
name:UIApplicationWillResignActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance
selector:@selector(handleWillResignActive)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance
selector:@selector(handleWillResignActive)
name:UIApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance
selector:@selector(handleWillEnterForeground)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance
selector:@selector(handleWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[sharedInstance initializePasscodeFileIfNeeded];
[sharedInstance loadLockoutState];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
sharedInstance.timeoutInterval = [defaults doubleForKey:@"timeoutInterval"];
if (sharedInstance.timeoutInterval <= 0) {
sharedInstance.timeoutInterval = 15.0;
}
});
return sharedInstance;
}
- (void)handleWillResignActive {
if (!self.lockWindow) self.lastBackgroundTime = [NSDate date];
[self setupLockWindow];
}
- (void)handleWillEnterForeground {
if (self.lastBackgroundTime) {
NSTimeInterval timeInBackground = [[NSDate date] timeIntervalSinceDate:self.lastBackgroundTime];
if (timeInBackground > self.timeoutInterval) {
[self presentLockScreenIfNeeded];
} else {
if (self.lockWindow) {
self.lockWindow.hidden = YES;
self.lockWindow = nil;
self.isLockScreenPresented = NO;
}
}
}
}
- (void)presentLockScreenIfNeeded {
if (self.isLockScreenPresented) return;
if (!self.lockWindow || self.lockWindow.hidden) {
[self setupLockWindow];
}
if ([self isLockedOut]) {
[self showLockoutMessage];
return;
}
[self showLockScreen];
self.isLockScreenPresented = YES;
}
- (void)setupLockWindow {
if (!self.lockWindow) {
self.lockWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.lockWindow.backgroundColor = [UIColor clearColor];
self.lockWindow.windowLevel = UIWindowLevelAlert + 99;
UIViewController *lockViewController = [[UIViewController alloc] init];
UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
blurEffectView.frame = lockViewController.view.bounds;
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[lockViewController.view addSubview:blurEffectView];
self.lockWindow.rootViewController = lockViewController;
[self.lockWindow makeKeyAndVisible];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTripleTap)];
tap.numberOfTapsRequired = 2;
tap.numberOfTouchesRequired = 3;
[self.lockWindow addGestureRecognizer:tap];
}
}
- (void)handleTripleTap {
if ([self isLockedOut]) {
[self showLockoutMessage];
return;
}
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Options"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *changePasscodeAction = [UIAlertAction actionWithTitle:@"Change Passcode" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self showChangePasscodeAlert];
}];
UIAlertAction *changeTimeoutAction = [UIAlertAction actionWithTitle:@"Change Timeout Interval" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self showChangeTimeoutAlert];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:changePasscodeAction];
[alert addAction:changeTimeoutAction];
[alert addAction:cancelAction];
[self.lockWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}
- (void)showChangePasscodeAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Change Passcode"
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Enter current passcode";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeNumberPad;
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Enter new passcode";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeNumberPad;
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Confirm new passcode";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeNumberPad;
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *changeAction = [UIAlertAction actionWithTitle:@"Change" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *currentPasscodeField = alert.textFields[0];
UITextField *newPasscodeField = alert.textFields[1];
UITextField *confirmPasscodeField = alert.textFields[2];
NSString *currentPasscode = currentPasscodeField.text;
NSString *newPasscode = newPasscodeField.text;
NSString *confirmPasscode = confirmPasscodeField.text;
if ([self isCurrentPasscodeValid:currentPasscode] && [self areNewPasscodesValid:newPasscode confirmPasscode:confirmPasscode]) {
self.failedAttempts = 0;
[self saveLockoutState];
[self savePasscodeToDefaults:newPasscode];
[self showAlertWithTitle:@"Success" message:@"Passcode has been changed."];
} else {
self.failedAttempts++;
if (self.failedAttempts >= 5) {
[self applyLockout];
[self showLockoutMessage];
} else {
[self showAlertWithTitle:@"Error" message:@"Invalid passcode or mismatch. Passcode must be 4 digits."];
}
}
}];
[alert addAction:cancelAction];
[alert addAction:changeAction];
[self.lockWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}
- (void)showChangeTimeoutAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Change Timeout Interval"
message:@"Enter the new timeout interval in seconds:"
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"Timeout interval in seconds";
textField.keyboardType = UIKeyboardTypeNumberPad;
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *changeAction = [UIAlertAction actionWithTitle:@"Change" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *timeoutField = alert.textFields.firstObject;
NSTimeInterval newTimeoutInterval = [timeoutField.text doubleValue];
if (newTimeoutInterval > 0) {
self.timeoutInterval = newTimeoutInterval;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setDouble:newTimeoutInterval forKey:@"timeoutInterval"];
[defaults synchronize];
[self showAlertWithTitle:@"Success" message:@"Timeout interval has been changed."];
} else {
[self showAlertWithTitle:@"Error" message:@"Invalid timeout interval."];
}
}];
[alert addAction:cancelAction];
[alert addAction:changeAction];
[self.lockWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}
- (BOOL)isCurrentPasscodeValid:(NSString *)currentPasscode {
NSString *savedPasscode = [self readPasscodeFromDefaults];
return [currentPasscode isEqualToString:savedPasscode];
}
- (BOOL)areNewPasscodesValid:(NSString *)newPasscode confirmPasscode:(NSString *)confirmPasscode {
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
BOOL isNumeric = ([newPasscode rangeOfCharacterFromSet:nonDigitCharacterSet].location == NSNotFound);
return isNumeric && newPasscode.length == 4 && [newPasscode isEqualToString:confirmPasscode];
}
- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[self.lockWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}
- (void)showLockScreen {
self.enteredCode = [NSMutableString string];
UIViewController *lockViewController = self.lockWindow.rootViewController;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat buttonWidth = screenWidth / 5;
CGFloat buttonHeight = buttonWidth;
CGFloat totalWidth = buttonWidth * 3 + 40;
CGFloat totalHeight = buttonHeight * 4 + 60;
CGFloat centerX = screenWidth / 2;
CGFloat startY = (screenHeight - totalHeight - 150) / 2;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 50)];
label.center = CGPointMake(centerX, startY);
label.text = @"Enter Passcode";
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
[lockViewController.view addSubview:label];
UIView *codeInputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
codeInputView.center = CGPointMake(centerX, CGRectGetMaxY(label.frame) + 40);
codeInputView.tag = 100;
[lockViewController.view addSubview:codeInputView];
CGFloat dotSize = 20;
CGFloat space = (codeInputView.bounds.size.width - 4 * dotSize) / 3;
for (int i = 0; i < 4; i++) {
UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(i * (dotSize + space), 0, dotSize, dotSize)];
dotView.layer.cornerRadius = dotSize / 2;
dotView.layer.borderColor = [[UIColor whiteColor] CGColor];
dotView.layer.borderWidth = 1.0;
dotView.backgroundColor = [UIColor clearColor];
dotView.layer.masksToBounds = YES;
dotView.tag = i + 1;
[codeInputView addSubview:dotView];
}
startY = CGRectGetMaxY(codeInputView.frame) + 40;
NSArray *buttonTitles = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"0"];
for (int i = 0; i < 9; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(centerX - totalWidth / 2 + (i % 3) * (buttonWidth + 20),
startY + (i / 3) * (buttonHeight + 20),
buttonWidth, buttonHeight);
[button setTitle:buttonTitles[i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:buttonHeight / 2];
button.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
button.layer.cornerRadius = buttonWidth / 2;
button.layer.masksToBounds = YES;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(numberButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = [buttonTitles[i] intValue];
[lockViewController.view addSubview:button];
}
UIButton *zeroButton = [UIButton buttonWithType:UIButtonTypeSystem];
zeroButton.frame = CGRectMake(centerX - buttonWidth / 2,
startY + 3 * (buttonHeight + 20),
buttonWidth, buttonHeight);
[zeroButton setTitle:@"0" forState:UIControlStateNormal];
zeroButton.titleLabel.font = [UIFont systemFontOfSize:buttonHeight / 2];
zeroButton.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
zeroButton.layer.cornerRadius = buttonWidth / 2;
zeroButton.layer.masksToBounds = YES;
[zeroButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[zeroButton addTarget:self action:@selector(numberButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
zeroButton.tag = 0;
[lockViewController.view addSubview:zeroButton];
}
- (void)numberButtonTapped:(UIButton *)sender {
if (self.enteredCode.length < 4) {
[self.enteredCode appendString:[NSString stringWithFormat:@"%ld", (long)sender.tag]];
UIView *codeInputView = [self.lockWindow.rootViewController.view viewWithTag:100];
UIView *dotView = [codeInputView viewWithTag:self.enteredCode.length];
dotView.backgroundColor = [UIColor whiteColor];
if (self.enteredCode.length == 4) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self checkPasscode];
});
}
}
}
- (void)checkPasscode {
NSString *correctPasscode = [self readPasscodeFromDefaults];
if ([self.enteredCode isEqualToString:correctPasscode]) {
self.failedAttempts = 0;
[self saveLockoutState];
[self.lockWindow resignKeyWindow];
self.lockWindow.hidden = YES;
self.lockWindow = nil;
self.isLockScreenPresented = NO;
} else {
self.failedAttempts++;
if (self.failedAttempts >= 5) {
[self applyLockout];
} else {
[self resetPasscode];
}
}
}
- (void)resetPasscode {
[self.enteredCode setString:@""];
UIView *codeInputView = [self.lockWindow.rootViewController.view viewWithTag:100];
for (int i = 1; i <= 4; i++) {
UIView *dotView = [codeInputView viewWithTag:i];
dotView.backgroundColor = [UIColor clearColor];
dotView.layer.borderColor = [[UIColor whiteColor] CGColor];
dotView.layer.borderWidth = 1.0;
}
}
- (BOOL)isLockedOut {
if (!self.lockoutEndTime) return NO;
return [[NSDate date] compare:self.lockoutEndTime] == NSOrderedAscending;
}
- (void)applyLockout {
NSInteger lockoutMinutes = pow(2, self.failedAttempts - 5);
self.lockoutEndTime = [[NSDate date] dateByAddingTimeInterval:lockoutMinutes * 60 + 1];
[self saveLockoutState];
[self showLockoutMessage];
}
- (void)showLockoutMessage {
NSTimeInterval timeRemaining = [self.lockoutEndTime timeIntervalSinceNow];
NSInteger seconds = (NSInteger)timeRemaining % 60;
NSInteger minutes = ((NSInteger)timeRemaining / 60) % 60;
NSInteger hours = ((NSInteger)timeRemaining / 3600) % 24;
NSInteger days = ((NSInteger)timeRemaining / (3600 * 24)) % 30;
NSInteger months = ((NSInteger)timeRemaining / (3600 * 24 * 30)) % 12;
NSInteger years = (NSInteger)timeRemaining / (3600 * 24 * 365);
NSMutableArray *timeComponents = [NSMutableArray array];
if (years > 0) {
[timeComponents addObject:[NSString stringWithFormat:@"%ld years", (long)years]];
}
if (months > 0) {
[timeComponents addObject:[NSString stringWithFormat:@"%ld months", (long)months]];
}
if (days > 0) {
[timeComponents addObject:[NSString stringWithFormat:@"%ld days", (long)days]];
}
if (hours > 0) {
[timeComponents addObject:[NSString stringWithFormat:@"%ld hours", (long)hours]];
}
if (minutes > 0) {
[timeComponents addObject:[NSString stringWithFormat:@"%ld minutes", (long)minutes]];
}
if (seconds > 0) {
[timeComponents addObject:[NSString stringWithFormat:@"%ld seconds", (long)seconds]];
}
NSString *message = [NSString stringWithFormat:@"Try again in %@", [timeComponents componentsJoinedByString:@", "]];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Locked Out"
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self.lockWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}
- (void)initializePasscodeFileIfNeeded {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *storedData = [defaults objectForKey:@"com.tien0246.ProtectedApp"];
if (!storedData || !storedData[@"passcode"]) {
[self savePasscodeToDefaults:@"0000"];
}
}
- (NSString *)readPasscodeFromDefaults {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *storedData = [defaults objectForKey:@"com.tien0246.ProtectedApp"];
return storedData[@"passcode"];
}
- (void)savePasscodeToDefaults:(NSString *)passcode {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *storedData = [[defaults objectForKey:@"com.tien0246.ProtectedApp"] mutableCopy] ?: [NSMutableDictionary dictionary];
storedData[@"passcode"] = passcode;
[defaults setObject:storedData forKey:@"com.tien0246.ProtectedApp"];
[defaults synchronize];
}
- (void)loadLockoutState {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *storedData = [defaults objectForKey:@"com.tien0246.ProtectedApp"];
if (storedData) {
self.failedAttempts = [storedData[@"failedAttempts"] integerValue];
self.lockoutEndTime = storedData[@"lockoutEndTime"];
} else {
self.failedAttempts = 0;
self.lockoutEndTime = nil;
}
}
- (void)saveLockoutState {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *storedData = [[defaults objectForKey:@"com.tien0246.ProtectedApp"] mutableCopy] ?: [NSMutableDictionary dictionary];
storedData[@"failedAttempts"] = @(self.failedAttempts);
storedData[@"lockoutEndTime"] = self.lockoutEndTime;
[defaults setObject:storedData forKey:@"com.tien0246.ProtectedApp"];
[defaults synchronize];
}
@end
%hook UIWindow
BOOL isShowLockScreen = NO;
- (void)makeKeyAndVisible {
if (!isShowLockScreen) {
[[ProtectedApp sharedInstance] presentLockScreenIfNeeded];
isShowLockScreen = YES;
}
return %orig;
}
%end