summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/SimulatorTunnelProvider/SimulatorTunnelProvider.swift
blob: 68ca1f51461eb153345ad987d6368a274d35bd48 (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
//
//  SimulatorTunnelProvider.swift
//  MullvadVPN
//
//  Created by pronebird on 05/02/2020.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import NetworkExtension

#if targetEnvironment(simulator)

    class SimulatorTunnelProviderDelegate {
        var connection: SimulatorVPNConnection?

        var protocolConfiguration: NEVPNProtocol {
            connection?.protocolConfiguration ?? NEVPNProtocol()
        }

        var reasserting: Bool {
            get {
                connection?.reasserting ?? false
            }
            set {
                connection?.reasserting = newValue
            }
        }

        func startTunnel(options: [String: NSObject]?, completionHandler: @escaping @Sendable (Error?) -> Void) {
            completionHandler(nil)
        }

        func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping @Sendable () -> Void) {
            completionHandler()
        }

        func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
            completionHandler?(nil)
        }
    }

    final class SimulatorTunnelProvider: Sendable {
        static let shared = SimulatorTunnelProvider()

        private let lock = NSLock()
        nonisolated(unsafe) private var _delegate: SimulatorTunnelProviderDelegate?

        var delegate: SimulatorTunnelProviderDelegate! {
            get {
                lock.lock()
                defer { lock.unlock() }

                return _delegate
            }
            set {
                lock.lock()
                _delegate = newValue
                lock.unlock()
            }
        }

        private init() {}

        func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)? = nil) {
            delegate.handleAppMessage(messageData, completionHandler: completionHandler)
        }
    }

#endif