2022-01-20 18:27:56 +00:00
|
|
|
// 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"
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
#define CHECKER_URL(port) [NSString stringWithFormat:@"https://portcheck.transmissionbt.com/%ld", port]
|
2009-12-28 20:05:33 +00:00
|
|
|
#define CHECK_FIRE 3.0
|
2007-12-19 20:46:00 +00:00
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface PortChecker ()
|
2008-09-06 21:57:13 +00:00
|
|
|
|
2022-05-10 14:54:39 +00:00
|
|
|
@property(nonatomic, weak) NSObject<PortCheckerDelegate>* fDelegate;
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic) port_status_t fStatus;
|
|
|
|
|
|
|
|
@property(nonatomic) NSURLConnection* fConnection;
|
|
|
|
@property(nonatomic) NSMutableData* fPortProbeData;
|
|
|
|
|
|
|
|
@property(nonatomic) NSTimer* fTimer;
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)startProbe:(NSTimer*)timer;
|
2008-09-06 21:57:13 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)callBackWithStatus:(port_status_t)status;
|
2008-09-07 16:08:29 +00:00
|
|
|
|
2008-09-06 21:57:13 +00:00
|
|
|
@end
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@implementation PortChecker
|
|
|
|
|
2022-05-10 14:54:39 +00:00
|
|
|
- (instancetype)initForPort:(NSInteger)portNumber delay:(BOOL)delay withDelegate:(NSObject<PortCheckerDelegate>*)delegate
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
_fDelegate = delegate;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fStatus = PORT_STATUS_CHECKING;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_fTimer = [NSTimer scheduledTimerWithTimeInterval:CHECK_FIRE target:self selector:@selector(startProbe:)
|
2022-03-30 21:52:23 +00:00
|
|
|
userInfo:@(portNumber)
|
|
|
|
repeats:NO];
|
2009-11-08 06:01:29 +00:00
|
|
|
if (!delay)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[_fTimer fire];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)dealloc
|
2007-12-18 20:02:49 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[_fTimer invalidate];
|
2007-12-18 20:02:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (port_status_t)status
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.fStatus;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)cancelProbe
|
2007-12-18 20:02:49 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fTimer invalidate];
|
|
|
|
self.fTimer = nil;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fConnection cancel];
|
2007-12-18 20:02:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fPortProbeData.length = 0;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fPortProbeData appendData:data];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
NSLog(@"Unable to get port status: connection failed (%@)", error.localizedDescription);
|
2021-08-15 09:41:48 +00:00
|
|
|
[self callBackWithStatus:PORT_STATUS_ERROR];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)connectionDidFinishLoading:(NSURLConnection*)connection
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSString* probeString = [[NSString alloc] initWithData:self.fPortProbeData encoding:NSUTF8StringEncoding];
|
|
|
|
self.fPortProbeData = nil;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-07-19 14:32:11 +00:00
|
|
|
if (probeString)
|
2007-12-31 02:17:27 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([probeString isEqualToString:@"1"])
|
|
|
|
{
|
|
|
|
[self callBackWithStatus:PORT_STATUS_OPEN];
|
|
|
|
}
|
|
|
|
else if ([probeString isEqualToString:@"0"])
|
|
|
|
{
|
|
|
|
[self callBackWithStatus:PORT_STATUS_CLOSED];
|
|
|
|
}
|
2007-12-31 05:48:44 +00:00
|
|
|
else
|
|
|
|
{
|
2009-09-12 02:48:57 +00:00
|
|
|
NSLog(@"Unable to get port status: invalid response (%@)", probeString);
|
2021-08-15 09:41:48 +00:00
|
|
|
[self callBackWithStatus:PORT_STATUS_ERROR];
|
2007-12-31 05:48:44 +00:00
|
|
|
}
|
2007-12-31 02:17:27 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
else
|
2007-12-31 02:17:27 +00:00
|
|
|
{
|
2008-07-19 14:32:11 +00:00
|
|
|
NSLog(@"Unable to get port status: invalid data received");
|
2021-08-15 09:41:48 +00:00
|
|
|
[self callBackWithStatus:PORT_STATUS_ERROR];
|
2007-12-31 02:17:27 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)startProbe:(NSTimer*)timer
|
2008-09-06 21:57:13 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fTimer = nil;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSURLRequest* portProbeRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:CHECKER_URL([[timer userInfo] integerValue])]
|
|
|
|
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
|
|
|
|
timeoutInterval:15.0];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if ((self.fConnection = [[NSURLConnection alloc] initWithRequest:portProbeRequest delegate:self]))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fPortProbeData = [[NSMutableData alloc] init];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2008-09-06 21:57:13 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: failed to initiate connection");
|
2021-08-15 09:41:48 +00:00
|
|
|
[self callBackWithStatus:PORT_STATUS_ERROR];
|
2008-09-06 21:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)callBackWithStatus:(port_status_t)status
|
2008-09-07 16:08:29 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fStatus = status;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.fDelegate && [self.fDelegate respondsToSelector:@selector(portCheckerDidFinishProbing:)])
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fDelegate performSelectorOnMainThread:@selector(portCheckerDidFinishProbing:) withObject:self waitUntilDone:NO];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2008-09-07 16:08:29 +00:00
|
|
|
}
|
|
|
|
|
2008-09-06 21:57:13 +00:00
|
|
|
@end
|