From 01dd28552d7226f5ceec9580de788ceb26c8ad2e Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Mon, 6 Jan 2025 12:16:37 -0800 Subject: [PATCH] Clean up isShareableRelayUrl, and allow bypassing check in router --- packages/app/src/router.ts | 10 ++++++---- packages/util/src/List.ts | 6 +++--- packages/util/src/Relay.ts | 26 +++++++++++++------------- packages/util/src/Tags.ts | 7 ++----- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/packages/app/src/router.ts b/packages/app/src/router.ts index dcf6f3e..ad25b3b 100644 --- a/packages/app/src/router.ts +++ b/packages/app/src/router.ts @@ -20,6 +20,7 @@ import { } from "@welshman/lib" import { getFilterId, + isRelayUrl, isShareableRelayUrl, PROFILE, RELAYS, @@ -225,6 +226,7 @@ export class Router { export type RouterScenarioOptions = { policy?: FallbackPolicy limit?: number + allowLocal?: boolean } export class RouterScenario { @@ -255,6 +257,8 @@ export class RouterScenario { limit = (limit: number) => this.clone({limit}) + allowLocal = (allowLocal: boolean) => this.clone({allowLocal}) + weight = (scale: number) => this.update(selection => ({...selection, weight: selection.weight * scale})) @@ -269,11 +273,9 @@ export class RouterScenario { for (const {weight, relays} of this.selections) { for (const relay of relays) { - if (!isShareableRelayUrl(relay)) { - continue + if (this.options.allowLocal ? isRelayUrl(relay) : isShareableRelayUrl(relay)) { + relayWeights.set(relay, add(weight, relayWeights.get(relay))) } - - relayWeights.set(relay, add(weight, relayWeights.get(relay))) } } diff --git a/packages/util/src/List.ts b/packages/util/src/List.ts index 26554e6..075e486 100644 --- a/packages/util/src/List.ts +++ b/packages/util/src/List.ts @@ -1,7 +1,7 @@ import {parseJson, nthEq} from "@welshman/lib" import {Address} from "./Address.js" import {uniqTags} from "./Tags.js" -import {isShareableRelayUrl} from "./Relay.js" +import {isRelayUrl} from "./Relay.js" import {Encryptable, DecryptedEvent} from "./Encryptable.js" import type {EncryptableUpdates} from "./Encryptable.js" @@ -30,8 +30,8 @@ const isValidTag = (tag: string[]) => { if (tag[0] === "e") return tag[1]?.length === 64 if (tag[0] === "a") return Address.isAddress(tag[1] || "") if (tag[0] === "t") return tag[1]?.length > 0 - if (tag[0] === "r") return isShareableRelayUrl(tag[1]) - if (tag[0] === "relay") return isShareableRelayUrl(tag[1]) + if (tag[0] === "r") return isRelayUrl(tag[1]) + if (tag[0] === "relay") return isRelayUrl(tag[1]) return true } diff --git a/packages/util/src/Relay.ts b/packages/util/src/Relay.ts index 8d047d5..263965a 100644 --- a/packages/util/src/Relay.ts +++ b/packages/util/src/Relay.ts @@ -35,6 +35,10 @@ export const isRelayUrl = (url: string) => { url = "wss://" + url } + if (!url.match(/^wss?:\/\//)) { + return false + } + try { new URL(url) } catch (e) { @@ -44,20 +48,16 @@ export const isRelayUrl = (url: string) => { return true } +export const isOnionUrl = (url: string) => Boolean(stripProtocol(url).match(/^[a-z2-7]{56}.onion$/)) + +export const isLocalhostUrl = (url: string) => Boolean(stripProtocol(url).match(/^localhost:/)) + +export const isLocalUrl = (url: string) => Boolean(url.match(/\.local(:[\d]+)?\/?$/)) + +export const isIPAddress = (url: string) => Boolean(url.match(/\d+\.\d+\.\d+\.\d+/)) + export const isShareableRelayUrl = (url: string) => - Boolean( - isRelayUrl(url) && - // Is it actually a websocket url and has a dot - url.match(/^wss:\/\/.+\..+/) && - // Don't match stuff with a port number - !url.slice(6).match(/:\d+/) && - // Don't match stuff with a numeric tld - !url.slice(6).match(/\.\d+\b/) && - // Don't match raw ip addresses - !url.slice(6).match(/\d+\.\d+\.\d+\.\d+/) && - // Skip nostr.wine's virtual relays - !url.slice(6).match(/\/npub/), - ) + Boolean(isRelayUrl(url) && !isLocalUrl(url) && !isLocalhostUrl(url)) export const normalizeRelayUrl = (url: string) => { const prefix = url.match(/^wss?:\/\//)?.[0] || "wss://" diff --git a/packages/util/src/Tags.ts b/packages/util/src/Tags.ts index 4d0be08..cd0f462 100644 --- a/packages/util/src/Tags.ts +++ b/packages/util/src/Tags.ts @@ -1,5 +1,5 @@ -import {uniq, uniqBy, mapVals, nth, nthEq, ensurePlural} from "@welshman/lib" -import {isRelayUrl, isShareableRelayUrl} from "./Relay.js" +import {uniqBy, mapVals, nth, nthEq, ensurePlural} from "@welshman/lib" +import {isRelayUrl} from "./Relay.js" import {Address} from "./Address.js" export const getTags = (types: string | string[], tags: string[][]) => { @@ -93,9 +93,6 @@ export const getAncestorTags = (tags: string[][]) => { export const getAncestorTagValues = (tags: string[][]) => mapVals(tags => tags.map(nth(1)), getAncestorTags(tags)) -export const getRelayHints = (tags: string[][]) => - uniq(tags.flatMap(t => t.slice(2).filter(isShareableRelayUrl))) - export const uniqTags = (tags: string[][]) => uniqBy(t => t.join(":"), tags) export const tagsFromIMeta = (imeta: string[]) => imeta.map((m: string) => m.split(" "))