enable onion routing for open groups

This commit is contained in:
Ryan ZHAO 2020-07-22 17:10:53 +10:00
parent e2219f986c
commit 89a56ba9bc
4 changed files with 335 additions and 141 deletions

View File

@ -200,4 +200,23 @@ public final class FileServerAPI : DotNetAPI {
return downloadURL
}
}
// MARK: Open Group Server Public Key
public static func getOpenGroupKey(for openGroupServer: String) -> Promise<String> {
let serverURL = URL(string: openGroupServer)!
let url = URL(string: "\(server)/loki/v1/getOpenGroupKey/\(serverURL.host!)")!
let request = TSRequest(url: url)
let token = "loki" // tokenless request, using a dummy token
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
return OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: fileServerPublicKey).map2 { rawResponse in
guard let bodyAsString = rawResponse["data"] as? String, let bodyAsData = bodyAsString.data(using: .utf8), let body = try JSONSerialization.jsonObject(with: bodyAsData, options: [ .fragmentsAllowed ]) as? JSON else { throw HTTP.Error.invalidJSON }
guard let base64EncodedPublicKey = body["data"] as? String else {
print("[Loki] Couldn't parse open group public key from: \(body).")
throw DotNetAPIError.parsingFailed
}
let publicKeyWithPrefix = Data(base64Encoded: base64EncodedPublicKey)!
let hexEncodedPublicKeyWithPrefix = publicKeyWithPrefix.toHexString()
return hexEncodedPublicKeyWithPrefix.removing05PrefixIfNeeded()
}
}
}

View File

@ -241,12 +241,15 @@ public enum OnionRequestAPI {
}
/// Sends an onion request to `file server`. Builds new paths as needed.
internal static func sendOnionRequestLsrpcDest(_ request: NSURLRequest, server: String, using x25519Key: String) -> Promise<JSON> {
internal static func sendOnionRequestLsrpcDest(_ request: NSURLRequest, server: String, using x25519Key: String, noJSON: Bool = false) -> Promise<JSON> {
var headers = getCanonicalHeaders(for: request)
let urlAsString = request.url!.absoluteString
let serverURLEndIndex = urlAsString.range(of: server)!.upperBound
let endpointStartIndex = urlAsString.index(after: serverURLEndIndex)
let endpoint = String(urlAsString[endpointStartIndex..<urlAsString.endIndex])
var endpoint = ""
if server.count < urlAsString.count {
let endpointStartIndex = urlAsString.index(after: serverURLEndIndex)
endpoint = String(urlAsString[endpointStartIndex..<urlAsString.endIndex])
}
let parametersAsString: String
if let tsRequest = request as? TSRequest {
headers["Content-Type"] = "application/json"
@ -269,7 +272,7 @@ public enum OnionRequestAPI {
let destination: JSON = [ "host" : request.url?.host,
"target" : "/loki/v1/lsrpc",
"method" : "POST"]
let promise = sendOnionRequest(on: nil, with: payload, to: destination, using: x25519Key, associatedWith: getUserHexEncodedPublicKey())
let promise = sendOnionRequest(on: nil, with: payload, to: destination, using: x25519Key, associatedWith: getUserHexEncodedPublicKey(), noJSON: noJSON)
promise.recover2{ error -> Promise<JSON> in
// TODO: File Server API handle Error
throw error
@ -277,7 +280,7 @@ public enum OnionRequestAPI {
return promise
}
internal static func sendOnionRequest(on snode: Snode?, with payload: JSON, to destination: JSON, using x25519Key: String?, associatedWith publicKey: String) -> Promise<JSON> {
internal static func sendOnionRequest(on snode: Snode?, with payload: JSON, to destination: JSON, using x25519Key: String?, associatedWith publicKey: String, noJSON: Bool = false) -> Promise<JSON> {
let (promise, seal) = Promise<JSON>.pending()
var guardSnode: Snode!
DispatchQueue.global(qos: .userInitiated).async {
@ -306,10 +309,18 @@ public enum OnionRequestAPI {
print("[Loki] The user's clock is out of sync with the service node network.")
seal.reject(SnodeAPI.SnodeAPIError.clockOutOfSync)
} else {
guard let bodyAsData = bodyAsString.data(using: .utf8),
if noJSON {
let body = ["body": bodyAsString]
guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtTargetSnode(statusCode: UInt(statusCode), json: body)) }
seal.fulfill(body)
} else {
guard let bodyAsData = bodyAsString.data(using: .utf8),
let body = try JSONSerialization.jsonObject(with: bodyAsData, options: [ .fragmentsAllowed ]) as? JSON else { return seal.reject(HTTP.Error.invalidJSON) }
guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtTargetSnode(statusCode: UInt(statusCode), json: body)) }
seal.fulfill(body)
guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtTargetSnode(statusCode: UInt(statusCode), json: body)) }
seal.fulfill(body)
}
}
} catch (let error) {
seal.reject(error)

View File

@ -8,7 +8,7 @@ public final class LokiPublicChatAPI : DotNetAPI {
public static var displayNameUpdatees: [String:Set<String>] = [:]
internal static var useOnionRequests = true
public static var useOnionRequests = true
// MARK: Settings
private static let attachmentType = "net.app.core.oembed"
@ -31,6 +31,28 @@ public final class LokiPublicChatAPI : DotNetAPI {
@objc public static let lastMessageServerIDCollection = "LokiGroupChatLastMessageServerIDCollection"
@objc public static let lastDeletionServerIDCollection = "LokiGroupChatLastDeletionServerIDCollection"
@objc public static let openGroupPublicKeyCollection = "LokiGroupChatPublicKeyCollection"
private static func getOpenGroupPublicKey(on server: String) -> String? {
var result: String? = nil
storage.dbReadConnection.read { transaction in
result = transaction.object(forKey: "\(server)", inCollection: openGroupPublicKeyCollection) as! String?
}
return result
}
private static func setOpneGroupPublicKey(on server: String, value publicKey: String) {
try! Storage.writeSync { transaction in
transaction.setObject(publicKey, forKey: "\(server)", inCollection: openGroupPublicKeyCollection)
}
}
private static func removeOpenGroupPublicKey(on server: String) {
try! Storage.writeSync { transaction in
transaction.removeObject(forKey: "\(server)", inCollection: openGroupPublicKeyCollection)
}
}
private static func getLastMessageServerID(for group: UInt64, on server: String) -> UInt? {
var result: UInt? = nil
storage.dbReadConnection.read { transaction in
@ -74,6 +96,23 @@ public final class LokiPublicChatAPI : DotNetAPI {
public static func clearCaches(for channel: UInt64, on server: String) {
removeLastMessageServerID(for: channel, on: server)
removeLastDeletionServerID(for: channel, on: server)
removeOpenGroupPublicKey(on: server)
}
// MARK: Open Group Public Key Validation
public static func getOpenGroupServerPublicKey(on server: String) -> Promise<String> {
if let publicKey = getOpenGroupPublicKey(on: server) {
return Promise.value(publicKey)
} else {
return FileServerAPI.getOpenGroupKey(for: server).then2 { hexEncodedPublicKey -> Promise<String> in
let url = URL(string: server)!
let request = TSRequest(url: url)
return OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey, noJSON: true).map2 { _ -> String in
setOpneGroupPublicKey(on: server, value: hexEncodedPublicKey)
return hexEncodedPublicKey
}
}
}
}
// MARK: Receiving
@ -83,6 +122,78 @@ public final class LokiPublicChatAPI : DotNetAPI {
}
public static func getMessages(for channel: UInt64, on server: String) -> Promise<[LokiPublicChatMessage]> {
func handleMessages(rawResponse: Any) throws -> [LokiPublicChatMessage] {
guard let json = rawResponse as? JSON, let rawMessages = json["data"] as? [JSON] else {
print("[Loki] Couldn't parse messages for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
return rawMessages.flatMap { message in
let isDeleted = (message["is_deleted"] as? Int == 1)
guard !isDeleted else { return nil }
guard let annotations = message["annotations"] as? [JSON], let annotation = annotations.first(where: { $0["type"] as? String == publicChatMessageType }), let value = annotation["value"] as? JSON,
let serverID = message["id"] as? UInt64, let hexEncodedSignatureData = value["sig"] as? String, let signatureVersion = value["sigver"] as? UInt64,
let body = message["text"] as? String, let user = message["user"] as? JSON, let hexEncodedPublicKey = user["username"] as? String,
let timestamp = value["timestamp"] as? UInt64 else {
print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(message).")
return nil
}
var profilePicture: LokiPublicChatMessage.ProfilePicture? = nil
let displayName = user["name"] as? String ?? NSLocalizedString("Anonymous", comment: "")
if let userAnnotations = user["annotations"] as? [JSON], let profilePictureAnnotation = userAnnotations.first(where: { $0["type"] as? String == profilePictureType }),
let profilePictureValue = profilePictureAnnotation["value"] as? JSON, let profileKeyString = profilePictureValue["profileKey"] as? String, let profileKey = Data(base64Encoded: profileKeyString), let url = profilePictureValue["url"] as? String {
profilePicture = LokiPublicChatMessage.ProfilePicture(profileKey: profileKey, url: url)
}
let lastMessageServerID = getLastMessageServerID(for: channel, on: server)
if serverID > (lastMessageServerID ?? 0) { setLastMessageServerID(for: channel, on: server, to: serverID) }
let quote: LokiPublicChatMessage.Quote?
if let quoteAsJSON = value["quote"] as? JSON, let quotedMessageTimestamp = quoteAsJSON["id"] as? UInt64, let quoteeHexEncodedPublicKey = quoteAsJSON["author"] as? String,
let quotedMessageBody = quoteAsJSON["text"] as? String {
let quotedMessageServerID = message["reply_to"] as? UInt64
quote = LokiPublicChatMessage.Quote(quotedMessageTimestamp: quotedMessageTimestamp, quoteeHexEncodedPublicKey: quoteeHexEncodedPublicKey, quotedMessageBody: quotedMessageBody,
quotedMessageServerID: quotedMessageServerID)
} else {
quote = nil
}
let signature = LokiPublicChatMessage.Signature(data: Data(hex: hexEncodedSignatureData), version: signatureVersion)
let attachmentsAsJSON = annotations.filter { $0["type"] as? String == attachmentType }
let attachments: [LokiPublicChatMessage.Attachment] = attachmentsAsJSON.compactMap { attachmentAsJSON in
guard let value = attachmentAsJSON["value"] as? JSON, let kindAsString = value["lokiType"] as? String, let kind = LokiPublicChatMessage.Attachment.Kind(rawValue: kindAsString),
let serverID = value["id"] as? UInt64, let contentType = value["contentType"] as? String, let size = value["size"] as? UInt, let url = value["url"] as? String else { return nil }
let fileName = value["fileName"] as? String ?? UUID().description
let width = value["width"] as? UInt ?? 0
let height = value["height"] as? UInt ?? 0
let flags = (value["flags"] as? UInt) ?? 0
let caption = value["caption"] as? String
let linkPreviewURL = value["linkPreviewUrl"] as? String
let linkPreviewTitle = value["linkPreviewTitle"] as? String
if kind == .linkPreview {
guard linkPreviewURL != nil && linkPreviewTitle != nil else {
print("[Loki] Ignoring public chat message with invalid link preview.")
return nil
}
}
return LokiPublicChatMessage.Attachment(kind: kind, server: server, serverID: serverID, contentType: contentType, size: size, fileName: fileName, flags: flags,
width: width, height: height, caption: caption, url: url, linkPreviewURL: linkPreviewURL, linkPreviewTitle: linkPreviewTitle)
}
let result = LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, profilePicture: profilePicture,
body: body, type: publicChatMessageType, timestamp: timestamp, quote: quote, attachments: attachments, signature: signature)
guard result.hasValidSignature() else {
print("[Loki] Ignoring public chat message with invalid signature.")
return nil
}
var existingMessageID: String? = nil
storage.dbReadConnection.read { transaction in
existingMessageID = storage.getIDForMessage(withServerID: UInt(result.serverID!), in: transaction)
}
guard existingMessageID == nil else {
print("[Loki] Ignoring duplicate public chat message.")
return nil
}
return result
}.sorted { $0.timestamp < $1.timestamp }
}
var queryParameters = "include_annotations=1"
if let lastMessageServerID = getLastMessageServerID(for: channel, on: server) {
queryParameters += "&since_id=\(lastMessageServerID)"
@ -93,75 +204,15 @@ public final class LokiPublicChatAPI : DotNetAPI {
let url = URL(string: "\(server)/channels/\(channel)/messages?\(queryParameters)")!
let request = TSRequest(url: url)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
guard let json = rawResponse as? JSON, let rawMessages = json["data"] as? [JSON] else {
print("[Loki] Couldn't parse messages for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublickey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublickey).map2 { rawResponse in
return try handleMessages(rawResponse: rawResponse)
}
}
return rawMessages.flatMap { message in
let isDeleted = (message["is_deleted"] as? Int == 1)
guard !isDeleted else { return nil }
guard let annotations = message["annotations"] as? [JSON], let annotation = annotations.first(where: { $0["type"] as? String == publicChatMessageType }), let value = annotation["value"] as? JSON,
let serverID = message["id"] as? UInt64, let hexEncodedSignatureData = value["sig"] as? String, let signatureVersion = value["sigver"] as? UInt64,
let body = message["text"] as? String, let user = message["user"] as? JSON, let hexEncodedPublicKey = user["username"] as? String,
let timestamp = value["timestamp"] as? UInt64 else {
print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(message).")
return nil
}
var profilePicture: LokiPublicChatMessage.ProfilePicture? = nil
let displayName = user["name"] as? String ?? NSLocalizedString("Anonymous", comment: "")
if let userAnnotations = user["annotations"] as? [JSON], let profilePictureAnnotation = userAnnotations.first(where: { $0["type"] as? String == profilePictureType }),
let profilePictureValue = profilePictureAnnotation["value"] as? JSON, let profileKeyString = profilePictureValue["profileKey"] as? String, let profileKey = Data(base64Encoded: profileKeyString), let url = profilePictureValue["url"] as? String {
profilePicture = LokiPublicChatMessage.ProfilePicture(profileKey: profileKey, url: url)
}
let lastMessageServerID = getLastMessageServerID(for: channel, on: server)
if serverID > (lastMessageServerID ?? 0) { setLastMessageServerID(for: channel, on: server, to: serverID) }
let quote: LokiPublicChatMessage.Quote?
if let quoteAsJSON = value["quote"] as? JSON, let quotedMessageTimestamp = quoteAsJSON["id"] as? UInt64, let quoteeHexEncodedPublicKey = quoteAsJSON["author"] as? String,
let quotedMessageBody = quoteAsJSON["text"] as? String {
let quotedMessageServerID = message["reply_to"] as? UInt64
quote = LokiPublicChatMessage.Quote(quotedMessageTimestamp: quotedMessageTimestamp, quoteeHexEncodedPublicKey: quoteeHexEncodedPublicKey, quotedMessageBody: quotedMessageBody,
quotedMessageServerID: quotedMessageServerID)
} else {
quote = nil
}
let signature = LokiPublicChatMessage.Signature(data: Data(hex: hexEncodedSignatureData), version: signatureVersion)
let attachmentsAsJSON = annotations.filter { $0["type"] as? String == attachmentType }
let attachments: [LokiPublicChatMessage.Attachment] = attachmentsAsJSON.compactMap { attachmentAsJSON in
guard let value = attachmentAsJSON["value"] as? JSON, let kindAsString = value["lokiType"] as? String, let kind = LokiPublicChatMessage.Attachment.Kind(rawValue: kindAsString),
let serverID = value["id"] as? UInt64, let contentType = value["contentType"] as? String, let size = value["size"] as? UInt, let url = value["url"] as? String else { return nil }
let fileName = value["fileName"] as? String ?? UUID().description
let width = value["width"] as? UInt ?? 0
let height = value["height"] as? UInt ?? 0
let flags = (value["flags"] as? UInt) ?? 0
let caption = value["caption"] as? String
let linkPreviewURL = value["linkPreviewUrl"] as? String
let linkPreviewTitle = value["linkPreviewTitle"] as? String
if kind == .linkPreview {
guard linkPreviewURL != nil && linkPreviewTitle != nil else {
print("[Loki] Ignoring public chat message with invalid link preview.")
return nil
}
}
return LokiPublicChatMessage.Attachment(kind: kind, server: server, serverID: serverID, contentType: contentType, size: size, fileName: fileName, flags: flags,
width: width, height: height, caption: caption, url: url, linkPreviewURL: linkPreviewURL, linkPreviewTitle: linkPreviewTitle)
}
let result = LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, profilePicture: profilePicture,
body: body, type: publicChatMessageType, timestamp: timestamp, quote: quote, attachments: attachments, signature: signature)
guard result.hasValidSignature() else {
print("[Loki] Ignoring public chat message with invalid signature.")
return nil
}
var existingMessageID: String? = nil
storage.dbReadConnection.read { transaction in
existingMessageID = storage.getIDForMessage(withServerID: UInt(result.serverID!), in: transaction)
}
guard existingMessageID == nil else {
print("[Loki] Ignoring duplicate public chat message.")
return nil
}
return result
}.sorted { $0.timestamp < $1.timestamp }
}
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
return try handleMessages(rawResponse: rawResponse)
}
}.handlingInvalidAuthTokenIfNeeded(for: server)
}
@ -173,6 +224,20 @@ public final class LokiPublicChatAPI : DotNetAPI {
}
public static func sendMessage(_ message: LokiPublicChatMessage, to channel: UInt64, on server: String) -> Promise<LokiPublicChatMessage> {
func handleSendMessageResult(rawResponse: Any, with displayName: String, for signedMessage: LokiPublicChatMessage) throws -> LokiPublicChatMessage {
// ISO8601DateFormatter doesn't support milliseconds before iOS 11
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
guard let json = rawResponse as? JSON, let messageAsJSON = json["data"] as? JSON, let serverID = messageAsJSON["id"] as? UInt64, let body = messageAsJSON["text"] as? String,
let dateAsString = messageAsJSON["created_at"] as? String, let date = dateFormatter.date(from: dateAsString) else {
print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
let timestamp = UInt64(date.timeIntervalSince1970) * 1000
return LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: getUserHexEncodedPublicKey(), displayName: displayName, profilePicture: signedMessage.profilePicture, body: body, type: publicChatMessageType, timestamp: timestamp, quote: signedMessage.quote, attachments: signedMessage.attachments, signature: signedMessage.signature)
}
print("[Loki] Sending message to public chat channel with ID: \(channel) on server: \(server).")
let (promise, seal) = Promise<LokiPublicChatMessage>.pending()
DispatchQueue.global(qos: .userInitiated).async { [privateKey = userKeyPair.privateKey] in
@ -184,17 +249,15 @@ public final class LokiPublicChatAPI : DotNetAPI {
let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
let displayName = userDisplayName
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
// ISO8601DateFormatter doesn't support milliseconds before iOS 11
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
guard let json = rawResponse as? JSON, let messageAsJSON = json["data"] as? JSON, let serverID = messageAsJSON["id"] as? UInt64, let body = messageAsJSON["text"] as? String,
let dateAsString = messageAsJSON["created_at"] as? String, let date = dateFormatter.date(from: dateAsString) else {
print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
return try handleSendMessageResult(rawResponse: rawResponse, with: displayName, for: signedMessage)
}
}
let timestamp = UInt64(date.timeIntervalSince1970) * 1000
return LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: getUserHexEncodedPublicKey(), displayName: displayName, profilePicture: signedMessage.profilePicture, body: body, type: publicChatMessageType, timestamp: timestamp, quote: signedMessage.quote, attachments: signedMessage.attachments, signature: signedMessage.signature)
}
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
return try handleSendMessageResult(rawResponse: rawResponse, with: displayName, for: signedMessage)
}
}.handlingInvalidAuthTokenIfNeeded(for: server)
}.done(on: DispatchQueue.global(qos: .default)) { message in
@ -208,6 +271,23 @@ public final class LokiPublicChatAPI : DotNetAPI {
// MARK: Deletion
public static func getDeletedMessageServerIDs(for channel: UInt64, on server: String) -> Promise<[UInt64]> {
func handleDeletedMessageServerIDs(rawResponse: Any) throws -> [UInt64] {
guard let json = rawResponse as? JSON, let deletions = json["data"] as? [JSON] else {
print("[Loki] Couldn't parse deleted messages for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
return deletions.flatMap { deletion in
guard let serverID = deletion["id"] as? UInt64, let messageServerID = deletion["message_id"] as? UInt64 else {
print("[Loki] Couldn't parse deleted message for public chat channel with ID: \(channel) on server: \(server) from: \(deletion).")
return nil
}
let lastDeletionServerID = getLastDeletionServerID(for: channel, on: server)
if serverID > (lastDeletionServerID ?? 0) { setLastDeletionServerID(for: channel, on: server, to: serverID) }
return messageServerID
}
}
print("[Loki] Getting deleted messages for public chat channel with ID: \(channel) on server: \(server).")
let queryParameters: String
if let lastDeletionServerID = getLastDeletionServerID(for: channel, on: server) {
@ -219,21 +299,16 @@ public final class LokiPublicChatAPI : DotNetAPI {
let url = URL(string: "\(server)/loki/v1/channel/\(channel)/deletes?\(queryParameters)")!
let request = TSRequest(url: url)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
guard let json = rawResponse as? JSON, let deletions = json["data"] as? [JSON] else {
print("[Loki] Couldn't parse deleted messages for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
return deletions.flatMap { deletion in
guard let serverID = deletion["id"] as? UInt64, let messageServerID = deletion["message_id"] as? UInt64 else {
print("[Loki] Couldn't parse deleted message for public chat channel with ID: \(channel) on server: \(server) from: \(deletion).")
return nil
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
return try handleDeletedMessageServerIDs(rawResponse: rawResponse)
}
let lastDeletionServerID = getLastDeletionServerID(for: channel, on: server)
if serverID > (lastDeletionServerID ?? 0) { setLastDeletionServerID(for: channel, on: server, to: serverID) }
return messageServerID
}
}
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
return try handleDeletedMessageServerIDs(rawResponse: rawResponse)
}
}.handlingInvalidAuthTokenIfNeeded(for: server)
}
@ -251,6 +326,13 @@ public final class LokiPublicChatAPI : DotNetAPI {
let url = URL(string: urlAsString)!
let request = TSRequest(url: url, method: "DELETE", parameters: [:])
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).done2 { result -> Void in
print("[Loki] Deleted message with ID: \(messageID) on server: \(server).")
}
}
}
return LokiFileServerProxy(for: server).perform(request).done(on: DispatchQueue.global(qos: .default)) { result -> Void in
print("[Loki] Deleted message with ID: \(messageID) on server: \(server).")
}
@ -260,6 +342,23 @@ public final class LokiPublicChatAPI : DotNetAPI {
// MARK: Display Name & Profile Picture
public static func getDisplayNames(for channel: UInt64, on server: String) -> Promise<Void> {
func handleDisplayNames(rawResponse: Any, for hexEncodedPublicKeys: Set<String>) throws {
guard let json = rawResponse as? JSON, let data = json["data"] as? [JSON] else {
print("[Loki] Couldn't parse display names for users: \(hexEncodedPublicKeys) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
try! Storage.writeSync { transaction in
data.forEach { data in
guard let user = data["user"] as? JSON, let hexEncodedPublicKey = user["username"] as? String, let rawDisplayName = user["name"] as? String else { return }
let endIndex = hexEncodedPublicKey.endIndex
let cutoffIndex = hexEncodedPublicKey.index(endIndex, offsetBy: -8)
let displayName = "\(rawDisplayName) (...\(hexEncodedPublicKey[cutoffIndex..<endIndex]))"
transaction.setObject(displayName, forKey: hexEncodedPublicKey, inCollection: "\(server).\(channel)")
}
}
}
let publicChatID = "\(server).\(channel)"
guard let hexEncodedPublicKeys = displayNameUpdatees[publicChatID] else { return Promise.value(()) }
displayNameUpdatees[publicChatID] = []
@ -268,21 +367,16 @@ public final class LokiPublicChatAPI : DotNetAPI {
let queryParameters = "ids=\(hexEncodedPublicKeys.map { "@\($0)" }.joined(separator: ","))&include_user_annotations=1"
let url = URL(string: "\(server)/users?\(queryParameters)")!
let request = TSRequest(url: url)
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
guard let json = rawResponse as? JSON, let data = json["data"] as? [JSON] else {
print("[Loki] Couldn't parse display names for users: \(hexEncodedPublicKeys) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
try! Storage.writeSync { transaction in
data.forEach { data in
guard let user = data["user"] as? JSON, let hexEncodedPublicKey = user["username"] as? String, let rawDisplayName = user["name"] as? String else { return }
let endIndex = hexEncodedPublicKey.endIndex
let cutoffIndex = hexEncodedPublicKey.index(endIndex, offsetBy: -8)
let displayName = "\(rawDisplayName) (...\(hexEncodedPublicKey[cutoffIndex..<endIndex]))"
transaction.setObject(displayName, forKey: hexEncodedPublicKey, inCollection: "\(server).\(channel)")
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
try handleDisplayNames(rawResponse: rawResponse, for: hexEncodedPublicKeys)
}
}
}
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
try handleDisplayNames(rawResponse: rawResponse, for: hexEncodedPublicKeys)
}
}.handlingInvalidAuthTokenIfNeeded(for: server)
}
@ -299,6 +393,14 @@ public final class LokiPublicChatAPI : DotNetAPI {
let url = URL(string: "\(server)/users/me")!
let request = TSRequest(url: url, method: "PATCH", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { _ in }.recover(on: DispatchQueue.global(qos: .default)) { error in
print("Couldn't update display name due to error: \(error).")
throw error
}
}
}
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { _ in }.recover(on: DispatchQueue.global(qos: .default)) { error in
print("Couldn't update display name due to error: \(error).")
throw error
@ -324,6 +426,14 @@ public final class LokiPublicChatAPI : DotNetAPI {
let url = URL(string: "\(server)/users/me")!
let request = TSRequest(url: url, method: "PATCH", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { _ in }.recover(on: DispatchQueue.global(qos: .default)) { error in
print("[Loki] Couldn't update profile picture due to error: \(error).")
throw error
}
}
}
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { _ in }.recover(on: DispatchQueue.global(qos: .default)) { error in
print("[Loki] Couldn't update profile picture due to error: \(error).")
throw error
@ -384,31 +494,43 @@ public final class LokiPublicChatAPI : DotNetAPI {
}
public static func getInfo(for channel: UInt64, on server: String) -> Promise<LokiPublicChatInfo> {
func handleInfo(rawResponse: Any) throws -> LokiPublicChatInfo {
guard let json = rawResponse as? JSON,
let data = json["data"] as? JSON,
let annotations = data["annotations"] as? [JSON],
let annotation = annotations.first,
let info = annotation["value"] as? JSON,
let displayName = info["name"] as? String,
let profilePictureURL = info["avatar"] as? String,
let countInfo = data["counts"] as? JSON,
let memberCount = countInfo["subscribers"] as? Int else {
print("[Loki] Couldn't parse info for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
let storage = OWSPrimaryStorage.shared()
try! Storage.writeSync { transaction in
storage.setUserCount(memberCount, forPublicChatWithID: "\(server).\(channel)", in: transaction)
}
let publicChatInfo = LokiPublicChatInfo(displayName: displayName, profilePictureURL: profilePictureURL, memberCount: memberCount)
updateProfileIfNeeded(for: channel, on: server, from: publicChatInfo)
return publicChatInfo
}
return attempt(maxRetryCount: maxRetryCount, recoveringOn: DispatchQueue.global(qos: .default)) {
getAuthToken(for: server).then(on: DispatchQueue.global(qos: .default)) { token -> Promise<LokiPublicChatInfo> in
let url = URL(string: "\(server)/channels/\(channel)?include_annotations=1")!
let request = TSRequest(url: url)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
return OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
return try handleInfo(rawResponse: rawResponse)
}
}
}
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
guard let json = rawResponse as? JSON,
let data = json["data"] as? JSON,
let annotations = data["annotations"] as? [JSON],
let annotation = annotations.first,
let info = annotation["value"] as? JSON,
let displayName = info["name"] as? String,
let profilePictureURL = info["avatar"] as? String,
let countInfo = data["counts"] as? JSON,
let memberCount = countInfo["subscribers"] as? Int else {
print("[Loki] Couldn't parse info for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
let storage = OWSPrimaryStorage.shared()
try! Storage.writeSync { transaction in
storage.setUserCount(memberCount, forPublicChatWithID: "\(server).\(channel)", in: transaction)
}
let publicChatInfo = LokiPublicChatInfo(displayName: displayName, profilePictureURL: profilePictureURL, memberCount: memberCount)
updateProfileIfNeeded(for: channel, on: server, from: publicChatInfo)
return publicChatInfo
return try handleInfo(rawResponse: rawResponse)
}
}.handlingInvalidAuthTokenIfNeeded(for: server)
}
@ -420,6 +542,13 @@ public final class LokiPublicChatAPI : DotNetAPI {
let url = URL(string: "\(server)/channels/\(channel)/subscribe")!
let request = TSRequest(url: url, method: "POST", parameters: [:])
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).done2 { result -> Void in
print("[Loki] Joined channel with ID: \(channel) on server: \(server).")
}
}
}
return LokiFileServerProxy(for: server).perform(request).done(on: DispatchQueue.global(qos: .default)) { result -> Void in
print("[Loki] Joined channel with ID: \(channel) on server: \(server).")
}
@ -433,6 +562,13 @@ public final class LokiPublicChatAPI : DotNetAPI {
let url = URL(string: "\(server)/channels/\(channel)/subscribe")!
let request = TSRequest(url: url, method: "DELETE", parameters: [:])
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).done2 { result -> Void in
print("[Loki] Left channel with ID: \(channel) on server: \(server).")
}
}
}
return LokiFileServerProxy(for: server).perform(request).done(on: DispatchQueue.global(qos: .default)) { result -> Void in
print("[Loki] Left channel with ID: \(channel) on server: \(server).")
}
@ -450,27 +586,44 @@ public final class LokiPublicChatAPI : DotNetAPI {
let url = URL(string: "\(server)/loki/v1/channels/\(channel)/messages/\(messageID)/report")!
let request = TSRequest(url: url, method: "POST", parameters: [:])
// Only used for the Loki Public Chat which doesn't require authentication
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { _ in}
}
}
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { _ in }
}
// MARK: Moderators
public static func getModerators(for channel: UInt64, on server: String) -> Promise<Set<String>> {
func handleModerators(rawResponse: Any) throws -> Set<String> {
guard let json = rawResponse as? JSON, let moderators = json["moderators"] as? [String] else {
print("[Loki] Couldn't parse moderators for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
let moderatorsAsSet = Set(moderators);
if self.moderators.keys.contains(server) {
self.moderators[server]![channel] = moderatorsAsSet
} else {
self.moderators[server] = [ channel : moderatorsAsSet ]
}
return moderatorsAsSet
}
return getAuthToken(for: server).then(on: DispatchQueue.global(qos: .default)) { token -> Promise<Set<String>> in
let url = URL(string: "\(server)/loki/v1/channel/\(channel)/get_moderators")!
let request = TSRequest(url: url)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
if (useOnionRequests) {
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
return try handleModerators(rawResponse: rawResponse)
}
}
}
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
guard let json = rawResponse as? JSON, let moderators = json["moderators"] as? [String] else {
print("[Loki] Couldn't parse moderators for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
throw DotNetAPIError.parsingFailed
}
let moderatorsAsSet = Set(moderators);
if self.moderators.keys.contains(server) {
self.moderators[server]![channel] = moderatorsAsSet
} else {
self.moderators[server] = [ channel : moderatorsAsSet ]
}
return moderatorsAsSet
return try handleModerators(rawResponse: rawResponse)
}
}.handlingInvalidAuthTokenIfNeeded(for: server)
}

View File

@ -56,6 +56,17 @@ public final class LokiPublicChatManager : NSObject {
return Promise(error: Error.chatCreationFailed)
}
}
if (LokiPublicChatAPI.useOnionRequests) {
return LokiPublicChatAPI.getOpenGroupServerPublicKey(on: server).then2 { publicKey in
return LokiPublicChatAPI.getAuthToken(for: server).then2 { token in
return LokiPublicChatAPI.getInfo(for: channel, on: server)
}.map2 { channelInfo -> LokiPublicChat in
guard let chat = self.addChat(server: server, channel: channel, name: channelInfo.displayName) else { throw Error.chatCreationFailed }
return chat
}
}
}
// TODO: Remove this when we use onion request totally
return LokiPublicChatAPI.getAuthToken(for: server).then2 { token in
return LokiPublicChatAPI.getInfo(for: channel, on: server)
}.map2 { channelInfo -> LokiPublicChat in