Files
shadowsocks-iOS/OnionBrowser/AppDelegate.m
2013-01-01 19:43:03 +08:00

213 lines
7.4 KiB
Objective-C

//
// AppDelegate.m
// OnionBrowser
//
// Copyright (c) 2012 Mike Tigas. All rights reserved.
//
#import "AppDelegate.h"
//#include <Openssl/sha.h>
#import "Bridge.h"
#import "ProxySettingsTableViewController.h"
@implementation AppDelegate
@synthesize
spoofUserAgent,
dntHeader,
usePipelining,
sslWhitelistedDomains,
appWebView,
// tor = _tor,
window = _window,
managedObjectContext = __managedObjectContext,
managedObjectModel = __managedObjectModel,
persistentStoreCoordinator = __persistentStoreCoordinator,
doPrepopulateBookmarks,
proxyThread
;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// self.proxyThread = [[NSThread alloc] initWithTarget:self
// selector:@selector(runProxy)
// object:nil];
// [proxyThread start];
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
}];
dispatch_queue_t dis = dispatch_queue_create("background", NULL);
dispatch_async(dis, ^{
[self runProxy];
});
// Detect
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Settings.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
doPrepopulateBookmarks = (![fileManager fileExistsAtPath:[storeURL path]]);
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
appWebView = [[WebViewController alloc] init];
[_window setRootViewController:appWebView];
[_window makeKeyAndVisible];
// [self updateTorrc];
// _tor = [[TorController alloc] init];
// [_tor startTor];
sslWhitelistedDomains = [[NSMutableArray alloc] init];
spoofUserAgent = UA_SPOOF_NO;
dntHeader = DNT_HEADER_UNSET;
usePipelining = YES;
return YES;
}
- (void)updateTorrc {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *destTorrc = [[[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"torrc"] relativePath];
if ([fileManager fileExistsAtPath:destTorrc]) {
[fileManager removeItemAtPath:destTorrc error:NULL];
}
NSString *sourceTorrc = [[NSBundle mainBundle] pathForResource:@"torrc" ofType:nil];
NSError *error = nil;
[fileManager copyItemAtPath:sourceTorrc toPath:destTorrc error:&error];
if (error != nil) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
if (![fileManager fileExistsAtPath:sourceTorrc]) {
NSLog(@"(Source torrc %@ doesnt exist)", sourceTorrc);
}
}
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Bridge" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
} else if ([mutableFetchResults count] > 0) {
NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:destTorrc];
[myHandle seekToEndOfFile];
[myHandle writeData:[@"UseBridges 1\n" dataUsingEncoding:NSUTF8StringEncoding]];
for (Bridge *bridge in mutableFetchResults) {
if ([bridge.conf isEqualToString:@"Tap Here To Edit"]||[bridge.conf isEqualToString:@""]) {
// skip
} else {
[myHandle writeData:[[NSString stringWithFormat:@"bridge %@\n", bridge.conf]
dataUsingEncoding:NSUTF8StringEncoding]];
}
}
}
}
#pragma mark - Core Data stack
// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil) {
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil) {
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Settings" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Settings.sqlite"];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
#pragma mark - Run proxy
-(void)runProxy {
[ProxySettingsTableViewController reloadConfig];
for(;;) {
if ([ProxySettingsTableViewController runProxy]) {
sleep(1);
} else {
sleep(2);
}
}
}
#pragma mark -
#pragma mark App lifecycle
- (void)applicationWillResignActive:(UIApplication *)application {
// [_tor disableTorCheckLoop];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// if (!_tor.didFirstConnect) {
// // User is trying to quit app before we have finished initial
// // connection. This is basically an "abort" situation because
// // backgrounding while Tor is attempting to connect will almost
// // definitely result in a hung Tor client. Quit the app entirely,
// // since this is also a good way to allow user to retry initial
// // connection if it fails.
// #ifdef DEBUG
// NSLog(@"Went to BG before initial connection completed: exiting.");
// #endif
// exit(0);
// } else {
// [_tor disableTorCheckLoop];
// }
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Don't want to call "activateTorCheckLoop" directly since we
// want to HUP tor first.
// [_tor appDidBecomeActive];
}
@end