diff --git a/macosx/PortChecker.h b/macosx/PortChecker.h index 9828789a6..8a67b9611 100644 --- a/macosx/PortChecker.h +++ b/macosx/PortChecker.h @@ -24,18 +24,26 @@ #import -typedef enum { PORT_STATUS_OPEN, PORT_STATUS_STEALTH, PORT_STATUS_CLOSED, PORT_STATUS_ERROR } port_status_t; +typedef enum +{ + PORT_STATUS_OPEN, + PORT_STATUS_CLOSED, + PORT_STATUS_ERROR +} port_status_t; @interface PortChecker : NSObject { id fDelegate; port_status_t fStatus; - + + NSURLConnection * fConnection; NSMutableData * fPortProbeData; } - (id) initWithDelegate: (id) delegate; - (void) probePort: (int) portNumber; +- (void) endProbe; + - (void) callBackWithStatus: (port_status_t) status; - (port_status_t) status; diff --git a/macosx/PortChecker.m b/macosx/PortChecker.m index ea379b8d8..0c27e83bd 100644 --- a/macosx/PortChecker.m +++ b/macosx/PortChecker.m @@ -23,6 +23,7 @@ *****************************************************************************/ #import "PortChecker.h" +#import "NSApplicationAdditions.h" @implementation PortChecker @@ -36,6 +37,13 @@ return self; } +- (void) dealloc +{ + [fPortProbeData release]; + [fConnection release]; + [super dealloc]; +} + - (port_status_t) status { return fStatus; @@ -44,10 +52,12 @@ - (void) probePort: (int) portNumber { NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: - [NSString stringWithFormat: @"https://www.grc.com/x/portprobe=%d", portNumber]] - cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 15.0]; + [NSString stringWithFormat: @"http://transmission.m0k.org/PortCheck.php?port=%d", portNumber]] cachePolicy: + [NSApp isOnLeopardOrBetter] ? NSURLRequestReloadIgnoringLocalAndRemoteCacheData : NSURLRequestReloadIgnoringCacheData + timeoutInterval: 15.0]; - if ([NSURLConnection connectionWithRequest: portProbeRequest delegate: self]) + + if ((fConnection = [[NSURLConnection alloc] initWithRequest: portProbeRequest delegate: self])) fPortProbeData = [[NSMutableData data] retain]; else { @@ -56,6 +66,11 @@ } } +- (void) endProbe +{ + [fConnection cancel]; +} + - (void) callBackWithStatus: (port_status_t) status { fStatus = status; @@ -80,55 +95,23 @@ { NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]); [self callBackWithStatus: PORT_STATUS_ERROR]; - [fPortProbeData release]; } - (void) connectionDidFinishLoading: (NSURLConnection *) connection { - NSXMLDocument * shieldsUpProbe = [[NSXMLDocument alloc] initWithData: fPortProbeData - options: NSXMLDocumentTidyHTML error: nil]; + NSString * probeString = [[NSString alloc] initWithData: fPortProbeData encoding: NSASCIIStringEncoding]; - if (shieldsUpProbe) - { - NSArray * nodes = [shieldsUpProbe nodesForXPath: @"/html/body/center/table[3]/tr/td[2]" error: nil]; - if ([nodes count] != 1) - { - NSArray * title = [shieldsUpProbe nodesForXPath: @"/html/head/title" error: nil]; - // This may happen when we probe twice too quickly - if ([title count] != 1 || ![[[title objectAtIndex: 0] stringValue] isEqualToString: - @"NanoProbe System Already In Use"]) - { - NSLog(@"Unable to get port status: invalid (outdated) XPath expression"); - [[shieldsUpProbe XMLData] writeToFile: @"/tmp/shieldsUpProbe.html" atomically: YES]; - [self callBackWithStatus: PORT_STATUS_ERROR]; - } - } - else - { - NSString * portStatus = [[[[nodes objectAtIndex: 0] stringValue] stringByTrimmingCharactersInSet: - [[NSCharacterSet letterCharacterSet] invertedSet]] lowercaseString]; - - if ([portStatus isEqualToString: @"open"]) - [self callBackWithStatus: PORT_STATUS_OPEN]; - else if ([portStatus isEqualToString: @"stealth"]) - [self callBackWithStatus: PORT_STATUS_STEALTH]; - else if ([portStatus isEqualToString: @"closed"]) - [self callBackWithStatus: PORT_STATUS_CLOSED]; - else - { - NSLog(@"Unable to get port status: unknown port state"); - [self callBackWithStatus: PORT_STATUS_ERROR]; - } - } - [shieldsUpProbe release]; - } + port_status_t status; + if ([probeString isEqualToString: @"0"]) + status = PORT_STATUS_OPEN; + else if ([probeString isEqualToString: @"1"]) + status = PORT_STATUS_CLOSED; else - { - NSLog(@"Unable to get port status: failed to create xml document"); - [self callBackWithStatus: PORT_STATUS_ERROR]; - } - - [fPortProbeData release]; + status = PORT_STATUS_ERROR; + + [self callBackWithStatus: status]; + + [probeString release]; } @end diff --git a/macosx/PrefsController.h b/macosx/PrefsController.h index f55546e8e..ecaafeefd 100644 --- a/macosx/PrefsController.h +++ b/macosx/PrefsController.h @@ -46,7 +46,8 @@ IBOutlet NSTextField * fUploadField, * fDownloadField, * fSpeedLimitUploadField, * fSpeedLimitDownloadField; - + + PortChecker * fPortChecker; IBOutlet NSTextField * fPortField, * fPortStatusField; IBOutlet NSButton * fNatCheck; IBOutlet NSImageView * fPortStatusImage; diff --git a/macosx/PrefsController.m b/macosx/PrefsController.m index e39c3cf36..27dc12a38 100644 --- a/macosx/PrefsController.m +++ b/macosx/PrefsController.m @@ -228,6 +228,7 @@ - (void) updatePortStatus { +#warning look into tr_handle_status * stat = tr_handleStatus(fHandle); if (fNatStatus != stat->natTraversalStatus || fPublicPort != stat->publicPort) { @@ -239,8 +240,13 @@ [fPortStatusImage setImage: nil]; [fPortStatusProgress startAnimation: self]; - PortChecker * portChecker = [[PortChecker alloc] initWithDelegate: self]; - [portChecker probePort: fPublicPort]; + if (fPortChecker) + { + [fPortChecker endProbe]; + [fPortChecker release]; + } + fPortChecker = [[PortChecker alloc] initWithDelegate: self]; + [fPortChecker probePort: fPublicPort]; } } @@ -253,10 +259,6 @@ [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Advanced -> port status")]; [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.png"]]; break; - case PORT_STATUS_STEALTH: - [fPortStatusField setStringValue: NSLocalizedString(@"Port is stealth", "Preferences -> Advanced -> port status")]; - [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]]; - break; case PORT_STATUS_CLOSED: [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Advanced -> port status")]; [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]]; @@ -267,7 +269,8 @@ [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.png"]]; break; } - [portChecker release]; + [fPortChecker release]; + fPortChecker = nil; } - (NSArray *) sounds