Skip to content

Commit

Permalink
Merge branch 'feature/swift6'
Browse files Browse the repository at this point in the history
  • Loading branch information
andrej-jasso committed Oct 18, 2024
2 parents c5fae65 + 0e04477 commit 5bc50bd
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "11e672e30d756c2fc3f7ae187f2eeb3995eb992a570a647e92d895d6d9451cf0",
"originHash" : "f9dfd3d27e1ca9dd0642d21a7c31821c461c85a2d3b3027e85706d64f9a0989c",
"pins" : [
{
"identity" : "alamofire",
Expand Down Expand Up @@ -42,17 +42,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/GoodRequest/GoodLogger.git",
"state" : {
"revision" : "3c1ef9289d23154fab9124f58c356a61917646d2",
"version" : "1.2.3"
"revision" : "8203802fcff9163a309bd5edd44cc6f649404050",
"version" : "1.2.4"
}
},
{
"identity" : "goodnetworking",
"kind" : "remoteSourceControl",
"location" : "https://github.com/GoodRequest/GoodNetworking.git",
"state" : {
"revision" : "a9c89c3713bf88b425935f2ccfbb807b24d2d5ac",
"version" : "3.2.0"
"revision" : "fa451eaf06e846465610e2b292c406c000f307a8",
"version" : "3.2.2"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ extension Container {

#if DEBUG
var configurableSessionProvider: Factory<ConfigurableSessionProvider> {
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// ProxySettingsProvider.swift
//
// Debugman
//
// Created by Matus Klasovity on 28/05/2024.
//
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
}

Expand All @@ -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
}
}
Expand Down
10 changes: 1 addition & 9 deletions Sources/AppDebugMode/Dependencies/PackageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5bc50bd

Please sign in to comment.