mirror of
https://github.com/transmission/transmission
synced 2025-03-04 02:28:03 +00:00
use the Transmission website for checking port status; stop previous port check before starting a new one
This commit is contained in:
parent
307ddcc74f
commit
896a19ae28
4 changed files with 51 additions and 56 deletions
|
@ -24,18 +24,26 @@
|
|||
|
||||
#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
|
||||
{
|
||||
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;
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -46,7 +46,8 @@
|
|||
|
||||
IBOutlet NSTextField * fUploadField, * fDownloadField,
|
||||
* fSpeedLimitUploadField, * fSpeedLimitDownloadField;
|
||||
|
||||
|
||||
PortChecker * fPortChecker;
|
||||
IBOutlet NSTextField * fPortField, * fPortStatusField;
|
||||
IBOutlet NSButton * fNatCheck;
|
||||
IBOutlet NSImageView * fPortStatusImage;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue