From 0e0447754ba9bea37d277b8463dc6013890aa000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20Jas=CC=8Cs=CC=8Co?= Date: Fri, 18 Oct 2024 09:01:52 +0200 Subject: [PATCH] fix: Newest goodnetworking comformance --- .../xcshareddata/swiftpm/Package.resolved | 10 +-- .../Application/AppDelegate.swift | 3 +- .../DependencyContainer/SampleContainer.swift | 11 +-- .../RequestManager/RequestManager.swift | 10 +-- .../Packages/GoodDependencies/Package.swift | 4 +- .../Logging/StandardOutputProcessor.swift | 4 + .../DebugManSessionManager.swift | 3 - .../ProxySettingsProvider.swift | 19 ++--- .../Dependencies/PackageManager.swift | 10 +-- .../ConfigurableSessionProvider.swift | 79 +++++++++++++++++++ .../ConsoleLogsSettingsView.swift | 11 +++ 11 files changed, 120 insertions(+), 44 deletions(-) create mode 100644 Sources/AppDebugMode/Dependencies/Provider/ConfigurableSessionProvider.swift diff --git a/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 379d927..9056cab 100644 --- a/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "11e672e30d756c2fc3f7ae187f2eeb3995eb992a570a647e92d895d6d9451cf0", + "originHash" : "f9dfd3d27e1ca9dd0642d21a7c31821c461c85a2d3b3027e85706d64f9a0989c", "pins" : [ { "identity" : "alamofire", @@ -42,8 +42,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/GoodRequest/GoodLogger.git", "state" : { - "revision" : "3c1ef9289d23154fab9124f58c356a61917646d2", - "version" : "1.2.3" + "revision" : "8203802fcff9163a309bd5edd44cc6f649404050", + "version" : "1.2.4" } }, { @@ -51,8 +51,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/GoodRequest/GoodNetworking.git", "state" : { - "revision" : "a9c89c3713bf88b425935f2ccfbb807b24d2d5ac", - "version" : "3.2.0" + "revision" : "fa451eaf06e846465610e2b292c406c000f307a8", + "version" : "3.2.2" } }, { diff --git a/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Application/AppDelegate.swift b/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Application/AppDelegate.swift index ea4eaf9..e076934 100644 --- a/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Application/AppDelegate.swift +++ b/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Application/AppDelegate.swift @@ -48,8 +48,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { await PackageManager.shared.setup( serverProviders: providers, configurableProxySessionProvider: Container.shared.configurableSessionProvider.resolve(), - customControls: CustomControlsView(model: model), - showDebugSwift: true + customControls: CustomControlsView(model: model) ) } diff --git a/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Managers/DependencyContainer/SampleContainer.swift b/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Managers/DependencyContainer/SampleContainer.swift index ed29d03..569e139 100644 --- a/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Managers/DependencyContainer/SampleContainer.swift +++ b/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Managers/DependencyContainer/SampleContainer.swift @@ -42,14 +42,15 @@ extension Container { #if DEBUG var configurableSessionProvider: Factory { - LoggingEventMonitor.maxVerboseLogSizeBytes = 1000000 + LoggingEventMonitor.maxVerboseLogSizeBytes = 1000000 // For testing 400kb large payload let monitor = LoggingEventMonitor(logger: AppDebugModeLogger()) - - let defaultConfiguration = NetworkSessionConfiguration( - urlSessionConfiguration: .default, + + let defaultSession = Alamofire.Session( + configuration: .default, eventMonitors: [monitor] ) - return Factory(self) { ConfigurableSessionProvider(defaultConfiguration: defaultConfiguration) }.singleton + + return Factory(self) { ConfigurableSessionProvider(defaultSession: defaultSession) }.singleton } #endif diff --git a/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Managers/RequestManager/RequestManager.swift b/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Managers/RequestManager/RequestManager.swift index 0938f61..bf96371 100644 --- a/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Managers/RequestManager/RequestManager.swift +++ b/AppDebugMode-iOS-Sample/AppDebugMode-iOS-Sample/Managers/RequestManager/RequestManager.swift @@ -25,12 +25,12 @@ actor RequestManager: RequestManagerType { init(baseUrlProvider: BaseUrlProviding) { -#if DEBUG + #if DEBUG session = NetworkSession( - baseUrl: baseUrlProvider, - configuration: Container.shared.configurableSessionProvider.resolve() + baseUrlProvider: baseUrlProvider, + sessionProvider: Container.shared.configurableSessionProvider.resolve() ) -#else + #else let monitor = LoggingEventMonitor(logger: OSLogLogger()) session = NetworkSession( @@ -46,7 +46,7 @@ actor RequestManager: RequestManagerType { } func fetchLargeObject() async throws -> LargeObjectResponse { - try await session.request(endpoint: Endpoint.large, baseUrl: "https://codepo8.github.io") + try await session.request(endpoint: Endpoint.large, baseUrlProvider: "https://codepo8.github.io") } func fetchCars(id: Int) async throws -> CarResponse { diff --git a/AppDebugMode-iOS-Sample/Packages/GoodDependencies/Package.swift b/AppDebugMode-iOS-Sample/Packages/GoodDependencies/Package.swift index bb3ac65..e612bc7 100644 --- a/AppDebugMode-iOS-Sample/Packages/GoodDependencies/Package.swift +++ b/AppDebugMode-iOS-Sample/Packages/GoodDependencies/Package.swift @@ -13,8 +13,8 @@ let package = Package( ), ], dependencies: [ - .package(url: "https://github.com/GoodRequest/GoodNetworking.git", .upToNextMajor(from: "3.0.0")), - .package(url: "https://github.com/GoodRequest/GoodLogger.git", .upToNextMajor(from: "1.2.1")) + .package(url: "https://github.com/GoodRequest/GoodNetworking.git", .upToNextMajor(from: "3.2.2")), + .package(url: "https://github.com/GoodRequest/GoodLogger.git", .upToNextMajor(from: "1.2.4")) ], targets: [ .target( diff --git a/Sources/AppDebugMode/Dependencies/Logging/StandardOutputProcessor.swift b/Sources/AppDebugMode/Dependencies/Logging/StandardOutputProcessor.swift index aa3a54e..0a1b793 100644 --- a/Sources/AppDebugMode/Dependencies/Logging/StandardOutputProcessor.swift +++ b/Sources/AppDebugMode/Dependencies/Logging/StandardOutputProcessor.swift @@ -29,9 +29,13 @@ public actor StandardOutputProcessor: ObservableObject { @AppStorage("capturedOutput", store: UserDefaults(suiteName: Constants.suiteName)) var capturedOutput: [Log] = [] + @AppStorage("numberOfStoredLogs") var numberOfStoredLogs = 30 + // MARK: - Helper functions func redirectLogsToAppDebugMode () { + capturedOutput = Array(capturedOutput.prefix(upTo: numberOfStoredLogs)) + guard !didRedirectLogs else { return } // redirect only once didRedirectLogs = true diff --git a/Sources/AppDebugMode/Dependencies/MultipeerConnectivity/DebugManSessionManager.swift b/Sources/AppDebugMode/Dependencies/MultipeerConnectivity/DebugManSessionManager.swift index 8bb7263..3f32667 100644 --- a/Sources/AppDebugMode/Dependencies/MultipeerConnectivity/DebugManSessionManager.swift +++ b/Sources/AppDebugMode/Dependencies/MultipeerConnectivity/DebugManSessionManager.swift @@ -402,9 +402,6 @@ extension DebugManSessionManager { func handleNewProxyConfiguration(_ proxyConfiguration: ProxyConfiguration) async throws(ProxyConfigurationError) { setProxyState(proxyState: .testing(configuration: proxyConfiguration)) - #warning("Add saving first configuration") - #warning("Add requesting first configuration again") - let port = proxyConfiguration.port var didAnyIpPass = false diff --git a/Sources/AppDebugMode/Dependencies/MultipeerConnectivity/ProxySettingsProvider.swift b/Sources/AppDebugMode/Dependencies/MultipeerConnectivity/ProxySettingsProvider.swift index c6b356b..06b92c6 100644 --- a/Sources/AppDebugMode/Dependencies/MultipeerConnectivity/ProxySettingsProvider.swift +++ b/Sources/AppDebugMode/Dependencies/MultipeerConnectivity/ProxySettingsProvider.swift @@ -1,6 +1,6 @@ // // ProxySettingsProvider.swift -// +// Debugman // // Created by Matus Klasovity on 28/05/2024. // @@ -39,12 +39,8 @@ public final class ProxySettingsProvider: Sendable { } // MARK: - Public - - public func urlSessionConfiguration() -> URLSessionConfiguration { - urlSessionConfiguration(proxyIpAddress: proxyIpAddress, proxyPort: proxyPortUInt16) - } - public func urlSessionConfiguration(proxyIpAddress: String, proxyPort: UInt16) -> URLSessionConfiguration { + nonisolated public func urlSessionConfiguration(proxyIpAddress: String, proxyPort: UInt16) -> URLSessionConfiguration { let urlSessionConfig = URLSessionConfiguration.default urlSessionConfig.connectionProxyDictionary = [ "HTTPEnable": true, @@ -69,13 +65,7 @@ public final class ProxySettingsProvider: Sendable { Task { let urlConfig = urlSessionConfiguration(proxyIpAddress: proxyIpAddress, proxyPort: proxyPortUInt16) - let newConfiguration = await NetworkSessionConfiguration( - urlSessionConfiguration: urlConfig, - interceptor: configurableProxySessionProvider.currentConfiguration.interceptor, - serverTrustManager: configurableProxySessionProvider.currentConfiguration.serverTrustManager, - eventMonitors: configurableProxySessionProvider.currentConfiguration.eventMonitors - ) - await configurableProxySessionProvider.updateConfiguration(with: newConfiguration) + await configurableProxySessionProvider.updateConfiguration(with: urlConfig) } } @@ -95,10 +85,13 @@ public final class ProxySettingsProvider: Sendable { do { let _ = try await testingSession.data(from: testingUrl) isProxyValidated = true + print("๐Ÿงช Testing Proxy: success") return .success } catch let error as NSError where error.code == 310 || error.code == -1200 { // certificate compromised + print("๐Ÿงช Testing Proxy: missing certificate") return .missingCertificate } catch _ { + print("๐Ÿงช Testing Proxy: connection failure") return .connectionFailed } } diff --git a/Sources/AppDebugMode/Dependencies/PackageManager.swift b/Sources/AppDebugMode/Dependencies/PackageManager.swift index 73326cf..7d0b12b 100644 --- a/Sources/AppDebugMode/Dependencies/PackageManager.swift +++ b/Sources/AppDebugMode/Dependencies/PackageManager.swift @@ -81,18 +81,10 @@ public extension PackageManager { serverProviders: [DebugSelectableServerProvider], configurableProxySessionProvider: ConfigurableSessionProvider?, firebaseMessaging: AnyObject? = nil, - customControls: (some View)? = nil, - showDebugSwift: Bool = true + customControls: (some View)? = nil ) async { NetworkLogger.enableProxy() - if showDebugSwift { - Task {@MainActor in -// DebugSwift.setup() -// DebugSwift.show() - } - } - if !serverProviders.isEmpty { Container.shared.setupServerProviders(providers: serverProviders) } diff --git a/Sources/AppDebugMode/Dependencies/Provider/ConfigurableSessionProvider.swift b/Sources/AppDebugMode/Dependencies/Provider/ConfigurableSessionProvider.swift new file mode 100644 index 0000000..e4e73c7 --- /dev/null +++ b/Sources/AppDebugMode/Dependencies/Provider/ConfigurableSessionProvider.swift @@ -0,0 +1,79 @@ +// +// ConfigurableSessionProvider.swift +// AppDebugMode +// +// Created by Andrej Jasso on 17/10/2024. +// + +@preconcurrency import Alamofire +import GoodNetworking +import GoodLogger +import Foundation + +public actor ConfigurableSessionProvider: NetworkSessionProviding { + + public var isSessionValid = false + + public func invalidateSession() async { + logger.log(level: .debug, message: "Invalidating session not supported", privacy: .auto) + } + + public func makeSession() async -> Alamofire.Session { + logger.log( + level: .debug, + message: "๐Ÿ›œ Resolved new URLSession with configuration: \(currentConfiguration)", + privacy: .auto + ) + isSessionValid = true + + currentSession = Alamofire.Session( + configuration: currentConfiguration, + interceptor: currentSession.interceptor, + serverTrustManager: currentSession.serverTrustManager, + eventMonitors: [currentSession.eventMonitor] + ) + + return currentSession + } + + public var currentConfiguration: URLSessionConfiguration + public var currentSession: Alamofire.Session + + /// A private property that provides the appropriate logger based on the iOS version. + /// + /// For iOS 14 and later, it uses `OSLogLogger`. For earlier versions, it defaults to `PrintLogger`. + private var logger: GoodLogger { + if #available(iOS 14, *) { + return OSLogLogger() + } else { + return PrintLogger() + } + } + + public init(defaultSession: Alamofire.Session = .default) { + self.currentConfiguration = defaultSession.sessionConfiguration + self.currentSession = defaultSession + + } + + public func updateConfiguration(with urlConfiguration: URLSessionConfiguration) async { + self.currentConfiguration = urlConfiguration + self.isSessionValid = false + logger.log( + level: .debug, + message: "โš™๏ธ Updated ConfigurableSessionProvider to \(String(describing: urlConfiguration.connectionProxyDictionary))", + privacy: .auto + ) + } + + public func resolveSession() async -> Alamofire.Session { + logger.log( + level: .debug, + message: "๐Ÿ›œ Resolved old URLSession with configuration: \(currentConfiguration)", + privacy: .auto + ) + + return currentSession + } + +} diff --git a/Sources/AppDebugMode/Screens/ConsoleLogsVIew/ConsoleLogsSettingsView.swift b/Sources/AppDebugMode/Screens/ConsoleLogsVIew/ConsoleLogsSettingsView.swift index 5bf51a7..231a038 100644 --- a/Sources/AppDebugMode/Screens/ConsoleLogsVIew/ConsoleLogsSettingsView.swift +++ b/Sources/AppDebugMode/Screens/ConsoleLogsVIew/ConsoleLogsSettingsView.swift @@ -16,6 +16,8 @@ struct ConsoleLogsSettingsView: View { @AppStorage("shouldRedirectLogsToAppDebugMode", store: UserDefaults(suiteName: Constants.suiteName)) var shouldRedirectLogsToAppDebugMode = !DebuggerService.debuggerConnected() + @AppStorage("numberOfStoredLogs") var numberOfStoredLogs = 30 + // MARK: - Binding @Binding var showSettings: Bool @@ -50,6 +52,15 @@ struct ConsoleLogsSettingsView: View { } .pickerStyle(MenuPickerStyle()) .accentColor(AppDebugColors.primary) + + Picker("Number of stored logs", selection: $numberOfStoredLogs) { + ForEach(1..<100) { + Text("\($0) newest logs cached") + .foregroundColor(AppDebugColors.textPrimary) + } + } + .pickerStyle(MenuPickerStyle()) + .accentColor(AppDebugColors.primary) } .listRowSeparatorColor(AppDebugColors.primary, for: .insetGrouped) .listRowBackground(AppDebugColors.backgroundSecondary)