2022-03-04 23:58:07 +00:00
|
|
|
//
|
|
|
|
// OpenVPN+ProviderConfiguration.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/6/22.
|
2023-03-17 15:58:36 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-03-04 23:58:07 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit 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.
|
|
|
|
//
|
|
|
|
// TunnelKit 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 TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import TunnelKitManager
|
|
|
|
import TunnelKitCore
|
|
|
|
import TunnelKitOpenVPNCore
|
|
|
|
import NetworkExtension
|
|
|
|
import SwiftyBeaver
|
|
|
|
import __TunnelKitUtils
|
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
extension OpenVPN {
|
|
|
|
|
|
|
|
/// Specific configuration for OpenVPN.
|
|
|
|
public struct ProviderConfiguration: Codable {
|
|
|
|
fileprivate enum Keys: String {
|
2022-06-16 18:52:29 +00:00
|
|
|
case logPath = "OpenVPN.LogPath"
|
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
case dataCount = "OpenVPN.DataCount"
|
|
|
|
|
|
|
|
case serverConfiguration = "OpenVPN.ServerConfiguration"
|
|
|
|
|
|
|
|
case lastError = "OpenVPN.LastError"
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/// Optional version identifier about the client pushed to server in peer-info as `IV_UI_VER`.
|
|
|
|
public var versionIdentifier: String?
|
|
|
|
|
|
|
|
/// The configuration title.
|
|
|
|
public let title: String
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/// The access group for shared data.
|
|
|
|
public let appGroup: String
|
|
|
|
|
|
|
|
/// The client configuration.
|
|
|
|
public let configuration: OpenVPN.Configuration
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/// The optional username.
|
|
|
|
public var username: String?
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/// Enables debugging.
|
|
|
|
public var shouldDebug = false
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-06-16 18:52:29 +00:00
|
|
|
/// Debug log path.
|
2023-04-20 19:52:45 +00:00
|
|
|
public var debugLogPath: String?
|
2022-06-16 18:52:29 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/// Optional debug log format (SwiftyBeaver format).
|
2023-04-20 19:52:45 +00:00
|
|
|
public var debugLogFormat: String?
|
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/// Mask private data in debug log (default is `true`).
|
|
|
|
public var masksPrivateData = true
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-10-26 22:40:46 +00:00
|
|
|
public init(_ title: String, appGroup: String, configuration: OpenVPN.Configuration) {
|
2022-03-04 23:58:07 +00:00
|
|
|
self.title = title
|
|
|
|
self.appGroup = appGroup
|
|
|
|
self.configuration = configuration
|
|
|
|
}
|
|
|
|
|
2022-09-30 19:43:33 +00:00
|
|
|
public func print() {
|
2022-03-04 23:58:07 +00:00
|
|
|
if let versionIdentifier = versionIdentifier {
|
|
|
|
log.info("Tunnel version: \(versionIdentifier)")
|
|
|
|
}
|
|
|
|
log.info("Debug: \(shouldDebug)")
|
|
|
|
log.info("Masks private data: \(masksPrivateData)")
|
2022-11-02 19:50:22 +00:00
|
|
|
log.info("Local options:")
|
2022-10-26 22:40:46 +00:00
|
|
|
configuration.print(isLocal: true)
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: NetworkExtensionConfiguration
|
|
|
|
|
|
|
|
extension OpenVPN.ProviderConfiguration: NetworkExtensionConfiguration {
|
|
|
|
|
2022-10-30 08:07:17 +00:00
|
|
|
public func asTunnelProtocol(
|
2022-03-04 23:58:07 +00:00
|
|
|
withBundleIdentifier tunnelBundleIdentifier: String,
|
|
|
|
extra: NetworkExtensionExtra?
|
|
|
|
) throws -> NETunnelProviderProtocol {
|
|
|
|
guard let firstRemote = configuration.remotes?.first else {
|
|
|
|
preconditionFailure("No remotes set")
|
|
|
|
}
|
|
|
|
|
|
|
|
let protocolConfiguration = NETunnelProviderProtocol()
|
|
|
|
protocolConfiguration.providerBundleIdentifier = tunnelBundleIdentifier
|
|
|
|
protocolConfiguration.serverAddress = "\(firstRemote.address):\(firstRemote.proto.port)"
|
|
|
|
if let username = username {
|
|
|
|
protocolConfiguration.username = username
|
|
|
|
protocolConfiguration.passwordReference = extra?.passwordReference
|
|
|
|
}
|
|
|
|
protocolConfiguration.disconnectOnSleep = extra?.disconnectsOnSleep ?? false
|
|
|
|
protocolConfiguration.providerConfiguration = try asDictionary()
|
2023-04-20 19:44:32 +00:00
|
|
|
protocolConfiguration.includeAllNetworks = extra?.killSwitch ?? false
|
2022-03-04 23:58:07 +00:00
|
|
|
return protocolConfiguration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Shared data
|
|
|
|
|
|
|
|
extension OpenVPN.ProviderConfiguration {
|
|
|
|
|
|
|
|
/**
|
|
|
|
The most recent (received, sent) count in bytes.
|
|
|
|
*/
|
|
|
|
public var dataCount: DataCount? {
|
2022-06-17 05:00:40 +00:00
|
|
|
return defaults?.openVPNDataCount
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2022-06-17 05:00:40 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/**
|
|
|
|
The server configuration pulled by the VPN.
|
|
|
|
*/
|
|
|
|
public var serverConfiguration: OpenVPN.Configuration? {
|
2022-06-17 05:00:40 +00:00
|
|
|
return defaults?.openVPNServerConfiguration
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
The last error reported by the tunnel, if any.
|
|
|
|
*/
|
2023-07-02 09:56:40 +00:00
|
|
|
public var lastError: TunnelKitOpenVPNError? {
|
2022-06-17 05:00:40 +00:00
|
|
|
return defaults?.openVPNLastError
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
/**
|
|
|
|
The URL of the latest debug log.
|
|
|
|
*/
|
|
|
|
public var urlForDebugLog: URL? {
|
2022-06-16 18:52:29 +00:00
|
|
|
return defaults?.openVPNURLForDebugLog(appGroup: appGroup)
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var defaults: UserDefaults? {
|
|
|
|
return UserDefaults(suiteName: appGroup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 05:00:40 +00:00
|
|
|
extension OpenVPN.ProviderConfiguration {
|
|
|
|
public func _appexSetDataCount(_ newValue: DataCount?) {
|
|
|
|
defaults?.openVPNDataCount = newValue
|
|
|
|
}
|
|
|
|
|
|
|
|
public func _appexSetServerConfiguration(_ newValue: OpenVPN.Configuration?) {
|
|
|
|
defaults?.openVPNServerConfiguration = newValue
|
|
|
|
}
|
|
|
|
|
2023-07-02 09:56:40 +00:00
|
|
|
public func _appexSetLastError(_ newValue: TunnelKitOpenVPNError?) {
|
2022-06-17 05:00:40 +00:00
|
|
|
defaults?.openVPNLastError = newValue
|
|
|
|
}
|
2022-06-16 18:52:29 +00:00
|
|
|
|
|
|
|
public var _appexDebugLogURL: URL? {
|
|
|
|
guard let path = debugLogPath else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)?
|
|
|
|
.appendingPathComponent(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func _appexSetDebugLogPath() {
|
|
|
|
defaults?.setValue(debugLogPath, forKey: OpenVPN.ProviderConfiguration.Keys.logPath.rawValue)
|
|
|
|
}
|
2022-06-17 05:00:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
extension UserDefaults {
|
2022-06-16 18:52:29 +00:00
|
|
|
public func openVPNURLForDebugLog(appGroup: String) -> URL? {
|
|
|
|
guard let path = string(forKey: OpenVPN.ProviderConfiguration.Keys.logPath.rawValue) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)?
|
|
|
|
.appendingPathComponent(path)
|
|
|
|
}
|
|
|
|
|
2022-06-17 05:00:40 +00:00
|
|
|
public fileprivate(set) var openVPNDataCount: DataCount? {
|
2022-03-04 23:58:07 +00:00
|
|
|
get {
|
|
|
|
guard let rawValue = openVPNDataCountArray else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard rawValue.count == 2 else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return DataCount(rawValue[0], rawValue[1])
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
guard let newValue = newValue else {
|
|
|
|
openVPNRemoveDataCountArray()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
openVPNDataCountArray = [newValue.received, newValue.sent]
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
@objc private var openVPNDataCountArray: [UInt]? {
|
|
|
|
get {
|
|
|
|
return array(forKey: OpenVPN.ProviderConfiguration.Keys.dataCount.rawValue) as? [UInt]
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
set(newValue, forKey: OpenVPN.ProviderConfiguration.Keys.dataCount.rawValue)
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
private func openVPNRemoveDataCountArray() {
|
|
|
|
removeObject(forKey: OpenVPN.ProviderConfiguration.Keys.dataCount.rawValue)
|
|
|
|
}
|
|
|
|
|
2022-06-17 05:00:40 +00:00
|
|
|
public fileprivate(set) var openVPNServerConfiguration: OpenVPN.Configuration? {
|
2022-03-04 23:58:07 +00:00
|
|
|
get {
|
|
|
|
guard let raw = data(forKey: OpenVPN.ProviderConfiguration.Keys.serverConfiguration.rawValue) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
do {
|
|
|
|
let cfg = try decoder.decode(OpenVPN.Configuration.self, from: raw)
|
|
|
|
return cfg
|
|
|
|
} catch {
|
|
|
|
log.error("Unable to decode server configuration: \(error)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
guard let newValue = newValue else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
do {
|
|
|
|
let raw = try encoder.encode(newValue)
|
|
|
|
set(raw, forKey: OpenVPN.ProviderConfiguration.Keys.serverConfiguration.rawValue)
|
|
|
|
} catch {
|
|
|
|
log.error("Unable to encode server configuration: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2023-07-02 09:56:40 +00:00
|
|
|
public fileprivate(set) var openVPNLastError: TunnelKitOpenVPNError? {
|
2022-03-04 23:58:07 +00:00
|
|
|
get {
|
|
|
|
guard let rawValue = string(forKey: OpenVPN.ProviderConfiguration.Keys.lastError.rawValue) else {
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-02 09:56:40 +00:00
|
|
|
return TunnelKitOpenVPNError(rawValue: rawValue)
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
guard let newValue = newValue else {
|
|
|
|
removeObject(forKey: OpenVPN.ProviderConfiguration.Keys.lastError.rawValue)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
set(newValue.rawValue, forKey: OpenVPN.ProviderConfiguration.Keys.lastError.rawValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|