forked from SelfControlApp/selfcontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCBlockUtilities.m
More file actions
76 lines (61 loc) · 2.44 KB
/
SCBlockUtilities.m
File metadata and controls
76 lines (61 loc) · 2.44 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
//
// SCBlockUtilities.m
// SelfControl
//
// Created by Charlie Stigler on 1/19/21.
//
#import "SCBlockUtilities.h"
#import "HostFileBlocker.h"
#import "PacketFilter.h"
@implementation SCBlockUtilities
+ (BOOL)anyBlockIsRunning {
BOOL blockIsRunning = [SCBlockUtilities modernBlockIsRunning] || [SCBlockUtilities legacyBlockIsRunning];
return blockIsRunning;
}
+ (BOOL)modernBlockIsRunning {
SCSettings* settings = [SCSettings sharedSettings];
return [settings boolForKey: @"BlockIsRunning"];
}
+ (BOOL)legacyBlockIsRunning {
// first see if there's a legacy settings file from v3.x
// which could be in any user's home folder
NSError* homeDirErr = nil;
NSArray<NSURL *>* homeDirectoryURLs = [SCMiscUtilities allUserHomeDirectoryURLs: &homeDirErr];
if (homeDirectoryURLs != nil) {
for (NSURL* homeDirURL in homeDirectoryURLs) {
NSString* relativeSettingsPath = [NSString stringWithFormat: @"/Library/Preferences/%@", SCSettings.settingsFileName];
NSURL* settingsFileURL = [homeDirURL URLByAppendingPathComponent: relativeSettingsPath isDirectory: NO];
if ([SCMigrationUtilities legacyBlockIsRunningInSettingsFile: settingsFileURL]) {
return YES;
}
}
}
// nope? OK, how about a lock file from pre-3.0?
if ([SCMigrationUtilities legacyLockFileExists]) {
return YES;
}
// we don't check defaults anymore, though pre-3.0 blocks did
// have data stored there. That should be covered by the lockfile anyway
return NO;
}
// returns YES if the block should have expired active based on the specified end time (i.e. the end time is in the past), or NO otherwise
+ (BOOL)currentBlockIsExpired {
// the block should be running if the end date hasn't arrived yet
SCSettings* settings = [SCSettings sharedSettings];
if ([[settings valueForKey: @"BlockEndDate"] timeIntervalSinceNow] > 0) {
return NO;
} else {
return YES;
}
}
+ (BOOL)blockRulesFoundOnSystem {
return [PacketFilter blockFoundInPF] || [HostFileBlocker blockFoundInHostsFile];
}
+ (void) removeBlockFromSettings {
SCSettings* settings = [SCSettings sharedSettings];
[settings setValue: @NO forKey: @"BlockIsRunning"];
[settings setValue: nil forKey: @"BlockEndDate"];
[settings setValue: nil forKey: @"ActiveBlocklist"];
[settings setValue: nil forKey: @"ActiveBlockAsWhitelist"];
}
@end