Commit 7d18089a authored by Jose Blaya's avatar Jose Blaya
Browse files

Use HotspotHelper API to connect the VPN when selecting an insecure network.

New setting option to connect or not when selecting an insecure network.
parent 97281977
......@@ -33,44 +33,7 @@ class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
Bootstrapper.shared.bootstrap()
application.shortcutItems = []
//NEHotspotHelper tests
let options: [String: NSObject] = [kNEHotspotHelperOptionDisplayName : "Protect your connection with PIA" as NSObject]
let queue: DispatchQueue = DispatchQueue(label: "com.myapp.appname", attributes: DispatchQueue.Attributes.concurrent)
NEHotspotHelper.supportedNetworkInterfaces()
NEHotspotHelper.register(options: options,
queue: queue) { (cmd: NEHotspotHelperCommand) in
if !Client.providers.vpnProvider.isVPNConnected {
if cmd.commandType == .filterScanList {
var unsecuredList: [NEHotspotNetwork] = []
for element in cmd.networkList! {
if !element.isSecure {
unsecuredList.append(element)
}
}
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.setNetworkList(unsecuredList)
response.deliver() //Respond back with the filtered list
} else if cmd.commandType == .evaluate {
if let network = cmd.network {
//Connect the VPN
if !Client.providers.vpnProvider.isVPNConnected {
// this time delay seems to fix a strange issue of the VPN connecting from a fresh launch
Macros.dispatch(after: .milliseconds(200)) {
Client.providers.vpnProvider.connect(nil)
}
}
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.setNetwork(network)
response.deliver() //Respond back
}
}
}
}
configureHotspotHelper()
return true
}
......@@ -223,4 +186,54 @@ class AppDelegate: NSObject, UIApplicationDelegate {
mainVC.selectRegion(animated: true)
}
}
//MARK: - NEHotspotHelper
private func configureHotspotHelper() {
let options: [String: NSObject] = [kNEHotspotHelperOptionDisplayName : L10n.Hotspothelper.Display.name as NSObject]
let queue: DispatchQueue = DispatchQueue(label: "com.privateinternetaccess.hotspot", attributes: DispatchQueue.Attributes.concurrent)
NEHotspotHelper.supportedNetworkInterfaces()
NEHotspotHelper.register(options: options,
queue: queue) { (cmd: NEHotspotHelperCommand) in
if !Client.providers.vpnProvider.isVPNConnected {
if cmd.commandType == .filterScanList {
var unsecuredList: [NEHotspotNetwork] = []
for element in cmd.networkList! {
if !element.isSecure {
element.setConfidence(.high)
unsecuredList.append(element)
}
}
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.setNetworkList(unsecuredList)
response.deliver()
} else if cmd.commandType == .evaluate {
if let network = cmd.network {
if Client.preferences.shouldConnectWithUnsecureNetworks && !network.isSecure {
network.setConfidence(.high)
//Connect the VPN
if !Client.providers.vpnProvider.isVPNConnected {
// this time delay seems to fix a strange issue of the VPN connecting from a fresh launch
Macros.dispatch(after: .milliseconds(200)) {
Client.providers.vpnProvider.connect(nil)
}
}
let response = cmd.createResponse(.success)
response.setNetwork(network)
response.deliver()
} else {
let response = cmd.createResponse(.failure)
response.setNetwork(network)
response.deliver()
}
}
}
}
}
}
}
......@@ -52,6 +52,10 @@
<string>Roboto-Regular.ttf</string>
<string>Roboto-Thin.ttf</string>
</array>
<key>UIBackgroundModes</key>
<array>
<string>network-authentication</string>
</array>
<key>UILaunchStoryboardName</key>
<string>Launch Screen</string>
<key>UIMainStoryboardFile</key>
......
......@@ -18,5 +18,7 @@
<array>
<string>$(AppIdentifierPrefix)group.com.privateinternetaccess</string>
</array>
<key>com.apple.developer.networking.HotspotHelper</key>
<true/>
</dict>
</plist>
......@@ -40,6 +40,8 @@ enum Setting: Int {
case automaticReconnection
case shouldConnectWithUnsecureNetworks
case contentBlockerState
case contentBlockerRefreshRules
......@@ -148,8 +150,10 @@ class SettingsViewController: AutolayoutViewController {
@IBOutlet private weak var tableView: UITableView!
private lazy var switchAutoJoinWiFi = UISwitch()
private lazy var switchPersistent = UISwitch()
private lazy var switchMACE = UISwitch()
private lazy var switchContentBlocker = FakeSwitch()
......@@ -212,6 +216,7 @@ class SettingsViewController: AutolayoutViewController {
tableView.estimatedSectionFooterHeight = 1.0
}
switchPersistent.addTarget(self, action: #selector(togglePersistentConnection(_:)), for: .valueChanged)
switchAutoJoinWiFi.addTarget(self, action: #selector(toggleAutoconnectWithUnsecureNetworks(_:)), for: .valueChanged)
switchMACE.addTarget(self, action: #selector(toggleMACE(_:)), for: .valueChanged)
// switchContentBlocker.isGrayed = true
switchContentBlocker.addTarget(self, action: #selector(showContentBlockerTutorial), for: .touchUpInside)
......@@ -258,6 +263,12 @@ class SettingsViewController: AutolayoutViewController {
reportUpdatedPreferences()
}
@objc private func toggleAutoconnectWithUnsecureNetworks(_ sender: UISwitch) {
pendingPreferences.shouldConnectWithUnsecureNetworks = sender.isOn
redisplaySettings()
reportUpdatedPreferences()
}
@objc private func toggleMACE(_ sender: UISwitch) {
pendingPreferences.mace = sender.isOn
redisplaySettings()
......@@ -512,12 +523,14 @@ class SettingsViewController: AutolayoutViewController {
rowsBySection[.applicationSettings] = [
.darkTheme,
.automaticReconnection,
.shouldConnectWithUnsecureNetworks,
.mace
]
} else {
rowsBySection[.applicationSettings] = [
.darkTheme,
.automaticReconnection
.automaticReconnection,
.shouldConnectWithUnsecureNetworks
]
}
if !Flags.shared.enablesContentBlockerSetting {
......@@ -741,6 +754,13 @@ extension SettingsViewController: UITableViewDataSource, UITableViewDelegate {
cell.selectionStyle = .none
switchPersistent.isOn = pendingPreferences.isPersistentConnection
case .shouldConnectWithUnsecureNetworks:
cell.textLabel?.text = L10n.Settings.Hotspothelper.description
cell.detailTextLabel?.text = nil
cell.accessoryView = switchAutoJoinWiFi
cell.selectionStyle = .none
switchAutoJoinWiFi.isOn = pendingPreferences.shouldConnectWithUnsecureNetworks
case .mace:
cell.textLabel?.text = L10n.Settings.ApplicationSettings.Mace.title
cell.detailTextLabel?.text = nil
......
......@@ -192,6 +192,13 @@ internal enum L10n {
internal static let update = L10n.tr("Localizable", "global.update")
}
internal enum Hotspothelper {
internal enum Display {
/// 🛡 Protect your connection with PIA
internal static let name = L10n.tr("Localizable", "hotspothelper.display.name")
}
}
internal enum Menu {
internal enum Accessibility {
/// Menu
......@@ -432,6 +439,10 @@ internal enum L10n {
internal static let title = L10n.tr("Localizable", "settings.encryption.handshake.title")
}
}
internal enum Hotspothelper {
/// Auto connect WiFi
internal static let description = L10n.tr("Localizable", "settings.hotspothelper.description")
}
internal enum Reset {
/// This will reset all of the above settings to default.
internal static let footer = L10n.tr("Localizable", "settings.reset.footer")
......
......@@ -196,3 +196,8 @@
"settings.dns.validation.primary.mandatory" = "Primary DNS is mandatory.";
"settings.dns.validation.primary.invalid" = "Primary DNS is not valid.";
"settings.dns.validation.secondary.invalid" = "Secondary DNS is not valid.";
// HOTSPOT HELPER
"settings.hotspothelper.description" = "Auto connect WiFi";
"hotspothelper.display.name" = "🛡 Protect your connection with PIA";
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment