2018-10-27 09:51:50 +00:00
|
|
|
//
|
2019-05-23 21:13:45 +00:00
|
|
|
// OpenVPN.ConfigurationParserResult+Alerts.swift
|
2018-10-27 09:51:50 +00:00
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/27/18.
|
2019-03-09 10:44:44 +00:00
|
|
|
// Copyright (c) 2019 Davide De Rosa. All rights reserved.
|
2018-10-27 09:51:50 +00:00
|
|
|
//
|
2018-11-03 21:33:30 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-10-27 09:51:50 +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 UIKit
|
|
|
|
import TunnelKit
|
|
|
|
import SwiftyBeaver
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2018-10-27 09:51:50 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
2019-05-23 21:13:45 +00:00
|
|
|
extension OpenVPN.ConfigurationParser.Result {
|
2019-07-08 06:44:37 +00:00
|
|
|
static func from(_ url: URL, withErrorAlertIn viewController: UIViewController, passphrase: String?, removeOnError: Bool,
|
2019-05-23 21:13:45 +00:00
|
|
|
passphraseBlock: @escaping (String) -> Void, passphraseCancelBlock: (() -> Void)?) -> OpenVPN.ConfigurationParser.Result? {
|
2019-03-25 18:44:53 +00:00
|
|
|
|
2019-05-23 21:13:45 +00:00
|
|
|
let result: OpenVPN.ConfigurationParser.Result
|
2019-03-25 19:33:07 +00:00
|
|
|
let fm = FileManager.default
|
|
|
|
|
2018-10-27 09:51:50 +00:00
|
|
|
log.debug("Parsing configuration URL: \(url)")
|
|
|
|
do {
|
2019-05-23 21:13:45 +00:00
|
|
|
result = try OpenVPN.ConfigurationParser.parsed(fromURL: url, passphrase: passphrase)
|
2019-04-04 09:21:51 +00:00
|
|
|
} catch let e as ConfigurationError {
|
2019-03-25 19:33:07 +00:00
|
|
|
switch e {
|
|
|
|
case .encryptionPassphrase, .unableToDecrypt(_):
|
2019-06-21 21:15:43 +00:00
|
|
|
let alert = Macros.alert(url.normalizedFilename, L10n.Core.ParsedFile.Alerts.EncryptionPassphrase.message)
|
2019-03-26 10:23:49 +00:00
|
|
|
alert.addTextField { (field) in
|
|
|
|
field.isSecureTextEntry = true
|
|
|
|
}
|
2019-06-21 21:15:43 +00:00
|
|
|
alert.addDefaultAction(L10n.Core.Global.ok) {
|
2019-03-25 19:33:07 +00:00
|
|
|
guard let passphrase = alert.textFields?.first?.text else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
passphraseBlock(passphrase)
|
|
|
|
}
|
2019-06-21 21:15:43 +00:00
|
|
|
alert.addCancelAction(L10n.Core.Global.cancel) {
|
2019-03-25 18:44:53 +00:00
|
|
|
passphraseCancelBlock?()
|
2019-03-25 19:33:07 +00:00
|
|
|
}
|
|
|
|
viewController.present(alert, animated: true, completion: nil)
|
|
|
|
|
|
|
|
default:
|
|
|
|
let message = localizedMessage(forError: e)
|
|
|
|
alertImportError(url: url, in: viewController, withMessage: message)
|
2019-07-08 06:44:37 +00:00
|
|
|
if removeOnError {
|
|
|
|
try? fm.removeItem(at: url)
|
|
|
|
}
|
2019-03-25 19:33:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
2018-10-27 09:51:50 +00:00
|
|
|
} catch let e {
|
2018-10-27 12:14:46 +00:00
|
|
|
let message = localizedMessage(forError: e)
|
|
|
|
alertImportError(url: url, in: viewController, withMessage: message)
|
2019-07-08 06:44:37 +00:00
|
|
|
if removeOnError {
|
|
|
|
try? fm.removeItem(at: url)
|
|
|
|
}
|
2018-10-27 12:14:46 +00:00
|
|
|
return nil
|
2018-10-27 09:51:50 +00:00
|
|
|
}
|
2018-11-10 09:29:51 +00:00
|
|
|
return result
|
2018-10-27 09:51:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 12:14:46 +00:00
|
|
|
private static func alertImportError(url: URL, in vc: UIViewController, withMessage message: String) {
|
2018-10-27 09:51:50 +00:00
|
|
|
let alert = Macros.alert(url.normalizedFilename, message)
|
2019-06-21 21:15:43 +00:00
|
|
|
// alert.addDefaultAction(L10n.Core.ParsedFile.Alerts.Buttons.report) {
|
2018-10-27 09:51:50 +00:00
|
|
|
// var attach = IssueReporter.Attachments(debugLog: false, configurationURL: url)
|
|
|
|
// attach.description = message
|
|
|
|
// IssueReporter.shared.present(in: vc, withAttachments: attach)
|
|
|
|
// }
|
2019-06-21 21:15:43 +00:00
|
|
|
alert.addCancelAction(L10n.Core.Global.ok)
|
2018-10-27 09:51:50 +00:00
|
|
|
vc.present(alert, animated: true, completion: nil)
|
|
|
|
}
|
2018-10-27 12:14:46 +00:00
|
|
|
|
2019-04-04 09:21:51 +00:00
|
|
|
static func alertImportWarning(url: URL, in vc: UIViewController, withWarning warning: ConfigurationError, completionHandler: @escaping (Bool) -> Void) {
|
2018-10-27 12:14:46 +00:00
|
|
|
let message = details(forWarning: warning)
|
2019-06-21 21:15:43 +00:00
|
|
|
let alert = Macros.alert(url.normalizedFilename, L10n.Core.ParsedFile.Alerts.PotentiallyUnsupported.message(message))
|
|
|
|
alert.addDefaultAction(L10n.Core.Global.ok) {
|
2018-10-27 12:14:46 +00:00
|
|
|
completionHandler(true)
|
|
|
|
}
|
2019-06-21 21:15:43 +00:00
|
|
|
alert.addCancelAction(L10n.Core.Global.cancel) {
|
2018-10-27 12:14:46 +00:00
|
|
|
completionHandler(false)
|
|
|
|
}
|
|
|
|
vc.present(alert, animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
private static func localizedMessage(forError error: Error) -> String {
|
2019-04-04 09:21:51 +00:00
|
|
|
if let appError = error as? ConfigurationError {
|
2018-10-27 12:14:46 +00:00
|
|
|
switch appError {
|
2019-04-04 09:21:51 +00:00
|
|
|
case .malformed(let option):
|
|
|
|
log.error("Could not parse configuration URL: malformed option, \(option)")
|
2019-06-21 21:15:43 +00:00
|
|
|
return L10n.Core.ParsedFile.Alerts.Malformed.message(option)
|
2019-04-04 09:21:51 +00:00
|
|
|
|
2018-10-27 12:14:46 +00:00
|
|
|
case .missingConfiguration(let option):
|
|
|
|
log.error("Could not parse configuration URL: missing configuration, \(option)")
|
2019-06-21 21:15:43 +00:00
|
|
|
return L10n.Core.ParsedFile.Alerts.Missing.message(option)
|
2018-10-27 12:14:46 +00:00
|
|
|
|
|
|
|
case .unsupportedConfiguration(let option):
|
|
|
|
log.error("Could not parse configuration URL: unsupported configuration, \(option)")
|
2019-06-21 21:15:43 +00:00
|
|
|
return L10n.Core.ParsedFile.Alerts.Unsupported.message(option)
|
2019-03-25 19:33:07 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
2018-10-27 12:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
log.error("Could not parse configuration URL: \(error)")
|
2019-06-21 21:15:43 +00:00
|
|
|
return L10n.Core.ParsedFile.Alerts.Parsing.message(error.localizedDescription)
|
2018-10-27 12:14:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 09:21:51 +00:00
|
|
|
private static func details(forWarning warning: ConfigurationError) -> String {
|
2018-10-27 12:14:46 +00:00
|
|
|
switch warning {
|
2019-04-04 09:21:51 +00:00
|
|
|
case .malformed(let option):
|
|
|
|
return option
|
|
|
|
|
2018-10-27 12:14:46 +00:00
|
|
|
case .missingConfiguration(let option):
|
|
|
|
return option
|
|
|
|
|
|
|
|
case .unsupportedConfiguration(let option):
|
|
|
|
return option
|
2019-03-25 19:33:07 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return "" // XXX: should never get here
|
2018-10-27 12:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-27 09:51:50 +00:00
|
|
|
}
|