Skip to content

Commit

Permalink
APPLE: add user agent details
Browse files Browse the repository at this point in the history
  • Loading branch information
rokas-ambrazevicius committed Sep 23, 2024
1 parent fb3dc7e commit 3212672
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 10 deletions.
10 changes: 10 additions & 0 deletions nym-vpn-apple/Scripts/UpdateCore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ else
exit 1
fi

# Update libVersion in AppVersionProvider.swift to the new version
app_version_file="../Services/Sources/Services/AppVersionProvider/AppVersionProvider.swift"
if [[ -f "$app_version_file" ]]; then
sed -i '' "s/public static let libVersion = \".*\"/public static let libVersion = \"$VERSION\"/g" "$app_version_file"
echo "libVersion updated to $VERSION in $app_version_file."
else
echo "Error: AppVersionProvider.swift file not found at $app_version_file."
exit 1
fi

# Find and download the _macos_universal.tar.gz file
macos_download_link="https://github.com/nymtech/nym-vpn-client/releases/download/nym-vpn-core-v${VERSION}/nym-vpn-core-v${VERSION}_macos_universal.tar.gz"

Expand Down
1 change: 1 addition & 0 deletions nym-vpn-apple/Services/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ let package = Package(
name: "CountriesManager",
dependencies: [
"AppSettings",
"AppVersionProvider",
"ConfigurationManager",
"Constants",
.product(name: "GRPCManager", package: "ServicesMacOS", condition: .when(platforms: [.macOS])),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

extension Date: RawRepresentable {
extension Foundation.Date: Swift.RawRepresentable {
public typealias RawValue = String

public init?(rawValue: RawValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import Foundation

public enum AppVersionProvider {
public static let libVersion = "0.2.1"

public static var app: String {
"nym-vpn-app"
}

public static var platform: String {
"\(osType()); \(osVersion()); \(hardwareString())"
}

public static func appVersion(in bundle: Bundle = .main) -> String {
guard let version = bundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
else {
Expand All @@ -9,3 +19,46 @@ public enum AppVersionProvider {
return version
}
}

private extension AppVersionProvider {
static func osType() -> String {
#if os(OSX)
return "macOS"
#elseif os(watchOS)
return "watchOS"
#elseif os(tvOS)
return "tvOS"
#elseif os(iOS)
#if targetEnvironment(macCatalyst)
return "macOSCatalyst"
#else
return "iOS"
#endif
#endif
}

static func osVersion() -> String {
let os = ProcessInfo().operatingSystemVersion
return "\(os.majorVersion).\(os.minorVersion).\(os.patchVersion)"
}

static func hardwareString() -> String {
var name: [Int32] = [CTL_HW, HW_MACHINE]
var size: Int = 2
sysctl(&name, 2, nil, &size, nil, 0)
var hwMachine = [CChar](repeating: 0, count: Int(size))
sysctl(&name, 2, &hwMachine, &size, nil, 0)

var hardware = String(cString: hwMachine)
let simulatorSet: Set<String> = [
"arm64",
"i386",
"x86_64"
]
if simulatorSet.contains(hardware),
let deviceID = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"] {
hardware = deviceID
}
return hardware
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Combine
import SwiftUI
import AppSettings
import AppVersionProvider
import ConfigurationManager
#if os(macOS)
import GRPCManager
Expand All @@ -20,12 +21,9 @@ public final class CountriesManager: ObservableObject {
#if os(macOS)
let grpcManager: GRPCManager
let helperManager: HelperManager
#endif

var isLoading = false
var timer: Timer?
var entryLastHopStore = EntryLastHopStore()
var cancellables = Set<AnyCancellable>()
var daemonVersion: String?
#endif
#if os(iOS)
public static let shared = CountriesManager(
appSettings: AppSettings.shared,
Expand All @@ -40,13 +38,20 @@ public final class CountriesManager: ObservableObject {
configurationManager: ConfigurationManager.shared
)
#endif
var isLoading = false
var timer: Timer?
var entryLastHopStore = EntryLastHopStore()
var cancellables = Set<AnyCancellable>()

@Published public var entryCountries: [Country]
@Published public var exitCountries: [Country]
@Published public var lastError: Error?

#if os(iOS)
public init(appSettings: AppSettings, configurationManager: ConfigurationManager) {
public init(
appSettings: AppSettings,
configurationManager: ConfigurationManager
) {
self.appSettings = appSettings
self.configurationManager = configurationManager
self.entryCountries = []
Expand Down Expand Up @@ -103,6 +108,9 @@ private extension CountriesManager {
setupAppSettingsObservers()
setupAutoUpdates()
fetchCountries()
#if os(macOS)
updateDaemonVersionIfNecessary()
#endif
}

func setupAppSettingsObservers() {
Expand Down Expand Up @@ -166,7 +174,16 @@ private extension CountriesManager {

#if os(macOS)
private extension CountriesManager {
func updateDaemonVersionIfNecessary() {
Task {
guard daemonVersion == nil else { return }
daemonVersion = try? await grpcManager.version()
}
}

func fetchEntryExitCountries() {
updateDaemonVersionIfNecessary()

guard helperManager.isHelperRunning()
else {
fetchCountriesAfterDelay()
Expand Down Expand Up @@ -229,7 +246,12 @@ private extension CountriesManager {
apiUrl: apiURL,
nymVpnApiUrl: configurationManager.nymVpnApiURL,
exitOnly: false,
userAgent: nil
userAgent: UserAgent(
application: AppVersionProvider.app,
version: "\(AppVersionProvider.appVersion()) (\(AppVersionProvider.libVersion))",
platform: AppVersionProvider.platform,
gitCommit: ""
)
)
let newEntryCountries = entryExitLocations.compactMap {
country(with: $0.twoLetterIsoCountryCode)
Expand All @@ -240,12 +262,17 @@ private extension CountriesManager {
apiUrl: apiURL,
nymVpnApiUrl: configurationManager.nymVpnApiURL,
exitOnly: true,
userAgent: nil
userAgent: UserAgent(
application: AppVersionProvider.app,
version: "\(AppVersionProvider.appVersion()) (\(AppVersionProvider.libVersion))",
platform: AppVersionProvider.platform,
gitCommit: ""
)
)
let newExitCountries = exitLocations.compactMap {
country(with: $0.twoLetterIsoCountryCode)
}
.sorted(by: { $0.name < $1.name })
.sorted(by: { $0.name < $1.name })

entryLastHopStore.entryCountries = entryCountries
entryLastHopStore.exitCountries = exitCountries
Expand Down

0 comments on commit 3212672

Please sign in to comment.