You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							363 lines
						
					
					
						
							8.8 KiB
						
					
					
				
			
		
		
	
	
							363 lines
						
					
					
						
							8.8 KiB
						
					
					
				//
 | 
						|
//  WRTStatusClient.m
 | 
						|
//  DDWRT-Monitor
 | 
						|
//
 | 
						|
//  Created by Matthieu Lalonde & Spike Grobstein on 11-05-27.
 | 
						|
//  Copyleft 2011 Spurf CC BY-SH-NC. Some rights reserved.
 | 
						|
//
 | 
						|
 | 
						|
#import "WRTStatusClient.h"
 | 
						|
#import <Security/Security.h>
 | 
						|
 | 
						|
@implementation WRTStatusClient
 | 
						|
 | 
						|
@synthesize receivedData;
 | 
						|
@synthesize delegate;
 | 
						|
@synthesize callback;
 | 
						|
@synthesize errorCallback;
 | 
						|
 | 
						|
@synthesize statusDelegate;
 | 
						|
@synthesize statusCallback;
 | 
						|
 | 
						|
- (id) initWithHostname:(NSString*)new_hostname port:(int)new_port protocol:(NSString*)new_protocol username:(NSString*)new_username password:(NSString*)new_password
 | 
						|
{
 | 
						|
	self = [super init];
 | 
						|
	
 | 
						|
	[self setHostname: new_hostname];
 | 
						|
	[self setProtocol: new_protocol];
 | 
						|
	[self setPort: new_port];
 | 
						|
	[self setUsername: new_username];
 | 
						|
	[self setPassword: new_password];
 | 
						|
	
 | 
						|
	wrtReachable	= false;
 | 
						|
	wanPort			= nil;
 | 
						|
	
 | 
						|
	return self;
 | 
						|
}
 | 
						|
 | 
						|
#pragma mark -
 | 
						|
 | 
						|
- (void) registerStatusCallback:(id)status_delegate callback:(SEL)status_callback
 | 
						|
{
 | 
						|
	statusDelegate	= status_delegate;
 | 
						|
	statusCallback	= status_callback;
 | 
						|
}
 | 
						|
 | 
						|
- (NSMutableURLRequest*) buildRequest:(NSString*)uri
 | 
						|
{
 | 
						|
	NSString *urlAddress = [NSString stringWithFormat:@"%@://%@:%d/%@",
 | 
						|
							protocol,
 | 
						|
							hostname, 
 | 
						|
							port,
 | 
						|
							uri];
 | 
						|
	
 | 
						|
	
 | 
						|
	//NSLog(@"Building request form %@", urlAddress);
 | 
						|
	
 | 
						|
	NSURL	*url	= [NSURL URLWithString:urlAddress];
 | 
						|
	
 | 
						|
	//[self request:url]; // ---V
 | 
						|
	
 | 
						|
	theRequest		= [[NSMutableURLRequest alloc] initWithURL: url];
 | 
						|
  
 | 
						|
	return [theRequest autorelease];
 | 
						|
}
 | 
						|
 | 
						|
- (NSMutableURLRequest*) requestForBandwidthViewerForInterface:(NSString*)interface
 | 
						|
{
 | 
						|
	NSString *urlAddress = [NSString stringWithFormat:@"%@://%@:%@@%@:%d/graph_if.svg?%@",
 | 
						|
							protocol,
 | 
						|
							username,
 | 
						|
							password,
 | 
						|
							hostname,
 | 
						|
							port,
 | 
						|
							wanPort];
 | 
						|
	
 | 
						|
	NSLog(@"Building request form %@", urlAddress);
 | 
						|
	
 | 
						|
	NSURL			*url		= [NSURL URLWithString:urlAddress];
 | 
						|
	
 | 
						|
	NSURLRequest	*request	= [[NSMutableURLRequest alloc] initWithURL: url];
 | 
						|
	
 | 
						|
	return [request autorelease];
 | 
						|
}
 | 
						|
 | 
						|
- (void) getStatusUpdate:(NSString*)uri delegate:(id)requestDelegate callback:(SEL)requestSelector
 | 
						|
{
 | 
						|
	delegate	= requestDelegate;
 | 
						|
	callback	= requestSelector;
 | 
						|
	
 | 
						|
	theConnection = [[NSURLConnection alloc] initWithRequest:[self buildRequest: uri] delegate:self];
 | 
						|
	
 | 
						|
	if (theConnection) {
 | 
						|
		receivedData	= [[NSMutableData data] retain];
 | 
						|
	}
 | 
						|
	
 | 
						|
	[theConnection autorelease];
 | 
						|
	
 | 
						|
	// this function returns immediately
 | 
						|
	// the delegate will receive a function call with the following signature upon completion:
 | 
						|
	//   - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 | 
						|
}
 | 
						|
 | 
						|
- (void) getConnectionStatus
 | 
						|
{
 | 
						|
	[self getStatusUpdate:@"Status_Bandwidth.asp" delegate:self callback:@selector(cbConnectionStatus:)];
 | 
						|
}
 | 
						|
 | 
						|
- (void) cbConnectionStatus:(NSData *)data
 | 
						|
{
 | 
						|
	if (data == nil) {
 | 
						|
		[self setWrtReachable:false];
 | 
						|
	} else {
 | 
						|
		NSString *stringData	= [[NSString alloc] initWithData:data  encoding:NSASCIIStringEncoding];
 | 
						|
		NSRange pageValid		= [stringData rangeOfString:@"Bandwidth Monitoring"];
 | 
						|
		NSRange startRange		= [stringData rangeOfString:@"WAN</h2>"];
 | 
						|
		
 | 
						|
		if (pageValid.location != NSNotFound && startRange.location != NSNotFound) {
 | 
						|
			// grab the wan port name
 | 
						|
			stringData			= [stringData substringFromIndex:startRange.location];
 | 
						|
			NSString *key		= @"<iframe src=\"/graph_if.svg?";
 | 
						|
			startRange			= [stringData rangeOfString:key];
 | 
						|
			stringData			= [stringData substringFromIndex:(startRange.location + [key length])];
 | 
						|
			NSRange endRange	= [stringData rangeOfString:@"\""];
 | 
						|
			
 | 
						|
			[self setWanPort:[stringData substringToIndex:endRange.location]];
 | 
						|
			[self setWrtReachable:true];
 | 
						|
		} else {
 | 
						|
			[self setWrtReachable:false];
 | 
						|
			wanPort				= nil;
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
#pragma mark -
 | 
						|
#pragma mark Utilities:
 | 
						|
 | 
						|
-(NSString *)getKey:(NSString *)stringData key:(NSString *)key
 | 
						|
{
 | 
						|
	NSString *keyString		= [NSString stringWithFormat:@"%@::", key];
 | 
						|
	NSString *returnData;
 | 
						|
	NSRange formatValid	= [stringData rangeOfString:@"}"];
 | 
						|
	
 | 
						|
	if (formatValid.location != NSNotFound) {
 | 
						|
		NSRange startRange		= [stringData rangeOfString:keyString];
 | 
						|
		
 | 
						|
		if (startRange.location != NSNotFound) {
 | 
						|
			
 | 
						|
			returnData	= [NSString stringWithString:stringData];
 | 
						|
			returnData	= [returnData substringFromIndex:(startRange.location + [keyString length])];
 | 
						|
			returnData	= [returnData substringToIndex:[returnData rangeOfString:@"}"].location];
 | 
						|
		}
 | 
						|
		
 | 
						|
		if (returnData) {
 | 
						|
			returnData	= [returnData stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 | 
						|
		}
 | 
						|
	}
 | 
						|
	
 | 
						|
	//[keyString release];
 | 
						|
	
 | 
						|
	return returnData;
 | 
						|
	
 | 
						|
}
 | 
						|
 | 
						|
#pragma mark -
 | 
						|
#pragma mark Accessors:
 | 
						|
 | 
						|
- (BOOL) getWrtReachable
 | 
						|
{
 | 
						|
	return wrtReachable;
 | 
						|
}
 | 
						|
 | 
						|
- (void) setWrtReachable:(BOOL)new_wrtReachable
 | 
						|
{
 | 
						|
	BOOL	old_wrtReachable	= wrtReachable;
 | 
						|
	wrtReachable				= new_wrtReachable;
 | 
						|
	
 | 
						|
	if (old_wrtReachable != new_wrtReachable && statusDelegate && statusCallback) {
 | 
						|
		if ([statusDelegate respondsToSelector:statusCallback]) {
 | 
						|
			[statusDelegate performSelector:statusCallback withObject:nil];
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
- (NSString*) getWanPort
 | 
						|
{
 | 
						|
	return wanPort;
 | 
						|
}
 | 
						|
 | 
						|
- (void) setWanPort:(NSString*)new_wanport
 | 
						|
{
 | 
						|
	NSString *old_wanport = hostname;
 | 
						|
	wanPort = [new_wanport retain];
 | 
						|
	[old_wanport autorelease];
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
- (NSString*) getHostname
 | 
						|
{
 | 
						|
	return hostname;
 | 
						|
}
 | 
						|
 | 
						|
- (void) setHostname:(NSString*)new_hostname
 | 
						|
{
 | 
						|
	NSString *old_hostname = hostname;
 | 
						|
	hostname = [new_hostname retain];
 | 
						|
	[old_hostname autorelease];
 | 
						|
}
 | 
						|
 | 
						|
- (NSString*) getProtocol
 | 
						|
{
 | 
						|
	return protocol;
 | 
						|
}
 | 
						|
 | 
						|
- (void) setProtocol:(NSString *)new_protocol
 | 
						|
{
 | 
						|
	NSString *old_protocol = protocol;
 | 
						|
	protocol = [new_protocol retain];
 | 
						|
	[old_protocol autorelease];
 | 
						|
}
 | 
						|
 | 
						|
- (int) getPort
 | 
						|
{
 | 
						|
	return port;
 | 
						|
}
 | 
						|
 | 
						|
- (void) setPort:(int)new_port
 | 
						|
{
 | 
						|
	port = new_port;
 | 
						|
}
 | 
						|
 | 
						|
- (NSString*) getUsername
 | 
						|
{
 | 
						|
	return username;
 | 
						|
}
 | 
						|
 | 
						|
- (void) setUsername:(NSString*)new_username
 | 
						|
{
 | 
						|
	NSString *old_username = username;
 | 
						|
	username = [new_username retain];
 | 
						|
	[old_username autorelease];
 | 
						|
}
 | 
						|
 | 
						|
- (NSString*) getPassword
 | 
						|
{
 | 
						|
	return password;
 | 
						|
}
 | 
						|
 | 
						|
- (void) setPassword:(NSString*)new_password
 | 
						|
{
 | 
						|
	NSString *old_password = password;
 | 
						|
	password = [new_password retain];
 | 
						|
	[old_password autorelease];
 | 
						|
}
 | 
						|
 | 
						|
#pragma mark -
 | 
						|
#pragma mark Credential Window Methods:
 | 
						|
/*
 | 
						|
- (void) showCredentialsWindow
 | 
						|
{
 | 
						|
	[credentialsWindow makeKeyAndOrderFront:nil];
 | 
						|
}
 | 
						|
 | 
						|
- (IBAction) hideCredentialsWindow
 | 
						|
{
 | 
						|
	[self setUsername:[fieldUsername stringValue]];
 | 
						|
	[self setPassword:[fieldPassword stringValue]];
 | 
						|
}
 | 
						|
*/
 | 
						|
#pragma mark -
 | 
						|
#pragma mark NSURLConnection Delegate Methods:
 | 
						|
 | 
						|
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
 | 
						|
{
 | 
						|
	//NSLog(@"1.0.1");
 | 
						|
	/*	NSAlert *alert	= [[NSAlert alloc] init];
 | 
						|
	[alert setAlertStyle:NSCriticalAlertStyle];
 | 
						|
	[alert setMessageText:@"LOGIN MOFO"];
 | 
						|
	[alert setAccessoryView:credentialsView];
 | 
						|
	[alert layout];
 | 
						|
	*/
 | 
						|
	/*NSInteger button	= *///[alert runModal];
 | 
						|
	
 | 
						|
 | 
						|
	return NO; // FIXME this need to return YES if an entry doesn't already exist in the keychain and the user will need to be prompted for credentials
 | 
						|
}
 | 
						|
 | 
						|
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
 | 
						|
{
 | 
						|
	//NSLog(@"1.0.2");
 | 
						|
	return YES;
 | 
						|
}
 | 
						|
 | 
						|
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
 | 
						|
{
 | 
						|
	NSLog(@"1");
 | 
						|
	if ([challenge previousFailureCount] == 0) {
 | 
						|
		NSLog(@"1.1.1");
 | 
						|
		NSURLCredential *newCredential;
 | 
						|
		newCredential	= [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistencePermanent];
 | 
						|
		
 | 
						|
		NSLog(@"1.1.2");
 | 
						|
		[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
 | 
						|
	} else {
 | 
						|
		NSLog(@"1.2");
 | 
						|
		[[challenge sender] cancelAuthenticationChallenge:challenge];
 | 
						|
		// TODO Pop up authentication error
 | 
						|
		NSLog(@"Invalid Username or Password");
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 | 
						|
{
 | 
						|
	//NSLog(@"2");
 | 
						|
	[receivedData setLength:0];
 | 
						|
}
 | 
						|
 | 
						|
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 | 
						|
{
 | 
						|
	[receivedData appendData:data];
 | 
						|
}
 | 
						|
 | 
						|
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 | 
						|
{
 | 
						|
	//NSLog(@"4");
 | 
						|
	//[connection release];
 | 
						|
	//[receivedData release];
 | 
						|
	//[theRequest release];
 | 
						|
	
 | 
						|
	//TODO Return alert
 | 
						|
	NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
 | 
						|
	
 | 
						|
	[self setWrtReachable:false];
 | 
						|
	
 | 
						|
	if (errorCallback) {
 | 
						|
		[delegate performSelector:errorCallback withObject:error];
 | 
						|
	} else if (delegate && callback) {
 | 
						|
		if ([delegate respondsToSelector:callback]) {
 | 
						|
			[delegate performSelector:callback withObject:nil];
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
 | 
						|
{
 | 
						|
	//NSLog(@"5");
 | 
						|
	// TODO: Return the data
 | 
						|
	if (delegate && callback) {
 | 
						|
		if ([delegate respondsToSelector:callback]) {
 | 
						|
			[delegate performSelector:callback withObject:receivedData];
 | 
						|
		} else {
 | 
						|
			// TODO: Return no data
 | 
						|
			NSLog(@"No response data from delegate");
 | 
						|
		}
 | 
						|
	} else {
 | 
						|
		NSLog(@"Invalid delegate callback");
 | 
						|
	}
 | 
						|
	
 | 
						|
	//[theConnection release];
 | 
						|
	//[receivedData release];
 | 
						|
	//[theRequest release];
 | 
						|
}
 | 
						|
@end
 |