From 170d089a44af0a93c73bd529a017d496ccbe9b7e Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Fri, 8 Jan 2021 18:41:48 +0100 Subject: [PATCH 1/2] Add ConnectionProfile.serviceDelegate Requires explicit CodingKey to skip field serialization. Waiting for @transient or the like. --- .../Sources/Model/ConnectionProfile.swift | 2 ++ .../Profiles/HostConnectionProfile.swift | 24 +++++++++++++++++ .../PlaceholderConnectionProfile.swift | 2 ++ .../Profiles/ProviderConnectionProfile.swift | 26 +++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/Passepartout/Core/Sources/Model/ConnectionProfile.swift b/Passepartout/Core/Sources/Model/ConnectionProfile.swift index 2d4edefc..6c0e1fea 100644 --- a/Passepartout/Core/Sources/Model/ConnectionProfile.swift +++ b/Passepartout/Core/Sources/Model/ConnectionProfile.swift @@ -48,6 +48,8 @@ public protocol ConnectionProfile: class, EndpointDataSource, CustomStringConver var manualNetworkSettings: ProfileNetworkSettings? { get set } + var serviceDelegate: ConnectionServiceDelegate? { get set } + func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration } diff --git a/Passepartout/Core/Sources/Model/Profiles/HostConnectionProfile.swift b/Passepartout/Core/Sources/Model/Profiles/HostConnectionProfile.swift index afd9fbf7..9a411f28 100644 --- a/Passepartout/Core/Sources/Model/Profiles/HostConnectionProfile.swift +++ b/Passepartout/Core/Sources/Model/Profiles/HostConnectionProfile.swift @@ -27,6 +27,28 @@ import Foundation import TunnelKit public class HostConnectionProfile: ConnectionProfile, Codable, Equatable { + + // XXX: drop after @transient serviceDelegate + public enum CodingKeys: CodingKey { + case hostname + + case parameters + + case customAddress + + case customProtocol + + case id + + case username + + case trustedNetworks + + case networkChoices + + case manualNetworkSettings + } + public let hostname: String public var parameters: OpenVPNTunnelProvider.Configuration @@ -64,6 +86,8 @@ public class HostConnectionProfile: ConnectionProfile, Codable, Equatable { public var manualNetworkSettings: ProfileNetworkSettings? + public weak var serviceDelegate: ConnectionServiceDelegate? + public func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration { guard let endpointProtocols = parameters.sessionConfiguration.endpointProtocols, !endpointProtocols.isEmpty else { preconditionFailure("No endpointProtocols") diff --git a/Passepartout/Core/Sources/Model/Profiles/PlaceholderConnectionProfile.swift b/Passepartout/Core/Sources/Model/Profiles/PlaceholderConnectionProfile.swift index 601f9d45..bc12ac0d 100644 --- a/Passepartout/Core/Sources/Model/Profiles/PlaceholderConnectionProfile.swift +++ b/Passepartout/Core/Sources/Model/Profiles/PlaceholderConnectionProfile.swift @@ -48,6 +48,8 @@ public class PlaceholderConnectionProfile: ConnectionProfile { public var manualNetworkSettings: ProfileNetworkSettings? + public weak var serviceDelegate: ConnectionServiceDelegate? + public func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration { fatalError("Generating configuration from a PlaceholderConnectionProfile") } diff --git a/Passepartout/Core/Sources/Model/Profiles/ProviderConnectionProfile.swift b/Passepartout/Core/Sources/Model/Profiles/ProviderConnectionProfile.swift index b6fc327b..618e6324 100644 --- a/Passepartout/Core/Sources/Model/Profiles/ProviderConnectionProfile.swift +++ b/Passepartout/Core/Sources/Model/Profiles/ProviderConnectionProfile.swift @@ -27,6 +27,30 @@ import Foundation import TunnelKit public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable { + + // XXX: drop after @transient serviceDelegate + public enum CodingKeys: CodingKey { + case name + + case poolId + + case presetId + + case customAddress + + case customProtocol + + case favoriteGroupIds + + case username + + case trustedNetworks + + case networkChoices + + case manualNetworkSettings + } + public let name: Infrastructure.Name public var infrastructure: Infrastructure { @@ -122,6 +146,8 @@ public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable { public var manualNetworkSettings: ProfileNetworkSettings? + public weak var serviceDelegate: ConnectionServiceDelegate? + public func generate(from configuration: OpenVPNTunnelProvider.Configuration, preferences: Preferences) throws -> OpenVPNTunnelProvider.Configuration { guard let pool = pool else { preconditionFailure("Nil pool?") From 8313cc94971d0046207a748d79d7c3e529c166bc Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Fri, 8 Jan 2021 18:46:57 +0100 Subject: [PATCH 2/2] Delegate profile updates internally --- Passepartout/App/macOS/Menu/StatusMenu.swift | 4 ++-- .../App/macOS/Scenes/Service/ServiceViewController.swift | 2 +- .../Model/Profiles/ProviderConnectionProfile.swift | 9 ++------- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/Passepartout/App/macOS/Menu/StatusMenu.swift b/Passepartout/App/macOS/Menu/StatusMenu.swift index 71229293..bfbdda05 100644 --- a/Passepartout/App/macOS/Menu/StatusMenu.swift +++ b/Passepartout/App/macOS/Menu/StatusMenu.swift @@ -430,7 +430,7 @@ class StatusMenu: NSObject { return } assert(!group.pools.isEmpty) - service.setPoolId(group.pools.randomElement()!.id, forProviderProfile: profile) + profile.poolId = group.pools.randomElement()!.id vpn.reconnect(completionHandler: nil) // update menu @@ -447,7 +447,7 @@ class StatusMenu: NSObject { guard let profile = service.activeProfile as? ProviderConnectionProfile else { return } - service.setPoolId(pool.id, forProviderProfile: profile) + profile.poolId = pool.id vpn.reconnect(completionHandler: nil) // update menu diff --git a/Passepartout/App/macOS/Scenes/Service/ServiceViewController.swift b/Passepartout/App/macOS/Scenes/Service/ServiceViewController.swift index 4e544b28..07417ec3 100644 --- a/Passepartout/App/macOS/Scenes/Service/ServiceViewController.swift +++ b/Passepartout/App/macOS/Scenes/Service/ServiceViewController.swift @@ -338,7 +338,7 @@ extension ServiceViewController: ProviderServiceViewDelegate { uncheckedProviderProfile.presetId = fallback } - service.setPoolId(pool.id, forProviderProfile: uncheckedProviderProfile) + uncheckedProviderProfile.poolId = pool.id // vpn.reinstallIfEnabled() } diff --git a/Passepartout/Core/Sources/Model/Profiles/ProviderConnectionProfile.swift b/Passepartout/Core/Sources/Model/Profiles/ProviderConnectionProfile.swift index 618e6324..8b1ec1b9 100644 --- a/Passepartout/Core/Sources/Model/Profiles/ProviderConnectionProfile.swift +++ b/Passepartout/Core/Sources/Model/Profiles/ProviderConnectionProfile.swift @@ -63,6 +63,7 @@ public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable { public var poolId: String { didSet { validateEndpoint() + serviceDelegate?.connectionService(didUpdate: self) } } @@ -73,6 +74,7 @@ public class ProviderConnectionProfile: ConnectionProfile, Codable, Equatable { public var presetId: String { didSet { validateEndpoint() + serviceDelegate?.connectionService(didUpdate: self) } } @@ -240,10 +242,3 @@ public extension ProviderConnectionProfile { return true } } - -public extension ConnectionService { - func setPoolId(_ poolId: String, forProviderProfile profile: ProviderConnectionProfile) { - profile.poolId = poolId - delegate?.connectionService(didUpdate: profile) - } -}