From 82d83dc5620ef56185df7f80c69391a1b21aa8ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 19 Feb 2024 00:41:20 +0800 Subject: [PATCH 1/2] Add support for `bypass_domain` and `match_domain` platform HTTP proxy options --- .../Network/ExtensionPlatformInterface.swift | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Library/Network/ExtensionPlatformInterface.swift b/Library/Network/ExtensionPlatformInterface.swift index 2623e4b..800467b 100644 --- a/Library/Network/ExtensionPlatformInterface.swift +++ b/Library/Network/ExtensionPlatformInterface.swift @@ -148,8 +148,28 @@ public class ExtensionPlatformInterface: NSObject, LibboxPlatformInterfaceProtoc proxySettings.httpEnabled = true proxySettings.httpsEnabled = true } + var bypassDomains: [String] = [] + let bypassDomainIterator = options.getHTTPProxyBypassDomain()! + while bypassDomainIterator.hasNext() { + bypassDomains.append(bypassDomainIterator.next()) + } if excludeAPNs { - proxySettings.exceptionList = ["push.apple.com"] + if !bypassDomains.contains(where: { it in + it == "push.apple.com" + }) { + bypassDomains.append("push.apple.com") + } + } + if !bypassDomains.isEmpty { + proxySettings.exceptionList = bypassDomains + } + var matchDomains: [String] = [] + let matchDomainIterator = options.getHTTPProxyMatchDomain()! + while matchDomainIterator.hasNext() { + matchDomains.append(matchDomainIterator.next()) + } + if !matchDomains.isEmpty { + proxySettings.matchDomains = matchDomains } settings.proxySettings = proxySettings } From c10d981e1ee165c53392cb5e8f60e80d5a6d4212 Mon Sep 17 00:00:00 2001 From: k3-cat Date: Mon, 22 Apr 2024 00:36:57 +1000 Subject: [PATCH 2/2] [feat] Update the remoteURL of profiles after receiving a 301 response --- .../Views/Profile/NewProfileView.swift | 8 ++++---- Library/Database/Profile+Update.swift | 7 ++++--- Library/Network/HTTPClient.swift | 18 ++++++++++++++++-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/ApplicationLibrary/Views/Profile/NewProfileView.swift b/ApplicationLibrary/Views/Profile/NewProfileView.swift index 91cd5ea..6f883aa 100644 --- a/ApplicationLibrary/Views/Profile/NewProfileView.swift +++ b/ApplicationLibrary/Views/Profile/NewProfileView.swift @@ -218,18 +218,18 @@ public struct NewProfileView: View { } savePath = remotePath } else if profileType == .remote { - let remoteContent = try HTTPClient().getString(remotePath) + let remoteContent = try HTTPClient().getConfigWithUpdatedURL(remotePath) var error: NSError? - LibboxCheckConfig(remoteContent, &error) + LibboxCheckConfig(remoteContent.config, &error) if let error { throw error } let profileConfigDirectory = FilePath.sharedDirectory.appendingPathComponent("configs", isDirectory: true) try FileManager.default.createDirectory(at: profileConfigDirectory, withIntermediateDirectories: true) let profileConfig = profileConfigDirectory.appendingPathComponent("config_\(nextProfileID).json") - try remoteContent.write(to: profileConfig, atomically: true, encoding: .utf8) + try remoteContent.config.write(to: profileConfig, atomically: true, encoding: .utf8) savePath = profileConfig.relativePath - remoteURL = remotePath + remoteURL = remoteContent.newURL lastUpdated = .now } try await ProfileManager.create(Profile( diff --git a/Library/Database/Profile+Update.swift b/Library/Database/Profile+Update.swift index c3bed66..4952dd0 100644 --- a/Library/Database/Profile+Update.swift +++ b/Library/Database/Profile+Update.swift @@ -7,13 +7,14 @@ public extension Profile { if type != .remote { return } - let remoteContent = try HTTPClient().getString(remoteURL) + let remoteContent = try HTTPClient().getConfigWithUpdatedURL(remoteURL) var error: NSError? - LibboxCheckConfig(remoteContent, &error) + LibboxCheckConfig(remoteContent.config, &error) if let error { throw error } - try write(remoteContent) + try write(remoteContent.config) + remoteURL = remoteContent.newURL lastUpdated = Date() try await ProfileManager.update(self) } diff --git a/Library/Network/HTTPClient.swift b/Library/Network/HTTPClient.swift index 5355b6e..b3eac84 100644 --- a/Library/Network/HTTPClient.swift +++ b/Library/Network/HTTPClient.swift @@ -21,11 +21,15 @@ public class HTTPClient { client.modernTLS() } - public func getString(_ url: String?) throws -> String { + private func _getResponse(_ url: String?) -> HTTPResponse { let request = client.newRequest()! request.setUserAgent(HTTPClient.userAgent) try request.setURL(url) - let response = try request.execute() + return try request.execute() + } + + public func getString(_ url: String?) throws -> String { + let response = _getResponse(url) var error: NSError? let contentString = response.getContentString(&error) if let error { @@ -34,6 +38,16 @@ public class HTTPClient { return contentString } + public func getConfigWithUpdatedURL(_ url: String?) throws -> (config: String, newURL: String) { + let response = _getResponse(url) + var error: NSError? + let contentString = response.getContentString(&error) + if let error { + throw error + } + return (config: contentString, newURL: response.getFinalURL) + } + deinit { client.close() }