Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import UIKit
import SwiftUI
import UniformTypeIdentifiers
import FactoryKit
import UtilsLib

class ShareViewController: UIViewController, Sendable, Loggable {
let viewModel = Container.shared.shareViewModel()
Expand Down
4 changes: 0 additions & 4 deletions Modules/CommonsLib/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ let package = Package(
name: "CommonsLibMocks",
dependencies: ["CommonsLib"],
path: "Tests/Mocks/Generated"
),
.testTarget(
name: "CommonsLibTests",
dependencies: ["CommonsLib"],
)
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ class ConfigurationViewModel: Loggable {
self.fileManager = fileManager
}

func fetchConfiguration(lastUpdate: TimeInterval, proxyInfo: ProxyInfo) async {
func fetchConfiguration(
lastUpdate: TimeInterval,
proxyInfo: ProxyInfo,
userAgent: String
) async {
do {
guard let updates = try await repository.getCentralConfigurationUpdates(
cacheDir: Directories.getConfigDirectory(fileManager: fileManager),
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
) else {
ConfigurationViewModel.logger().error("No configuration updates available.")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public actor ConfigurationLoader: ConfigurationLoaderProtocol, Loggable {
self.bundle = bundle ?? Bundle.module
}

public func initConfiguration(cacheDir: URL, proxyInfo: ProxyInfo) async throws {
public func initConfiguration(
cacheDir: URL,
proxyInfo: ProxyInfo,
userAgent: String
) async throws {
ConfigurationLoader.logger().debug("Initializing configuration")

if !fileManager.fileExists(atPath: cacheDir.resolvedPath) {
Expand All @@ -71,7 +75,11 @@ public actor ConfigurationLoader: ConfigurationLoaderProtocol, Loggable {

if try await shouldCheckForUpdates() {
ConfigurationLoader.logger().debug("Checking for configuration updates...")
try await loadCentralConfiguration(cacheDir: cacheDir, proxyInfo: proxyInfo)
try await loadCentralConfiguration(
cacheDir: cacheDir,
proxyInfo: proxyInfo,
userAgent: userAgent
)
}

ConfigurationLoader.logger().debug("Finished initializing configuration")
Expand Down Expand Up @@ -261,7 +269,8 @@ public actor ConfigurationLoader: ConfigurationLoaderProtocol, Loggable {

public func loadCentralConfiguration(
cacheDir: URL?,
proxyInfo: ProxyInfo
proxyInfo: ProxyInfo,
userAgent: String
) async throws {
let configDir = try cacheDir ?? Directories.getConfigDirectory(fileManager: fileManager)

Expand All @@ -277,17 +286,21 @@ public actor ConfigurationLoader: ConfigurationLoaderProtocol, Loggable {
var centralSignature = ""

centralSignature = try await centralConfigurationRepository.fetchSignature(
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
).trimmingCharacters(in: .whitespaces)

if !centralSignature.isEmpty && currentSignature != centralSignature.data(using: .utf8) {
ConfigurationLoader.logger().debug("Found new configuration")

let centralConfig = try await centralConfigurationRepository.fetchConfiguration(
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
)

let centralPublicKey = try await centralConfigurationRepository.fetchPublicKey(
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
)

let centralConfigurationProvider = try JSONDecoder().decode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ import CommonsLib

/// @mockable
public protocol ConfigurationLoaderProtocol: Sendable {
func initConfiguration(cacheDir: URL, proxyInfo: ProxyInfo) async throws
func initConfiguration(
cacheDir: URL,
proxyInfo: ProxyInfo,
userAgent: String
) async throws

func loadConfigurationProperty() async throws -> ConfigurationProperty

func loadCachedConfiguration(afterCentralCheck: Bool, cacheDir: URL?) async throws

func loadDefaultConfiguration(cacheDir: URL?) async throws

func loadCentralConfiguration(cacheDir: URL?, proxyInfo: ProxyInfo) async throws
func loadCentralConfiguration(
cacheDir: URL?,
proxyInfo: ProxyInfo,
userAgent: String
) async throws

func shouldCheckForUpdates() async throws -> Bool

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,33 @@ public struct CentralConfigurationRepository: CentralConfigurationRepositoryProt
self.centralConfigurationService = centralConfigurationService
}

public func fetchConfiguration(proxyInfo: ProxyInfo) async throws -> String {
public func fetchConfiguration(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String {
return try await centralConfigurationService.fetchConfiguration(
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
)
}

public func fetchPublicKey(proxyInfo: ProxyInfo) async throws -> String {
public func fetchPublicKey(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String {
return try await centralConfigurationService.fetchPublicKey(
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
)
}

public func fetchSignature(proxyInfo: ProxyInfo) async throws -> String {
public func fetchSignature(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String {
return try await centralConfigurationService.fetchSignature(
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ import Foundation

/// @mockable
public protocol CentralConfigurationRepositoryProtocol: Sendable {
func fetchConfiguration(proxyInfo: ProxyInfo) async throws -> String
func fetchPublicKey(proxyInfo: ProxyInfo) async throws -> String
func fetchSignature(proxyInfo: ProxyInfo) async throws -> String
func fetchConfiguration(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String

func fetchPublicKey(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String

func fetchSignature(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,38 @@ public actor ConfigurationRepository: ConfigurationRepositoryProtocol {

public func getCentralConfiguration(
cacheDir: URL?,
proxyInfo: ProxyInfo
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> ConfigurationProvider? {
let configDir = try cacheDir ?? Directories.getConfigDirectory(fileManager: fileManager)

try await configurationLoader.loadCentralConfiguration(cacheDir: configDir, proxyInfo: proxyInfo)
try await configurationLoader
.loadCentralConfiguration(
cacheDir: configDir,
proxyInfo: proxyInfo,
userAgent: userAgent
)

return await getConfiguration()
}

public func getCentralConfigurationUpdates(
cacheDir: URL?,
proxyInfo: ProxyInfo
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> AsyncThrowingStream<
ConfigurationProvider?,
Error
>? {
let configDir = try cacheDir ?? Directories.getConfigDirectory(fileManager: fileManager)

try await configurationLoader.loadCentralConfiguration(cacheDir: configDir, proxyInfo: proxyInfo)
try await configurationLoader
.loadCentralConfiguration(
cacheDir: configDir,
proxyInfo: proxyInfo,
userAgent: userAgent
)

return await getConfigurationUpdates()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ import Foundation
public protocol ConfigurationRepositoryProtocol: Sendable {
func getConfiguration() async -> ConfigurationProvider?

func getCentralConfiguration(cacheDir: URL?, proxyInfo: ProxyInfo) async throws -> ConfigurationProvider?
func getCentralConfiguration(
cacheDir: URL?,
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> ConfigurationProvider?

func observeConfigurationUpdates(
) async -> AsyncThrowingStream<ConfigurationProvider?, Error>?
func observeConfigurationUpdates() async -> AsyncThrowingStream<ConfigurationProvider?, Error>?

func getConfigurationUpdates() async -> AsyncThrowingStream<ConfigurationProvider?, Error>?

func getCentralConfigurationUpdates(
cacheDir: URL?,
proxyInfo: ProxyInfo
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> AsyncThrowingStream<
ConfigurationProvider?,
Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@
import Foundation
import Alamofire
import CommonsLib
import UtilsLib

public actor CentralConfigurationService: CentralConfigurationServiceProtocol, Loggable {
private let userAgent: String
private let configurationProperty: ConfigurationProperty
private let session: Session?

public init(
userAgent: String,
configurationProperty: ConfigurationProperty,
session: Session? = nil
) {
self.userAgent = userAgent
self.configurationProperty = configurationProperty
self.session = session
}

public func fetchConfiguration(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String {
let session = self.session ?? constructHttpClient(
defaultTimeout: CommonsLib.Constants.Configuration.DefaultTimeout,
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
)

let url = "\(await configurationProperty.centralConfigurationServiceUrl)/config.json"
Expand All @@ -61,11 +61,13 @@ public actor CentralConfigurationService: CentralConfigurationServiceProtocol, L
}

public func fetchPublicKey(
proxyInfo: ProxyInfo
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String {
let session = self.session ?? constructHttpClient(
defaultTimeout: CommonsLib.Constants.Configuration.DefaultTimeout,
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
)

let url = "\(await configurationProperty.centralConfigurationServiceUrl)/config.ecpub"
Expand All @@ -85,11 +87,13 @@ public actor CentralConfigurationService: CentralConfigurationServiceProtocol, L
}

public func fetchSignature(
proxyInfo: ProxyInfo
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String {
let session = self.session ?? constructHttpClient(
defaultTimeout: CommonsLib.Constants.Configuration.DefaultTimeout,
proxyInfo: proxyInfo
proxyInfo: proxyInfo,
userAgent: userAgent
)

let url = "\(await configurationProperty.centralConfigurationServiceUrl)/config.ecc"
Expand All @@ -115,43 +119,37 @@ public actor CentralConfigurationService: CentralConfigurationServiceProtocol, L
defaultTimeout: TimeInterval,
proxyInfo: ProxyInfo,
customConfiguration: URLSessionConfiguration? = nil,
userAgent: String
) -> Session {
let interceptor = constructAlamofireRequestInterceptor()
let retryInterceptor = constructAlamofireRetryRequestInterceptor()

let configuration = customConfiguration ?? {
let config = URLSessionConfiguration.default
let config = URLSessionConfiguration.af.default
config.timeoutIntervalForRequest = defaultTimeout
config.timeoutIntervalForResource = defaultTimeout

var headers = config.httpAdditionalHeaders ?? [:]
headers["User-Agent"] = userAgent
headers["Content-Type"] = "application/json; charset=utf-8"
headers["Cache-Control"] = "no-cache"
headers["Pragma"] = "no-cache"
config.httpAdditionalHeaders = headers
return config
}()

return Session.withProxy(
proxyInfo: proxyInfo,
configuration: configuration,
interceptor: interceptor
interceptor: retryInterceptor
)
}

private func constructAlamofireRequestInterceptor() -> RequestInterceptor {
return CustomRequestInterceptor(userAgent: userAgent)
private func constructAlamofireRetryRequestInterceptor() -> RequestInterceptor {
return RetryRequestInterceptor()
}
}

struct CustomRequestInterceptor: RequestInterceptor {

private let userAgent: String

init(userAgent: String) {
self.userAgent = userAgent
}

// swiftlint:disable:next unused_parameter
func adapt(_ request: URLRequest, for session: Session, completion: @escaping (Result<URLRequest, Error>) -> Void) {
var modifiedRequest = request
modifiedRequest.setValue(userAgent, forHTTPHeaderField: "User-Agent")

completion(.success(modifiedRequest))
}
struct RetryRequestInterceptor: RequestInterceptor {

// swiftlint:disable:next blanket_disable_command
// swiftlint:disable unused_parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ import Foundation

/// @mockable
public protocol CentralConfigurationServiceProtocol: Sendable {
func fetchConfiguration(proxyInfo: ProxyInfo) async throws -> String
func fetchPublicKey(proxyInfo: ProxyInfo) async throws -> String
func fetchSignature(proxyInfo: ProxyInfo) async throws -> String
func fetchConfiguration(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String

func fetchPublicKey(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String

func fetchSignature(
proxyInfo: ProxyInfo,
userAgent: String
) async throws -> String
}
Loading