diff --git a/Sources/OpenVPNAdapter/OpenVPNAdapter.mm b/Sources/OpenVPNAdapter/OpenVPNAdapter.mm index 84a6e29..e49cdd8 100644 --- a/Sources/OpenVPNAdapter/OpenVPNAdapter.mm +++ b/Sources/OpenVPNAdapter/OpenVPNAdapter.mm @@ -48,7 +48,8 @@ #pragma mark - OpenVPNClient Lifecycle - (OpenVPNProperties *)applyConfiguration:(OpenVPNConfiguration *)configuration error:(NSError * __autoreleasing *)error { - ClientAPI::EvalConfig eval = self.vpnClient->eval_config(configuration.config); + ClientAPI::Config *config = new ClientAPI::Config(configuration.config); + ClientAPI::EvalConfig eval = self.vpnClient->apply_config(config); if (eval.error) { if (error) { @@ -398,7 +399,9 @@ _sessionName = nil; _packetFlowBridge = nil; _networkSettingsBuilder = nil; - +} + +- (void)resetTun { dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); void (^completionHandler)(id _Nullable) = ^(id flow) { diff --git a/Sources/OpenVPNAdapter/OpenVPNClient.h b/Sources/OpenVPNAdapter/OpenVPNClient.h index fe5f6fc..999b38f 100644 --- a/Sources/OpenVPNAdapter/OpenVPNClient.h +++ b/Sources/OpenVPNAdapter/OpenVPNClient.h @@ -51,6 +51,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)tick; - (void)resetSettings; +- (void)resetTun; @end NS_ASSUME_NONNULL_END @@ -60,6 +61,9 @@ using namespace openvpn; class OpenVPNClient : public ClientAPI::OpenVPNClient { public: OpenVPNClient(id _Nonnull delegate); + ~OpenVPNClient(); + + ClientAPI::EvalConfig apply_config(ClientAPI::Config* _Nonnull config); bool tun_builder_new() override; @@ -99,6 +103,7 @@ public: private: __weak id _Nonnull delegate; + ClientAPI::Config * _Nullable config; }; diff --git a/Sources/OpenVPNAdapter/OpenVPNClient.mm b/Sources/OpenVPNAdapter/OpenVPNClient.mm index f5c0333..8843bdc 100644 --- a/Sources/OpenVPNAdapter/OpenVPNClient.mm +++ b/Sources/OpenVPNAdapter/OpenVPNClient.mm @@ -17,10 +17,24 @@ using ::IPv4::Addr; OpenVPNClient::OpenVPNClient(id delegate): ClientAPI::OpenVPNClient() { this->delegate = delegate; + this->config = nullptr; +} + +OpenVPNClient::~OpenVPNClient() { + if (this->config != nullptr) { delete this->config; } +} + +ClientAPI::EvalConfig OpenVPNClient::apply_config(ClientAPI::Config* _Nonnull config) { + if (this->config != nullptr) { delete this->config; } + this->config = config; + + return eval_config(*config); } bool OpenVPNClient::tun_builder_new() { [this->delegate resetSettings]; + [this->delegate resetTun]; + return true; } @@ -134,11 +148,15 @@ int OpenVPNClient::tun_builder_establish() { } bool OpenVPNClient::tun_builder_persist() { - return true; + return config->tunPersist; } void OpenVPNClient::tun_builder_teardown(bool disconnect) { [this->delegate resetSettings]; + + if (disconnect || !this->tun_builder_persist()) { + [this->delegate resetTun]; + } } bool OpenVPNClient::socket_protect(int socket, std::string remote, bool ipv6) {