Skip to content

Commit

Permalink
UBL: Adds possibility to add local proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
choli committed May 10, 2024
1 parent 66df310 commit 7a795f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Sources/UBDevTools/DevToolsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ public struct DevToolsView: View {
}
Section(header: Text("Proxy settings")) {
Toggle("Start Proxy", isOn: Binding(get: { Self.enableNetworkingProxySettings }, set: { Self.enableNetworkingProxySettings = $0 }))
TextField("Host", text: Binding(get: { Self.proxySettingsHost ?? "" }, set: { Self.proxySettingsHost = $0 }))
TextField("Port", text: Binding(get: {
Self.proxySettingsPort != nil ? String(Self.proxySettingsPort!) : ""
}, set: {
Self.proxySettingsPort = Int($0)
}))
}

#if !targetEnvironment(simulator)
Expand Down Expand Up @@ -194,4 +200,10 @@ public struct DevToolsView: View {

@UBUserDefault(key: "ubkit.devtools.proxy.enabled.key", defaultValue: false)
public static var enableNetworkingProxySettings: Bool

@UBUserDefault(key: "ubkit.devtools.proxy.host.key", defaultValue: nil)
public static var proxySettingsHost: String?

@UBUserDefault(key: "ubkit.devtools.proxy.port.key", defaultValue: nil)
public static var proxySettingsPort: Int?
}
20 changes: 14 additions & 6 deletions Sources/UBDevTools/ProxyDevTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,28 @@ public class UBFriendlyEvaluator: UBServerTrustEvaluator {
public extension Networking {
/// This is a copy of the sharedSession including the proxy and friendly trust settings
static let friendlyDefaultSession: UBURLSession = {
guard DevToolsView.enableNetworkingProxySettings,
let proxy = UBDevToolsProxyHelper.shared.proxy
else { return Networking.sharedSession }
guard DevToolsView.enableNetworkingProxySettings else { return Networking.sharedSession }

let queue = OperationQueue()
queue.name = "Friendly UBURLSession Shared"
queue.qualityOfService = .userInitiated

let proxy: UBDevToolsProxyHelper.Proxy?
if let host = DevToolsView.proxySettingsHost, host.isEmpty == false,
let port = DevToolsView.proxySettingsPort {
proxy = UBDevToolsProxyHelper.Proxy(host: host, port: port)
} else if let devProy = UBDevToolsProxyHelper.shared.proxy {
proxy = devProy
} else {
proxy = nil
}

let configuration: UBURLSessionConfiguration
if let domains = proxy.proxiedDomains, domains.isEmpty == false {
if let domains = proxy?.proxiedDomains, domains.isEmpty == false {
let trusts: [String: UBFriendlyEvaluator] = domains.reduce([:], { var dict = $0; dict[$1] = UBFriendlyEvaluator(); return dict })
configuration = UBURLSessionConfiguration(hostsServerTrusts: trusts, proxy: UBDevToolsProxyHelper.shared.proxy)
configuration = UBURLSessionConfiguration(hostsServerTrusts: trusts, proxy: proxy)
} else {
configuration = UBURLSessionConfiguration(defaultServerTrust: UBFriendlyEvaluator(), proxy: UBDevToolsProxyHelper.shared.proxy)
configuration = UBURLSessionConfiguration(defaultServerTrust: UBFriendlyEvaluator(), proxy: proxy)
}

configuration.sessionConfiguration.networkServiceType = .responsiveData
Expand Down

0 comments on commit 7a795f2

Please sign in to comment.