1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-04 10:38:13 +00:00

use the Transmission website for checking port status; stop previous port check before starting a new one

This commit is contained in:
Mitchell Livingston 2007-12-18 20:02:49 +00:00
parent 307ddcc74f
commit 896a19ae28
4 changed files with 51 additions and 56 deletions

View file

@ -24,18 +24,26 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
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 @interface PortChecker : NSObject
{ {
id fDelegate; id fDelegate;
port_status_t fStatus; port_status_t fStatus;
NSURLConnection * fConnection;
NSMutableData * fPortProbeData; NSMutableData * fPortProbeData;
} }
- (id) initWithDelegate: (id) delegate; - (id) initWithDelegate: (id) delegate;
- (void) probePort: (int) portNumber; - (void) probePort: (int) portNumber;
- (void) endProbe;
- (void) callBackWithStatus: (port_status_t) status; - (void) callBackWithStatus: (port_status_t) status;
- (port_status_t) status; - (port_status_t) status;

View file

@ -23,6 +23,7 @@
*****************************************************************************/ *****************************************************************************/
#import "PortChecker.h" #import "PortChecker.h"
#import "NSApplicationAdditions.h"
@implementation PortChecker @implementation PortChecker
@ -36,6 +37,13 @@
return self; return self;
} }
- (void) dealloc
{
[fPortProbeData release];
[fConnection release];
[super dealloc];
}
- (port_status_t) status - (port_status_t) status
{ {
return fStatus; return fStatus;
@ -44,10 +52,12 @@
- (void) probePort: (int) portNumber - (void) probePort: (int) portNumber
{ {
NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:
[NSString stringWithFormat: @"https://www.grc.com/x/portprobe=%d", portNumber]] [NSString stringWithFormat: @"http://transmission.m0k.org/PortCheck.php?port=%d", portNumber]] cachePolicy:
cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 15.0]; [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]; fPortProbeData = [[NSMutableData data] retain];
else else
{ {
@ -56,6 +66,11 @@
} }
} }
- (void) endProbe
{
[fConnection cancel];
}
- (void) callBackWithStatus: (port_status_t) status - (void) callBackWithStatus: (port_status_t) status
{ {
fStatus = status; fStatus = status;
@ -80,55 +95,23 @@
{ {
NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]); NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
[self callBackWithStatus: PORT_STATUS_ERROR]; [self callBackWithStatus: PORT_STATUS_ERROR];
[fPortProbeData release];
} }
- (void) connectionDidFinishLoading: (NSURLConnection *) connection - (void) connectionDidFinishLoading: (NSURLConnection *) connection
{ {
NSXMLDocument * shieldsUpProbe = [[NSXMLDocument alloc] initWithData: fPortProbeData NSString * probeString = [[NSString alloc] initWithData: fPortProbeData encoding: NSASCIIStringEncoding];
options: NSXMLDocumentTidyHTML error: nil];
if (shieldsUpProbe) port_status_t status;
{ if ([probeString isEqualToString: @"0"])
NSArray * nodes = [shieldsUpProbe nodesForXPath: @"/html/body/center/table[3]/tr/td[2]" error: nil]; status = PORT_STATUS_OPEN;
if ([nodes count] != 1) else if ([probeString isEqualToString: @"1"])
{ status = PORT_STATUS_CLOSED;
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 else
{ status = PORT_STATUS_ERROR;
NSString * portStatus = [[[[nodes objectAtIndex: 0] stringValue] stringByTrimmingCharactersInSet:
[[NSCharacterSet letterCharacterSet] invertedSet]] lowercaseString];
if ([portStatus isEqualToString: @"open"]) [self callBackWithStatus: status];
[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];
}
else
{
NSLog(@"Unable to get port status: failed to create xml document");
[self callBackWithStatus: PORT_STATUS_ERROR];
}
[fPortProbeData release]; [probeString release];
} }
@end @end

View file

@ -47,6 +47,7 @@
IBOutlet NSTextField * fUploadField, * fDownloadField, IBOutlet NSTextField * fUploadField, * fDownloadField,
* fSpeedLimitUploadField, * fSpeedLimitDownloadField; * fSpeedLimitUploadField, * fSpeedLimitDownloadField;
PortChecker * fPortChecker;
IBOutlet NSTextField * fPortField, * fPortStatusField; IBOutlet NSTextField * fPortField, * fPortStatusField;
IBOutlet NSButton * fNatCheck; IBOutlet NSButton * fNatCheck;
IBOutlet NSImageView * fPortStatusImage; IBOutlet NSImageView * fPortStatusImage;

View file

@ -228,6 +228,7 @@
- (void) updatePortStatus - (void) updatePortStatus
{ {
#warning look into
tr_handle_status * stat = tr_handleStatus(fHandle); tr_handle_status * stat = tr_handleStatus(fHandle);
if (fNatStatus != stat->natTraversalStatus || fPublicPort != stat->publicPort) if (fNatStatus != stat->natTraversalStatus || fPublicPort != stat->publicPort)
{ {
@ -239,8 +240,13 @@
[fPortStatusImage setImage: nil]; [fPortStatusImage setImage: nil];
[fPortStatusProgress startAnimation: self]; [fPortStatusProgress startAnimation: self];
PortChecker * portChecker = [[PortChecker alloc] initWithDelegate: self]; if (fPortChecker)
[portChecker probePort: fPublicPort]; {
[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")]; [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Advanced -> port status")];
[fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.png"]]; [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.png"]];
break; 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: case PORT_STATUS_CLOSED:
[fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Advanced -> port status")]; [fPortStatusField setStringValue: NSLocalizedString(@"Port is closed", "Preferences -> Advanced -> port status")];
[fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]]; [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]];
@ -267,7 +269,8 @@
[fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.png"]]; [fPortStatusImage setImage: [NSImage imageNamed: @"YellowDot.png"]];
break; break;
} }
[portChecker release]; [fPortChecker release];
fPortChecker = nil;
} }
- (NSArray *) sounds - (NSArray *) sounds