summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/AccessMethodRepository/ProxyConfigurationTester.swift
blob: 03e108968a9c49c93bb0092eecc83d90163ef502 (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
//
//  ProxyConfigurationTester.swift
//  MullvadVPN
//
//  Created by pronebird on 28/11/2023.
//  Copyright © 2023 Mullvad VPN AB. All rights reserved.
//

import Combine
import Foundation
import MullvadREST
import MullvadSettings
import MullvadTypes

/// A concrete implementation of an access method proxy configuration.
class ProxyConfigurationTester: ProxyConfigurationTesterProtocol {
    private var cancellable: MullvadTypes.Cancellable?
    private let transportProvider: ProxyConfigurationTransportProvider
    private var headRequest: REST.APIAvailabilityTestRequest?

    init(transportProvider: ProxyConfigurationTransportProvider) {
        self.transportProvider = transportProvider
    }

    func start(configuration: PersistentProxyConfiguration, completion: @escaping (Error?) -> Void) {
        do {
            let transport = try transportProvider.makeTransport(with: configuration)
            let request = REST.APIAvailabilityTestRequest(transport: transport)
            headRequest = request
            cancellable = request.makeRequest { error in
                DispatchQueue.main.async {
                    completion(error)
                }
            }
        } catch {
            completion(error)
        }
    }

    func cancel() {
        cancellable?.cancel()
        cancellable = nil
        headRequest = nil
    }
}