|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | +#import <UIKit/UIKit.h> |
| 5 | +#import <LocalAuthentication/LocalAuthentication.h> |
| 6 | +#import <AudioToolbox/AudioServices.h> |
| 7 | + |
| 8 | +@interface SBApplication : NSObject |
| 9 | +- (NSString *)bundleIdentifier; |
| 10 | +- (NSString *)displayName; |
| 11 | +@end |
| 12 | + |
| 13 | +@interface SBUIController : NSObject |
| 14 | ++ (instancetype)sharedInstance; |
| 15 | +- (void)activateApplication:(id)app fromIcon:(id)icon location:(long long)location activationSettings:(id)settings actions:(id)actions; |
| 16 | +@end |
| 17 | + |
| 18 | + |
| 19 | +static NSString * const kPrefsID = @"com.batues.biolock"; |
| 20 | +static BOOL gEnabled = YES; |
| 21 | +static BOOL gAllowPasscode = YES; |
| 22 | +static BOOL gVibrateOnFail = YES; |
| 23 | +static NSInteger gAuthCacheDuration = 0; |
| 24 | +static NSString *gCustomPrompt = nil; |
| 25 | +static NSSet<NSString *> *gProtectedApps = nil; |
| 26 | + |
| 27 | +static NSMutableDictionary<NSString *, NSDate *> *gAuthCache = nil; |
| 28 | +static NSMutableSet<NSString *> *gInTransition = nil; |
| 29 | +static dispatch_queue_t gAuthQueue = nil; |
| 30 | + |
| 31 | +#pragma mark - Logic |
| 32 | + |
| 33 | +static void LoadPrefs() { |
| 34 | + @autoreleasepool { |
| 35 | + |
| 36 | + NSString *prefsPath = @"/var/mobile/Library/Preferences/com.batues.biolock.plist"; |
| 37 | + NSDictionary *prefs = [NSDictionary dictionaryWithContentsOfFile:prefsPath]; |
| 38 | + |
| 39 | + |
| 40 | + if (!prefs) { |
| 41 | + CFStringRef appID = (__bridge CFStringRef)kPrefsID; |
| 42 | + CFPreferencesAppSynchronize(appID); |
| 43 | + CFArrayRef keyList = CFPreferencesCopyKeyList(appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost); |
| 44 | + if (keyList) { |
| 45 | + prefs = (__bridge_transfer NSDictionary *)CFPreferencesCopyMultiple(keyList, appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost); |
| 46 | + CFRelease(keyList); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + gEnabled = prefs[@"Enabled"] ? [prefs[@"Enabled"] boolValue] : YES; |
| 51 | + gAllowPasscode = prefs[@"AllowPasscode"] ? [prefs[@"AllowPasscode"] boolValue] : YES; |
| 52 | + gVibrateOnFail = prefs[@"VibrateOnFail"] ? [prefs[@"VibrateOnFail"] boolValue] : YES; |
| 53 | + gAuthCacheDuration = [prefs[@"AuthCacheDuration"] integerValue]; |
| 54 | + gCustomPrompt = [prefs[@"CustomPrompt"] copy]; |
| 55 | + |
| 56 | + |
| 57 | + id protected = prefs[@"ProtectedApps"]; |
| 58 | + if ([protected isKindOfClass:[NSArray class]]) { |
| 59 | + gProtectedApps = [NSSet setWithArray:protected]; |
| 60 | + } else if ([protected isKindOfClass:[NSDictionary class]]) { |
| 61 | + |
| 62 | + gProtectedApps = [NSSet setWithArray:[protected allKeys]]; |
| 63 | + } |
| 64 | + |
| 65 | + NSLog(@"[BioLock] Loaded %lu protected apps. Enabled: %d", (unsigned long)gProtectedApps.count, gEnabled); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +static void HandlePrefsChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { |
| 70 | + LoadPrefs(); |
| 71 | +} |
| 72 | + |
| 73 | +static void HandleClearCache(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { |
| 74 | + if (gAuthQueue) { |
| 75 | + dispatch_async(gAuthQueue, ^{ |
| 76 | + [gAuthCache removeAllObjects]; |
| 77 | + }); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +static BOOL IsAuthCached(NSString *bundleID) { |
| 82 | + if (!bundleID) return NO; |
| 83 | + __block BOOL valid = NO; |
| 84 | + dispatch_sync(gAuthQueue, ^{ |
| 85 | + NSDate *authDate = gAuthCache[bundleID]; |
| 86 | + if (authDate && [[NSDate date] timeIntervalSinceDate:authDate] < gAuthCacheDuration) { |
| 87 | + valid = YES; |
| 88 | + } |
| 89 | + }); |
| 90 | + return valid; |
| 91 | +} |
| 92 | + |
| 93 | +#pragma mark - Hooks |
| 94 | + |
| 95 | +%hook SBUIController |
| 96 | + |
| 97 | +- (void)activateApplication:(id)app fromIcon:(id)icon location:(long long)location activationSettings:(id)settings actions:(id)actions { |
| 98 | + SBApplication *sbApp = (SBApplication *)app; |
| 99 | + NSString *bundleID = [sbApp bundleIdentifier]; |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + if (!gEnabled || !bundleID || ![gProtectedApps containsObject:bundleID]) { |
| 106 | + %orig; |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + __block BOOL isBypassing = NO; |
| 112 | + dispatch_sync(gAuthQueue, ^{ |
| 113 | + if ([gInTransition containsObject:bundleID]) { |
| 114 | + isBypassing = YES; |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + if (isBypassing) { |
| 119 | + %orig; |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + if (gAuthCacheDuration > 0 && IsAuthCached(bundleID)) { |
| 125 | + %orig; |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + NSString *appName = [sbApp respondsToSelector:@selector(displayName)] ? [sbApp displayName] : bundleID; |
| 131 | + NSString *reason = (gCustomPrompt && gCustomPrompt.length > 0) ? |
| 132 | + [gCustomPrompt stringByReplacingOccurrencesOfString:@"%app%" withString:appName] : |
| 133 | + [NSString stringWithFormat:@"Unlock %@", appName]; |
| 134 | + |
| 135 | + LAContext *context = [[LAContext alloc] init]; |
| 136 | + LAPolicy policy = gAllowPasscode ? LAPolicyDeviceOwnerAuthentication : LAPolicyDeviceOwnerAuthenticationWithBiometrics; |
| 137 | + |
| 138 | + [context evaluatePolicy:policy localizedReason:reason reply:^(BOOL success, NSError *error) { |
| 139 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 140 | + if (success) { |
| 141 | + dispatch_sync(gAuthQueue, ^{ |
| 142 | + [gInTransition addObject:bundleID]; |
| 143 | + if (gAuthCacheDuration > 0) gAuthCache[bundleID] = [NSDate date]; |
| 144 | + }); |
| 145 | + |
| 146 | + |
| 147 | + [self activateApplication:app fromIcon:icon location:location activationSettings:settings actions:actions]; |
| 148 | + |
| 149 | + |
| 150 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), gAuthQueue, ^{ |
| 151 | + [gInTransition removeObject:bundleID]; |
| 152 | + }); |
| 153 | + } else { |
| 154 | + if (gVibrateOnFail) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); |
| 155 | + NSLog(@"[BioLock] Auth failed for %@", bundleID); |
| 156 | + } |
| 157 | + }); |
| 158 | + }]; |
| 159 | +} |
| 160 | + |
| 161 | +%end |
| 162 | + |
| 163 | +#pragma mark - Constructor |
| 164 | + |
| 165 | +%ctor { |
| 166 | + gAuthQueue = dispatch_queue_create("com.batues.biolock.queue", DISPATCH_QUEUE_SERIAL); |
| 167 | + gAuthCache = [[NSMutableDictionary alloc] init]; |
| 168 | + gInTransition = [[NSMutableSet alloc] init]; |
| 169 | + |
| 170 | + LoadPrefs(); |
| 171 | + |
| 172 | + CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, HandlePrefsChanged, CFSTR("com.batues.biolock/ReloadPrefs"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); |
| 173 | + CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, HandleClearCache, CFSTR("com.batues.biolock/ClearCache"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); |
| 174 | +} |
0 commit comments