Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Update the remoteURL of profiles after receiving a 301 response #4

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
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
8 changes: 4 additions & 4 deletions ApplicationLibrary/Views/Profile/NewProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions Library/Database/Profile+Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
22 changes: 21 additions & 1 deletion Library/Network/ExtensionPlatformInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 16 additions & 2 deletions Library/Network/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
Expand Down