Permalink
Browse files

Whitelist blocks can include linked domains (option)

  • Loading branch information...
1 parent 6978338 commit 9671184973149a4bd225c2fd4a3fd5f8f97e8383 @cstigler cstigler committed Oct 8, 2014
View
@@ -40,6 +40,7 @@ - (AppController*) init {
@"BlockStartedDate": [NSDate distantFuture],
@"HostBlacklist": @[],
@"EvaluateCommonSubdomains": @YES,
+ @"IncludeLinkedDomains": @YES,
@"HighlightInvalidHosts": @YES,
@"VerifyInternetConnection": @YES,
@"TimerWindowFloats": @NO,
View
@@ -33,11 +33,13 @@
BOOL isWhitelist;
BOOL allowLocal;
BOOL includeCommonSubdomains;
+ BOOL includeLinkedDomains;
}
- (BlockManager*)initAsWhitelist:(BOOL)whitelist;
- (BlockManager*)initAsWhitelist:(BOOL)whitelist allowLocal:(BOOL)local;
- (BlockManager*)initAsWhitelist:(BOOL)whitelist allowLocal:(BOOL)local includeCommonSubdomains:(BOOL)blockCommon;
+- (BlockManager*)initAsWhitelist:(BOOL)whitelist allowLocal:(BOOL)local includeCommonSubdomains:(BOOL)blockCommon includeLinkedDomains:(BOOL)includeLinked;
- (void)prepareToAddBlock;
- (void)finalizeBlock;
View
@@ -21,6 +21,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#import "BlockManager.h"
+#import "WhitelistScraper.h"
@implementation BlockManager
@@ -35,12 +36,14 @@ - (BlockManager*)initAsWhitelist:(BOOL)whitelist {
- (BlockManager*)initAsWhitelist:(BOOL)whitelist allowLocal:(BOOL)local {
return [self initAsWhitelist: whitelist allowLocal: local includeCommonSubdomains: YES];
}
-
-
- (BlockManager*)initAsWhitelist:(BOOL)whitelist allowLocal:(BOOL)local includeCommonSubdomains:(BOOL)blockCommon {
+ return [self initAsWhitelist: whitelist allowLocal: local includeCommonSubdomains: blockCommon includeLinkedDomains: YES];
+}
+
+- (BlockManager*)initAsWhitelist:(BOOL)whitelist allowLocal:(BOOL)local includeCommonSubdomains:(BOOL)blockCommon includeLinkedDomains:(BOOL)includeLinked {
if(self = [super init]) {
opQueue = [[NSOperationQueue alloc] init];
- [opQueue setMaxConcurrentOperationCount: 10];
+ [opQueue setMaxConcurrentOperationCount: 20];
pf = [[PacketFilter alloc] initAsWhitelist: whitelist];
hostsBlocker = [[HostFileBlocker alloc] init];
@@ -49,6 +52,7 @@ - (BlockManager*)initAsWhitelist:(BOOL)whitelist allowLocal:(BOOL)local includeC
isWhitelist = whitelist;
allowLocal = local;
includeCommonSubdomains = blockCommon;
+ includeLinkedDomains = includeLinked;
}
return self;
@@ -134,6 +138,13 @@ - (void)addBlockEntryFromString:(NSString*)entry {
[self addBlockEntryWithHostName: hostName port: portNum maskLen: maskLen];
+ if (isWhitelist && includeLinkedDomains) {
+ NSSet* relatedDomains = [WhitelistScraper relatedDomains: hostName];
+ [relatedDomains enumerateObjectsUsingBlock:^(NSString* host, BOOL* stop) {
+ [self enqueueBlockEntryWithHostName: host port: 0 maskLen: 0];
+ }];
+ }
+
if(![hostName isValidIPAddress] && includeCommonSubdomains) {
NSArray* commonSubdomains = [self commonSubdomainsForHostName: hostName];
View
@@ -20,6 +20,7 @@ void registerDefaults(uid_t controllingUID) {
@"BlockStartedDate": [NSDate distantFuture],
@"HostBlacklist": @[],
@"EvaluateCommonSubdomains": @YES,
+ @"IncludeLinkedDomains": @YES,
@"HighlightInvalidHosts": @YES,
@"VerifyInternetConnection": @YES,
@"TimerWindowFloats": @NO,
@@ -61,6 +62,7 @@ void addRulesToFirewall(uid_t controllingUID) {
NSDictionary* defaults = getDefaultsDict(controllingUID);
BOOL shouldEvaluateCommonSubdomains = [defaults[@"EvaluateCommonSubdomains"] boolValue];
BOOL allowLocalNetworks = [defaults[@"AllowLocalNetworks"] boolValue];
+ BOOL includeLinkedDomains = [defaults[@"IncludeLinkedDomains"] boolValue];
// get value for BlockAsWhitelist
BOOL blockAsWhitelist;
@@ -71,7 +73,7 @@ void addRulesToFirewall(uid_t controllingUID) {
blockAsWhitelist = [curDictionary[@"BlockAsWhitelist"] boolValue];
}
- BlockManager* blockManager = [[BlockManager alloc] initAsWhitelist: blockAsWhitelist allowLocal: allowLocalNetworks includeCommonSubdomains: shouldEvaluateCommonSubdomains];
+ BlockManager* blockManager = [[BlockManager alloc] initAsWhitelist: blockAsWhitelist allowLocal: allowLocalNetworks includeCommonSubdomains: shouldEvaluateCommonSubdomains includeLinkedDomains: includeLinkedDomains];
[blockManager prepareToAddBlock];
[blockManager addBlockEntries: domainList];
@@ -81,7 +83,7 @@ void addRulesToFirewall(uid_t controllingUID) {
void removeRulesFromFirewall(uid_t controllingUID) {
// options don't really matter because we're only using it to clear
- BlockManager* blockManager = [[BlockManager alloc] initAsWhitelist: FALSE allowLocal: TRUE includeCommonSubdomains: TRUE];
+ BlockManager* blockManager = [[BlockManager alloc] init];
[blockManager clearBlock];
// We'll play the sound now rather than putting it in the "defaults block"
@@ -107,7 +107,7 @@ int main(int argc, char* argv[]) {
status = [task terminationStatus];
[log appendFormat: @"Unloading the launchd daemon returned: %d\n\n", status];
- BlockManager* bm = [[BlockManager alloc] initAsWhitelist: NO];
+ BlockManager* bm = [[BlockManager alloc] init];
BOOL cleared = [bm forceClearBlock];
if (cleared) {
[log appendString: @"SUCCESSFULLY CLEARED BLOCK!!! Used [BlockManager forceClearBlock]\n"];
@@ -39,6 +39,8 @@
CB529BBF0F32B7ED00564FB8 /* AppController.m in Sources */ = {isa = PBXBuildFile; fileRef = CB529BBE0F32B7ED00564FB8 /* AppController.m */; };
CB54D44C0F93E33300AA22E9 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CB9E901D0F397FFA006DE6E4 /* Security.framework */; };
CB587E500F50FE8800C66A09 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CB587E4F0F50FE8800C66A09 /* SystemConfiguration.framework */; };
+ CB73616119E5086900E0924F /* WhitelistScraper.m in Sources */ = {isa = PBXBuildFile; fileRef = CB73615F19E4FDA000E0924F /* WhitelistScraper.m */; };
+ CB73616219E5086A00E0924F /* WhitelistScraper.m in Sources */ = {isa = PBXBuildFile; fileRef = CB73615F19E4FDA000E0924F /* WhitelistScraper.m */; };
CB90BF830F49F430006D202D /* HostImporter.m in Sources */ = {isa = PBXBuildFile; fileRef = CB90BF820F49F430006D202D /* HostImporter.m */; };
CB9365620F8581B000EF284E /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = CB9365610F8581B000EF284E /* dsa_pub.pem */; };
CB9366E80F85BEF100EF284E /* NSRemoveTemplate.jpg in Resources */ = {isa = PBXBuildFile; fileRef = CB9366E60F85BEF100EF284E /* NSRemoveTemplate.jpg */; };
@@ -93,6 +95,7 @@
CBD4848F19D768C90020F949 /* PreferencesAdvancedViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = CBD4848D19D768C90020F949 /* PreferencesAdvancedViewController.m */; };
CBE2BB5619D10CCC0077124F /* SCKillerHelper in Copy Helper Tools */ = {isa = PBXBuildFile; fileRef = CB9C811B19CFBA8500CDCAE1 /* SCKillerHelper */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
CBE4401B0F4BE0670062A1FE /* ThunderbirdPreferenceParser.m in Sources */ = {isa = PBXBuildFile; fileRef = CBE4401A0F4BE0670062A1FE /* ThunderbirdPreferenceParser.m */; };
+ CBE44FEB19E50900004E9706 /* WhitelistScraper.m in Sources */ = {isa = PBXBuildFile; fileRef = CB73615F19E4FDA000E0924F /* WhitelistScraper.m */; };
CBE5C40B0F4D4531003DB900 /* ButtonWithPopupMenu.m in Sources */ = {isa = PBXBuildFile; fileRef = CBE5C40A0F4D4531003DB900 /* ButtonWithPopupMenu.m */; };
CBEE50C10F48C21F00F5DF1C /* TimerWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = CBEE50C00F48C21F00F5DF1C /* TimerWindowController.m */; };
CF32FCC0BE72492A9CC7BE68 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5BF4C56EF9E741B8994020A7 /* libPods.a */; };
@@ -237,6 +240,8 @@
CB529BBE0F32B7ED00564FB8 /* AppController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppController.m; sourceTree = "<group>"; };
CB587E4F0F50FE8800C66A09 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = /System/Library/Frameworks/SystemConfiguration.framework; sourceTree = "<absolute>"; };
CB5E20BF0FAE0CDC006DBFC8 /* scheckup */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = scheckup; sourceTree = BUILT_PRODUCTS_DIR; };
+ CB73615E19E4FDA000E0924F /* WhitelistScraper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WhitelistScraper.h; sourceTree = "<group>"; };
+ CB73615F19E4FDA000E0924F /* WhitelistScraper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WhitelistScraper.m; sourceTree = "<group>"; };
CB90BF810F49F430006D202D /* HostImporter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HostImporter.h; sourceTree = "<group>"; };
CB90BF820F49F430006D202D /* HostImporter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HostImporter.m; sourceTree = "<group>"; };
CB9365610F8581B000EF284E /* dsa_pub.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = dsa_pub.pem; sourceTree = "<group>"; };
@@ -314,7 +319,7 @@
CBD2671E11ED882700042CD8 /* CheckupMain.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CheckupMain.h; sourceTree = "<group>"; };
CBD2671F11ED882700042CD8 /* CheckupMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CheckupMain.m; sourceTree = "<group>"; };
CBD4848219D75F6D0020F949 /* Sparkle.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Sparkle.framework; sourceTree = "<group>"; };
- CBD4848519D7611F0020F949 /* Podfile */ = {isa = PBXFileReference; lastKnownFileType = text; path = Podfile; sourceTree = "<group>"; };
+ CBD4848519D7611F0020F949 /* Podfile */ = {isa = PBXFileReference; lastKnownFileType = text; path = Podfile; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
CBD4848619D7611F0020F949 /* TemplateIcon2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = TemplateIcon2x.png; sourceTree = "<group>"; };
CBD4848719D764440020F949 /* PreferencesGeneralViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PreferencesGeneralViewController.h; sourceTree = "<group>"; };
CBD4848819D764440020F949 /* PreferencesGeneralViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PreferencesGeneralViewController.m; sourceTree = "<group>"; };
@@ -504,6 +509,8 @@
CBE4401A0F4BE0670062A1FE /* ThunderbirdPreferenceParser.m */,
CB90BF810F49F430006D202D /* HostImporter.h */,
CB90BF820F49F430006D202D /* HostImporter.m */,
+ CB73615E19E4FDA000E0924F /* WhitelistScraper.h */,
+ CB73615F19E4FDA000E0924F /* WhitelistScraper.m */,
CBEE50BF0F48C21F00F5DF1C /* TimerWindowController.h */,
CBEE50C00F48C21F00F5DF1C /* TimerWindowController.m */,
CBC2F8650F4674E300CF2A42 /* LaunchctlHelper.h */,
@@ -833,6 +840,7 @@
files = (
CBD2676C11ED926300042CD8 /* LaunchctlHelper.m in Sources */,
CBD266B111ED7D9C00042CD8 /* HelperCommon.m in Sources */,
+ CB73616119E5086900E0924F /* WhitelistScraper.m in Sources */,
CBD2672011ED882700042CD8 /* CheckupMain.m in Sources */,
CBD2676A11ED925700042CD8 /* HostFileBlocker.m in Sources */,
CB25806316C1FDBE0059C99A /* BlockManager.m in Sources */,
@@ -856,6 +864,7 @@
files = (
CB9C811E19CFBA8500CDCAE1 /* main.m in Sources */,
CB9C812719CFBB6400CDCAE1 /* NSString+IPAddress.m in Sources */,
+ CBE44FEB19E50900004E9706 /* WhitelistScraper.m in Sources */,
CB9C812619CFBB5E00CDCAE1 /* BlockManager.m in Sources */,
CB9C812519CFBB5500CDCAE1 /* HelperCommon.m in Sources */,
CB9C812419CFBB4E00CDCAE1 /* HostFileBlocker.m in Sources */,
@@ -870,6 +879,7 @@
files = (
CBA2AFD90F39EC46005AFEBE /* HelperMain.m in Sources */,
CBCA91121960D87300AFD20C /* PacketFilter.m in Sources */,
+ CB73616219E5086A00E0924F /* WhitelistScraper.m in Sources */,
CBC2F8580F4672FE00CF2A42 /* LaunchctlHelper.m in Sources */,
CBB0AE2A0FA74566006229B3 /* HostFileBlocker.m in Sources */,
CBD266B011ED7D9C00042CD8 /* HelperCommon.m in Sources */,
View
@@ -0,0 +1,15 @@
+//
+// WhitelistScraper.h
+// SelfControl
+//
+// Created by Charles Stigler on 10/7/14.
+//
+//
+
+#import <Foundation/Foundation.h>
+
+@interface WhitelistScraper : NSObject
+
++ (NSSet*)relatedDomains:(NSString*)domain;
+
+@end
View
@@ -0,0 +1,48 @@
+//
+// WhitelistScraper.m
+// SelfControl
+//
+// Created by Charles Stigler on 10/7/14.
+//
+//
+
+#import "WhitelistScraper.h"
+
+@implementation WhitelistScraper
+
++ (NSSet*)relatedDomains:(NSString*)domain; {
+ NSURL* rootURL = [NSURL URLWithString: [NSString stringWithFormat: @"http://%@", domain]];
+ if (!rootURL) {
+ return nil;;
+ }
+
+ // stale data is OK
+ NSURLRequest* request = [NSURLRequest requestWithURL: rootURL cachePolicy: NSURLRequestReturnCacheDataElseLoad timeoutInterval: 5];
+ NSURLResponse* response = nil;
+ NSError* error = nil;
+ NSData* data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
+ if (!response) {
+ return nil;
+ }
+ NSString* html = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
+ if (!html) {
+ return nil;
+ }
+
+ NSDataDetector* dataDetector = [[NSDataDetector alloc] initWithTypes: NSTextCheckingTypeLink error: nil];
+ NSCountedSet* relatedDomains = [NSCountedSet set];
+ [dataDetector enumerateMatchesInString: html
+ options: kNilOptions
+ range: NSMakeRange(0, [html length])
+ usingBlock:^(NSTextCheckingResult* result, NSMatchingFlags flags, BOOL* stop) {
+ if (([result.URL.scheme isEqualToString: @"http"] || [result.URL.scheme isEqualToString: @"https"])
+ && [result.URL.host length]
+ && ![result.URL.host isEqualToString: rootURL.host]) {
+ [relatedDomains addObject: result.URL.host];
+ }
+ }];
+
+ return relatedDomains;
+}
+
+@end
@@ -13,11 +13,11 @@
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="Hz6-mo-xeY">
- <rect key="frame" x="0.0" y="0.0" width="480" height="195"/>
+ <rect key="frame" x="0.0" y="0.0" width="480" height="228"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dSg-wR-arw">
- <rect key="frame" x="18" y="125" width="444" height="18"/>
+ <rect key="frame" x="18" y="158" width="444" height="18"/>
<buttonCell key="cell" type="check" title="Browsercache leeren" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="Y7x-Zc-xZU">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
@@ -27,7 +27,7 @@
</connections>
</button>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="tez-LB-kje">
- <rect key="frame" x="18" y="57" width="444" height="18"/>
+ <rect key="frame" x="18" y="90" width="444" height="18"/>
<buttonCell key="cell" type="check" title="Allgemeine Subdomains sperren" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="gOx-e1-ZUm">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
@@ -37,7 +37,7 @@
</connections>
</button>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="q9x-EI-6xR">
- <rect key="frame" x="18" y="23" width="444" height="18"/>
+ <rect key="frame" x="18" y="56" width="444" height="18"/>
<buttonCell key="cell" type="check" title="Ungültige Hosts markieren" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="x53-JT-2sG">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
@@ -47,7 +47,7 @@
</connections>
</button>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="gkT-O1-AZS">
- <rect key="frame" x="18" y="91" width="444" height="18"/>
+ <rect key="frame" x="18" y="124" width="444" height="18"/>
<buttonCell key="cell" type="check" title="Lokale Netzwerke zulassen" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="4wN-EQ-U6a">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
@@ -57,7 +57,7 @@
</connections>
</button>
<button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="YfV-CO-6Ud">
- <rect key="frame" x="18" y="159" width="444" height="18"/>
+ <rect key="frame" x="18" y="192" width="444" height="18"/>
<buttonCell key="cell" type="check" title="Internetverbindung überprüfen" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="QeK-Oh-Gga">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
@@ -66,8 +66,18 @@
<binding destination="ELT-Ca-Hib" name="value" keyPath="values.VerifyInternetConnection" id="23Y-uI-JTZ"/>
</connections>
</button>
+ <button fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="CBJ-mf-Fw0">
+ <rect key="frame" x="18" y="18" width="444" height="18"/>
+ <buttonCell key="cell" type="check" title="Whitelist blocks should include linked sites (slow)" bezelStyle="regularSquare" imagePosition="left" alignment="left" state="on" inset="2" id="WeC-1O-ZHw">
+ <behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
+ <font key="font" metaFont="system"/>
+ </buttonCell>
+ <connections>
+ <binding destination="ELT-Ca-Hib" name="value" keyPath="values.IncludeLinkedDomains" id="TDU-VR-KTf"/>
+ </connections>
+ </button>
</subviews>
- <point key="canvasLocation" x="345" y="323.5"/>
+ <point key="canvasLocation" x="345" y="340"/>
</customView>
<userDefaultsController representsSharedInstance="YES" id="ELT-Ca-Hib"/>
</objects>
Oops, something went wrong.

0 comments on commit 9671184

Please sign in to comment.