mirror of
https://github.com/deneraraujo/OpenVPNAdapter.git
synced 2026-06-01 00:00:02 +08:00
Change argument type of the completion handler and add packetFlow parameter to initializer
This commit is contained in:
@@ -39,7 +39,7 @@ typedef NS_ENUM(NSInteger, OpenVPNAdapterEvent);
|
|||||||
*/
|
*/
|
||||||
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter
|
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter
|
||||||
configureTunnelWithNetworkSettings:(nullable NEPacketTunnelNetworkSettings *)networkSettings
|
configureTunnelWithNetworkSettings:(nullable NEPacketTunnelNetworkSettings *)networkSettings
|
||||||
completionHandler:(void (^)(id<OpenVPNAdapterPacketFlow> _Nullable packetFlow))completionHandler
|
completionHandler:(void (^)(NSError *error))completionHandler
|
||||||
NS_SWIFT_NAME(openVPNAdapter(_:configureTunnelWithNetworkSettings:completionHandler:));
|
NS_SWIFT_NAME(openVPNAdapter(_:configureTunnelWithNetworkSettings:completionHandler:));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -126,6 +126,11 @@ NS_SWIFT_NAME(openVPNAdapter(_:handleEvent:message:));
|
|||||||
*/
|
*/
|
||||||
@property (nonatomic, readonly) OpenVPNTransportStats *transportStatistics;
|
@property (nonatomic, readonly) OpenVPNTransportStats *transportStatistics;
|
||||||
|
|
||||||
|
/**
|
||||||
|
|
||||||
|
*/
|
||||||
|
- (instancetype)initWithPacketFlow:(id<OpenVPNAdapterPacketFlow>)packetFlow;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Applies the given configuration object.
|
Applies the given configuration object.
|
||||||
Call this method prior to connecting, this method has no effect after calling connect.
|
Call this method prior to connecting, this method has no effect after calling connect.
|
||||||
|
|||||||
@@ -40,9 +40,10 @@
|
|||||||
|
|
||||||
@implementation OpenVPNAdapter
|
@implementation OpenVPNAdapter
|
||||||
|
|
||||||
- (instancetype)init {
|
- (instancetype)initWithPacketFlow:(id<OpenVPNAdapterPacketFlow>)packetFlow {
|
||||||
if (self = [super init]) {
|
if (self = [super init]) {
|
||||||
_vpnClient = new OpenVPNClient(self);
|
_vpnClient = new OpenVPNClient(self);
|
||||||
|
_packetFlowBridge = [[OpenVPNPacketFlowBridge alloc] initWithPacketFlow:packetFlow];
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
@@ -56,7 +57,7 @@
|
|||||||
if (error) {
|
if (error) {
|
||||||
NSString *message = [NSString stringWithUTF8String:eval.message.c_str()];
|
NSString *message = [NSString stringWithUTF8String:eval.message.c_str()];
|
||||||
*error = [NSError ovpn_errorObjectForAdapterError:OpenVPNAdapterErrorConfigurationFailure
|
*error = [NSError ovpn_errorObjectForAdapterError:OpenVPNAdapterErrorConfigurationFailure
|
||||||
description:@"Failed to apply OpenVPN configuration"
|
description:@"Failed to apply OpenVPN configuration."
|
||||||
message:message
|
message:message
|
||||||
fatal:YES];
|
fatal:YES];
|
||||||
}
|
}
|
||||||
@@ -74,7 +75,7 @@
|
|||||||
if (error) {
|
if (error) {
|
||||||
NSString *message = [NSString stringWithUTF8String:status.message.c_str()];
|
NSString *message = [NSString stringWithUTF8String:status.message.c_str()];
|
||||||
*error = [NSError ovpn_errorObjectForAdapterError:OpenVPNAdapterErrorCredentialsFailure
|
*error = [NSError ovpn_errorObjectForAdapterError:OpenVPNAdapterErrorCredentialsFailure
|
||||||
description:@"Failed to provide OpenVPN credentials"
|
description:@"Failed to provide OpenVPN credentials."
|
||||||
message:message
|
message:message
|
||||||
fatal:YES];
|
fatal:YES];
|
||||||
}
|
}
|
||||||
@@ -87,7 +88,7 @@
|
|||||||
|
|
||||||
- (void)connect {
|
- (void)connect {
|
||||||
dispatch_queue_attr_t attributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0);
|
dispatch_queue_attr_t attributes = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_UTILITY, 0);
|
||||||
dispatch_queue_t connectQueue = dispatch_queue_create("me.ss-abramchuk.openvpn-adapter.connection", attributes);
|
dispatch_queue_t connectQueue = dispatch_queue_create("me.ss-abramchuk.openvpn-adapter.connection.", attributes);
|
||||||
dispatch_async(connectQueue, ^{
|
dispatch_async(connectQueue, ^{
|
||||||
OpenVPNClient::init_process();
|
OpenVPNClient::init_process();
|
||||||
|
|
||||||
@@ -123,7 +124,7 @@
|
|||||||
|
|
||||||
NSString *message = [NSString stringWithUTF8String:status.message.c_str()];
|
NSString *message = [NSString stringWithUTF8String:status.message.c_str()];
|
||||||
NSError *error = [NSError ovpn_errorObjectForAdapterError:adapterError
|
NSError *error = [NSError ovpn_errorObjectForAdapterError:adapterError
|
||||||
description:@"Failed to establish connection with OpenVPN server"
|
description:@"Failed to establish connection with OpenVPN server."
|
||||||
message:message
|
message:message
|
||||||
fatal:YES];
|
fatal:YES];
|
||||||
|
|
||||||
@@ -315,23 +316,35 @@
|
|||||||
|
|
||||||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||||||
|
|
||||||
__weak typeof(self) weakSelf = self;
|
__block NSError *configurationError;
|
||||||
void (^completionHandler)(id<OpenVPNAdapterPacketFlow> _Nullable) = ^(id<OpenVPNAdapterPacketFlow> flow) {
|
void (^completionHandler)(NSError *error) = ^(NSError *error) {
|
||||||
__strong typeof(self) self = weakSelf;
|
configurationError = error;
|
||||||
|
|
||||||
if (flow && (self.packetFlowBridge == nil || self.packetFlowBridge != flow)) {
|
|
||||||
self.packetFlowBridge = [[OpenVPNPacketFlowBridge alloc] initWithPacketFlow:flow];
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatch_semaphore_signal(semaphore);
|
dispatch_semaphore_signal(semaphore);
|
||||||
};
|
};
|
||||||
|
|
||||||
[self.delegate openVPNAdapter:self configureTunnelWithNetworkSettings:networkSettings completionHandler:completionHandler];
|
[self.delegate openVPNAdapter:self configureTunnelWithNetworkSettings:networkSettings completionHandler:completionHandler];
|
||||||
|
|
||||||
dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TUNNEL_CONFIGURATION_TIMEOUT * NSEC_PER_SEC));
|
long timeout = dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TUNNEL_CONFIGURATION_TIMEOUT * NSEC_PER_SEC));
|
||||||
|
if (timeout) { return NO; }
|
||||||
|
|
||||||
|
if (configurationError) {
|
||||||
|
NSDictionary *userInfo = @{
|
||||||
|
NSLocalizedDescriptionKey: @"Failed to configure tunnel using provided settings. Check underlying error for more details.",
|
||||||
|
NSUnderlyingErrorKey: configurationError,
|
||||||
|
OpenVPNAdapterErrorFatalKey: @(YES)
|
||||||
|
};
|
||||||
|
|
||||||
|
NSError *error = [NSError errorWithDomain:OpenVPNAdapterErrorDomain
|
||||||
|
code:OpenVPNAdapterErrorTUNSetupFailed
|
||||||
|
userInfo:userInfo];
|
||||||
|
|
||||||
|
[self.delegate openVPNAdapter:self handleError:error];
|
||||||
|
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
NSError *socketError;
|
NSError *socketError;
|
||||||
if (self.packetFlowBridge && [self.packetFlowBridge configureSocketsWithError:&socketError]) {
|
if ([self.packetFlowBridge configureSocketsWithError:&socketError]) {
|
||||||
[self.packetFlowBridge startReading];
|
[self.packetFlowBridge startReading];
|
||||||
return YES;
|
return YES;
|
||||||
} else {
|
} else {
|
||||||
@@ -407,13 +420,29 @@
|
|||||||
|
|
||||||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||||||
|
|
||||||
void (^completionHandler)(id<OpenVPNAdapterPacketFlow> _Nullable) = ^(id<OpenVPNAdapterPacketFlow> flow) {
|
__block NSError *configurationError;
|
||||||
|
void (^completionHandler)(NSError *error) = ^(NSError *error) {
|
||||||
|
configurationError = error;
|
||||||
dispatch_semaphore_signal(semaphore);
|
dispatch_semaphore_signal(semaphore);
|
||||||
};
|
};
|
||||||
|
|
||||||
[self.delegate openVPNAdapter:self configureTunnelWithNetworkSettings:nil completionHandler:completionHandler];
|
[self.delegate openVPNAdapter:self configureTunnelWithNetworkSettings:nil completionHandler:completionHandler];
|
||||||
|
|
||||||
dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TUNNEL_CONFIGURATION_TIMEOUT * NSEC_PER_SEC));
|
dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TUNNEL_CONFIGURATION_TIMEOUT * NSEC_PER_SEC));
|
||||||
|
|
||||||
|
if (configurationError) {
|
||||||
|
NSDictionary *userInfo = @{
|
||||||
|
NSLocalizedDescriptionKey: @"Failed to reset tunnel. Check underlying error for more details.",
|
||||||
|
NSUnderlyingErrorKey: configurationError,
|
||||||
|
OpenVPNAdapterErrorFatalKey: @(YES)
|
||||||
|
};
|
||||||
|
|
||||||
|
NSError *error = [NSError errorWithDomain:OpenVPNAdapterErrorDomain
|
||||||
|
code:OpenVPNAdapterErrorTUNSetupFailed
|
||||||
|
userInfo:userInfo];
|
||||||
|
|
||||||
|
[self.delegate openVPNAdapter:self handleError:error];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
|
|||||||
Reference in New Issue
Block a user