Changeset 4203


Ignore:
Timestamp:
Dec 18, 2007, 8:02:49 PM (14 years ago)
Author:
livings124
Message:

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

Location:
trunk/macosx
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/PortChecker.h

    r3083 r4203  
    2525#import <Cocoa/Cocoa.h>
    2626
    27 typedef enum { PORT_STATUS_OPEN, PORT_STATUS_STEALTH, PORT_STATUS_CLOSED, PORT_STATUS_ERROR } port_status_t;
     27typedef enum
     28{
     29    PORT_STATUS_OPEN,
     30    PORT_STATUS_CLOSED,
     31    PORT_STATUS_ERROR
     32} port_status_t;
    2833
    2934@interface PortChecker : NSObject
     
    3136    id              fDelegate;
    3237    port_status_t   fStatus;
    33 
     38   
     39    NSURLConnection * fConnection;
    3440    NSMutableData   * fPortProbeData;
    3541}
     
    3743- (id) initWithDelegate: (id) delegate;
    3844- (void) probePort: (int) portNumber;
     45- (void) endProbe;
     46
    3947- (void) callBackWithStatus: (port_status_t) status;
    4048- (port_status_t) status;
  • trunk/macosx/PortChecker.m

    r3464 r4203  
    2424
    2525#import "PortChecker.h"
     26#import "NSApplicationAdditions.h"
    2627
    2728@implementation PortChecker
     
    3738}
    3839
     40- (void) dealloc
     41{
     42    [fPortProbeData release];
     43    [fConnection release];
     44    [super dealloc];
     45}
     46
    3947- (port_status_t) status
    4048{
     
    4553{
    4654    NSURLRequest * portProbeRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:
    47                                 [NSString stringWithFormat: @"https://www.grc.com/x/portprobe=%d", portNumber]]
    48                                 cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 15.0];
     55                [NSString stringWithFormat: @"http://transmission.m0k.org/PortCheck.php?port=%d", portNumber]] cachePolicy:
     56                [NSApp isOnLeopardOrBetter] ? NSURLRequestReloadIgnoringLocalAndRemoteCacheData : NSURLRequestReloadIgnoringCacheData
     57                timeoutInterval: 15.0];
    4958   
    50     if ([NSURLConnection connectionWithRequest: portProbeRequest delegate: self])
     59   
     60    if ((fConnection = [[NSURLConnection alloc] initWithRequest: portProbeRequest delegate: self]))
    5161        fPortProbeData = [[NSMutableData data] retain];
    5262    else
     
    5565        [self callBackWithStatus: PORT_STATUS_ERROR];
    5666    }
     67}
     68
     69- (void) endProbe
     70{
     71    [fConnection cancel];
    5772}
    5873
     
    8196    NSLog(@"Unable to get port status: connection failed (%@)", [error localizedDescription]);
    8297    [self callBackWithStatus: PORT_STATUS_ERROR];
    83     [fPortProbeData release];
    8498}
    8599
    86100- (void) connectionDidFinishLoading: (NSURLConnection *) connection
    87101{
    88     NSXMLDocument * shieldsUpProbe = [[NSXMLDocument alloc] initWithData: fPortProbeData
    89                                         options: NSXMLDocumentTidyHTML error: nil];
     102    NSString * probeString = [[NSString alloc] initWithData: fPortProbeData encoding: NSASCIIStringEncoding];
    90103   
    91     if (shieldsUpProbe)
    92     {
    93         NSArray * nodes = [shieldsUpProbe nodesForXPath: @"/html/body/center/table[3]/tr/td[2]" error: nil];
    94         if ([nodes count] != 1)
    95         {
    96             NSArray * title = [shieldsUpProbe nodesForXPath: @"/html/head/title" error: nil];
    97             // This may happen when we probe twice too quickly
    98             if ([title count] != 1 || ![[[title objectAtIndex: 0] stringValue] isEqualToString:
    99                                                                     @"NanoProbe System Already In Use"])
    100             {
    101                 NSLog(@"Unable to get port status: invalid (outdated) XPath expression");
    102                 [[shieldsUpProbe XMLData] writeToFile: @"/tmp/shieldsUpProbe.html" atomically: YES];
    103                 [self callBackWithStatus: PORT_STATUS_ERROR];
    104             }
    105         }
    106         else
    107         {
    108             NSString * portStatus = [[[[nodes objectAtIndex: 0] stringValue] stringByTrimmingCharactersInSet:
    109                                                 [[NSCharacterSet letterCharacterSet] invertedSet]] lowercaseString];
    110            
    111             if ([portStatus isEqualToString: @"open"])
    112                 [self callBackWithStatus: PORT_STATUS_OPEN];
    113             else if ([portStatus isEqualToString: @"stealth"])
    114                 [self callBackWithStatus: PORT_STATUS_STEALTH];
    115             else if ([portStatus isEqualToString: @"closed"])
    116                 [self callBackWithStatus: PORT_STATUS_CLOSED];
    117             else
    118             {
    119                 NSLog(@"Unable to get port status: unknown port state");
    120                 [self callBackWithStatus: PORT_STATUS_ERROR];
    121             }
    122         }
    123         [shieldsUpProbe release];
    124     }
     104    port_status_t status;
     105    if ([probeString isEqualToString: @"0"])
     106        status = PORT_STATUS_OPEN;
     107    else if ([probeString isEqualToString: @"1"])
     108        status = PORT_STATUS_CLOSED;
    125109    else
    126     {
    127         NSLog(@"Unable to get port status: failed to create xml document");
    128         [self callBackWithStatus: PORT_STATUS_ERROR];
    129     }
    130 
    131     [fPortProbeData release];
     110        status = PORT_STATUS_ERROR;
     111   
     112    [self callBackWithStatus: status];
     113   
     114    [probeString release];
    132115}
    133116
  • trunk/macosx/PrefsController.h

    r4076 r4203  
    4747    IBOutlet NSTextField    * fUploadField, * fDownloadField,
    4848                            * fSpeedLimitUploadField, * fSpeedLimitDownloadField;
    49 
     49   
     50    PortChecker                     * fPortChecker;
    5051    IBOutlet NSTextField            * fPortField, * fPortStatusField;
    5152    IBOutlet NSButton               * fNatCheck;
  • trunk/macosx/PrefsController.m

    r4080 r4203  
    229229- (void) updatePortStatus
    230230{
     231#warning look into
    231232    tr_handle_status * stat = tr_handleStatus(fHandle);
    232233    if (fNatStatus != stat->natTraversalStatus || fPublicPort != stat->publicPort)
     
    240241        [fPortStatusProgress startAnimation: self];
    241242       
    242         PortChecker * portChecker = [[PortChecker alloc] initWithDelegate: self];
    243         [portChecker probePort: fPublicPort];
     243        if (fPortChecker)
     244        {
     245            [fPortChecker endProbe];
     246            [fPortChecker release];
     247        }
     248        fPortChecker = [[PortChecker alloc] initWithDelegate: self];
     249        [fPortChecker probePort: fPublicPort];
    244250    }
    245251}
     
    253259            [fPortStatusField setStringValue: NSLocalizedString(@"Port is open", "Preferences -> Advanced -> port status")];
    254260            [fPortStatusImage setImage: [NSImage imageNamed: @"GreenDot.png"]];
    255             break;
    256         case PORT_STATUS_STEALTH:
    257             [fPortStatusField setStringValue: NSLocalizedString(@"Port is stealth", "Preferences -> Advanced -> port status")];
    258             [fPortStatusImage setImage: [NSImage imageNamed: @"RedDot.png"]];
    259261            break;
    260262        case PORT_STATUS_CLOSED:
     
    268270            break;
    269271    }
    270     [portChecker release];
     272    [fPortChecker release];
     273    fPortChecker = nil;
    271274}
    272275
Note: See TracChangeset for help on using the changeset viewer.