Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Adding protocol Changer to option menu for tunnel items
Browse files Browse the repository at this point in the history
  • Loading branch information
soner-yuksel committed Apr 26, 2023
1 parent 737993c commit a668f0b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 32 deletions.
40 changes: 37 additions & 3 deletions Sources/BraveVPN/BraveVPN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
21 changes: 11 additions & 10 deletions Sources/BraveVPN/BraveVPNPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand Down
47 changes: 36 additions & 11 deletions Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -35,6 +34,8 @@ class BraveVPNProtocolPickerViewController: BraveVPNPickerViewController {
tableView.dataSource = self

super.viewDidLoad()

print("Transport Protocol \(GRDTransportProtocol.getUserPreferredTransportProtocol())")
}

override func vpnConfigChanged(notification: NSNotification) {
Expand All @@ -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? {
Expand All @@ -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
}

Expand All @@ -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.")
}
}

}
}
12 changes: 4 additions & 8 deletions Sources/BraveVPN/BraveVPNRegionPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit a668f0b

Please sign in to comment.