Normalize tor urls to use ws://
This commit is contained in:
@@ -231,6 +231,7 @@ export type RouterScenarioOptions = {
|
|||||||
limit?: number
|
limit?: number
|
||||||
allowLocal?: boolean
|
allowLocal?: boolean
|
||||||
allowOnion?: boolean
|
allowOnion?: boolean
|
||||||
|
allowInsecure?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class RouterScenario {
|
export class RouterScenario {
|
||||||
@@ -265,6 +266,8 @@ export class RouterScenario {
|
|||||||
|
|
||||||
allowOnion = (allowOnion: boolean) => this.clone({allowOnion})
|
allowOnion = (allowOnion: boolean) => this.clone({allowOnion})
|
||||||
|
|
||||||
|
allowInsecure = (allowInsecure: boolean) => this.clone({allowInsecure})
|
||||||
|
|
||||||
weight = (scale: number) =>
|
weight = (scale: number) =>
|
||||||
this.update(selection => ({...selection, weight: selection.weight * scale}))
|
this.update(selection => ({...selection, weight: selection.weight * scale}))
|
||||||
|
|
||||||
@@ -276,12 +279,14 @@ export class RouterScenario {
|
|||||||
const limit = this.getLimit()
|
const limit = this.getLimit()
|
||||||
const fallbackPolicy = this.getPolicy()
|
const fallbackPolicy = this.getPolicy()
|
||||||
const relayWeights = new Map<string, number>()
|
const relayWeights = new Map<string, number>()
|
||||||
|
const {allowOnion, allowLocal, allowInsecure} = this.options
|
||||||
|
|
||||||
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 (!isRelayUrl(relay)) continue
|
if (!isRelayUrl(relay)) continue
|
||||||
if (!this.options.allowOnion && isOnionUrl(relay)) continue
|
if (!allowOnion && isOnionUrl(relay)) continue
|
||||||
if (!this.options.allowLocal && isLocalUrl(relay)) continue
|
if (!allowLocal && isLocalUrl(relay)) continue
|
||||||
|
if (!allowInsecure && relay.startsWith("ws://") && !isOnionUrl(relay)) continue
|
||||||
|
|
||||||
relayWeights.set(relay, add(weight, relayWeights.get(relay)))
|
relayWeights.set(relay, add(weight, relayWeights.get(relay)))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1102,7 +1102,9 @@ export const fetchJson = async (url: string, opts: FetchOpts = {}) => {
|
|||||||
opts.headers = {}
|
opts.headers = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.headers["Accept"] = "application/json"
|
if (!opts.headers["Accept"]) {
|
||||||
|
opts.headers["Accept"] = "application/json"
|
||||||
|
}
|
||||||
|
|
||||||
const res = await fetch(url, opts as RequestInit)
|
const res = await fetch(url, opts as RequestInit)
|
||||||
const json = await res.json()
|
const json = await res.json()
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@welshman/util",
|
"name": "@welshman/util",
|
||||||
"version": "0.0.56",
|
"version": "0.0.57",
|
||||||
"author": "hodlbod",
|
"author": "hodlbod",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"description": "A collection of nostr-related utilities.",
|
"description": "A collection of nostr-related utilities.",
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ export const isIPAddress = (url: string) => Boolean(url.match(/\d+\.\d+\.\d+\.\d
|
|||||||
export const isShareableRelayUrl = (url: string) => Boolean(isRelayUrl(url) && !isLocalUrl(url))
|
export const isShareableRelayUrl = (url: string) => Boolean(isRelayUrl(url) && !isLocalUrl(url))
|
||||||
|
|
||||||
export const normalizeRelayUrl = (url: string) => {
|
export const normalizeRelayUrl = (url: string) => {
|
||||||
const prefix = url.match(/^wss?:\/\//)?.[0] || "wss://"
|
const prefix = url.match(/^wss?:\/\//)?.[0] || (isOnionUrl(url) ? "ws://" : "wss://")
|
||||||
|
|
||||||
// Use our library to normalize
|
// Use our library to normalize
|
||||||
url = normalizeUrl(url, {stripHash: true, stripAuthentication: false})
|
url = normalizeUrl(url, {stripHash: true, stripAuthentication: false})
|
||||||
|
|||||||
Reference in New Issue
Block a user