From 285c508329ab0db91c110543eb0e89dbfba27801 Mon Sep 17 00:00:00 2001 From: pokamest Date: Sun, 10 Sep 2023 17:40:18 -0700 Subject: [PATCH] VPN mode fixes for Android and iOS --- client/platforms/ios/ios_controller.h | 2 +- client/platforms/ios/ios_controller.mm | 24 ++++-------------------- client/settings.cpp | 9 +++++++++ client/settings.h | 2 +- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/client/platforms/ios/ios_controller.h b/client/platforms/ios/ios_controller.h index 0750f7cd..cc409f71 100644 --- a/client/platforms/ios/ios_controller.h +++ b/client/platforms/ios/ios_controller.h @@ -71,7 +71,7 @@ private: NETunnelProviderManager *m_currentTunnel{}; NSString *m_serverAddress{}; bool isOurManager(NETunnelProviderManager* manager); - void sendVpnExtensionMessage(NSDictionary* message, std::function callback); + void sendVpnExtensionMessage(NSDictionary* message, std::function callback = nullptr); #endif amnezia::Proto m_proto; diff --git a/client/platforms/ios/ios_controller.mm b/client/platforms/ios/ios_controller.mm index c8e27252..23bd75e0 100644 --- a/client/platforms/ios/ios_controller.mm +++ b/client/platforms/ios/ios_controller.mm @@ -55,7 +55,6 @@ IosController* s_instance = nullptr; IosController::IosController() : QObject() { - qDebug() << "IosController::IosController() init"; s_instance = this; m_iosControllerWrapper = [[IosControllerWrapper alloc] initWithCppController:this]; @@ -78,8 +77,6 @@ IosController* IosController::Instance() { bool IosController::initialize() { - qDebug() << "IosController::initialize"; - __block bool ok = true; [NETunnelProviderManager loadAllFromPreferencesWithCompletionHandler:^(NSArray * _Nullable managers, NSError * _Nullable error) { @try { @@ -115,8 +112,6 @@ bool IosController::initialize() bool IosController::connectVpn(amnezia::Proto proto, const QJsonObject& configuration) { - qDebug() << "IosController::connectVpn called from thread:" << QThread::currentThread(); - m_proto = proto; m_rawConfig = configuration; m_serverAddress = configuration.value(config_key::hostName).toString().toNSString(); @@ -136,7 +131,6 @@ bool IosController::connectVpn(amnezia::Proto proto, const QJsonObject& configur qDebug() << "IosController::connectVpn" << tunnelName; - // reset m_currentTunnel m_currentTunnel = nullptr; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); @@ -260,8 +254,6 @@ void IosController::vpnConfigurationDidChange(void *pNotification) bool IosController::setupOpenVPN() { - qDebug() << "IosController::setupOpenVPN"; - QJsonObject ovpn = m_rawConfig[ProtocolProps::key_proto_config_data(amnezia::Proto::OpenVpn)].toObject(); QString ovpnConfig = ovpn[config_key::config].toString(); @@ -270,7 +262,6 @@ bool IosController::setupOpenVPN() bool IosController::setupCloak() { - qDebug() << "IosController::setupCloak"; m_serverAddress = @"127.0.0.1"; QJsonObject ovpn = m_rawConfig[ProtocolProps::key_proto_config_data(amnezia::Proto::OpenVpn)].toObject(); QString ovpnConfig = ovpn[config_key::config].toString(); @@ -308,7 +299,6 @@ bool IosController::setupCloak() bool IosController::setupWireGuard() { - qDebug() << "IosController::setupWireGuard"; QJsonObject config = m_rawConfig[ProtocolProps::key_proto_config_data(amnezia::Proto::WireGuard)].toObject(); QString wgConfig = config[config_key::config].toString(); @@ -352,17 +342,13 @@ void IosController::startTunnel() [m_currentTunnel saveToPreferencesWithCompletionHandler:^(NSError *saveError) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - qDebug() << "IosController::saveToPreferencesWithCompletionHandler called from thread:" << QThread::currentThread(); if (saveError) { - qDebug() << "IosController::startOpenVPN : Connect OpenVPN Tunnel Save Error" << saveError.localizedDescription.UTF8String; emit connectionStateChanged(VpnProtocol::VpnConnectionState::Error); return; } [m_currentTunnel loadFromPreferencesWithCompletionHandler:^(NSError *loadError) { - qDebug() << "IosController::loadFromPreferencesWithCompletionHandler called from thread:" << QThread::currentThread(); - if (loadError) { qDebug() << "IosController::startOpenVPN : Connect OpenVPN Tunnel Load Error" << loadError.localizedDescription.UTF8String; emit connectionStateChanged(VpnProtocol::VpnConnectionState::Error); @@ -379,9 +365,7 @@ void IosController::startTunnel() NSString *tunnelIdValue = !m_tunnelId.isEmpty() ? m_tunnelId.toNSString() : @""; NSDictionary* message = @{actionKey: actionValue, tunnelIdKey: tunnelIdValue}; - sendVpnExtensionMessage(message, [&](NSDictionary* response){ - qDebug() << "sendVpnExtensionMessage" << response; - }); + sendVpnExtensionMessage(message); BOOL started = [m_currentTunnel.connection startVPNTunnelWithOptions:nil andReturnError:&startError]; @@ -440,7 +424,7 @@ void IosController::sendVpnExtensionMessage(NSDictionary* message, std::function void (^completionHandler)(NSData *) = ^(NSData *responseData) { if (!responseData) { - callback(nil); + if (callback) callback(nil); return; } @@ -448,13 +432,13 @@ void IosController::sendVpnExtensionMessage(NSDictionary* message, std::function NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&deserializeError]; if (response && [response isKindOfClass:[NSDictionary class]]) { - callback(response); + if (callback) callback(response); return; } else if (deserializeError) { qDebug() << "Failed to deserialize the VpnExtension response"; } - callback(nil); + if (callback) callback(nil); }; NETunnelProviderSession *session = (NETunnelProviderSession *)m_currentTunnel.connection; diff --git a/client/settings.cpp b/client/settings.cpp index afcfd4d8..53377f07 100644 --- a/client/settings.cpp +++ b/client/settings.cpp @@ -232,6 +232,15 @@ QString Settings::routeModeString(RouteMode mode) const } } +Settings::RouteMode Settings::routeMode() const +{ +// TODO implement for mobiles +#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) + return RouteMode::VpnAllSites; +#endif + return static_cast(m_settings.value("Conf/routeMode", 0).toInt()); +} + void Settings::addVpnSite(RouteMode mode, const QString &site, const QString &ip) { QVariantMap sites = vpnSites(mode); diff --git a/client/settings.h b/client/settings.h index 28a49e18..ec3484d7 100644 --- a/client/settings.h +++ b/client/settings.h @@ -79,7 +79,7 @@ public: QString routeModeString(RouteMode mode) const; - RouteMode routeMode() const { return static_cast(m_settings.value("Conf/routeMode", 0).toInt()); } + RouteMode routeMode() const; void setRouteMode(RouteMode mode) { m_settings.setValue("Conf/routeMode", mode); } QVariantMap vpnSites(RouteMode mode) const { return m_settings.value("Conf/" + routeModeString(mode)).toMap(); }