Added public key to LokiAPITarget

pull/79/head
Mikunj 5 years ago
parent b37378a2e9
commit 920520077d

@ -58,7 +58,8 @@ public extension LokiAPI {
"limit" : 24, "limit" : 24,
"fields" : [ "fields" : [
"public_ip" : true, "public_ip" : true,
"storage_port" : true "storage_port" : true,
"pubkey_ed25519": true
] ]
] ]
]) ])
@ -67,11 +68,11 @@ public extension LokiAPI {
let rawResponse = intermediate.responseObject 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)." } 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 randomSnodePool = try Set(rawTargets.flatMap { rawTarget in
guard let address = rawTarget["public_ip"] as? String, let port = rawTarget["storage_port"] as? Int, address != "0.0.0.0" else { guard let address = rawTarget["public_ip"] as? String, let port = rawTarget["storage_port"] as? Int, let publicKey = rawTarget["pubkey_ed25519"] as? String, address != "0.0.0.0" else {
print("Failed to update random snode pool from: \(rawTarget).") print("Failed to update random snode pool from: \(rawTarget).")
return nil return nil
} }
return LokiAPITarget(address: "https://\(address)", port: UInt16(port)) return LokiAPITarget(address: "https://\(address)", port: UInt16(port), publicKey: publicKey)
}) })
return randomSnodePool.randomElement()! return randomSnodePool.randomElement()!
}.recover(on: DispatchQueue.global()) { error -> Promise<LokiAPITarget> in }.recover(on: DispatchQueue.global()) { error -> Promise<LokiAPITarget> in
@ -108,11 +109,11 @@ public extension LokiAPI {
return [] return []
} }
return rawSnodes.flatMap { rawSnode in return rawSnodes.flatMap { rawSnode in
guard let address = rawSnode["ip"] as? String, let portAsString = rawSnode["port"] as? String, let port = UInt16(portAsString), address != "0.0.0.0" else { guard let address = rawSnode["ip"] as? String, let portAsString = rawSnode["port"] as? String, let port = UInt16(portAsString), let publicKey = rawSnode["pubkey_ed25519"] as? String, address != "0.0.0.0" else {
print("[Loki] Failed to parse target from: \(rawSnode).") print("[Loki] Failed to parse target from: \(rawSnode).")
return nil return nil
} }
return LokiAPITarget(address: "https://\(address)", port: port) return LokiAPITarget(address: "https://\(address)", port: port, publicKey: publicKey)
} }
} }
} }

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

@ -2,6 +2,7 @@
internal final class LokiAPITarget : NSObject, NSCoding { internal final class LokiAPITarget : NSObject, NSCoding {
internal let address: String internal let address: String
internal let port: UInt16 internal let port: UInt16
internal let publicKey: String?
// MARK: Types // MARK: Types
internal enum Method : String { internal enum Method : String {
@ -13,21 +14,24 @@ internal final class LokiAPITarget : NSObject, NSCoding {
} }
// MARK: Initialization // MARK: Initialization
internal init(address: String, port: UInt16) { internal init(address: String, port: UInt16, publicKey: String?) {
self.address = address self.address = address
self.port = port self.port = port
self.publicKey = publicKey
} }
// MARK: Coding // MARK: Coding
internal init?(coder: NSCoder) { internal init?(coder: NSCoder) {
address = coder.decodeObject(forKey: "address") as! String address = coder.decodeObject(forKey: "address") as! String
port = coder.decodeObject(forKey: "port") as! UInt16 port = coder.decodeObject(forKey: "port") as! UInt16
publicKey = coder.decodeObject(forKey: "publicKey") as? String
super.init() super.init()
} }
internal func encode(with coder: NSCoder) { internal func encode(with coder: NSCoder) {
coder.encode(address, forKey: "address") coder.encode(address, forKey: "address")
coder.encode(port, forKey: "port") coder.encode(port, forKey: "port")
coder.encode(publicKey, forKey: "publicKey")
} }
// MARK: Equality // MARK: Equality

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

Loading…
Cancel
Save