summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/VPNSettings/VPNSettingsViewController.swift
blob: ce6729c82dc37819461e6c0be545ba0ad30bb607 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
//  VPNSettingsViewController.swift
//  MullvadVPN
//
//  Created by pronebird on 19/05/2021.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import MullvadSettings
import SwiftUI
import UIKit

protocol VPNSettingsViewControllerDelegate: AnyObject {
    func showIPOverrides()
}

class VPNSettingsViewController: UITableViewController {
    private let interactor: VPNSettingsInteractor
    private var dataSource: VPNSettingsDataSource?
    private let alertPresenter: AlertPresenter
    private let section: VPNSettingsSection?
    weak var delegate: VPNSettingsViewControllerDelegate?

    override var preferredStatusBarStyle: UIStatusBarStyle {
        .lightContent
    }

    init(
        interactor: VPNSettingsInteractor,
        alertPresenter: AlertPresenter,
        section: VPNSettingsSection?
    ) {
        self.interactor = interactor
        self.alertPresenter = alertPresenter
        self.section = section
        super.init(style: .grouped)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.setAccessibilityIdentifier(.vpnSettingsTableView)
        tableView.backgroundColor = .secondaryColor
        tableView.separatorColor = .secondaryColor
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 60
        tableView.estimatedSectionHeaderHeight = tableView.estimatedRowHeight
        tableView.allowsMultipleSelection = true

        dataSource = VPNSettingsDataSource(
            tableView: tableView,
            section: section
        )

        dataSource?.delegate = self

        navigationItem.title = NSLocalizedString("VPN settings", comment: "")

        interactor.tunnelSettingsDidChange = { [weak self] newSettings in
            self?.dataSource?.reload(from: newSettings)
        }
        dataSource?.update(from: interactor.tunnelSettings)

        dataSource?.setAvailablePortRanges(interactor.cachedRelays?.relays.wireguard.portRanges ?? [])
        interactor.cachedRelaysDidChange = { [weak self] cachedRelays in
            self?.dataSource?.setAvailablePortRanges(cachedRelays.relays.wireguard.portRanges)
        }

        let showsSingleSection = section != nil
        tableView.tableHeaderView = UIView(
            frame: CGRect(
                origin: .zero,
                size: CGSize(width: 0, height: showsSingleSection ? 0 : UIMetrics.TableView.emptyHeaderHeight)
            ))
    }
}

extension VPNSettingsViewController: @preconcurrency VPNSettingsDataSourceDelegate {
    func humanReadablePortRepresentation() -> String {
        let ranges = interactor.cachedRelays?.relays.wireguard.portRanges ?? []
        return
            ranges
            .compactMap { range in
                if let minPort = range.first, let maxPort = range.last {
                    return minPort == maxPort ? String(minPort) : "\(minPort)-\(maxPort)"
                } else {
                    return nil
                }
            }
            .joined(separator: ", ")
    }

    func didUpdateTunnelSettings(_ update: TunnelSettingsUpdate) {
        interactor.updateSettings([update])
    }

    func showInfo(for item: VPNSettingsInfoButtonItem) {
        let presentation = AlertPresentation(
            id: "vpn-settings-content-blockers-alert",
            icon: .info,
            message: item.description,
            buttons: [
                AlertAction(
                    title: NSLocalizedString("Got it!", comment: ""),
                    style: .default
                )
            ]
        )

        alertPresenter.showAlert(presentation: presentation, animated: true)
    }

    func showDetails(for item: VPNSettingsDetailsButtonItem) {
        switch item {
        case .udpOverTcp:
            showUDPOverTCPObfuscationSettings()
        case .wireguardOverShadowsocks:
            showShadowsocksObfuscationSettings()
        }
    }

    func showDNSSettings() {
        let viewController = CustomDNSViewController(interactor: interactor, alertPresenter: alertPresenter)
        navigationController?.pushViewController(viewController, animated: true)
    }

    func showIPOverrides() {
        delegate?.showIPOverrides()
    }

    private func showUDPOverTCPObfuscationSettings() {
        let viewModel = TunnelUDPOverTCPObfuscationSettingsViewModel(tunnelManager: interactor.tunnelManager)
        let view = UDPOverTCPObfuscationSettingsView(viewModel: viewModel)
        let vc = UIHostingController(rootView: view)
        vc.title = NSLocalizedString("UDP-over-TCP", comment: "")
        navigationController?.pushViewController(vc, animated: true)
    }

    private func showShadowsocksObfuscationSettings() {
        let viewModel = TunnelShadowsocksObfuscationSettingsViewModel(tunnelManager: interactor.tunnelManager)
        let view = ShadowsocksObfuscationSettingsView(viewModel: viewModel)
        let vc = UIHostingController(rootView: view)
        vc.title = NSLocalizedString("Shadowsocks", comment: "")
        navigationController?.pushViewController(vc, animated: true)
    }

    func didSelectWireGuardPort(_ port: UInt16?) {
        interactor.setPort(port)
    }

    func showLocalNetworkSharingWarning(_ enable: Bool, completion: @escaping (Bool) -> Void) {
        if interactor.tunnelManager.tunnelStatus.state.isSecured {
            let status =
                enable
                ? NSLocalizedString("Enabling", comment: "")
                : NSLocalizedString("Disabling", comment: "")
            let description = NSLocalizedString(
                """
                “%@ Local network sharing” requires restarting the VPN connection, \
                which will disconnect you and briefly expose your traffic.
                To prevent this, manually enable Airplane Mode and turn off Wi-Fi before continuing.
                Would you like to continue to enable “Local network sharing”?
                """,
                comment: ""
            )

            let presentation = AlertPresentation(
                id: "vpn-settings-local-network-sharing-warning",
                icon: .info,
                message: String(format: description, status),
                buttons: [
                    AlertAction(
                        title: NSLocalizedString("Yes, continue", comment: ""),
                        style: .destructive,
                        accessibilityId: .acceptLocalNetworkSharingButton,
                        handler: {
                            completion(true)
                        }
                    ),
                    AlertAction(
                        title: NSLocalizedString("Cancel", comment: ""),
                        style: .default,
                        handler: { completion(false) }
                    ),
                ]
            )

            alertPresenter.showAlert(presentation: presentation, animated: true)
        } else {
            completion(true)
        }
    }
}