2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2006-2023 Transmission authors and contributors.
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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"
|
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static NSTimeInterval const kCheckFireInterval = 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;
|
|
|
|
|
2022-11-12 03:29:48 +00:00
|
|
|
@property(nonatomic) NSURLSession* fSession;
|
|
|
|
@property(nonatomic) NSURLSessionDataTask* fTask;
|
2022-02-22 16:04:20 +00:00
|
|
|
|
|
|
|
@property(nonatomic) NSTimer* fTimer;
|
|
|
|
|
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-11-12 03:29:48 +00:00
|
|
|
_fSession = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.ephemeralSessionConfiguration delegate:nil
|
|
|
|
delegateQueue:nil];
|
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-10-19 19:28:21 +00:00
|
|
|
_fTimer = [NSTimer scheduledTimerWithTimeInterval:kCheckFireInterval 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-11-12 03:29:48 +00:00
|
|
|
[self cancelProbe];
|
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-11-12 03:29:48 +00:00
|
|
|
[self.fTask cancel];
|
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
|
|
|
|
2022-11-12 03:29:48 +00:00
|
|
|
NSString* urlString = [NSString stringWithFormat:@"https://portcheck.transmissionbt.com/%ld", [(NSNumber*)timer.userInfo integerValue]];
|
2022-10-19 19:28:21 +00:00
|
|
|
NSURLRequest* portProbeRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]
|
2021-08-15 09:41:48 +00:00
|
|
|
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
|
|
|
|
timeoutInterval:15.0];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-11-12 03:29:48 +00:00
|
|
|
_fTask = [_fSession dataTaskWithRequest:portProbeRequest
|
|
|
|
completionHandler:^(NSData* _Nullable data, NSURLResponse* _Nullable response, NSError* _Nullable error) {
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: connection failed (%@)", error.localizedDescription);
|
|
|
|
[self callBackWithStatus:PORT_STATUS_ERROR];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSString* probeString = [[NSString alloc] initWithData:data ?: NSData.data encoding:NSUTF8StringEncoding];
|
|
|
|
if (!probeString)
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: invalid data received");
|
|
|
|
[self callBackWithStatus:PORT_STATUS_ERROR];
|
|
|
|
}
|
|
|
|
else if ([probeString isEqualToString:@"1"])
|
|
|
|
{
|
|
|
|
[self callBackWithStatus:PORT_STATUS_OPEN];
|
|
|
|
}
|
|
|
|
else if ([probeString isEqualToString:@"0"])
|
|
|
|
{
|
|
|
|
[self callBackWithStatus:PORT_STATUS_CLOSED];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to get port status: invalid response (%@)", probeString);
|
|
|
|
[self callBackWithStatus:PORT_STATUS_ERROR];
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
[_fTask resume];
|
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-11-12 03:29:48 +00:00
|
|
|
NSObject<PortCheckerDelegate>* delegate = self.fDelegate;
|
|
|
|
[delegate performSelectorOnMainThread:@selector(portCheckerDidFinishProbing:) withObject:self waitUntilDone:NO];
|
2008-09-07 16:08:29 +00:00
|
|
|
}
|
|
|
|
|
2008-09-06 21:57:13 +00:00
|
|
|
@end
|