From 9bcf4c1c852cbce5857eaa5201736514afd24018 Mon Sep 17 00:00:00 2001 From: koalasat Date: Fri, 8 Nov 2024 13:04:57 +0100 Subject: [PATCH] Randomize nostr relay connection --- frontend/src/services/RoboPool/index.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/frontend/src/services/RoboPool/index.ts b/frontend/src/services/RoboPool/index.ts index 34173304..ee423d94 100644 --- a/frontend/src/services/RoboPool/index.ts +++ b/frontend/src/services/RoboPool/index.ts @@ -10,7 +10,9 @@ interface RoboPoolEvents { class RoboPool { constructor(settings: Settings, origin: string) { this.network = settings.network ?? 'mainnet'; - this.relays = Object.values(defaultFederation) + + this.relays = []; + const federationRelays = Object.values(defaultFederation) .map((coord) => { const url: string = coord[this.network][settings.selfhostedClient ? 'onion' : origin]; @@ -19,6 +21,19 @@ class RoboPool { return `ws://${url.replace(/^https?:\/\//, '')}/nostr`; }) .filter((item) => item !== undefined); + if (settings.host) { + const hostNostr = `ws://${settings.host.replace(/^https?:\/\//, '')}/nostr`; + if (federationRelays.includes(hostNostr)) { + this.relays.push(hostNostr); + } + } + while (this.relays.length < 3) { + const randomRelay = + federationRelays[Math.floor(Math.random() * Object.keys(federationRelays).length)]; + if (!this.relays.includes(randomRelay)) { + this.relays.push(randomRelay); + } + } } public relays: string[];