Re-work connection auth
This commit is contained in:
@@ -2,10 +2,10 @@ import {partition} from "@welshman/lib"
|
||||
import {defaultOptimizeSubscriptions, getDefaultNetContext as originalGetDefaultNetContext} from "@welshman/net"
|
||||
import type {Subscription, RelaysAndFilters, NetContext} from "@welshman/net"
|
||||
import {WRAP, unionFilters, isSignedEvent, hasValidSignature} from "@welshman/util"
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
import type {TrustedEvent, StampedEvent} from "@welshman/util"
|
||||
import {tracker, repository} from './core'
|
||||
import {makeRouter, getFilterSelections} from './router'
|
||||
import {onAuth, getSession} from './session'
|
||||
import {getSession, signer} from './session'
|
||||
import type {Router} from './router'
|
||||
import {loadProfile} from './profiles'
|
||||
|
||||
@@ -20,7 +20,7 @@ export type AppContext = {
|
||||
|
||||
export const getDefaultNetContext = (overrides: Partial<NetContext> = {}) => ({
|
||||
...originalGetDefaultNetContext(),
|
||||
onAuth: onAuth,
|
||||
signEvent: (event: StampedEvent) => signer.get()?.sign(event),
|
||||
onEvent: (url: string, event: TrustedEvent) => {
|
||||
tracker.track(event.id, url)
|
||||
repository.publish(event)
|
||||
|
||||
@@ -3,7 +3,7 @@ import {withGetter} from '@welshman/store'
|
||||
import {ctx, groupBy, indexBy, batch, now, uniq, batcher, postJson} from '@welshman/lib'
|
||||
import type {RelayProfile} from "@welshman/util"
|
||||
import {normalizeRelayUrl, displayRelayUrl, displayRelayProfile} from "@welshman/util"
|
||||
import {AuthStatus, asMessage, type Connection, type SocketMessage} from '@welshman/net'
|
||||
import {asMessage, type Connection, type SocketMessage} from '@welshman/net'
|
||||
import {collection} from './collection'
|
||||
|
||||
export type RelayStats = {
|
||||
@@ -13,7 +13,6 @@ export type RelayStats = {
|
||||
publish_count: number
|
||||
connect_count: number
|
||||
recent_errors: number[]
|
||||
last_auth_status: AuthStatus
|
||||
}
|
||||
|
||||
// Relays
|
||||
@@ -25,7 +24,6 @@ export const makeRelayStats = (): RelayStats => ({
|
||||
publish_count: 0,
|
||||
connect_count: 0,
|
||||
recent_errors: [],
|
||||
last_auth_status: AuthStatus.Pending,
|
||||
})
|
||||
|
||||
export type Relay = {
|
||||
@@ -136,14 +134,6 @@ const onConnectionReceive = ({url}: Connection, socketMessage: SocketMessage) =>
|
||||
|
||||
if (verb === 'EVENT') {
|
||||
updateRelayStats([url, stats => ++stats.event_count])
|
||||
} else if (verb === 'OK') {
|
||||
updateRelayStats([url, stats => {
|
||||
stats.last_auth_status = AuthStatus.Ok
|
||||
}])
|
||||
} else if (verb === 'AUTH') {
|
||||
updateRelayStats([url, stats => {
|
||||
stats.last_auth_status = AuthStatus.Unauthorized
|
||||
}])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
PROFILE, RELAYS, INBOX_RELAYS, FOLLOWS, LOCAL_RELAY_URL, WRAP,
|
||||
} from '@welshman/util'
|
||||
import type {TrustedEvent, Filter} from '@welshman/util'
|
||||
import {ConnectionStatus} from '@welshman/net'
|
||||
import {ConnectionStatus, AuthStatus} from '@welshman/net'
|
||||
import type {RelaysAndFilters} from '@welshman/net'
|
||||
import {pubkey} from './session'
|
||||
import {relaySelectionsByPubkey, inboxRelaySelectionsByPubkey, getReadRelayUrls, getWriteRelayUrls, getRelayUrls} from './relaySelections'
|
||||
@@ -415,15 +415,21 @@ export const getRelayQuality = (url: string) => {
|
||||
return Math.max(0, Math.min(0.5, (now() - oneMinute - lastFault) / oneHour))
|
||||
}
|
||||
|
||||
return switcher(connection.meta.getStatus(), {
|
||||
[ConnectionStatus.Unauthorized]: 0.5,
|
||||
[ConnectionStatus.Forbidden]: 0,
|
||||
const authScore = switcher(connection.auth.status, {
|
||||
[AuthStatus.Forbidden]: 0,
|
||||
[AuthStatus.Ok]: 1,
|
||||
default: 0.5,
|
||||
})
|
||||
|
||||
const connectionScore = switcher(connection.meta.getStatus(), {
|
||||
[ConnectionStatus.Error]: 0,
|
||||
[ConnectionStatus.Closed]: 0.6,
|
||||
[ConnectionStatus.Slow]: 0.5,
|
||||
[ConnectionStatus.Ok]: 1,
|
||||
default: clamp([0.5, 1], connect_count / 1000),
|
||||
})
|
||||
|
||||
return authScore * connectionScore
|
||||
}
|
||||
|
||||
export const getPubkeyRelays = (pubkey: string, mode?: string) => {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import {derived} from "svelte/store"
|
||||
import {ctx, memoize, omit, equals, assoc} from "@welshman/lib"
|
||||
import {createEvent} from "@welshman/util"
|
||||
import {memoize, omit, equals, assoc} from "@welshman/lib"
|
||||
import {withGetter, synced} from "@welshman/store"
|
||||
import {type Nip46Handler} from "@welshman/signer"
|
||||
import {Nip46Broker, Nip46Signer, Nip07Signer, Nip01Signer, Nip55Signer} from "@welshman/signer"
|
||||
@@ -60,29 +59,6 @@ export const getSigner = memoize((session: Session) => {
|
||||
|
||||
export const signer = withGetter(derived(session, getSigner))
|
||||
|
||||
export const authChallenges = new Set()
|
||||
|
||||
export const onAuth = async (url: string, challenge: string) => {
|
||||
if (authChallenges.has(challenge) || !signer.get()) {
|
||||
return
|
||||
}
|
||||
|
||||
authChallenges.add(challenge)
|
||||
|
||||
const event = await signer.get()!.sign(
|
||||
createEvent(22242, {
|
||||
tags: [
|
||||
["relay", url],
|
||||
["challenge", challenge],
|
||||
],
|
||||
}),
|
||||
)
|
||||
|
||||
ctx.net.pool.get(url).send(["AUTH", event])
|
||||
|
||||
return event
|
||||
}
|
||||
|
||||
export const nip44EncryptToSelf = (payload: string) => {
|
||||
const $pubkey = pubkey.get()
|
||||
const $signer = signer.get()
|
||||
|
||||
Reference in New Issue
Block a user