-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ce0496
commit b8f0e5d
Showing
11 changed files
with
248 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Sources/NetworkProtection/Settings/VPNLocationFormatting.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// VPNLocationFormatting.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
public protocol VPNLocationFormatting { | ||
func emoji(for country: String?, | ||
preferredLocation: VPNSettings.SelectedLocation) -> String? | ||
|
||
func string(from location: String?, | ||
preferredLocation: VPNSettings.SelectedLocation) -> String | ||
|
||
@available(macOS 12, iOS 15, *) | ||
func string(from location: String?, | ||
preferredLocation: VPNSettings.SelectedLocation, | ||
locationTextColor: Color, | ||
preferredLocationTextColor: Color) -> AttributedString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// DataVolume.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct DataVolume: Codable, Equatable { | ||
public let bytesSent: Int64 | ||
public let bytesReceived: Int64 | ||
|
||
public init(bytesSent: Int64 = 0, bytesReceived: Int64 = 0) { | ||
self.bytesSent = bytesSent | ||
self.bytesReceived = bytesReceived | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Sources/NetworkProtection/Status/DataVolumeObserver/DataVolumeObserver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// DataVolumeObserver.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import NetworkExtension | ||
|
||
public protocol DataVolumeObserver { | ||
var publisher: AnyPublisher<DataVolume, Never> { get } | ||
var recentValue: DataVolume { get } | ||
} |
109 changes: 109 additions & 0 deletions
109
Sources/NetworkProtection/Status/DataVolumeObserver/DataVolumeObserverThroughSession.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// DataVolumeObserverThroughSession.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import NetworkExtension | ||
import NotificationCenter | ||
import Common | ||
|
||
public class DataVolumeObserverThroughSession: DataVolumeObserver { | ||
public lazy var publisher = subject.eraseToAnyPublisher() | ||
public var recentValue: DataVolume { | ||
subject.value | ||
} | ||
|
||
private let subject = CurrentValueSubject<DataVolume, Never>(.init()) | ||
|
||
private let tunnelSessionProvider: TunnelSessionProvider | ||
|
||
// MARK: - Notifications | ||
|
||
private let platformNotificationCenter: NotificationCenter | ||
private let platformDidWakeNotification: Notification.Name | ||
private var cancellables = Set<AnyCancellable>() | ||
|
||
// MARK: - Timer | ||
|
||
private static let interval: TimeInterval = .seconds(1) | ||
|
||
// MARK: - Logging | ||
|
||
private let log: OSLog | ||
|
||
// MARK: - Initialization | ||
|
||
public init(tunnelSessionProvider: TunnelSessionProvider, | ||
platformNotificationCenter: NotificationCenter, | ||
platformDidWakeNotification: Notification.Name, | ||
log: OSLog = .networkProtection) { | ||
|
||
self.platformNotificationCenter = platformNotificationCenter | ||
self.platformDidWakeNotification = platformDidWakeNotification | ||
self.tunnelSessionProvider = tunnelSessionProvider | ||
self.log = log | ||
|
||
start() | ||
} | ||
|
||
public func start() { | ||
updateDataVolume() | ||
|
||
Timer.publish(every: Self.interval, on: .main, in: .common) | ||
.autoconnect() | ||
.sink { [weak self] _ in | ||
self?.updateDataVolume() | ||
}.store(in: &cancellables) | ||
|
||
platformNotificationCenter.publisher(for: platformDidWakeNotification).sink { [weak self] notification in | ||
self?.handleDidWake(notification) | ||
}.store(in: &cancellables) | ||
} | ||
|
||
// MARK: - Handling Notifications | ||
|
||
private func handleDidWake(_ notification: Notification) { | ||
updateDataVolume() | ||
} | ||
|
||
// MARK: - Obtaining the data volume | ||
|
||
private func updateDataVolume(session: NETunnelProviderSession) async { | ||
guard let data: ExtensionMessageString = try? await session.sendProviderMessage(.getDataVolume) else { | ||
return | ||
} | ||
|
||
let bytes = data.value.components(separatedBy: ",") | ||
guard let receivedString = bytes.first, let sentString = bytes.last, | ||
let received = Int64(receivedString), let sent = Int64(sentString) else { | ||
return | ||
} | ||
|
||
subject.send(DataVolume(bytesSent: sent, bytesReceived: received)) | ||
} | ||
|
||
private func updateDataVolume() { | ||
Task { | ||
guard let session = await tunnelSessionProvider.activeSession() else { | ||
return | ||
} | ||
|
||
await updateDataVolume(session: session) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
Sources/NetworkProtectionTestUtils/Status/MockDataVolumeObserver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// MockDataVolumeObserver.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import NetworkProtection | ||
|
||
public final class MockDataVolumeObserver: DataVolumeObserver { | ||
public init() {} | ||
public let subject = CurrentValueSubject<DataVolume, Never>(.init()) | ||
lazy public var publisher = subject.eraseToAnyPublisher() | ||
public var recentValue: DataVolume { | ||
subject.value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters