2022-03-04 23:58:07 +00:00
|
|
|
//
|
|
|
|
// WireGuard+ProviderConfiguration.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 11/21/21.
|
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 NetworkExtension
|
2023-12-14 21:01:26 +00:00
|
|
|
import TunnelKitCore
|
2022-03-04 23:58:07 +00:00
|
|
|
import TunnelKitManager
|
|
|
|
import TunnelKitWireGuardCore
|
|
|
|
import WireGuardKit
|
|
|
|
import SwiftyBeaver
|
2022-06-16 18:52:29 +00:00
|
|
|
import __TunnelKitUtils
|
2022-03-04 23:58:07 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
extension WireGuard {
|
|
|
|
|
|
|
|
/// Specific configuration for WireGuard.
|
|
|
|
public struct ProviderConfiguration: Codable {
|
|
|
|
fileprivate enum Keys: String {
|
2022-06-16 18:52:29 +00:00
|
|
|
case logPath = "WireGuard.LogPath"
|
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
case lastError = "WireGuard.LastError"
|
2023-12-14 21:01:26 +00:00
|
|
|
|
|
|
|
case dataCount = "WireGuard.DataCount"
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
public let title: String
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
public let appGroup: String
|
|
|
|
|
|
|
|
public let configuration: WireGuard.Configuration
|
|
|
|
|
|
|
|
public var shouldDebug = false
|
|
|
|
|
2023-04-20 19:52:45 +00:00
|
|
|
public var debugLogPath: String?
|
2022-06-16 18:52:29 +00:00
|
|
|
|
2023-04-20 19:52:45 +00:00
|
|
|
public var debugLogFormat: String?
|
2022-03-24 07:59:48 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
public init(_ title: String, appGroup: String, configuration: WireGuard.Configuration) {
|
|
|
|
self.title = title
|
|
|
|
self.appGroup = appGroup
|
|
|
|
self.configuration = configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
private init(_ title: String, appGroup: String, wgQuickConfig: String) throws {
|
|
|
|
self.title = title
|
|
|
|
self.appGroup = appGroup
|
|
|
|
configuration = try WireGuard.Configuration(wgQuickConfig: wgQuickConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
// MARK: NetworkExtensionConfiguration
|
|
|
|
|
|
|
|
extension WireGuard.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 {
|
|
|
|
let protocolConfiguration = NETunnelProviderProtocol()
|
|
|
|
protocolConfiguration.providerBundleIdentifier = tunnelBundleIdentifier
|
|
|
|
protocolConfiguration.serverAddress = configuration.endpointRepresentation
|
|
|
|
protocolConfiguration.passwordReference = extra?.passwordReference
|
|
|
|
protocolConfiguration.disconnectOnSleep = extra?.disconnectsOnSleep ?? false
|
|
|
|
protocolConfiguration.providerConfiguration = try asDictionary()
|
2023-12-16 19:50:10 +00:00
|
|
|
#if !os(tvOS)
|
2023-04-20 19:44:32 +00:00
|
|
|
protocolConfiguration.includeAllNetworks = extra?.killSwitch ?? false
|
2023-12-16 19:50:10 +00:00
|
|
|
#endif
|
2022-03-04 23:58:07 +00:00
|
|
|
return protocolConfiguration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Shared data
|
|
|
|
|
|
|
|
extension WireGuard.ProviderConfiguration {
|
2023-12-14 21:01:26 +00:00
|
|
|
|
|
|
|
/// The most recent (received, sent) count in bytes.
|
|
|
|
public var dataCount: DataCount? {
|
|
|
|
return defaults?.wireGuardDataCount
|
|
|
|
}
|
|
|
|
|
2023-07-02 09:56:40 +00:00
|
|
|
public var lastError: TunnelKitWireGuardError? {
|
2022-06-17 05:00:40 +00:00
|
|
|
return defaults?.wireGuardLastError
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var urlForDebugLog: URL? {
|
2022-06-16 18:52:29 +00:00
|
|
|
return defaults?.wireGuardURLForDebugLog(appGroup: appGroup)
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 18:52:29 +00:00
|
|
|
private var defaults: UserDefaults? {
|
|
|
|
return UserDefaults(suiteName: appGroup)
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
2023-12-14 21:01:26 +00:00
|
|
|
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 05:00:40 +00:00
|
|
|
extension WireGuard.ProviderConfiguration {
|
2023-12-14 21:01:26 +00:00
|
|
|
public func _appexSetDataCount(_ newValue: DataCount?) {
|
|
|
|
defaults?.wireGuardDataCount = newValue
|
|
|
|
}
|
|
|
|
|
2023-07-02 09:56:40 +00:00
|
|
|
public func _appexSetLastError(_ newValue: TunnelKitWireGuardError?) {
|
2022-06-17 05:00:40 +00:00
|
|
|
defaults?.wireGuardLastError = 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: WireGuard.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 wireGuardURLForDebugLog(appGroup: String) -> URL? {
|
|
|
|
guard let path = string(forKey: WireGuard.ProviderConfiguration.Keys.logPath.rawValue) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup)?
|
|
|
|
.appendingPathComponent(path)
|
|
|
|
}
|
|
|
|
|
2023-07-02 09:56:40 +00:00
|
|
|
public fileprivate(set) var wireGuardLastError: TunnelKitWireGuardError? {
|
2022-03-04 23:58:07 +00:00
|
|
|
get {
|
|
|
|
guard let rawValue = string(forKey: WireGuard.ProviderConfiguration.Keys.lastError.rawValue) else {
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-02 09:56:40 +00:00
|
|
|
return TunnelKitWireGuardError(rawValue: rawValue)
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
guard let newValue = newValue else {
|
|
|
|
removeObject(forKey: WireGuard.ProviderConfiguration.Keys.lastError.rawValue)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
set(newValue.rawValue, forKey: WireGuard.ProviderConfiguration.Keys.lastError.rawValue)
|
|
|
|
}
|
|
|
|
}
|
2023-12-14 21:01:26 +00:00
|
|
|
|
|
|
|
public fileprivate(set) var wireGuardDataCount: DataCount? {
|
|
|
|
get {
|
|
|
|
guard let rawValue = wireGuardDataCountArray else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
guard rawValue.count == 2 else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return DataCount(rawValue[0], rawValue[1])
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
guard let newValue = newValue else {
|
|
|
|
wireGuardRemoveDataCountArray()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wireGuardDataCountArray = [newValue.received, newValue.sent]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private var wireGuardDataCountArray: [UInt]? {
|
|
|
|
get {
|
|
|
|
return array(forKey: WireGuard.ProviderConfiguration.Keys.dataCount.rawValue) as? [UInt]
|
|
|
|
}
|
|
|
|
set {
|
|
|
|
set(newValue, forKey: WireGuard.ProviderConfiguration.Keys.dataCount.rawValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private func wireGuardRemoveDataCountArray() {
|
|
|
|
removeObject(forKey: WireGuard.ProviderConfiguration.Keys.dataCount.rawValue)
|
|
|
|
}
|
2022-03-04 23:58:07 +00:00
|
|
|
}
|