This commit is contained in:
Niels Andriesse 2020-01-24 13:59:47 +11:00
parent 8f7ba1407f
commit 8c18439893
7 changed files with 26 additions and 26 deletions

2
Pods

@ -1 +1 @@
Subproject commit 69d7254eda0f6422c98ce7a3d70a14e7a49ea1c5
Subproject commit 3a470c921a599097807df0cdbff4b2f238c91f37

View File

@ -69,11 +69,11 @@ public extension LokiAPI {
let rawResponse = intermediate.responseObject
guard let json = rawResponse as? JSON, let intermediate = json["result"] as? JSON, let rawTargets = intermediate["service_node_states"] as? [JSON] else { throw "Failed to update random snode pool from: \(rawResponse)." }
randomSnodePool = try Set(rawTargets.flatMap { rawTarget in
guard let address = rawTarget["public_ip"] as? String, let port = rawTarget["storage_port"] as? Int, let identificationKey = rawTarget["pubkey_ed25519"] as? String, let encryptionKey = rawTarget["pubkey_x25519"] as? String, address != "0.0.0.0" else {
print("Failed to update random snode pool from: \(rawTarget).")
guard let address = rawTarget["public_ip"] as? String, let port = rawTarget["storage_port"] as? Int, let idKey = rawTarget["pubkey_ed25519"] as? String, let encryptionKey = rawTarget["pubkey_x25519"] as? String, address != "0.0.0.0" else {
print("[Loki] Failed to parse target from: \(rawTarget).")
return nil
}
return LokiAPITarget(address: "https://\(address)", port: UInt16(port), publicKeys: LokiAPITarget.Keys(identification: identificationKey, encryption: encryptionKey))
return LokiAPITarget(address: "https://\(address)", port: UInt16(port), publicKeySet: LokiAPITarget.KeySet(idKey: idKey, encryptionKey: encryptionKey))
})
return randomSnodePool.randomElement()!
}.recover(on: DispatchQueue.global()) { error -> Promise<LokiAPITarget> in
@ -105,16 +105,16 @@ public extension LokiAPI {
// MARK: Parsing
private static func parseTargets(from rawResponse: Any) -> [LokiAPITarget] {
guard let json = rawResponse as? JSON, let rawSnodes = json["snodes"] as? [JSON] else {
guard let json = rawResponse as? JSON, let rawTargets = json["snodes"] as? [JSON] else {
print("[Loki] Failed to parse targets from: \(rawResponse).")
return []
}
return rawSnodes.flatMap { rawSnode in
guard let address = rawSnode["ip"] as? String, let portAsString = rawSnode["port"] as? String, let port = UInt16(portAsString), let identificationKey = rawSnode["pubkey_ed25519"] as? String, let encryptionKey = rawSnode["pubkey_x25519"] as? String, address != "0.0.0.0" else {
print("[Loki] Failed to parse target from: \(rawSnode).")
return rawTargets.flatMap { rawTarget in
guard let address = rawTarget["ip"] as? String, let portAsString = rawTarget["port"] as? String, let port = UInt16(portAsString), let idKey = rawTarget["pubkey_ed25519"] as? String, let encryptionKey = rawTarget["pubkey_x25519"] as? String, address != "0.0.0.0" else {
print("[Loki] Failed to parse target from: \(rawTarget).")
return nil
}
return LokiAPITarget(address: "https://\(address)", port: port, publicKeys: LokiAPITarget.Keys(identification: identificationKey, encryption: encryptionKey))
return LokiAPITarget(address: "https://\(address)", port: port, publicKeySet: LokiAPITarget.KeySet(idKey: idKey, encryptionKey: encryptionKey))
}
}
}

View File

@ -181,7 +181,7 @@ public final class LokiAPI : NSObject {
}
}
if let peer = LokiP2PAPI.getInfo(for: destination), (lokiMessage.isPing || peer.isOnline) {
let target = LokiAPITarget(address: peer.address, port: peer.port, publicKeys: nil)
let target = LokiAPITarget(address: peer.address, port: peer.port, publicKeySet: nil)
return Promise.value([ target ]).mapValues { sendLokiMessage(lokiMessage, to: $0) }.map { Set($0) }.retryingIfNeeded(maxRetryCount: maxRetryCount).get { _ in
LokiP2PAPI.markOnline(destination)
onP2PSuccess()

View File

@ -2,7 +2,7 @@
internal final class LokiAPITarget : NSObject, NSCoding {
internal let address: String
internal let port: UInt16
internal let publicKeys: Keys?
internal let publicKeySet: KeySet?
// MARK: Types
internal enum Method : String {
@ -13,26 +13,26 @@ internal final class LokiAPITarget : NSObject, NSCoding {
case sendMessage = "store"
}
internal struct Keys {
let identification: String
let encryption: String
internal struct KeySet {
let idKey: String
let encryptionKey: String
}
// MARK: Initialization
internal init(address: String, port: UInt16, publicKeys: Keys?) {
internal init(address: String, port: UInt16, publicKeySet: KeySet?) {
self.address = address
self.port = port
self.publicKeys = publicKeys
self.publicKeySet = publicKeySet
}
// MARK: Coding
internal init?(coder: NSCoder) {
address = coder.decodeObject(forKey: "address") as! String
port = coder.decodeObject(forKey: "port") as! UInt16
if let identificationKey = coder.decodeObject(forKey: "identificationKey") as? String, let encryptionKey = coder.decodeObject(forKey: "encryptionKey") as? String {
publicKeys = Keys(identification: identificationKey, encryption: encryptionKey)
if let idKey = coder.decodeObject(forKey: "idKey") as? String, let encryptionKey = coder.decodeObject(forKey: "encryptionKey") as? String {
publicKeySet = KeySet(idKey: idKey, encryptionKey: encryptionKey)
} else {
publicKeys = nil
publicKeySet = nil
}
super.init()
}
@ -40,9 +40,9 @@ internal final class LokiAPITarget : NSObject, NSCoding {
internal func encode(with coder: NSCoder) {
coder.encode(address, forKey: "address")
coder.encode(port, forKey: "port")
if let keys = publicKeys {
coder.encode(keys.identification, forKey: "identificationKey")
coder.encode(keys.encryption, forKey: "encryptionKey")
if let keySet = publicKeySet {
coder.encode(keySet.idKey, forKey: "idKey")
coder.encode(keySet.encryptionKey, forKey: "encryptionKey")
}
}

View File

@ -33,7 +33,7 @@ public class LokiP2PAPI : NSObject {
/// - Parameter url: The url to our local server
@objc public static func setOurP2PAddress(url: URL) {
guard let scheme = url.scheme, let host = url.host, let port = url.port else { return }
let target = LokiAPITarget(address: "\(scheme)://\(host)", port: UInt16(port), publicKeys: nil)
let target = LokiAPITarget(address: "\(scheme)://\(host)", port: UInt16(port), publicKeySet: nil)
ourP2PAddress = target
}

View File

@ -41,11 +41,11 @@ internal class LokiSnodeProxy: LokiHttpClient {
}
override func perform(_ request: TSRequest, withCompletionQueue queue: DispatchQueue = DispatchQueue.main) -> Promise<Any> {
guard let targetHexEncodedPublicKeys = target.publicKeys else {
guard let targetHexEncodedPublicKeys = target.publicKeySet else {
return Promise(error: Error.invalidPublicKeys)
}
guard let symmetricKey = try? Curve25519.generateSharedSecret(fromPublicKey: Data(hex: targetHexEncodedPublicKeys.encryption), privateKey: keyPair.privateKey) else {
guard let symmetricKey = try? Curve25519.generateSharedSecret(fromPublicKey: Data(hex: targetHexEncodedPublicKeys.encryptionKey), privateKey: keyPair.privateKey) else {
return Promise(error: Error.failedToEncryptRequest)
}
@ -62,7 +62,7 @@ internal class LokiSnodeProxy: LokiHttpClient {
let ivAndCipherText = try DiffieHellman.encrypt(proxyParams, using: symmetricKey)
let headers = [
"X-Sender-Public-Key" : self.keyPair.publicKey.hexadecimalString,
"X-Target-Snode-Key" : targetHexEncodedPublicKeys.identification
"X-Target-Snode-Key" : targetHexEncodedPublicKeys.idKey
]
return self.post(url: url, body: ivAndCipherText, headers: headers, timeoutInterval: request.timeoutInterval)
}.map { response in