Refactor OpenVPNAdapter, update tests

This commit is contained in:
Jonathan Downing
2017-10-11 13:39:41 +01:00
parent 9ce5e4c989
commit d1456adf85
11 changed files with 746 additions and 1104 deletions
-40
View File
@@ -1,40 +0,0 @@
//
// OpenVPNAdapter+Internal.h
// OpenVPN Adapter
//
// Created by Sergey Abramchuk on 11.02.17.
//
//
#import <client/ovpncli.hpp>
#import "OpenVPNAdapter.h"
using namespace openvpn;
@interface OpenVPNAdapter (Internal)
- (BOOL)configureSockets;
- (BOOL)setRemoteAddress:(NSString *)address isIPv6:(BOOL)isIPv6;
- (BOOL)addLocalAddress:(NSString *)address prefixLength:(NSNumber *)prefixLength gateway:(NSString *)gateway isIPv6:(BOOL)isIPv6;
- (BOOL)defaultGatewayRerouteIPv4:(BOOL)rerouteIPv4 rerouteIPv6:(BOOL)rerouteIPv6;
- (BOOL)addRoute:(NSString *)route prefixLength:(NSNumber *)prefixLength isIPv6:(BOOL)isIPv6;
- (BOOL)excludeRoute:(NSString *)route prefixLength:(NSNumber *)prefixLength isIPv6:(BOOL)isIPv6;
- (BOOL)addDNSAddress:(NSString *)address isIPv6:(BOOL)isIPv6;
- (BOOL)addSearchDomain:(NSString *)domain;
- (BOOL)setMTU:(NSNumber *)mtu;
- (CFSocketNativeHandle)establishTunnel;
- (void)teardownTunnel:(BOOL)disconnect;
- (void)handleEvent:(const ClientAPI::Event *)event;
- (void)handleLog:(const ClientAPI::LogInfo *)log;
- (void)tick;
@end
-191
View File
@@ -1,191 +0,0 @@
//
// OpenVPNAdapter+Public.h
// OpenVPN Adapter
//
// Created by Sergey Abramchuk on 11.02.17.
//
//
#import "OpenVPNAdapterEvent.h"
#import "OpenVPNAdapter.h"
@class OpenVPNConfiguration;
@class OpenVPNProperties;
@class OpenVPNCredentials;
@class OpenVPNConnectionInfo;
@class OpenVPNSessionToken;
@class OpenVPNTransportStats;
@class OpenVPNInterfaceStats;
@class NEPacketTunnelNetworkSettings;
// TODO: Add documentation to properties and methods
/**
<#Description#>
*/
@protocol OpenVPNAdapterPacketFlow <NSObject>
/**
<#Description#>
@param completionHandler <#completionHandler description#>
*/
- (void)readPacketsWithCompletionHandler:(nonnull void (^)(NSArray<NSData *> * _Nonnull packets, NSArray<NSNumber *> * _Nonnull protocols))completionHandler;
/**
<#Description#>
@param packets <#packets description#>
@param protocols <#protocols description#>
@return <#return value description#>
*/
- (BOOL)writePackets:(nonnull NSArray<NSData *> *)packets withProtocols:(nonnull NSArray<NSNumber *> *)protocols;
@end
/**
<#Description#>
*/
@protocol OpenVPNAdapterDelegate <NSObject>
/**
<#Description#>
@param settings <#settings description#>
@param callback <#callback description#>
*/
- (void)configureTunnelWithSettings:(nonnull NEPacketTunnelNetworkSettings *)settings
callback:(nonnull void (^)(id<OpenVPNAdapterPacketFlow> _Nullable flow))callback
NS_SWIFT_NAME(configureTunnel(settings:callback:));
/**
<#Description#>
@param event <#event description#>
@param message <#message description#>
*/
- (void)handleEvent:(OpenVPNAdapterEvent)event
message:(nullable NSString *)message
NS_SWIFT_NAME(handle(event:message:));
/**
<#Description#>
@param error <#error description#>
*/
- (void)handleError:(nonnull NSError *)error
NS_SWIFT_NAME(handle(error:));
@optional
/**
<#Description#>
@param logMessage <#logMessage description#>
*/
- (void)handleLog:(nonnull NSString *)logMessage
NS_SWIFT_NAME(handle(logMessage:));
/**
<#Description#>
*/
- (void)tick;
@end
/**
<#Description#>
*/
@interface OpenVPNAdapter (Public)
/**
Return core copyright
*/
@property (class, nonnull, readonly, nonatomic) NSString *copyright;
/**
Return platform description
*/
@property (class, nonnull, readonly, nonatomic) NSString *platform;
/**
<#Description#>
*/
@property (weak, nonatomic, null_unspecified) id<OpenVPNAdapterDelegate> delegate;
/**
Return information about the most recent connection. Will be available
after an event of type "OpenVPNAdapterEventConnected, otherwise return nil.
*/
@property (nullable, readonly, nonatomic) OpenVPNConnectionInfo *connectionInfo;
/**
Return current session token or nil if session token is unavailable
*/
@property (nullable, readonly, nonatomic) OpenVPNSessionToken *sessionToken;
/**
Return transport stats
*/
@property (nonnull, readonly, nonatomic) OpenVPNTransportStats *transportStats;
/**
Return tun stats
*/
@property (nonnull, readonly, nonatomic) OpenVPNInterfaceStats *interfaceStats;
/**
<#Description#>
@param configuration <#configuration description#>
@param error <#error description#>
@return <#return value description#>
*/
- (nullable OpenVPNProperties *)applyConfiguration:(nonnull OpenVPNConfiguration *)configuration
error:(out NSError * __nullable * __nullable)error
NS_SWIFT_NAME(apply(configuration:));
/**
<#Description#>
@param credentials <#credentials description#>
@param error <#error description#>
@return <#return value description#>
*/
- (BOOL)provideCredentials:(nonnull OpenVPNCredentials *)credentials
error:(out NSError * __nullable * __nullable)error
NS_SWIFT_NAME(provide(credentials:));
/**
Establish connection with the VPN server
*/
- (void)connect;
/**
Pause the client - useful to avoid continuous reconnection attempts
when network is down
@param pauseReason <#reason description#>
*/
- (void)pauseWithReason:(nullable NSString *)pauseReason
NS_SWIFT_NAME(pause(reason:));
/**
Resume the client after it has been paused
*/
- (void)resume;
/**
Do a disconnect/reconnect cycle after given amount of seconds from now
@param interval <#interval description#>
*/
- (void)reconnectAfterTimeInterval:(NSInteger)interval
NS_SWIFT_NAME(reconnect(interval:));
/**
Close connection with the VPN server
*/
- (void)disconnect;
@end
+156
View File
@@ -7,7 +7,163 @@
//
#import <Foundation/Foundation.h>
#import "OpenVPNAdapterEvent.h"
NS_ASSUME_NONNULL_BEGIN
@class NEPacketTunnelFlow;
@class NEPacketTunnelNetworkSettings;
@class OpenVPNAdapter;
@class OpenVPNConfiguration;
@class OpenVPNConnectionInfo;
@class OpenVPNCredentials;
@class OpenVPNInterfaceStats;
@class OpenVPNProperties;
@class OpenVPNTransportStats;
@class OpenVPNSessionToken;
@protocol OpenVPNAdapterDelegate <NSObject>
/**
This method is called once the network settings to be used have been established.
The receiver should call the completion handler once these settings have been set, returning a NEPacketTunnelFlow object for the TUN interface, or nil if an error occurred.
@param openVPNAdapter The OpenVPNAdapter instance requesting this information.
@param networkSettings The NEPacketTunnelNetworkSettings to be used for the tunnel.
@param completionHandler The completion handler to be called with a NEPacketTunnelFlow object, or nil if an error occurred.
*/
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter configureTunnelWithNetworkSettings:(NEPacketTunnelNetworkSettings *)networkSettings completionHandler:(void (^)(NEPacketTunnelFlow * _Nullable packetFlow))completionHandler NS_SWIFT_NAME(openVPNAdapter(_:configureTunnelWithNetworkSettings:completionHandler:));
/**
Informs the receiver that an OpenVPN error has occurred.
Some errors are fatal and should trigger the diconnection of the tunnel, check for fatal errors with the OpenVPNAdapterErrorFatalKey.
@param openVPNAdapter The OpenVPNAdapter instance which encountered the error.
@param error The error which has occurred.
*/
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter handleError:(NSError *)error;
/**
Informs the receiver that an OpenVPN event has occurred.
@param openVPNAdapter The OpenVPNAdapter instance which encountered the event.
@param event The event which has occurred.
@param message An accompanying message, may be nil.
*/
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter handleEvent:(OpenVPNAdapterEvent)event message:(nullable NSString *)message NS_SWIFT_NAME(openVPNAdapter(_:handleEvent:message:));
@optional
/**
Informs the receiver that an OpenVPN message has been logged.
@param openVPNAdapter The OpenVPNAdapter instance which encountered the log message.
@param logMessage The log message.
*/
- (void)openVPNAdapter:(OpenVPNAdapter *)openVPNAdapter handleLogMessage:(NSString *)logMessage;
/**
Informs the receiver that a clock tick has occurred.
Clock ticks can be configured with an OpenVPNConfiguration object.
@param openVPNAdapter The OpenVPNAdapter instance which encountered the clock tick.
*/
- (void)openVPNAdapterDidReceiveClockTick:(OpenVPNAdapter *)openVPNAdapter;
@end
@interface OpenVPNAdapter : NSObject
/**
The OpenVPN core copyright message.
*/
@property (nonatomic, class, readonly) NSString *copyright;
/**
The OpenVPN platform.
*/
@property (nonatomic, class, readonly) NSString *platform;
/**
The object that acts as the delegate of the adapter.
*/
@property (nonatomic, weak) id<OpenVPNAdapterDelegate> delegate;
/**
The session name, nil unless the tunnel is connected.
*/
@property (nonatomic, nullable, readonly) NSString *sessionName;
/**
The connection information, nil unless the tunnel is connected.
*/
@property (nonatomic, nullable, readonly) OpenVPNConnectionInfo *connectionInformation;
/**
The interface statistics.
*/
@property (nonatomic, readonly) OpenVPNInterfaceStats *interfaceStatistics;
/**
The session token, nil unless the tunnel is connected.
*/
@property (nonatomic, nullable, readonly) OpenVPNSessionToken *sessionToken;
/**
The transport statistics.
*/
@property (nonatomic, readonly) OpenVPNTransportStats *transportStatistics;
/**
Applies the given configuration object.
Call this method prior to connecting, this method has no effect after calling connect.
@param configuration The configuration object.
@param error If there is an error applying the configuration, upon return contains an error object that describes the problem.
@return A properties object describing the configuration which has been applied.
*/
- (nullable OpenVPNProperties *)applyConfiguration:(OpenVPNConfiguration *)configuration error:(NSError **)error NS_SWIFT_NAME(apply(configuration:));
/**
Provides credentials to the receiver.
@param credentials The credentials object.
@param error If there is an error providing the credentials, upon return contains an error object that describes the problem.
@return Returns YES if this method was successful, otherwise NO.
*/
- (BOOL)provideCredentials:(OpenVPNCredentials *)credentials error:(NSError **)error NS_SWIFT_NAME(provide(credentials:));
/**
Starts the tunnel.
*/
- (void)connect;
/**
Pauses the tunnel.
@param reason The reason for pausing the tunnel.
*/
- (void)pauseWithReason:(NSString *)reason NS_SWIFT_NAME(pause(withReason:));
/**
Resumes the connection.
*/
- (void)resume;
/**
Reconnects after a given time period, perhaps due to an interface change.
@param timeInterval The time interval to wait before reconnecting.
*/
- (void)reconnectAfterTimeInterval:(NSTimeInterval)timeInterval NS_SWIFT_NAME(reconnect(afterTimeInterval:));
/**
Disconnect from the tunnel.
*/
- (void)disconnect;
@end
NS_ASSUME_NONNULL_END
File diff suppressed because it is too large Load Diff
-69
View File
@@ -1,69 +0,0 @@
//
// OpenVPNClient.h
// OpenVPN Adapter
//
// Created by Sergey Abramchuk on 11.02.17.
//
//
#import <openvpn/tun/client/tunbase.hpp>
#import <client/ovpncli.hpp>
using namespace openvpn;
class OpenVPNClient : public ClientAPI::OpenVPNClient
{
public:
OpenVPNClient(void * adapter);
virtual bool tun_builder_new() override;
virtual bool tun_builder_set_remote_address(const std::string& address, bool ipv6) override;
virtual bool tun_builder_add_address(const std::string& address,
int prefix_length,
const std::string& gateway,
bool ipv6,
bool net30) override;
virtual bool tun_builder_reroute_gw(bool ipv4,
bool ipv6,
unsigned int flags) override;
virtual bool tun_builder_add_route(const std::string& address,
int prefix_length,
int metric,
bool ipv6) override;
virtual bool tun_builder_exclude_route(const std::string& address,
int prefix_length,
int metric,
bool ipv6) override;
virtual bool tun_builder_add_dns_server(const std::string& address, bool ipv6) override;
virtual bool tun_builder_add_search_domain(const std::string& domain) override;
virtual bool tun_builder_set_mtu(int mtu) override;
virtual bool tun_builder_set_session_name(const std::string& name) override;
virtual bool tun_builder_add_proxy_bypass(const std::string& bypass_host) override;
virtual bool tun_builder_set_proxy_auto_config_url(const std::string& url) override;
virtual bool tun_builder_set_proxy_http(const std::string& host, int port) override;
virtual bool tun_builder_set_proxy_https(const std::string& host, int port) override;
virtual bool tun_builder_add_wins_server(const std::string& address) override;
virtual int tun_builder_establish() override;
virtual bool tun_builder_persist() override;
virtual void tun_builder_establish_lite() override;
virtual void tun_builder_teardown(bool disconnect) override;
virtual bool socket_protect(int socket) override;
virtual bool pause_on_connection_timeout() override;
virtual void external_pki_cert_request(ClientAPI::ExternalPKICertRequest& certreq) override;
virtual void external_pki_sign_request(ClientAPI::ExternalPKISignRequest& signreq) override;
virtual void event(const ClientAPI::Event& ev) override;
virtual void log(const ClientAPI::LogInfo& log) override;
virtual void clock_tick() override;
private:
void* adapter;
};
-121
View File
@@ -1,121 +0,0 @@
//
// OpenVPNClient.m
// OpenVPN Adapter
//
// Created by Sergey Abramchuk on 11.02.17.
//
//
#import <Foundation/Foundation.h>
#import "OpenVPNAdapter+Internal.h"
#import "OpenVPNClient.h"
OpenVPNClient::OpenVPNClient(void *adapter) : ClientAPI::OpenVPNClient() {
this->adapter = adapter;
}
bool OpenVPNClient::tun_builder_new() {
return [(__bridge OpenVPNAdapter *)adapter configureSockets];
}
bool OpenVPNClient::tun_builder_set_remote_address(const std::string &address, bool ipv6) {
NSString *remoteAddress = [NSString stringWithUTF8String:address.c_str()];
return [(__bridge OpenVPNAdapter *)adapter setRemoteAddress:remoteAddress isIPv6:ipv6];
}
bool OpenVPNClient::tun_builder_add_address(const std::string &address, int prefix_length, const std::string &gateway, bool ipv6, bool net30) {
NSString *localAddress = [NSString stringWithUTF8String:address.c_str()];
NSString *gatewayAddress = [NSString stringWithUTF8String:gateway.c_str()];
return [(__bridge OpenVPNAdapter *)adapter addLocalAddress:localAddress prefixLength:@(prefix_length) gateway:gatewayAddress isIPv6:ipv6];
}
bool OpenVPNClient::tun_builder_reroute_gw(bool ipv4, bool ipv6, unsigned int flags) {
return [(__bridge OpenVPNAdapter *)adapter defaultGatewayRerouteIPv4:ipv4 rerouteIPv6:ipv6];
}
bool OpenVPNClient::tun_builder_add_route(const std::string& address, int prefix_length, int metric, bool ipv6) {
NSString *route = [NSString stringWithUTF8String:address.c_str()];
return [(__bridge OpenVPNAdapter *)adapter addRoute:route prefixLength:@(prefix_length) isIPv6:ipv6];
}
bool OpenVPNClient::tun_builder_exclude_route(const std::string& address, int prefix_length, int metric, bool ipv6) {
NSString *route = [NSString stringWithUTF8String:address.c_str()];
return [(__bridge OpenVPNAdapter *)adapter excludeRoute:route prefixLength:@(prefix_length) isIPv6:ipv6];
}
bool OpenVPNClient::tun_builder_add_dns_server(const std::string& address, bool ipv6) {
NSString *dnsAddress = [NSString stringWithUTF8String:address.c_str()];
return [(__bridge OpenVPNAdapter *)adapter addDNSAddress:dnsAddress isIPv6:ipv6];
}
bool OpenVPNClient::tun_builder_add_search_domain(const std::string& domain) {
NSString *searchDomain = [NSString stringWithUTF8String:domain.c_str()];
return [(__bridge OpenVPNAdapter *)adapter addSearchDomain:searchDomain];
}
bool OpenVPNClient::tun_builder_set_mtu(int mtu) {
return [(__bridge OpenVPNAdapter *)adapter setMTU:@(mtu)];
}
bool OpenVPNClient::tun_builder_set_session_name(const std::string& name) {
return true;
}
bool OpenVPNClient::tun_builder_add_proxy_bypass(const std::string& bypass_host) {
return true;
}
bool OpenVPNClient::tun_builder_set_proxy_auto_config_url(const std::string& url) {
return true;
}
bool OpenVPNClient::tun_builder_set_proxy_http(const std::string& host, int port) {
return true;
}
bool OpenVPNClient::tun_builder_set_proxy_https(const std::string& host, int port) {
return true;
}
bool OpenVPNClient::tun_builder_add_wins_server(const std::string& address) {
return true;
}
int OpenVPNClient::tun_builder_establish() {
return (int)[(__bridge OpenVPNAdapter *)adapter establishTunnel];
}
bool OpenVPNClient::tun_builder_persist() {
return true;
}
void OpenVPNClient::tun_builder_establish_lite() { }
void OpenVPNClient::tun_builder_teardown(bool disconnect) {
[(__bridge OpenVPNAdapter *)adapter teardownTunnel:disconnect];
}
bool OpenVPNClient::socket_protect(int socket) {
return true;
}
bool OpenVPNClient::pause_on_connection_timeout() {
return false;
}
void OpenVPNClient::external_pki_cert_request(ClientAPI::ExternalPKICertRequest& certreq) { }
void OpenVPNClient::external_pki_sign_request(ClientAPI::ExternalPKISignRequest& signreq) { }
void OpenVPNClient::event(const ClientAPI::Event& ev) {
[(__bridge OpenVPNAdapter* )adapter handleEvent:&ev];
}
void OpenVPNClient::log(const ClientAPI::LogInfo& log) {
[(__bridge OpenVPNAdapter* )adapter handleLog:&log];
}
void OpenVPNClient::clock_tick() {
[(__bridge OpenVPNAdapter* )adapter tick];
}
-23
View File
@@ -1,23 +0,0 @@
//
// OpenVPNTunnelSettings.h
// OpenVPN Adapter
//
// Created by Sergey Abramchuk on 26.02.17.
//
//
#import <Foundation/Foundation.h>
@interface OpenVPNTunnelSettings : NSObject
@property (nonatomic) BOOL initialized;
@property (readonly, strong, nonatomic) NSMutableArray *localAddresses;
@property (readonly, strong, nonatomic) NSMutableArray *prefixLengths;
@property (readonly, strong, nonatomic) NSMutableArray *includedRoutes;
@property (readonly, strong, nonatomic) NSMutableArray *excludedRoutes;
@property (readonly, strong, nonatomic) NSMutableArray *dnsAddresses;
@end
-30
View File
@@ -1,30 +0,0 @@
//
// OpenVPNTunnelSettings.m
// OpenVPN Adapter
//
// Created by Sergey Abramchuk on 26.02.17.
//
//
#import "OpenVPNTunnelSettings.h"
@implementation OpenVPNTunnelSettings
- (instancetype)init
{
self = [super init];
if (self) {
_initialized = NO;
_localAddresses = [NSMutableArray new];
_prefixLengths = [NSMutableArray new];
_includedRoutes = [NSMutableArray new];
_excludedRoutes = [NSMutableArray new];
_dnsAddresses = [NSMutableArray new];
}
return self;
}
@end
-1
View File
@@ -32,7 +32,6 @@ FOUNDATION_EXPORT const unsigned char OpenVPNAdapterVersionString[];
#import <OpenVPNAdapter/OpenVPNTransportStats.h>
#import <OpenVPNAdapter/OpenVPNInterfaceStats.h>
#import <OpenVPNAdapter/OpenVPNAdapter.h>
#import <OpenVPNAdapter/OpenVPNAdapter+Public.h>
#import <OpenVPNAdapter/OpenVPNKeyType.h>
#import <OpenVPNAdapter/OpenVPNCertificate.h>
#import <OpenVPNAdapter/OpenVPNPrivateKey.h>