aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/Coordinators/AppCoordinator+TunnelsTableViewControllerDelegate.swift
blob: 0357c40cf944ab894a02557607c9d9ab0ff0d738 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
//  Copyright © 2018 WireGuard LLC. All rights reserved.
//

import UIKit
import NetworkExtension
import os.log

import MobileCoreServices

import ZIPFoundation
import PromiseKit

extension AppCoordinator: TunnelsTableViewControllerDelegate {
    func status(for tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) -> NEVPNStatus {
        let session = self.providerManager(for: tunnel)?.connection as? NETunnelProviderSession
        return session?.status ?? .invalid
    }

    func addProvider(tunnelsTableViewController: TunnelsTableViewController) {
        let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        actionSheet.addAction(UIAlertAction(title: NSLocalizedString("Create from file or archive", comment: ""), style: .default) { [unowned self] _ in
            self.addProviderFromFile()
        })
        actionSheet.addAction(UIAlertAction(title: NSLocalizedString("Create from QR code", comment: ""), style: .default) { [unowned self] _ in
            self.addProviderWithQRScan()
        })
        actionSheet.addAction(UIAlertAction(title: NSLocalizedString("Create from scratch", comment: ""), style: .default) { [unowned self] _ in
            self.addProviderManually()
        })
        actionSheet.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel))

        tunnelsTableViewController.present(actionSheet, animated: true, completion: nil)
    }

    func addProviderFromFile() {
        let documentPickerController = UIDocumentPickerViewController(documentTypes: [String(kUTTypeZipArchive), "com.wireguard.config.quick"], in: .import)
        documentPickerController.delegate = documentPickerDelegateObject
        tunnelsTableViewController.present(documentPickerController, animated: true, completion: nil)
    }

    func addProviderManually() {
        let addContext = persistentContainer.newBackgroundContext()
        showTunnelConfigurationViewController(tunnel: nil, context: addContext)
    }

    func addProviderWithQRScan() {
        let addContext = persistentContainer.newBackgroundContext()

        let qrScanViewController = storyboard.instantiateViewController(type: QRScanViewController.self)

        qrScanViewController.configure(context: addContext, delegate: self)

        self.navigationController.pushViewController(qrScanViewController, animated: true)
    }

    func connect(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
        connect(tunnel: tunnel)
    }

    func disconnect(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
        disconnect(tunnel: tunnel)
    }

    func info(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
        showTunnelInfoViewController(tunnel: tunnel, context: self.persistentContainer.viewContext)
    }

    func delete(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
        if let moc = tunnel.managedObjectContext {
            moc.perform {
                moc.delete(tunnel)
                moc.saveContextToStore()
            }
            let manager = providerManager(for: tunnel)
            manager?.removeFromPreferences { (error) in
                if let error = error {
                    os_log("error removing preferences: %{public}@", log: Log.general, type: .error, error.localizedDescription)
                    return
                }
                self.providerManagers?.removeAll { $0 == manager }
                os_log("removed preferences", log: Log.general, type: .info)
            }
        }
    }

    func saveTunnel(_ tunnel: Tunnel) {
        let manager = providerManager(for: tunnel) ?? NETunnelProviderManager()
        manager.localizedDescription = tunnel.title

        let protocolConfiguration = NETunnelProviderProtocol()
        protocolConfiguration.providerBundleIdentifier = VPNBUNDLE
        protocolConfiguration.serverAddress = (tunnel.peers?.array as? [Peer])?.compactMap { $0.endpoint}.joined(separator: ", ")
        protocolConfiguration.providerConfiguration = tunnel.generateProviderConfiguration()

        manager.protocolConfiguration = protocolConfiguration
        let connectRule = NEOnDemandRuleConnect()
        connectRule.interfaceTypeMatch = .any
        manager.onDemandRules = [connectRule]

        manager.saveToPreferences { (error) in
            if let error = error {
                os_log("error saving preferences: %{public}@", log: Log.general, type: .error, error.localizedDescription)
                return
            }
            os_log("saved preferences", log: Log.general, type: .info)
        }

        _ = refreshProviderManagers().then { () -> Promise<Void> in
            self.navigationController.popViewController(animated: true)
            return Promise.value(())
        }
    }
}