From bdd178b227968618ef2104f426ce4632d594caab Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Wed, 14 Oct 2020 14:07:18 +1100 Subject: [PATCH 1/2] onion routing attachment download for open groups --- .../src/Loki/API/FileServerAPI.swift | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/FileServerAPI.swift b/SignalServiceKit/src/Loki/API/FileServerAPI.swift index 4ea2414dc..f1c7b1959 100644 --- a/SignalServiceKit/src/Loki/API/FileServerAPI.swift +++ b/SignalServiceKit/src/Loki/API/FileServerAPI.swift @@ -66,24 +66,38 @@ public final class FileServerAPI : DotNetAPI { public static func downloadAttachment(_ downloadURL: String) -> Promise { var error: NSError? - var url = downloadURL - if downloadURL.contains(fileStaticServer) { - url = downloadURL.replacingOccurrences(of: fileStaticServer, with: "\(server)/loki/v1") + let url = URL(string: downloadURL)! + var host = "https://\(url.host!)" + var actualDownloadURL = downloadURL + if fileStaticServer.contains(host) { + actualDownloadURL = downloadURL.replacingOccurrences(of: fileStaticServer, with: "\(server)/loki/v1") + host = server + } else { + actualDownloadURL = downloadURL.replacingOccurrences(of: host, with: "\(host)/loki/v1") } - let request = AFHTTPRequestSerializer().request(withMethod: "GET", urlString: url, parameters: nil, error: &error) + let request = AFHTTPRequestSerializer().request(withMethod: "GET", urlString: actualDownloadURL, parameters: nil, error: &error) if let error = error { print("[Loki] Couldn't download attachment due to error: \(error).") return Promise(error: error) } - return OnionRequestAPI.sendOnionRequest(request, to: server, using: fileServerPublicKey, isJSONRequired: false).map2 { json in - guard let body = json["body"] as? JSON, let dataArray = body["data"] as? [UInt8] else { - print("[Loki] Couldn't download attachment.") - return Data() + return getServerPublicKey(for: host).then2 { serverPublicKey in + OnionRequestAPI.sendOnionRequest(request, to: host, using: serverPublicKey, isJSONRequired: false).map2 { json in + guard let body = json["body"] as? JSON, let dataArray = body["data"] as? [UInt8] else { + print("[Loki] Couldn't download attachment.") + return Data() + } + return Data(dataArray) } - return Data(dataArray) } } + public static func getServerPublicKey(for host: String) -> Promise { + if server.contains(host) { + return Promise.value(fileServerPublicKey) + } + return PublicChatAPI.getOpenGroupServerPublicKey(for: host) + } + // MARK: Open Group Server Public Key public static func getPublicKey(for openGroupServer: String) -> Promise { let url = URL(string: "\(server)/loki/v1/getOpenGroupKey/\(URL(string: openGroupServer)!.host!)")! From 24825a2eea613b3c8c09527f1ba6e160190b093c Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Wed, 14 Oct 2020 14:58:49 +1100 Subject: [PATCH 2/2] minor refactor --- .../Loki/API/Open Groups/PublicChatAPI.swift | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/Open Groups/PublicChatAPI.swift b/SignalServiceKit/src/Loki/API/Open Groups/PublicChatAPI.swift index bc1cad0f6..ccd3a9ce3 100644 --- a/SignalServiceKit/src/Loki/API/Open Groups/PublicChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/Open Groups/PublicChatAPI.swift @@ -369,7 +369,7 @@ public final class PublicChatAPI : DotNetAPI { } } - static func updateProfileIfNeeded(for channel: UInt64, on server: String, from info: PublicChatInfo, token: String, serverPublicKey: String) { + static func updateProfileIfNeeded(for channel: UInt64, on server: String, from info: PublicChatInfo) { let storage = OWSPrimaryStorage.shared() let publicChatID = "\(server).\(channel)" try! Storage.writeSync { transaction in @@ -388,20 +388,7 @@ public final class PublicChatAPI : DotNetAPI { if oldProfilePictureURL != info.profilePictureURL || groupModel.groupImage == nil { storage.setProfilePictureURL(info.profilePictureURL, forPublicChatWithID: publicChatID, in: transaction) if let profilePictureURL = info.profilePictureURL { - var error: NSError? - let url = "\(server)/loki/v1\(profilePictureURL)" - let request = AFHTTPRequestSerializer().request(withMethod: "GET", urlString: url, parameters: nil, error: &error) - request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)"] - if let error = error { - print("[Loki] Couldn't download open group avatar due to error: \(error).") - return - } - OnionRequestAPI.sendOnionRequest(request, to: server, using: serverPublicKey, isJSONRequired: false).map{ json in - guard let body = json["body"] as? JSON, let dataArray = body["data"] as? [UInt8] else { - print("[Loki] Couldn't download open group avatar.") - return - } - let avatarData = Data(dataArray) + FileServerAPI.downloadAttachment("\(server)\(profilePictureURL)").map { avatarData in let attachmentStream = TSAttachmentStream(contentType: OWSMimeTypeImageJpeg, byteCount: UInt32(avatarData.count), sourceFilename: nil, caption: nil, albumMessageId: nil) try! attachmentStream.write(avatarData) groupThread.updateAvatar(with: attachmentStream) @@ -441,7 +428,7 @@ public final class PublicChatAPI : DotNetAPI { storage.setUserCount(memberCount, forPublicChatWithID: "\(server).\(channel)", in: transaction) } let publicChatInfo = PublicChatInfo(displayName: displayName, profilePictureURL: profilePictureURL, memberCount: memberCount) - updateProfileIfNeeded(for: channel, on: server, from: publicChatInfo, token: token, serverPublicKey: serverPublicKey) + updateProfileIfNeeded(for: channel, on: server, from: publicChatInfo) return publicChatInfo } }