Include concrete device model in user agent (PSG-754)

Fixes: #6742
This commit is contained in:
Johannes Marbach 2022-09-26 10:23:31 +02:00
parent 5635bf7a9f
commit ee1a2c8b90
8 changed files with 90 additions and 1 deletions

View file

@ -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 {
@ -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
@ -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

View file

@ -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",

View file

@ -39,6 +39,7 @@ targets:
- package: Mapbox
- package: OrderedCollections
- package: SwiftOGG
- package: DeviceKit
configFiles:
Debug: Debug.xcconfig

View file

@ -31,6 +31,9 @@ targets:
platform: iOS
type: app-extension
dependencies:
- package: DeviceKit
configFiles:
Debug: Debug.xcconfig
Release: Release.xcconfig

View file

@ -31,6 +31,9 @@ targets:
platform: iOS
type: app-extension
dependencies:
- package: DeviceKit
configFiles:
Debug: Debug.xcconfig
Release: Release.xcconfig

View file

@ -33,6 +33,7 @@ targets:
dependencies:
- sdk: Intents.framework
- package: DeviceKit
configFiles:
Debug: Debug.xcconfig

1
changelog.d/6742.change Normal file
View file

@ -0,0 +1 @@
Include concrete device model in user agent (PSG-754)

View file

@ -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