Permalink
Browse files

custom home page setting. refactor settings storage.

  • Loading branch information...
1 parent be2f349 commit 497be74840df8e5c3ae36130ab79c0f361f9dabb @mtigas committed Jan 26, 2014
@@ -347,7 +347,9 @@ - (CFHTTPMessageRef)makeHTTPMessage
//[NSMakeCollectable(result) autorelease];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
- Byte spoofUserAgent = appDelegate.spoofUserAgent;
+ NSMutableDictionary *settings = appDelegate.getSettings;
+
+ Byte spoofUserAgent = [[settings valueForKey:@"uaspoof"] integerValue];
NSDictionary *HTTPHeaderFields = [self allHTTPHeaderFields];
@@ -375,7 +377,8 @@ - (CFHTTPMessageRef)makeHTTPMessage
(__bridge CFStringRef)[HTTPHeaderFields objectForKey:aHTTPHeaderField]);
}
/* Do not track (DNT) header */
- Byte dntHeader = appDelegate.dntHeader;
+
+ Byte dntHeader = [[settings valueForKey:@"dnt"] integerValue];
if (dntHeader != DNT_HEADER_UNSET) {
// DNT_HEADER_CANTRACK is 0 and DNT_HEADER_NOTRACK is 1,
// so we can pass that value in as the "DNT: X" value
@@ -9,14 +9,21 @@
#import "WebViewController.h"
#import "TorController.h"
-#define DNT_HEADER_UNSET 0
-#define DNT_HEADER_CANTRACK 1
-#define DNT_HEADER_NOTRACK 2
+#define COOKIES_ALLOW_ALL 0
+#define COOKIES_BLOCK_THIRDPARTY 1
+#define COOKIES_BLOCK_ALL 2
#define UA_SPOOF_NO 0
#define UA_SPOOF_WIN7_TORBROWSER 1
#define UA_SPOOF_SAFARI_MAC 2
+#define PIPELINING_OFF 0
+#define PIPELINING_ON 1
+
+#define DNT_HEADER_UNSET 0
+#define DNT_HEADER_CANTRACK 1
+#define DNT_HEADER_NOTRACK 2
+
#define X_DEVICE_IS_IPHONE 0
#define X_DEVICE_IS_IPAD 1
#define X_DEVICE_IS_SIM 2
@@ -33,12 +40,8 @@
@property (nonatomic) WebViewController *appWebView;
-@property (nonatomic) Byte spoofUserAgent;
-@property (nonatomic) Byte dntHeader;
-@property (nonatomic) Boolean usePipelining;
-
-// either nil (for standard "onionbrowser:start" ) or url given by a "onionbrowser://" (HTTP)
-// or "onionbrowsers://" (HTTPS) callback url
+// either nil (to load regular homepage) or url given by a "onionbrowser://" (HTTP)
+// or "onionbrowsers://" (HTTPS) callback url -- when this app is started from another app
@property (nonatomic) NSURL *startUrl;
// list for known domains w/self-signed certs
@@ -52,5 +55,9 @@
- (NSUInteger) deviceType;
- (Boolean) isRunningTests;
+- (NSString *)settingsFile;
+- (NSMutableDictionary *)getSettings;
+- (void)saveSettings:(NSMutableDictionary *)settings;
+- (NSString *)homepage;
@end
@@ -14,9 +14,6 @@
@implementation AppDelegate
@synthesize
- spoofUserAgent,
- dntHeader,
- usePipelining,
sslWhitelistedDomains,
startUrl,
appWebView,
@@ -50,9 +47,15 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
sslWhitelistedDomains = [[NSMutableArray alloc] init];
- spoofUserAgent = UA_SPOOF_NO;
- dntHeader = DNT_HEADER_UNSET;
- usePipelining = YES;
+ NSMutableDictionary *settings = self.getSettings;
+ NSInteger cookieSetting = [[settings valueForKey:@"cookies"] integerValue];
+ if (cookieSetting == COOKIES_ALLOW_ALL) {
+ [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
+ } else if (cookieSetting == COOKIES_BLOCK_THIRDPARTY) {
+ [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain];
+ } else if (cookieSetting == COOKIES_BLOCK_ALL) {
+ [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
+ }
// Start the spinner for the "connecting..." phase
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
@@ -308,5 +311,52 @@ - (Boolean)isRunningTests {
}
+- (NSString *)settingsFile {
+ return [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"Settings.plist"];
+}
+
+- (NSMutableDictionary *)getSettings {
+ NSString *errorDesc = nil;
+ NSPropertyListFormat format;
+ NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:self.settingsFile];
+ if (plistXML == nil) {
+ // We didn't have a settings file, so we'll want to initialize one now.
+ NSMutableDictionary *d = [NSMutableDictionary dictionary];
+ // SETTINGS DEFAULTS
+ [d setObject:@"onionbrowser:home" forKey:@"homepage"]; // DEFAULT HOMEPAGE
+ [d setObject:[NSNumber numberWithInteger:COOKIES_BLOCK_THIRDPARTY] forKey:@"cookies"];
+ [d setObject:[NSNumber numberWithInteger:UA_SPOOF_NO] forKey:@"uaspoof"];
+ [d setObject:[NSNumber numberWithInteger:PIPELINING_ON] forKey:@"pipelining"];
+ [d setObject:[NSNumber numberWithInteger:DNT_HEADER_UNSET] forKey:@"dnt"];
+ // END SETTINGS DEFAULTS
+ [self saveSettings:d];
+ return d;
+ } else {
+ return (NSMutableDictionary *)[NSPropertyListSerialization
+ propertyListFromData:plistXML
+ mutabilityOption:NSPropertyListMutableContainersAndLeaves
+ format:&format errorDescription:&errorDesc];
+ }
+}
+
+- (void)saveSettings:(NSMutableDictionary *)settings {
+ NSError *error;
+ NSData *data =
+ [NSPropertyListSerialization dataWithPropertyList:settings
+ format:NSPropertyListXMLFormat_v1_0
+ options:0
+ error:&error];
+ if (data == nil) {
+ NSLog (@"error serializing to xml: %@", error);
+ return;
+ } else {
+ [data writeToFile:self.settingsFile atomically:YES];
+ }
+}
+
+- (NSString *)homepage {
+ NSMutableDictionary *d = self.getSettings;
+ return [d objectForKey:@"homepage"];
+}
@end
@@ -93,7 +93,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}
if(indexPath.section == 0) {
- cell.textLabel.text = @"https://check.torproject.org/";
+ AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
+ NSMutableDictionary *settings2 = appDelegate.getSettings;
+ cell.textLabel.text = [settings2 objectForKey:@"homepage"];
} else if(indexPath.section == 1) {
// Cookies
NSHTTPCookie *cookie;
@@ -128,23 +130,26 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
} else if (indexPath.section == 2) {
// User-Agent
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
+ NSMutableDictionary *settings = appDelegate.getSettings;
+ NSInteger spoofUserAgent = [[settings valueForKey:@"uaspoof"] integerValue];
+
if (indexPath.row == 0) {
cell.textLabel.text = @"No Spoofing: iOS Safari";
- if (appDelegate.spoofUserAgent == UA_SPOOF_NO) {
+ if (spoofUserAgent == UA_SPOOF_NO) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
} else if (indexPath.row == 1) {
cell.textLabel.text = @"Windows 7 (NT 6.1), Firefox 17";
- if (appDelegate.spoofUserAgent == UA_SPOOF_WIN7_TORBROWSER) {
+ if (spoofUserAgent == UA_SPOOF_WIN7_TORBROWSER) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
} else if (indexPath.row == 2) {
cell.textLabel.text = @"Mac OS X 10.8.4, Safari 6.0";
- if (appDelegate.spoofUserAgent == UA_SPOOF_SAFARI_MAC) {
+ if (spoofUserAgent == UA_SPOOF_SAFARI_MAC) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
@@ -153,17 +158,19 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
} else if (indexPath.section == 3) {
// Pipelining
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
-
+ NSMutableDictionary *settings = appDelegate.getSettings;
+ NSInteger usePipelining = [[settings valueForKey:@"pipelining"] integerValue];
+
if (indexPath.row == 0) {
cell.textLabel.text = @"Enabled (Better Performance)";
- if (appDelegate.usePipelining == YES) {
+ if (usePipelining == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
} else if (indexPath.row == 1) {
cell.textLabel.text = @"Disabled (Better Compatibility)";
- if (appDelegate.usePipelining == NO) {
+ if (usePipelining == NO) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
@@ -172,17 +179,19 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
} else if (indexPath.section == 4) {
// DNT
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
-
+ NSMutableDictionary *settings = appDelegate.getSettings;
+ NSInteger dntHeader = [[settings valueForKey:@"dnt"] integerValue];
+
if (indexPath.row == 0) {
cell.textLabel.text = @"No Header";
- if (appDelegate.dntHeader == DNT_HEADER_UNSET) {
+ if (dntHeader == DNT_HEADER_UNSET) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
} else if (indexPath.row == 1) {
cell.textLabel.text = @"Opt Out Of Tracking";
- if (appDelegate.dntHeader == DNT_HEADER_NOTRACK) {
+ if (dntHeader == DNT_HEADER_NOTRACK) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
@@ -217,37 +226,50 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
#pragma mark - Table view delegate
-- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
- if(indexPath.section == 0) {
- return YES;
- }
- return NO;
-}
-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
- UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Home Page" message:@"Home page URL:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save",nil];
+ UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Home Page" message:@"Leave blank to use default\nhome page with Tor Check." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
+ UITextField *textField = [alert textFieldAtIndex:0];
+
+ AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
+ NSMutableDictionary *settings2 = appDelegate.getSettings;
+ textField.text = [settings2 objectForKey:@"homepage"];
+
[alert show];
} else if(indexPath.section == 1) {
// Cookies
+ AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
+ NSMutableDictionary *settings = appDelegate.getSettings;
+
if (indexPath.row == 0) {
+ [settings setObject:[NSNumber numberWithInteger:COOKIES_ALLOW_ALL] forKey:@"uaspoof"];
+ [appDelegate saveSettings:settings];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
} else if (indexPath.row == 1) {
+ [settings setObject:[NSNumber numberWithInteger:COOKIES_BLOCK_THIRDPARTY] forKey:@"uaspoof"];
+ [appDelegate saveSettings:settings];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain];
} else if (indexPath.row == 2) {
+ [settings setObject:[NSNumber numberWithInteger:COOKIES_BLOCK_ALL] forKey:@"uaspoof"];
+ [appDelegate saveSettings:settings];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyNever];
}
} else if (indexPath.section == 2) {
// User-Agent
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
+ NSMutableDictionary *settings = appDelegate.getSettings;
+
if (indexPath.row == 0) {
- appDelegate.spoofUserAgent = UA_SPOOF_NO;
+ [settings setObject:[NSNumber numberWithInteger:UA_SPOOF_NO] forKey:@"uaspoof"];
+ [appDelegate saveSettings:settings];
} else {
if (indexPath.row == 1) {
- appDelegate.spoofUserAgent = UA_SPOOF_WIN7_TORBROWSER;
+ [settings setObject:[NSNumber numberWithInteger:UA_SPOOF_WIN7_TORBROWSER] forKey:@"uaspoof"];
+ [appDelegate saveSettings:settings];
} else if (indexPath.row == 2) {
- appDelegate.spoofUserAgent = UA_SPOOF_SAFARI_MAC;
+ [settings setObject:[NSNumber numberWithInteger:UA_SPOOF_SAFARI_MAC] forKey:@"uaspoof"];
+ [appDelegate saveSettings:settings];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:[NSString stringWithFormat:@"User Agent spoofing enabled.\n\nNote that JavaScript cannot be disabled due to framework limitations. Scripts and other iOS features may still identify your browser.\n\nSome mobile or tablet websites may not work properly without the original mobile User Agent."]
@@ -259,24 +281,32 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
} else if (indexPath.section == 3) {
// Pipelining
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
+ NSMutableDictionary *settings = appDelegate.getSettings;
+
if (indexPath.row == 0) {
- appDelegate.usePipelining = YES;
+ [settings setObject:[NSNumber numberWithInteger:PIPELINING_ON] forKey:@"pipelining"];
+ [appDelegate saveSettings:settings];
} else if (indexPath.row == 1) {
- appDelegate.usePipelining = NO;
+ [settings setObject:[NSNumber numberWithInteger:PIPELINING_OFF] forKey:@"pipelining"];
+ [appDelegate saveSettings:settings];
}
} else if (indexPath.section == 4) {
// DNT
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
+ NSMutableDictionary *settings = appDelegate.getSettings;
+
if (indexPath.row == 0) {
- appDelegate.dntHeader = DNT_HEADER_UNSET;
+ [settings setObject:[NSNumber numberWithInteger:DNT_HEADER_UNSET] forKey:@"dnt"];
+ [appDelegate saveSettings:settings];
} else if (indexPath.row == 1) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
+ [settings setObject:[NSNumber numberWithInteger:DNT_HEADER_NOTRACK] forKey:@"dnt"];
+ [appDelegate saveSettings:settings];
+ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:[NSString stringWithFormat:@"Onion Browser will now send the 'DNT: 1' header. Note that because only very new browsers send this optional header, this opt-in feature may allow websites to uniquely identify you."]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
- appDelegate.dntHeader = DNT_HEADER_NOTRACK;
}
} else if (indexPath.section == 5) {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
@@ -294,8 +324,21 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
- NSLog(@"Entered: %d %@", buttonIndex, [[alertView textFieldAtIndex:0] text]);
+ AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
+ NSMutableDictionary *settings = appDelegate.getSettings;
+
+ if ([[[alertView textFieldAtIndex:0] text] length] == 0) {
+ [settings setValue:@"onionbrowser:home" forKey:@"homepage"]; // DEFAULT HOMEPAGE
+ } else {
+ [settings setValue:[[alertView textFieldAtIndex:0] text] forKey:@"homepage"];
+ }
+ [appDelegate saveSettings:settings];
+ [self.tableView reloadData];
}
}
+
+
+
+
@end
Oops, something went wrong.

0 comments on commit 497be74

Please sign in to comment.