Skip to content

Commit

Permalink
Merge pull request #6761 from vector-im/johannes/device-model-in-user…
Browse files Browse the repository at this point in the history
…-agent

Include concrete device model in user agent (PSG-754)
  • Loading branch information
Johennes authored Sep 26, 2022
2 parents 6b7e8a6 + a1361ce commit b846c39
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 1 deletion.
64 changes: 63 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,59 @@ 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 appInfo = AppInfo.current
let clientName = appInfo.displayName
let clientVersion = appInfo.appVersion?.bundleShortVersion ?? "unknown"

#if os(iOS)
return String(
format: "%@/%@ (%@; iOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
UIDevice.current.systemVersion,
UIScreen.main.scale)
#elseif os(tvOS)
return String(
format: "%@/%@ (%@; tvOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
UIDevice.current.systemVersion,
UIScreen.main.scale)
#elseif os(watchOS)
return String(
format: "%@/%@ (%@; watchOS %@; Scale/%0.2f)",
clientName,
clientVersion,
Device.current.safeDescription,
WKInterfaceDevice.current.systemVersion,
WKInterfaceDevice.currentDevice.screenScale)
#elseif os(OSX)
return String(
format: "%@/%@ (Mac; Mac OS X %@)",
clientName,
clientVersion,
NSProcessInfo.processInfo.operatingSystemVersionString)
#else
return nil
#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
26 changes: 26 additions & 0 deletions Riot/Assets/third_party_licenses.html
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,32 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<br/><br/>
</li>
<li>
<b>DeviceKit</b> (<a href="https://github.com/devicekit/DeviceKit">https://github.com/devicekit/DeviceKit</a>)
<br/><br/>
MIT License
<br/><br/>
Copyright (c) 2015 Dennis Weissmann
<br/><br/>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
<br/><br/>
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
<br/><br/>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
<br/><br/>
</li>
</ul>
</body>
</html>
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
2 changes: 2 additions & 0 deletions RiotTests/target.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ targets:
- path: ../Config/AppConfiguration.swift
- path: ../Config/CommonConfiguration.swift
- path: ../Riot/Categories/Bundle.swift
- path: ../Riot/Managers/AppInfo/AppInfo.swift
- path: ../Riot/Managers/AppInfo/AppVersion.swift
- path: ../Riot/Managers/Settings/RiotSettings.swift
- path: ../Riot/Managers/EncryptionKeyManager/EncryptionKeyManager.swift
- path: ../Riot/Managers/KeyValueStorage/
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)
3 changes: 3 additions & 0 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ packages:
SwiftOGG:
url: https://github.com/vector-im/swift-ogg
branch: main
DeviceKit:
url: https://github.com/devicekit/DeviceKit
majorVersion: 4.7.0

0 comments on commit b846c39

Please sign in to comment.