transmission/macosx/PortChecker.mm

142 lines
3.8 KiB
Plaintext
Raw Normal View History

// This file Copyright © 2006-2022 Transmission authors and contributors.
// It may be used under the MIT (SPDX: MIT) license.
// License text can be found in the licenses/ folder.
2007-09-16 01:02:06 +00:00
#import "PortChecker.h"
#define CHECKER_URL(port) [NSString stringWithFormat:@"https://portcheck.transmissionbt.com/%ld", port]
#define CHECK_FIRE 3.0
2007-12-19 20:46:00 +00:00
@interface PortChecker ()
@property(nonatomic, weak) NSObject<PortCheckerDelegate>* fDelegate;
@property(nonatomic) port_status_t fStatus;
@property(nonatomic) NSURLConnection* fConnection;
@property(nonatomic) NSMutableData* fPortProbeData;
@property(nonatomic) NSTimer* fTimer;
- (void)startProbe:(NSTimer*)timer;
- (void)callBackWithStatus:(port_status_t)status;
2008-09-07 16:08:29 +00:00
@end
2007-09-16 01:02:06 +00:00
@implementation PortChecker
- (instancetype)initForPort:(NSInteger)portNumber delay:(BOOL)delay withDelegate:(NSObject<PortCheckerDelegate>*)delegate
2007-09-16 01:02:06 +00:00
{
if ((self = [super init]))
{
_fDelegate = delegate;
_fStatus = PORT_STATUS_CHECKING;
_fTimer = [NSTimer scheduledTimerWithTimeInterval:CHECK_FIRE target:self selector:@selector(startProbe:)
userInfo:@(portNumber)
repeats:NO];
if (!delay)
{
[_fTimer fire];
}
2007-09-16 01:02:06 +00:00
}
2007-09-16 01:02:06 +00:00
return self;
}
- (void)dealloc
{
[_fTimer invalidate];
}
- (port_status_t)status
2007-09-16 01:02:06 +00:00
{
return self.fStatus;
2007-09-16 01:02:06 +00:00
}
- (void)cancelProbe
{
[self.fTimer invalidate];
self.fTimer = nil;
[self.fConnection cancel];
}
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
2007-09-16 01:02:06 +00:00
{
self.fPortProbeData.length = 0;
2007-09-16 01:02:06 +00:00
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
2007-09-16 01:02:06 +00:00
{
[self.fPortProbeData appendData:data];
2007-09-16 01:02:06 +00:00
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
2007-09-16 01:02:06 +00:00
{
NSLog(@"Unable to get port status: connection failed (%@)", error.localizedDescription);
[self callBackWithStatus:PORT_STATUS_ERROR];
2007-09-16 01:02:06 +00:00
}
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
2007-09-16 01:02:06 +00:00
{
NSString* probeString = [[NSString alloc] initWithData:self.fPortProbeData encoding:NSUTF8StringEncoding];
self.fPortProbeData = nil;
if (probeString)
{
if ([probeString isEqualToString:@"1"])
{
[self callBackWithStatus:PORT_STATUS_OPEN];
}
else if ([probeString isEqualToString:@"0"])
{
[self callBackWithStatus:PORT_STATUS_CLOSED];
}
else
{
2009-09-12 02:48:57 +00:00
NSLog(@"Unable to get port status: invalid response (%@)", probeString);
[self callBackWithStatus:PORT_STATUS_ERROR];
}
}
2007-09-16 01:02:06 +00:00
else
{
NSLog(@"Unable to get port status: invalid data received");
[self callBackWithStatus:PORT_STATUS_ERROR];
}
2007-09-16 01:02:06 +00:00
}
#pragma mark - Private
- (void)startProbe:(NSTimer*)timer
{
self.fTimer = nil;
NSURLRequest* portProbeRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:CHECKER_URL([[timer userInfo] integerValue])]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:15.0];
if ((self.fConnection = [[NSURLConnection alloc] initWithRequest:portProbeRequest delegate:self]))
{
self.fPortProbeData = [[NSMutableData alloc] init];
}
else
{
NSLog(@"Unable to get port status: failed to initiate connection");
[self callBackWithStatus:PORT_STATUS_ERROR];
}
}
- (void)callBackWithStatus:(port_status_t)status
2008-09-07 16:08:29 +00:00
{
self.fStatus = status;
if (self.fDelegate && [self.fDelegate respondsToSelector:@selector(portCheckerDidFinishProbing:)])
{
[self.fDelegate performSelectorOnMainThread:@selector(portCheckerDidFinishProbing:) withObject:self waitUntilDone:NO];
}
2008-09-07 16:08:29 +00:00
}
@end