Clean up isShareableRelayUrl, and allow bypassing check in router
This commit is contained in:
@@ -20,6 +20,7 @@ import {
|
|||||||
} from "@welshman/lib"
|
} from "@welshman/lib"
|
||||||
import {
|
import {
|
||||||
getFilterId,
|
getFilterId,
|
||||||
|
isRelayUrl,
|
||||||
isShareableRelayUrl,
|
isShareableRelayUrl,
|
||||||
PROFILE,
|
PROFILE,
|
||||||
RELAYS,
|
RELAYS,
|
||||||
@@ -225,6 +226,7 @@ export class Router {
|
|||||||
export type RouterScenarioOptions = {
|
export type RouterScenarioOptions = {
|
||||||
policy?: FallbackPolicy
|
policy?: FallbackPolicy
|
||||||
limit?: number
|
limit?: number
|
||||||
|
allowLocal?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class RouterScenario {
|
export class RouterScenario {
|
||||||
@@ -255,6 +257,8 @@ export class RouterScenario {
|
|||||||
|
|
||||||
limit = (limit: number) => this.clone({limit})
|
limit = (limit: number) => this.clone({limit})
|
||||||
|
|
||||||
|
allowLocal = (allowLocal: boolean) => this.clone({allowLocal})
|
||||||
|
|
||||||
weight = (scale: number) =>
|
weight = (scale: number) =>
|
||||||
this.update(selection => ({...selection, weight: selection.weight * scale}))
|
this.update(selection => ({...selection, weight: selection.weight * scale}))
|
||||||
|
|
||||||
@@ -269,11 +273,9 @@ export class RouterScenario {
|
|||||||
|
|
||||||
for (const {weight, relays} of this.selections) {
|
for (const {weight, relays} of this.selections) {
|
||||||
for (const relay of relays) {
|
for (const relay of relays) {
|
||||||
if (!isShareableRelayUrl(relay)) {
|
if (this.options.allowLocal ? isRelayUrl(relay) : isShareableRelayUrl(relay)) {
|
||||||
continue
|
relayWeights.set(relay, add(weight, relayWeights.get(relay)))
|
||||||
}
|
}
|
||||||
|
|
||||||
relayWeights.set(relay, add(weight, relayWeights.get(relay)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {parseJson, nthEq} from "@welshman/lib"
|
import {parseJson, nthEq} from "@welshman/lib"
|
||||||
import {Address} from "./Address.js"
|
import {Address} from "./Address.js"
|
||||||
import {uniqTags} from "./Tags.js"
|
import {uniqTags} from "./Tags.js"
|
||||||
import {isShareableRelayUrl} from "./Relay.js"
|
import {isRelayUrl} from "./Relay.js"
|
||||||
import {Encryptable, DecryptedEvent} from "./Encryptable.js"
|
import {Encryptable, DecryptedEvent} from "./Encryptable.js"
|
||||||
import type {EncryptableUpdates} 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] === "e") return tag[1]?.length === 64
|
||||||
if (tag[0] === "a") return Address.isAddress(tag[1] || "")
|
if (tag[0] === "a") return Address.isAddress(tag[1] || "")
|
||||||
if (tag[0] === "t") return tag[1]?.length > 0
|
if (tag[0] === "t") return tag[1]?.length > 0
|
||||||
if (tag[0] === "r") return isShareableRelayUrl(tag[1])
|
if (tag[0] === "r") return isRelayUrl(tag[1])
|
||||||
if (tag[0] === "relay") return isShareableRelayUrl(tag[1])
|
if (tag[0] === "relay") return isRelayUrl(tag[1])
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-13
@@ -35,6 +35,10 @@ export const isRelayUrl = (url: string) => {
|
|||||||
url = "wss://" + url
|
url = "wss://" + url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!url.match(/^wss?:\/\//)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
new URL(url)
|
new URL(url)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -44,20 +48,16 @@ export const isRelayUrl = (url: string) => {
|
|||||||
return true
|
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) =>
|
export const isShareableRelayUrl = (url: string) =>
|
||||||
Boolean(
|
Boolean(isRelayUrl(url) && !isLocalUrl(url) && !isLocalhostUrl(url))
|
||||||
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/),
|
|
||||||
)
|
|
||||||
|
|
||||||
export const normalizeRelayUrl = (url: string) => {
|
export const normalizeRelayUrl = (url: string) => {
|
||||||
const prefix = url.match(/^wss?:\/\//)?.[0] || "wss://"
|
const prefix = url.match(/^wss?:\/\//)?.[0] || "wss://"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import {uniq, uniqBy, mapVals, nth, nthEq, ensurePlural} from "@welshman/lib"
|
import {uniqBy, mapVals, nth, nthEq, ensurePlural} from "@welshman/lib"
|
||||||
import {isRelayUrl, isShareableRelayUrl} from "./Relay.js"
|
import {isRelayUrl} from "./Relay.js"
|
||||||
import {Address} from "./Address.js"
|
import {Address} from "./Address.js"
|
||||||
|
|
||||||
export const getTags = (types: string | string[], tags: string[][]) => {
|
export const getTags = (types: string | string[], tags: string[][]) => {
|
||||||
@@ -93,9 +93,6 @@ export const getAncestorTags = (tags: string[][]) => {
|
|||||||
export const getAncestorTagValues = (tags: string[][]) =>
|
export const getAncestorTagValues = (tags: string[][]) =>
|
||||||
mapVals(tags => tags.map(nth(1)), getAncestorTags(tags))
|
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 uniqTags = (tags: string[][]) => uniqBy(t => t.join(":"), tags)
|
||||||
|
|
||||||
export const tagsFromIMeta = (imeta: string[]) => imeta.map((m: string) => m.split(" "))
|
export const tagsFromIMeta = (imeta: string[]) => imeta.map((m: string) => m.split(" "))
|
||||||
|
|||||||
Reference in New Issue
Block a user