Commit c3b19f55 authored by Jose Blaya's avatar Jose Blaya
Browse files

Merge branch '106-perform-negative-actions-with-network-tools' into 'release/2.0.1'

Resolve "Perform negative actions with Network Tools"

See merge request ios/client-library-apple!162
parents 34756d98 5d7da2ab
Pod::Spec.new do |s|
s.name = "PIALibrary"
s.version = "2.0.0"
s.version = "2.0.1"
s.summary = "PIA client library in Swift."
s.homepage = "https://www.privateinternetaccess.com/"
......
......@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.0.0</string>
<string>2.0.1</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
......
......@@ -72,4 +72,9 @@ protocol PlainStore: class {
var cachedNetworks: [String] { get set }
var trustedNetworks: [String] { get set }
var connectOnUntrusted: Bool? { get set }
var disconnectOnTrusted: Bool? { get set }
}
......@@ -35,6 +35,10 @@ private protocol PreferencesStore: class {
var availableNetworks: [String] { get set }
var trustedNetworks: [String] { get set }
var connectOnUntrusted: Bool { get set }
var disconnectOnTrusted: Bool { get set }
func vpnCustomConfiguration(for vpnType: String) -> VPNCustomConfiguration?
......@@ -59,6 +63,8 @@ private extension PreferencesStore {
vpnCustomConfigurations = source.vpnCustomConfigurations
availableNetworks = source.availableNetworks
trustedNetworks = source.trustedNetworks
connectOnUntrusted = source.connectOnUntrusted
disconnectOnTrusted = source.disconnectOnTrusted
}
}
......@@ -248,6 +254,26 @@ extension Client {
}
}
/// Connect the VPN when joining an untrusted network. True by default
public fileprivate(set) var connectOnUntrusted: Bool {
get {
return accessedDatabase.plain.connectOnUntrusted ?? true
}
set {
accessedDatabase.plain.connectOnUntrusted = newValue
}
}
/// Disconnect the VPN when joining a trusted network. False by default
public fileprivate(set) var disconnectOnTrusted: Bool {
get {
return accessedDatabase.plain.disconnectOnTrusted ?? false
}
set {
accessedDatabase.plain.disconnectOnTrusted = newValue
}
}
}
}
......@@ -257,6 +283,7 @@ extension Client.Preferences {
/// Provides a means to edit `Client.Preferences` in a buffered way. Changes can be committed or reverted.
public class Editable: PreferencesStore {
fileprivate var target: Client.Preferences?
fileprivate init() {
......@@ -272,6 +299,8 @@ extension Client.Preferences {
vpnCustomConfigurations = [:]
availableNetworks = []
trustedNetworks = []
connectOnUntrusted = true
disconnectOnTrusted = false
}
/**
......@@ -332,6 +361,12 @@ extension Client.Preferences {
/// :nodoc:
public var trustedNetworks: [String]
/// :nodoc:
public var connectOnUntrusted: Bool
/// :nodoc:
public var disconnectOnTrusted: Bool
/// :nodoc:
public func vpnCustomConfiguration(for vpnType: String) -> VPNCustomConfiguration? {
......@@ -361,6 +396,15 @@ extension Client.Preferences {
if (trustCellularData != target.trustCellularData) {
queue.append(VPNActionDisconnectAndReinstall())
}
if (trustedNetworks != target.trustedNetworks) {
queue.append(VPNActionDisconnectAndReinstall())
}
if (connectOnUntrusted != target.connectOnUntrusted) {
queue.append(VPNActionDisconnectAndReinstall())
}
if (disconnectOnTrusted != target.disconnectOnTrusted) {
queue.append(VPNActionDisconnectAndReinstall())
}
if (vpnDisconnectsOnSleep != target.vpnDisconnectsOnSleep) {
queue.append(VPNActionReinstall())
}
......
......@@ -50,7 +50,7 @@ class UserDefaultsStore: PlainStore, ConfigurationAccess {
static let trustCellularData = "TrustCellularData"
static let authMigrationSuccess = "TokenMigrationSuccess"
static let authMigrationSuccess = "AuthenticationTokenMigrationSuccess"
static let shouldConnectForAllNetworks = "ShouldConnectForAllNetworks"
......@@ -58,6 +58,10 @@ class UserDefaultsStore: PlainStore, ConfigurationAccess {
static let trustedNetworks = "TrustedNetworks"
static let connectOnUntrusted = "ConnectOnUntrusted"
static let disconnectOnTrusted = "DisconnectOnTrusted"
}
private let backend: UserDefaults
......@@ -396,6 +400,30 @@ class UserDefaultsStore: PlainStore, ConfigurationAccess {
}
}
var connectOnUntrusted: Bool? {
get {
guard let value = backend.object(forKey: Entries.connectOnUntrusted) as? Bool else {
return nil
}
return value
}
set {
backend.set(newValue, forKey: Entries.connectOnUntrusted)
}
}
var disconnectOnTrusted: Bool? {
get {
guard let value = backend.object(forKey: Entries.disconnectOnTrusted) as? Bool else {
return nil
}
return value
}
set {
backend.set(newValue, forKey: Entries.disconnectOnTrusted)
}
}
// MARK: Lifecycle
func reset() {
......@@ -408,6 +436,8 @@ class UserDefaultsStore: PlainStore, ConfigurationAccess {
backend.removeObject(forKey: Entries.historicalServers)
backend.removeObject(forKey: Entries.cachedNetworks)
backend.removeObject(forKey: Entries.trustedNetworks)
backend.removeObject(forKey: Entries.connectOnUntrusted)
backend.removeObject(forKey: Entries.disconnectOnTrusted)
backend.removeObject(forKey: Entries.shouldConnectForAllNetworks)
backend.removeObject(forKey: Entries.useWiFiProtection)
backend.removeObject(forKey: Entries.trustCellularData)
......
......@@ -56,14 +56,23 @@ extension NetworkExtensionProfile {
}
vpn.localizedDescription = configuration.name
if force {
vpn.isOnDemandEnabled = configuration.isOnDemand
} else {
vpn.isOnDemandEnabled = vpn.isOnDemandEnabled && configuration.isOnDemand
}
vpn.isOnDemandEnabled = configuration.isOnDemand
let trustedNetworks = Client.preferences.trustedNetworks
let ruleDisconnect = NEOnDemandRuleDisconnect()
ruleDisconnect.ssidMatch = trustedNetworks
vpn.onDemandRules = []
if vpn.isOnDemandEnabled {
vpn.onDemandRules = [NEOnDemandRuleConnect()]
let wiFiRule = NEOnDemandRuleConnect()
wiFiRule.interfaceTypeMatch = .wiFi
vpn.onDemandRules = [wiFiRule]
if Client.preferences.useWiFiProtection,
Client.preferences.disconnectOnTrusted,
trustedNetworks.count > 0 {
vpn.onDemandRules?.append(ruleDisconnect)
}
}
//Configure onDemand rules
......@@ -71,7 +80,34 @@ extension NetworkExtensionProfile {
vpn.isOnDemandEnabled = true
let cellularRule = NEOnDemandRuleConnect()
cellularRule.interfaceTypeMatch = .cellular
vpn.onDemandRules = [cellularRule]
vpn.onDemandRules?.append(cellularRule)
if Client.preferences.useWiFiProtection,
Client.preferences.disconnectOnTrusted,
trustedNetworks.count > 0 {
vpn.onDemandRules?.append(ruleDisconnect)
}
} else {
//trust cellular data = true
if Client.preferences.disconnectOnTrusted {
let cellularRule = NEOnDemandRuleDisconnect()
cellularRule.interfaceTypeMatch = .cellular
vpn.onDemandRules?.append(cellularRule)
} else {
let cellularRule = NEOnDemandRuleConnect()
cellularRule.interfaceTypeMatch = .cellular
vpn.onDemandRules?.append(cellularRule)
}
}
if !Client.preferences.connectOnUntrusted {
let wiFiRule = NEOnDemandRuleDisconnect()
wiFiRule.interfaceTypeMatch = .wiFi
vpn.onDemandRules = [wiFiRule]
if !Client.preferences.trustCellularData {
let cellularRule = NEOnDemandRuleDisconnect()
cellularRule.interfaceTypeMatch = .cellular
vpn.onDemandRules?.append(cellularRule)
}
}
log.debug("Configured with server: \(protocolConfiguration.serverAddress!)")
......
Supports Markdown
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