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 b305970
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import Foundation

public enum AppVersionProvider {
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 +17,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 Down Expand Up @@ -46,7 +47,9 @@ public final class CountriesManager: ObservableObject {
@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 @@ -229,7 +232,12 @@ private extension CountriesManager {
apiUrl: apiURL,
nymVpnApiUrl: configurationManager.nymVpnApiURL,
exitOnly: false,
userAgent: nil
userAgent: UserAgent(
application: AppVersionProvider.app,
version: AppVersionProvider.appVersion(),
platform: AppVersionProvider.platform,
gitCommit: ""
)
)
let newEntryCountries = entryExitLocations.compactMap {
country(with: $0.twoLetterIsoCountryCode)
Expand All @@ -240,12 +248,17 @@ private extension CountriesManager {
apiUrl: apiURL,
nymVpnApiUrl: configurationManager.nymVpnApiURL,
exitOnly: true,
userAgent: nil
userAgent: UserAgent(
application: AppVersionProvider.app,
version: AppVersionProvider.appVersion(),
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 b305970

Please sign in to comment.