Fix crash when you have invalid relays in your relay list
Changelog-Fixed: Fix crash when you have invalid relays in your relay list
This commit is contained in:
@@ -694,7 +694,7 @@ func generate_private_keypair(our_privkey: String, id: String, created_at: Int64
|
||||
func make_zap_request_event(keypair: FullKeypair, content: String, relays: [RelayDescriptor], target: ZapTarget, zap_type: ZapType) -> NostrEvent? {
|
||||
var tags = zap_target_to_tags(target)
|
||||
var relay_tag = ["relays"]
|
||||
relay_tag.append(contentsOf: relays.map { $0.url.absoluteString })
|
||||
relay_tag.append(contentsOf: relays.map { $0.url.id })
|
||||
tags.append(relay_tag)
|
||||
|
||||
var kp = keypair
|
||||
|
||||
@@ -14,8 +14,8 @@ public struct RelayInfo: Codable {
|
||||
static let rw = RelayInfo(read: true, write: true)
|
||||
}
|
||||
|
||||
public struct RelayDescriptor: Codable {
|
||||
public let url: URL
|
||||
public struct RelayDescriptor {
|
||||
public let url: RelayURL
|
||||
public let info: RelayInfo
|
||||
}
|
||||
|
||||
@@ -79,6 +79,6 @@ enum RelayError: Error {
|
||||
case RelayNotFound
|
||||
}
|
||||
|
||||
func get_relay_id(_ url: URL) -> String {
|
||||
return url.absoluteString
|
||||
func get_relay_id(_ url: RelayURL) -> String {
|
||||
return url.url.absoluteString
|
||||
}
|
||||
|
||||
@@ -13,18 +13,42 @@ enum NostrConnectionEvent {
|
||||
case nostr_event(NostrResponse)
|
||||
}
|
||||
|
||||
public struct RelayURL: Hashable {
|
||||
private(set) var url: URL
|
||||
|
||||
var id: String {
|
||||
return url.absoluteString
|
||||
}
|
||||
|
||||
init?(_ str: String) {
|
||||
guard let url = URL(string: str) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let scheme = url.scheme else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard scheme == "ws" || scheme == "wss" else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.url = url
|
||||
}
|
||||
}
|
||||
|
||||
final class RelayConnection {
|
||||
private(set) var isConnected = false
|
||||
private(set) var isConnecting = false
|
||||
|
||||
private(set) var last_connection_attempt: TimeInterval = 0
|
||||
private lazy var socket = WebSocket(url)
|
||||
private lazy var socket = WebSocket(url.url)
|
||||
private var subscriptionToken: AnyCancellable?
|
||||
|
||||
private var handleEvent: (NostrConnectionEvent) -> ()
|
||||
private let url: URL
|
||||
private let url: RelayURL
|
||||
|
||||
init(url: URL, handleEvent: @escaping (NostrConnectionEvent) -> ()) {
|
||||
init(url: RelayURL, handleEvent: @escaping (NostrConnectionEvent) -> ()) {
|
||||
self.url = url
|
||||
self.handleEvent = handleEvent
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ class RelayPool {
|
||||
}
|
||||
}
|
||||
|
||||
func add_relay(_ url: URL, info: RelayInfo) throws {
|
||||
func add_relay(_ url: RelayURL, info: RelayInfo) throws {
|
||||
let relay_id = get_relay_id(url)
|
||||
if get_relay(relay_id) != nil {
|
||||
throw RelayError.RelayAlreadyExists
|
||||
@@ -127,7 +127,7 @@ class RelayPool {
|
||||
let is_connecting = c.isConnecting
|
||||
|
||||
if is_connecting && (Date.now.timeIntervalSince1970 - c.last_connection_attempt) > 5 {
|
||||
print("stale connection detected (\(relay.descriptor.url.absoluteString)). retrying...")
|
||||
print("stale connection detected (\(relay.descriptor.url.url.absoluteString)). retrying...")
|
||||
relay.connection.reconnect()
|
||||
} else if relay.is_broken || is_connecting || c.isConnected {
|
||||
continue
|
||||
@@ -271,8 +271,10 @@ class RelayPool {
|
||||
}
|
||||
|
||||
func add_rw_relay(_ pool: RelayPool, _ url: String) {
|
||||
let url_ = URL(string: url)!
|
||||
try? pool.add_relay(url_, info: RelayInfo.rw)
|
||||
guard let url = RelayURL(url) else {
|
||||
return
|
||||
}
|
||||
try? pool.add_relay(url, info: RelayInfo.rw)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user