passepartout-apple/Passepartout/Sources/VPN/GracefulVPN.swift

132 lines
3.8 KiB
Swift
Raw Normal View History

2018-10-11 07:13:19 +00:00
//
// GracefulVPN.swift
// Passepartout
//
// Created by Davide De Rosa on 9/18/18.
2019-03-09 10:44:44 +00:00
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
2018-10-11 07:13:19 +00:00
//
2018-11-03 21:33:30 +00:00
// https://github.com/passepartoutvpn
2018-10-11 07:13:19 +00:00
//
// 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 SwiftyBeaver
private let log = SwiftyBeaver.self
public class GracefulVPN {
2018-10-11 07:13:19 +00:00
private let service: ConnectionService
public var profile: ConnectionProfile?
2018-10-11 07:13:19 +00:00
private var vpn: VPNProvider? {
guard let profile = profile else {
return nil
}
guard service.isActiveProfile(profile) else {
return nil
}
return VPN.shared
}
public var isEnabled: Bool {
2018-10-11 07:13:19 +00:00
return vpn?.isEnabled ?? false
}
public var status: VPNStatus? {
2018-10-11 07:13:19 +00:00
return vpn?.status
}
public init(service: ConnectionService) {
2018-10-11 07:13:19 +00:00
self.service = service
}
public func prepare(completionHandler: (() -> Void)?) {
2018-10-21 22:14:40 +00:00
service.clearVpnLastError()
guard let vpn = vpn else {
completionHandler?()
return
}
log.info("Preparing...")
vpn.prepare(completionHandler: completionHandler)
2018-10-11 07:13:19 +00:00
}
public func reconnect(completionHandler: ((Error?) -> Void)?) {
2018-10-21 22:14:40 +00:00
service.clearVpnLastError()
guard let vpn = vpn else {
completionHandler?(ApplicationError.inactiveProfile)
return
}
2018-10-11 07:13:19 +00:00
do {
log.info("Reconnecting...")
try vpn.reconnect(configuration: service.vpnConfiguration(), completionHandler: completionHandler)
2018-10-11 07:13:19 +00:00
} catch let e {
log.error("Could not reconnect: \(e)")
}
}
public func reinstall(completionHandler: ((Error?) -> Void)?) {
2018-10-21 22:14:40 +00:00
service.clearVpnLastError()
guard let vpn = vpn else {
completionHandler?(ApplicationError.inactiveProfile)
return
}
2018-10-11 07:13:19 +00:00
do {
log.info("Reinstalling...")
try vpn.install(configuration: service.vpnConfiguration(), completionHandler: completionHandler)
2018-10-11 07:13:19 +00:00
} catch let e {
log.error("Could not reinstall: \(e)")
}
}
public func reinstallIfEnabled() {
2018-10-11 07:13:19 +00:00
guard isEnabled else {
log.warning("Not reinstalling (VPN is disabled)")
return
}
if status != .disconnected {
reconnect(completionHandler: nil)
} else {
reinstall(completionHandler: nil)
}
}
public func disconnect(completionHandler: ((Error?) -> Void)?) {
guard let vpn = vpn else {
completionHandler?(ApplicationError.inactiveProfile)
return
}
vpn.disconnect(completionHandler: completionHandler)
2018-10-11 07:13:19 +00:00
}
public func uninstall(completionHandler: (() -> Void)?) {
guard let vpn = vpn else {
completionHandler?()
return
}
vpn.uninstall(completionHandler: completionHandler)
2018-10-11 07:13:19 +00:00
}
public func requestBytesCount(completionHandler: @escaping ((UInt, UInt)?) -> Void) {
guard let vpn = vpn else {
completionHandler(nil)
return
}
vpn.requestBytesCount(completionHandler: completionHandler)
2018-10-11 07:13:19 +00:00
}
}