Skip to content

Commit

Permalink
Include concrete device model in user agent (PSG-754)
Browse files Browse the repository at this point in the history
Fixes: #6742
  • Loading branch information
Johennes committed Sep 26, 2022
1 parent 5635bf7 commit ee1a2c8
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 1 deletion.
69 changes: 68 additions & 1 deletion Config/CommonConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import Foundation
import MatrixSDK

#if !os(OSX)
import DeviceKit
#endif

/// CommonConfiguration is the central point to setup settings for MatrixSDK, MatrixKit and common configurations for all targets.
class CommonConfiguration: NSObject, Configurable {

Expand Down Expand Up @@ -66,8 +70,13 @@ class CommonConfiguration: NSObject, Configurable {
// Disable identicon use
sdkOptions.disableIdenticonUseForUserAvatar = true

// Set up user agent
if let userAgent = makeASCIIUserAgent() {
sdkOptions.httpAdditionalHeaders = ["User-Agent": userAgent]
}

// Pass httpAdditionalHeaders to the SDK
sdkOptions.httpAdditionalHeaders = BuildSettings.httpAdditionalHeaders
sdkOptions.httpAdditionalHeaders = (sdkOptions.httpAdditionalHeaders ?? [:]).merging(BuildSettings.httpAdditionalHeaders, uniquingKeysWith: { _, value in value })

// Disable key backup on common
sdkOptions.enableKeyBackupWhenStartingMXCrypto = false
Expand All @@ -82,6 +91,64 @@ class CommonConfiguration: NSObject, Configurable {
MXKeyProvider.sharedInstance().delegate = EncryptionKeyManager.shared
}

private func makeASCIIUserAgent() -> String? {
guard var userAgent = makeUserAgent() else {
return nil
}
if !userAgent.canBeConverted(to: .ascii) {
let mutableUserAgent = NSMutableString(string: userAgent)
if CFStringTransform(mutableUserAgent, nil, "Any-Latin; Latin-ASCII; [:^ASCII:] Remove" as CFString, false) {
userAgent = mutableUserAgent as String
}
}
return userAgent
}

private func makeUserAgent() -> String? {
let clientName = Bundle.main.object(forInfoDictionaryKey: kCFBundleExecutableKey as String) as? String ?? Bundle.main.object(forInfoDictionaryKey: kCFBundleIdentifierKey as String) as? String ?? "unknown"
let clientVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? Bundle.main.object(forInfoDictionaryKey: kCFBundleVersionKey as String) as? String ?? "unknown"

#if os(iOS)
return String(
format: "%@/%@ (%@; iOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
UIDevice.current.systemVersion,
UIScreen.main.scale)
#else
#if os(tvOS)
return String(
format: "%@/%@ (%@; tvOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
UIDevice.current.systemVersion,
UIScreen.main.scale)
#else
#if os(watchOS)
return String(
format: "%@/%@ (%@; watchOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
WKInterfaceDevice.current.systemVersion,
WKInterfaceDevice.currentDevice.screenScale)
#else
#if os(OSX)
return String(
format: "%@/%@ (Mac; Mac OS X %@)",
clientName,
clientVersion,
NSProcessInfo.processInfo.operatingSystemVersionString)
#else
return nil
#endif
#endif
#endif
#endif
}


// MARK: - Per matrix session settings

Expand Down
9 changes: 9 additions & 0 deletions Riot.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "devicekit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/devicekit/DeviceKit",
"state" : {
"revision" : "20e0991f3975916ab0f6d58db84d8bc64f883537",
"version" : "4.7.0"
}
},
{
"identity" : "maplibre-gl-native-distribution",
"kind" : "remoteSourceControl",
Expand Down
1 change: 1 addition & 0 deletions Riot/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ targets:
- package: Mapbox
- package: OrderedCollections
- package: SwiftOGG
- package: DeviceKit

configFiles:
Debug: Debug.xcconfig
Expand Down
3 changes: 3 additions & 0 deletions RiotNSE/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ targets:
platform: iOS
type: app-extension

dependencies:
- package: DeviceKit

configFiles:
Debug: Debug.xcconfig
Release: Release.xcconfig
Expand Down
3 changes: 3 additions & 0 deletions RiotShareExtension/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ targets:
platform: iOS
type: app-extension

dependencies:
- package: DeviceKit

configFiles:
Debug: Debug.xcconfig
Release: Release.xcconfig
Expand Down
1 change: 1 addition & 0 deletions SiriIntents/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ targets:

dependencies:
- sdk: Intents.framework
- package: DeviceKit

configFiles:
Debug: Debug.xcconfig
Expand Down
1 change: 1 addition & 0 deletions changelog.d/6742.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Include concrete device model in user agent (PSG-754)
4 changes: 4 additions & 0 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ packages:
SwiftOGG:
url: https://github.com/vector-im/swift-ogg
branch: main
DeviceKit:
url: https://github.com/devicekit/DeviceKit
minVersion: 4.7.0
maxVersion: 4.7.0

0 comments on commit ee1a2c8

Please sign in to comment.