Clean up isShareableRelayUrl, and allow bypassing check in router

This commit is contained in:
Jon Staab
2025-01-06 12:16:37 -08:00
parent aab3974879
commit 01dd28552d
4 changed files with 24 additions and 25 deletions
+6 -4
View File
@@ -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)))
}
}
+3 -3
View File
@@ -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
}
+13 -13
View File
@@ -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://"
+2 -5
View File
@@ -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(" "))