2022-04-12 13:09:14 +00:00
|
|
|
//
|
2022-06-23 21:31:01 +00:00
|
|
|
// DefaultVPNManager.swift
|
2022-04-12 13:09:14 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/9/22.
|
|
|
|
// Copyright (c) 2022 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
2022-06-15 18:53:37 +00:00
|
|
|
import TunnelKitCore
|
2022-04-12 13:09:14 +00:00
|
|
|
import TunnelKitManager
|
2022-06-23 21:31:01 +00:00
|
|
|
import PassepartoutCore
|
|
|
|
import PassepartoutProfiles
|
2022-06-15 18:53:37 +00:00
|
|
|
import PassepartoutUtils
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
public class DefaultVPNManager<ProfileManagerType: ProfileManagerWithCurrentProfile>: VPNManagerWithCurrentState, ObservableObject where ProfileManagerType.WrappedType == ObservableProfile {
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
// MARK: Initialization
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
let appGroup: String
|
|
|
|
|
2022-06-15 18:53:37 +00:00
|
|
|
private let store: KeyValueStore
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
let profileManager: ProfileManagerType
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
let providerManager: ProviderManager
|
|
|
|
|
|
|
|
private let strategy: VPNManagerStrategy
|
|
|
|
|
2022-04-13 18:12:25 +00:00
|
|
|
public var isNetworkSettingsSupported: () -> Bool
|
|
|
|
|
2022-04-13 17:12:16 +00:00
|
|
|
public var isOnDemandRulesSupported: () -> Bool
|
2022-04-12 13:09:14 +00:00
|
|
|
|
|
|
|
// MARK: State
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
public let currentState: ObservableVPNState
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-05-20 06:29:14 +00:00
|
|
|
public private(set) var lastError: Error? {
|
2022-04-12 13:09:14 +00:00
|
|
|
get {
|
|
|
|
currentState.lastError
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
currentState.lastError = newValue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
public let configurationError = PassthroughSubject<VPNConfigurationError, Never>()
|
2022-05-20 06:29:14 +00:00
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
// MARK: Internals
|
|
|
|
|
|
|
|
private var lastProfile: Profile = .placeholder
|
|
|
|
|
|
|
|
private var cancellables: Set<AnyCancellable> = []
|
|
|
|
|
|
|
|
public init(
|
2022-06-23 21:31:01 +00:00
|
|
|
appGroup: String,
|
2022-06-15 18:53:37 +00:00
|
|
|
store: KeyValueStore,
|
2022-06-23 21:31:01 +00:00
|
|
|
profileManager: ProfileManagerType,
|
2022-04-12 13:09:14 +00:00
|
|
|
providerManager: ProviderManager,
|
|
|
|
strategy: VPNManagerStrategy
|
|
|
|
) {
|
2022-06-23 21:31:01 +00:00
|
|
|
self.appGroup = appGroup
|
2022-06-15 18:53:37 +00:00
|
|
|
self.store = store
|
2022-04-12 13:09:14 +00:00
|
|
|
self.profileManager = profileManager
|
|
|
|
self.providerManager = providerManager
|
|
|
|
self.strategy = strategy
|
2022-04-13 18:12:25 +00:00
|
|
|
isNetworkSettingsSupported = { true }
|
2022-04-13 17:12:16 +00:00
|
|
|
isOnDemandRulesSupported = { true }
|
2022-04-12 13:09:14 +00:00
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
currentState = ObservableVPNState()
|
2022-06-15 18:53:37 +00:00
|
|
|
|
|
|
|
CoreConfiguration.masksPrivateData = masksPrivateData
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func reinstate(_ configuration: VPNConfiguration) async {
|
|
|
|
pp_log.info("Reinstating VPN")
|
2022-05-01 13:35:40 +00:00
|
|
|
clearLastError()
|
2022-04-12 13:09:14 +00:00
|
|
|
await strategy.reinstate(configuration: configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reconnect(_ configuration: VPNConfiguration) async {
|
|
|
|
pp_log.info("Reconnecting VPN")
|
2022-05-19 09:08:36 +00:00
|
|
|
clearLastError()
|
2022-04-12 13:09:14 +00:00
|
|
|
await strategy.connect(configuration: configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func disable() async {
|
|
|
|
pp_log.info("Disabling VPN")
|
2022-05-01 13:35:40 +00:00
|
|
|
clearLastError()
|
2022-04-12 13:09:14 +00:00
|
|
|
await strategy.disconnect()
|
|
|
|
}
|
|
|
|
|
|
|
|
public func uninstall() async {
|
|
|
|
pp_log.info("Uninstalling VPN")
|
2022-05-01 13:35:40 +00:00
|
|
|
clearLastError()
|
2022-04-12 13:09:14 +00:00
|
|
|
await strategy.removeConfigurations()
|
|
|
|
}
|
|
|
|
|
|
|
|
public func serverConfiguration(forProtocol vpnProtocol: VPNProtocolType) -> Any? {
|
|
|
|
return strategy.serverConfiguration(forProtocol: vpnProtocol)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func debugLogURL(forProtocol vpnProtocol: VPNProtocolType) -> URL? {
|
|
|
|
return strategy.debugLogURL(forProtocol: vpnProtocol)
|
|
|
|
}
|
2022-05-01 13:35:40 +00:00
|
|
|
|
|
|
|
private func clearLastError() {
|
|
|
|
guard currentState.lastError != nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentState.lastError = nil
|
|
|
|
}
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Observation
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
extension DefaultVPNManager {
|
2022-04-18 13:32:28 +00:00
|
|
|
public func observeUpdates() {
|
|
|
|
observeStrategy()
|
|
|
|
observeProfileManager()
|
|
|
|
}
|
|
|
|
|
2022-04-12 13:09:14 +00:00
|
|
|
private func observeStrategy() {
|
|
|
|
strategy.observe(into: currentState)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func observeProfileManager() {
|
2022-07-14 15:05:35 +00:00
|
|
|
profileManager.didUpdateActiveProfile
|
2022-05-04 08:17:54 +00:00
|
|
|
.dropFirst()
|
2022-04-12 13:09:14 +00:00
|
|
|
.removeDuplicates()
|
|
|
|
.sink { newId in
|
|
|
|
Task {
|
|
|
|
await self.willUpdateActiveId(newId)
|
|
|
|
}
|
|
|
|
}.store(in: &cancellables)
|
2022-05-03 08:25:10 +00:00
|
|
|
|
|
|
|
profileManager.currentProfile.$value
|
|
|
|
.dropFirst()
|
2022-04-12 13:09:14 +00:00
|
|
|
.removeDuplicates()
|
|
|
|
.sink { newProfile in
|
|
|
|
Task {
|
|
|
|
await self.willUpdateCurrentProfile(newProfile)
|
|
|
|
}
|
|
|
|
}.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func willUpdateActiveId(_ newId: UUID?) async {
|
2022-05-04 08:17:54 +00:00
|
|
|
guard let newId = newId else {
|
2022-04-12 13:09:14 +00:00
|
|
|
pp_log.info("No active profile, disconnecting VPN...")
|
|
|
|
await disable()
|
|
|
|
return
|
|
|
|
}
|
2022-05-04 08:17:54 +00:00
|
|
|
pp_log.debug("Active profile: \(newId)")
|
2022-04-12 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func willUpdateCurrentProfile(_ newProfile: Profile) async {
|
|
|
|
defer {
|
|
|
|
lastProfile = newProfile
|
|
|
|
}
|
|
|
|
// ignore if VPN disabled
|
|
|
|
guard currentState.isEnabled else {
|
|
|
|
pp_log.debug("Ignoring updates, VPN is disabled")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// ignore non-active profiles
|
|
|
|
guard profileManager.isActiveProfile(newProfile.id) else {
|
|
|
|
pp_log.debug("Ignoring updates, profile \(newProfile.logDescription) is not active")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// ignore profile changes, react on changes within same profile
|
|
|
|
guard newProfile.id == lastProfile.id else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_log.debug("Active profile updated: \(newProfile.header.name)")
|
|
|
|
|
|
|
|
var isHandled = false
|
|
|
|
var shouldReconnect = false
|
|
|
|
let notDisconnected = (currentState.vpnStatus != .disconnected)
|
|
|
|
|
|
|
|
// do not reconnect if connected
|
|
|
|
if newProfile.isProvider {
|
|
|
|
|
|
|
|
// server changed?
|
|
|
|
if newProfile.providerServerId() != lastProfile.providerServerId() {
|
|
|
|
pp_log.info("Provider server changed: \(newProfile.providerServerId()?.description ?? "nil")")
|
|
|
|
isHandled = true
|
|
|
|
shouldReconnect = notDisconnected
|
|
|
|
}
|
|
|
|
|
|
|
|
// endpoint changed?
|
|
|
|
else if newProfile.providerCustomEndpoint() != lastProfile.providerCustomEndpoint() {
|
|
|
|
pp_log.info("Provider endpoint changed: \(newProfile.providerCustomEndpoint()?.description ?? "automatic")")
|
|
|
|
isHandled = true
|
|
|
|
shouldReconnect = notDisconnected
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// endpoint changed?
|
|
|
|
if newProfile.hostCustomEndpoint != lastProfile.hostCustomEndpoint {
|
|
|
|
pp_log.info("Host endpoint changed: \(newProfile.hostCustomEndpoint?.description ?? "automatic")")
|
|
|
|
isHandled = true
|
|
|
|
shouldReconnect = notDisconnected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isHandled {
|
|
|
|
if newProfile.onDemand != lastProfile.onDemand {
|
|
|
|
pp_log.info("On-demand settings changed")
|
|
|
|
isHandled = true
|
|
|
|
shouldReconnect = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
guard isHandled else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let cfg = vpnConfigurationWithCurrentProfile() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if shouldReconnect {
|
|
|
|
await reconnect(cfg)
|
|
|
|
} else {
|
|
|
|
await reinstate(cfg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-15 18:53:37 +00:00
|
|
|
|
|
|
|
// MARK: KeyValueStore
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
extension DefaultVPNManager {
|
2022-06-17 06:27:56 +00:00
|
|
|
public var tunnelLogPath: String? {
|
|
|
|
get {
|
|
|
|
store.value(forLocation: StoreKey.tunnelLogPath)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
store.setValue(newValue, forLocation: StoreKey.tunnelLogPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 18:53:37 +00:00
|
|
|
public var tunnelLogFormat: String? {
|
|
|
|
get {
|
|
|
|
store.value(forLocation: StoreKey.tunnelLogFormat)
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
store.setValue(newValue, forLocation: StoreKey.tunnelLogFormat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public var masksPrivateData: Bool {
|
|
|
|
get {
|
|
|
|
store.value(forLocation: StoreKey.masksPrivateData) ?? true
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
store.setValue(newValue, forLocation: StoreKey.masksPrivateData)
|
|
|
|
|
|
|
|
CoreConfiguration.masksPrivateData = masksPrivateData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 21:31:01 +00:00
|
|
|
private extension DefaultVPNManager {
|
2022-06-15 18:53:37 +00:00
|
|
|
private enum StoreKey: String, KeyStoreDomainLocation {
|
2022-06-17 06:27:56 +00:00
|
|
|
case tunnelLogPath
|
|
|
|
|
2022-06-15 18:53:37 +00:00
|
|
|
case tunnelLogFormat
|
|
|
|
|
|
|
|
case masksPrivateData
|
|
|
|
|
|
|
|
var domain: String {
|
2022-06-17 07:32:56 +00:00
|
|
|
"Passepartout.VPNManager"
|
2022-06-15 18:53:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|