summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/Tunnel/TunnelViewControllerInteractor.swift
blob: 288c382ff90e954168eddcaa396dc2c5da3328b9 (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
//
//  TunnelViewControllerInteractor.swift
//  MullvadVPN
//
//  Created by pronebird on 26/10/2022.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Combine
import MullvadSettings
import MullvadTypes

final class TunnelViewControllerInteractor: @unchecked Sendable {
    private let tunnelManager: TunnelManager
    private let outgoingConnectionService: OutgoingConnectionServiceHandling
    private var tunnelObserver: TunnelObserver?
    private var outgoingConnectionTask: Task<Void, Error>?
    private var ipOverrideRepository: IPOverrideRepositoryProtocol
    private var cancellables: Set<Combine.AnyCancellable> = []

    var didUpdateTunnelStatus: ((TunnelStatus) -> Void)?
    var didUpdateDeviceState: ((_ deviceState: DeviceState, _ previousDeviceState: DeviceState) -> Void)?
    var didUpdateTunnelSettings: ((LatestTunnelSettings) -> Void)?
    var didUpdateIpOverrides: (([IPOverride]) -> Void)?
    var didGetOutgoingAddress: (@MainActor (OutgoingConnectionInfo) -> Void)?

    var tunnelStatus: TunnelStatus {
        tunnelManager.tunnelStatus
    }

    var deviceState: DeviceState {
        tunnelManager.deviceState
    }

    var tunnelSettings: LatestTunnelSettings {
        tunnelManager.settings
    }

    var ipOverrides: [IPOverride] {
        ipOverrideRepository.fetchAll()
    }

    deinit {
        outgoingConnectionTask?.cancel()
    }

    init(
        tunnelManager: TunnelManager,
        outgoingConnectionService: OutgoingConnectionServiceHandling,
        ipOverrideRepository: IPOverrideRepositoryProtocol
    ) {
        self.tunnelManager = tunnelManager
        self.outgoingConnectionService = outgoingConnectionService
        self.ipOverrideRepository = ipOverrideRepository

        let tunnelObserver = TunnelBlockObserver(
            didUpdateTunnelStatus: { [weak self] _, tunnelStatus in
                guard let self else { return }
                outgoingConnectionTask?.cancel()
                didUpdateTunnelStatus?(tunnelStatus)
                if case .connected = tunnelStatus.state {
                    outgoingConnectionTask = Task(priority: .high) { [weak self] in
                        guard
                            let outgoingConnectionInfo = try await self?.outgoingConnectionService
                                .getOutgoingConnectionInfo()
                        else {
                            return
                        }
                        await self?.didGetOutgoingAddress?(outgoingConnectionInfo)
                    }
                }
            },
            didUpdateDeviceState: { [weak self] _, deviceState, previousDeviceState in
                self?.didUpdateDeviceState?(deviceState, previousDeviceState)
            },
            didUpdateTunnelSettings: { [weak self] _, tunnelSettings in
                self?.didUpdateTunnelSettings?(tunnelSettings)
            }
        )

        tunnelManager.addObserver(tunnelObserver)

        self.tunnelObserver = tunnelObserver

        ipOverrideRepository.overridesPublisher
            .sink { [weak self] overrides in
                self?.didUpdateIpOverrides?(overrides)
            }
            .store(in: &cancellables)
    }

    func startTunnel() {
        tunnelManager.startTunnel()
    }

    func stopTunnel() {
        tunnelManager.stopTunnel()
    }

    func reconnectTunnel(selectNewRelay: Bool) {
        tunnelManager.reconnectTunnel(selectNewRelay: selectNewRelay)
    }
}