Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })
ismailgulek marked this conversation as resolved.
Show resolved Hide resolved

// 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"
Johennes marked this conversation as resolved.
Show resolved Hide resolved

#if os(iOS)
return String(
format: "%@/%@ (%@; iOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
UIDevice.current.systemVersion,
UIScreen.main.scale)
#else
Johennes marked this conversation as resolved.
Show resolved Hide resolved
#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
Johennes marked this conversation as resolved.
Show resolved Hide resolved
}


// 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
Johennes marked this conversation as resolved.
Show resolved Hide resolved

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little unfortunate to have to bundle this in all the extensions but I think we want the same user agent also for any networking done from extensions and luckily DeviceKit is rather small.


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
Johennes marked this conversation as resolved.
Show resolved Hide resolved