2018-10-11 07:13:19 +00:00
|
|
|
//
|
|
|
|
// WizardHostViewController.swift
|
|
|
|
// Passepartout-iOS
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/4/18.
|
2020-12-27 16:30:25 +00:00
|
|
|
// Copyright (c) 2021 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 UIKit
|
|
|
|
import TunnelKit
|
|
|
|
import SwiftyBeaver
|
2019-05-26 06:56:39 +00:00
|
|
|
import PassepartoutCore
|
2019-10-11 08:56:23 +00:00
|
|
|
import Convenience
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
class WizardHostViewController: UITableViewController, StrongTableHost {
|
2018-10-11 07:13:19 +00:00
|
|
|
@IBOutlet private weak var itemNext: UIBarButtonItem!
|
|
|
|
|
2020-02-28 17:32:04 +00:00
|
|
|
private let existingHostIds = TransientStore.shared.service.sortedHostIds()
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-05-23 21:13:45 +00:00
|
|
|
var parsingResult: OpenVPN.ConfigurationParser.Result? {
|
2018-10-11 07:13:19 +00:00
|
|
|
didSet {
|
|
|
|
useSuggestedTitle()
|
|
|
|
}
|
|
|
|
}
|
2018-10-27 11:20:23 +00:00
|
|
|
|
|
|
|
var removesConfigurationOnCancel = false
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
private var createdProfile: HostConnectionProfile?
|
2020-02-28 17:32:04 +00:00
|
|
|
|
|
|
|
private var createdTitle: String?
|
|
|
|
|
|
|
|
private var replacedProfile: ConnectionProfile?
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
// MARK: StrongTableHost
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2019-10-11 08:56:23 +00:00
|
|
|
lazy var model: StrongTableModel<SectionType, RowType> = {
|
|
|
|
let model: StrongTableModel<SectionType, RowType> = StrongTableModel()
|
2018-10-11 07:13:19 +00:00
|
|
|
model.add(.meta)
|
2020-02-28 17:32:04 +00:00
|
|
|
// model.setFooter(L10n.Core.Global.Host.TitleInput.message, forSection: .meta)
|
|
|
|
if !existingHostIds.isEmpty {
|
2018-10-11 07:13:19 +00:00
|
|
|
model.add(.existing)
|
2019-10-11 08:56:23 +00:00
|
|
|
model.setHeader(L10n.App.Wizards.Host.Sections.Existing.header, forSection: .existing)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2019-10-11 08:56:23 +00:00
|
|
|
model.set([.titleInput], forSection: .meta)
|
2020-02-28 17:32:04 +00:00
|
|
|
model.set(.existingHost, count: existingHostIds.count, forSection: .existing)
|
2018-10-11 07:13:19 +00:00
|
|
|
return model
|
|
|
|
}()
|
|
|
|
|
|
|
|
func reloadModel() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: UIViewController
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-06-21 21:15:43 +00:00
|
|
|
title = L10n.Core.Organizer.Sections.Hosts.header
|
|
|
|
itemNext.title = L10n.Core.Global.next
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
useSuggestedTitle()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
cellTitle?.field.becomeFirstResponder()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
private func useSuggestedTitle() {
|
2018-11-10 09:29:51 +00:00
|
|
|
cellTitle?.field.text = parsingResult?.url?.normalizedFilename
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction private func next() {
|
|
|
|
guard let enteredTitle = cellTitle?.field.text?.trimmingCharacters(in: .whitespaces), !enteredTitle.isEmpty else {
|
|
|
|
return
|
|
|
|
}
|
2018-11-10 09:29:51 +00:00
|
|
|
guard let result = parsingResult else {
|
2018-10-11 07:13:19 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-04 09:21:51 +00:00
|
|
|
guard let hostname = result.configuration.hostname else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-28 17:32:04 +00:00
|
|
|
let profile = HostConnectionProfile(hostname: hostname)
|
2019-05-23 21:13:45 +00:00
|
|
|
let builder = OpenVPNTunnelProvider.ConfigurationBuilder(sessionConfiguration: result.configuration)
|
2018-11-10 09:29:51 +00:00
|
|
|
profile.parameters = builder.build()
|
2018-10-11 07:13:19 +00:00
|
|
|
|
2018-10-27 13:56:33 +00:00
|
|
|
let service = TransientStore.shared.service
|
2020-02-28 17:32:04 +00:00
|
|
|
replacedProfile = nil
|
2020-05-11 20:28:10 +00:00
|
|
|
if let existingProfile = service.hostProfile(withTitle: enteredTitle) {
|
|
|
|
replacedProfile = existingProfile
|
2019-10-11 08:56:23 +00:00
|
|
|
let alert = UIAlertController.asAlert(title, L10n.Core.Wizards.Host.Alerts.Existing.message)
|
|
|
|
alert.addPreferredAction(L10n.Core.Global.ok) {
|
2020-02-28 17:32:04 +00:00
|
|
|
self.next(withProfile: profile, title: enteredTitle)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
2019-06-21 21:15:43 +00:00
|
|
|
alert.addCancelAction(L10n.Core.Global.cancel)
|
2018-10-11 07:13:19 +00:00
|
|
|
present(alert, animated: true, completion: nil)
|
|
|
|
return
|
|
|
|
}
|
2020-02-28 17:32:04 +00:00
|
|
|
next(withProfile: profile, title: enteredTitle)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 17:32:04 +00:00
|
|
|
private func next(withProfile profile: HostConnectionProfile, title: String) {
|
2018-10-11 07:13:19 +00:00
|
|
|
createdProfile = profile
|
2020-02-28 17:32:04 +00:00
|
|
|
createdTitle = title
|
2018-10-11 07:13:19 +00:00
|
|
|
|
|
|
|
let accountVC = StoryboardScene.Main.accountIdentifier.instantiate()
|
2018-10-27 13:56:33 +00:00
|
|
|
if let replacedProfile = replacedProfile {
|
|
|
|
accountVC.currentCredentials = TransientStore.shared.service.credentials(for: replacedProfile)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
accountVC.delegate = self
|
|
|
|
navigationController?.pushViewController(accountVC, animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func finish(withCredentials credentials: Credentials) {
|
2020-02-28 17:32:04 +00:00
|
|
|
guard let profile = createdProfile, let title = createdTitle else {
|
2018-10-11 07:13:19 +00:00
|
|
|
fatalError("No profile created?")
|
|
|
|
}
|
2018-11-02 13:20:40 +00:00
|
|
|
let service = TransientStore.shared.service
|
2018-11-10 09:29:51 +00:00
|
|
|
if let url = parsingResult?.url {
|
2018-10-22 08:28:53 +00:00
|
|
|
do {
|
2018-11-02 13:20:40 +00:00
|
|
|
let savedURL = try service.save(configurationURL: url, for: profile)
|
2018-10-26 22:08:48 +00:00
|
|
|
log.debug("Associated .ovpn configuration file to profile '\(profile.id)': \(savedURL)")
|
2018-10-27 10:40:45 +00:00
|
|
|
|
|
|
|
// can now delete imported file
|
|
|
|
try? FileManager.default.removeItem(at: url)
|
2018-10-22 08:28:53 +00:00
|
|
|
} catch let e {
|
|
|
|
log.error("Could not associate .ovpn configuration file to profile: \(e)")
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
dismiss(animated: true) {
|
2020-02-28 17:32:04 +00:00
|
|
|
if let replacedProfile = self.replacedProfile {
|
|
|
|
service.removeProfile(ProfileKey(replacedProfile))
|
|
|
|
}
|
|
|
|
service.addOrReplaceProfile(profile, credentials: credentials, title: title)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction private func close() {
|
2018-11-10 09:29:51 +00:00
|
|
|
if removesConfigurationOnCancel, let url = parsingResult?.url {
|
2018-10-27 11:20:23 +00:00
|
|
|
try? FileManager.default.removeItem(at: url)
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
dismiss(animated: true, completion: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
extension WizardHostViewController {
|
|
|
|
enum SectionType: Int {
|
|
|
|
case meta
|
|
|
|
|
|
|
|
case existing
|
|
|
|
}
|
|
|
|
|
|
|
|
enum RowType: Int {
|
|
|
|
case titleInput
|
|
|
|
|
|
|
|
case existingHost
|
|
|
|
}
|
|
|
|
|
|
|
|
private var cellTitle: FieldTableViewCell? {
|
2019-10-11 08:56:23 +00:00
|
|
|
guard let ip = model.indexPath(forRow: .titleInput, ofSection: .meta) else {
|
2018-10-11 07:13:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return tableView.cellForRow(at: ip) as? FieldTableViewCell
|
|
|
|
}
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfSections
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.header(forSection: section)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 10:19:59 +00:00
|
|
|
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.footer(forSection: section)
|
2018-11-02 10:19:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:19 +00:00
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-10-11 08:56:23 +00:00
|
|
|
return model.numberOfRows(forSection: section)
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .titleInput:
|
|
|
|
let cell = Cells.field.dequeue(from: tableView, for: indexPath)
|
2019-06-21 21:15:43 +00:00
|
|
|
cell.caption = L10n.App.Wizards.Host.Cells.TitleInput.caption
|
2018-10-11 07:13:19 +00:00
|
|
|
cell.captionWidth = 100.0
|
2020-02-28 17:32:04 +00:00
|
|
|
// cell.allowedCharset = .filename
|
2020-05-11 20:53:09 +00:00
|
|
|
cell.field.applyHostTitle(.current)
|
2018-10-11 07:13:19 +00:00
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
|
|
|
|
|
|
|
case .existingHost:
|
2020-02-28 17:32:04 +00:00
|
|
|
let existingTitle = hostTitle(at: indexPath.row)
|
2018-10-11 07:13:19 +00:00
|
|
|
let cell = Cells.setting.dequeue(from: tableView, for: indexPath)
|
2020-02-28 17:32:04 +00:00
|
|
|
cell.leftText = existingTitle
|
2018-10-11 07:13:19 +00:00
|
|
|
cell.accessoryType = .none
|
|
|
|
cell.isTappable = true
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
switch model.row(at: indexPath) {
|
|
|
|
case .existingHost:
|
2019-10-11 08:56:23 +00:00
|
|
|
guard let titleIndexPath = model.indexPath(forRow: .titleInput, ofSection: .meta) else {
|
2018-10-11 07:13:19 +00:00
|
|
|
fatalError("Could not found title cell?")
|
|
|
|
}
|
2020-02-28 17:32:04 +00:00
|
|
|
let existingTitle = hostTitle(at: indexPath.row)
|
2018-10-11 07:13:19 +00:00
|
|
|
let cellTitle = tableView.cellForRow(at: titleIndexPath) as? FieldTableViewCell
|
2020-02-28 17:32:04 +00:00
|
|
|
cellTitle?.field.text = existingTitle
|
2018-10-11 07:13:19 +00:00
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 17:32:04 +00:00
|
|
|
|
|
|
|
private func hostTitle(at row: Int) -> String {
|
|
|
|
return TransientStore.shared.service.screenTitle(forHostId: existingHostIds[row])
|
|
|
|
}
|
2018-10-11 07:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
|
|
extension WizardHostViewController: FieldTableViewCellDelegate {
|
|
|
|
func fieldCellDidEdit(_: FieldTableViewCell) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func fieldCellDidEnter(_: FieldTableViewCell) {
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension WizardHostViewController: AccountViewControllerDelegate {
|
|
|
|
func accountController(_: AccountViewController, didEnterCredentials credentials: Credentials) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func accountControllerDidComplete(_ vc: AccountViewController) {
|
|
|
|
finish(withCredentials: vc.credentials)
|
|
|
|
}
|
|
|
|
}
|