From a668f0b6f3c8deba89554f94e8d60dbfb03fc36d Mon Sep 17 00:00:00 2001 From: Soner Yuksel Date: Tue, 25 Apr 2023 13:19:23 -0400 Subject: [PATCH] Adding protocol Changer to option menu for tunnel items --- Sources/BraveVPN/BraveVPN.swift | 40 ++++++++++++++-- .../BraveVPNPickerViewController.swift | 21 +++++---- ...BraveVPNProtocolPickerViewController.swift | 47 ++++++++++++++----- .../BraveVPNRegionPickerViewController.swift | 12 ++--- 4 files changed, 88 insertions(+), 32 deletions(-) diff --git a/Sources/BraveVPN/BraveVPN.swift b/Sources/BraveVPN/BraveVPN.swift index 71109f8bc65..e9f796903b1 100644 --- a/Sources/BraveVPN/BraveVPN.swift +++ b/Sources/BraveVPN/BraveVPN.swift @@ -16,6 +16,7 @@ public class BraveVPN { private static let housekeepingApi = GRDHousekeepingAPI() private static let helper = GRDVPNHelper.sharedInstance() private static let serverManager = GRDServerManager() + private static let tunnelManager = GRDTunnelManager() public static let iapObserver = IAPObserver() @@ -278,11 +279,14 @@ public class BraveVPN { completion?(status == .success) } } else { - // Setting preferred protocol in order to determine which tunnel to be activated while region selection + // Setting User preferred Transport Protocol to WireGuard + // In order to easily fetch and change in settings later GRDTransportProtocol.setUserPreferred(.wireGuard) - + // New user or no credentials and have to remake them. - helper.configureFirstTimeUser(for: .wireGuard, postCredential: nil) { success, error in + helper.configureFirstTimeUser( + for: GRDTransportProtocol.getUserPreferredTransportProtocol(), + postCredential: nil) { success, error in if let error = error { logAndStoreError("configureFirstTimeUserPostCredential \(error)") } else { @@ -308,6 +312,36 @@ public class BraveVPN { completion?(status) } } + + public static func changePreferredTransportProtocol(with transportProtocol: TransportProtocol, completion: ((Bool) -> Void)? = nil) { + helper.forceDisconnectVPNIfNecessary() + GRDVPNHelper.clearVpnConfiguration() + + GRDTransportProtocol.setUserPreferred(transportProtocol) + + // New user or no credentials and have to remake them. + helper.configureFirstTimeUser(for: transportProtocol, postCredential: nil) { success, error in + if let error = error { + logAndStoreError("Change Preferred transport FirstTimeUserPostCredential \(error)") + } else { + removeConfigurationFromPreferences(for: transportProtocol) + } + + reconnectPending = false + completion?(success) + } + + func removeConfigurationFromPreferences(for transportProtocol: TransportProtocol) { + switch transportProtocol { + case .wireGuard: + helper.ikev2VPNManager.removeFromPreferences() + case .ikEv2: + tunnelManager.removeTunnel() + default: + return + } + } + } public static func clearCredentials() { GRDKeychain.removeGuardianKeychainItems() diff --git a/Sources/BraveVPN/BraveVPNPickerViewController.swift b/Sources/BraveVPN/BraveVPNPickerViewController.swift index 6c22c13c6fe..8ab29b2828c 100644 --- a/Sources/BraveVPN/BraveVPNPickerViewController.swift +++ b/Sources/BraveVPN/BraveVPNPickerViewController.swift @@ -73,8 +73,7 @@ class BraveVPNPickerViewController: UIViewController { tableView.reloadData() } - @objc func vpnConfigChanged(notification: NSNotification) { - } + @objc func vpnConfigChanged(notification: NSNotification) { } func showSuccessAlert() { let animation = AnimationView(name: "vpncheckmark", bundle: .module).then { @@ -92,15 +91,17 @@ class BraveVPNPickerViewController: UIViewController { } func showErrorAlert(title: String, message: String?) { - let alert = AlertController(title: Strings.VPN.regionPickerErrorTitle, - message: Strings.VPN.regionPickerErrorMessage, - preferredStyle: .alert) - let okAction = UIAlertAction(title: Strings.OKString, style: .default) { _ in - self.dismiss(animated: true) + DispatchQueue.main.async { + let alert = AlertController(title: Strings.VPN.regionPickerErrorTitle, + message: Strings.VPN.regionPickerErrorMessage, + preferredStyle: .alert) + let okAction = UIAlertAction(title: Strings.OKString, style: .default) { _ in + self.dismiss(animated: true) + } + alert.addAction(okAction) + + self.present(alert, animated: true) } - alert.addAction(okAction) - - present(alert, animated: true) } } diff --git a/Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift b/Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift index 5ccdfb4813d..4946d7fb153 100644 --- a/Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift +++ b/Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift @@ -12,15 +12,14 @@ import GuardianConnect class BraveVPNProtocolPickerViewController: BraveVPNPickerViewController { - private let regionList: [GRDRegion] + private let tunnelProtocolList: [TransportProtocol] /// This group monitors vpn connection status. private var dispatchGroup: DispatchGroup? private var vpnRegionChangeSuccess = false override init() { - self.regionList = BraveVPN.regions - .sorted { $0.displayName < $1.displayName } + self.tunnelProtocolList = [.wireGuard, .ikEv2] super.init() } @@ -35,6 +34,8 @@ class BraveVPNProtocolPickerViewController: BraveVPNPickerViewController { tableView.dataSource = self super.viewDidLoad() + + print("Transport Protocol \(GRDTransportProtocol.getUserPreferredTransportProtocol())") } override func vpnConfigChanged(notification: NSNotification) { @@ -52,12 +53,8 @@ class BraveVPNProtocolPickerViewController: BraveVPNPickerViewController { extension BraveVPNProtocolPickerViewController: UITableViewDelegate, UITableViewDataSource { - func numberOfSections(in tableView: UITableView) -> Int { - 1 - } - func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - regionList.count + tunnelProtocolList.count } func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { @@ -68,10 +65,12 @@ extension BraveVPNProtocolPickerViewController: UITableViewDelegate, UITableView let cell = tableView.dequeueReusableCell(for: indexPath) as VPNRegionCell cell.accessoryType = .none - guard let server = regionList[safe: indexPath.row] else { return cell } - cell.textLabel?.text = server.displayName + guard let tunnelProtocol = tunnelProtocolList[safe: indexPath.row] else { return cell } + cell.textLabel?.text = GRDTransportProtocol.prettyTransportProtocolString(for: tunnelProtocol) + + let activeProtocolOption = GRDTransportProtocol.getUserPreferredTransportProtocol() - if server.displayName == BraveVPN.selectedRegion?.displayName { + if activeProtocolOption == tunnelProtocol { cell.accessoryType = .checkmark } @@ -81,5 +80,31 @@ extension BraveVPNProtocolPickerViewController: UITableViewDelegate, UITableView func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) + guard let tunnelProtocol = tunnelProtocolList[safe: indexPath.row] else { return } + + let activeProtocolOption = GRDTransportProtocol.getUserPreferredTransportProtocol() + + // Same option is selected do nothing + if activeProtocolOption == tunnelProtocol { + return + } + + isLoading = true + + BraveVPN.changePreferredTransportProtocol(with: tunnelProtocol) { [weak self] success in + guard let self else { return } + + isLoading = false + + if success { + self.dismiss(animated: true) { + self.showSuccessAlert() + } + } else { + self.showErrorAlert(title: "Error", + message: "Failed to change transport protocol.") + } + } + } } diff --git a/Sources/BraveVPN/BraveVPNRegionPickerViewController.swift b/Sources/BraveVPN/BraveVPNRegionPickerViewController.swift index 0060bd5c3cb..b10da979f93 100644 --- a/Sources/BraveVPN/BraveVPNRegionPickerViewController.swift +++ b/Sources/BraveVPN/BraveVPNRegionPickerViewController.swift @@ -121,10 +121,8 @@ extension BraveVPNRegionPickerViewController: UITableViewDelegate, UITableViewDa guard let self = self else { return } if !success { - DispatchQueue.main.async { - self.showErrorAlert(title: Strings.VPN.regionPickerErrorTitle, - message: Strings.VPN.regionPickerErrorMessage) - } + self.showErrorAlert(title: Strings.VPN.regionPickerErrorTitle, + message: Strings.VPN.regionPickerErrorMessage) } // Changing vpn server settings takes lot of time, @@ -147,10 +145,8 @@ extension BraveVPNRegionPickerViewController: UITableViewDelegate, UITableViewDa self.showSuccessAlert() } } else { - DispatchQueue.main.async { - self.showErrorAlert(title: Strings.VPN.regionPickerErrorTitle, - message: Strings.VPN.regionPickerErrorMessage) - } + self.showErrorAlert(title: Strings.VPN.regionPickerErrorTitle, + message: Strings.VPN.regionPickerErrorMessage) } } }