-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathPacketTunnelPathObserver.swift
56 lines (47 loc) · 1.49 KB
/
PacketTunnelPathObserver.swift
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
//
// PacketTunnelPathObserver.swift
// PacketTunnel
//
// Created by pronebird on 10/08/2023.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import Combine
import MullvadLogging
import MullvadTypes
import Network
import NetworkExtension
import PacketTunnelCore
final class PacketTunnelPathObserver: DefaultPathObserverProtocol, Sendable {
private let eventQueue: DispatchQueue
private let pathMonitor: NWPathMonitor
nonisolated(unsafe) let logger = Logger(label: "PacketTunnelPathObserver")
private let stateLock = NSLock()
nonisolated(unsafe) private var started = false
public var currentPathStatus: Network.NWPath.Status {
stateLock.withLock {
pathMonitor.currentPath.status
}
}
init(eventQueue: DispatchQueue) {
self.eventQueue = eventQueue
pathMonitor = NWPathMonitor(prohibitedInterfaceTypes: [.other])
}
func start(_ body: @escaping @Sendable (Network.NWPath.Status) -> Void) {
stateLock.withLock {
guard started == false else { return }
defer { started = true }
pathMonitor.pathUpdateHandler = { updatedPath in
body(updatedPath.status)
}
pathMonitor.start(queue: eventQueue)
}
}
func stop() {
stateLock.withLock {
guard started == true else { return }
defer { started = false }
pathMonitor.pathUpdateHandler = nil
pathMonitor.cancel()
}
}
}