From b34e4923a83238970daf6478dce816045567b651 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Fri, 18 Oct 2024 09:41:23 -0700 Subject: [PATCH] Reset auth when connection closes --- packages/net/src/ConnectionAuth.ts | 11 ++++++++++- packages/util/src/Relay.ts | 6 +++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/net/src/ConnectionAuth.ts b/packages/net/src/ConnectionAuth.ts index aeb8daf..3c7a137 100644 --- a/packages/net/src/ConnectionAuth.ts +++ b/packages/net/src/ConnectionAuth.ts @@ -37,6 +37,7 @@ export class ConnectionAuth { constructor(readonly connection: Connection) { this.connection.on('receive', this.#onReceive) + this.connection.on('close', this.#onClose) } #onReceive = (connection: Connection, message: SocketMessage) => { @@ -63,6 +64,13 @@ export class ConnectionAuth { } } + #onClose = (connection: Connection) => { + this.challenge = undefined + this.request = undefined + this.message = undefined + this.status = None + } + attempt = async () => { if (!this.challenge) { throw new Error("Attempted to authenticate with no challenge") @@ -124,6 +132,7 @@ export class ConnectionAuth { } destroy = () => { - this.connection.off('recieve', this.#onReceive) + this.connection.off('receive', this.#onReceive) + this.connection.off('close', this.#onClose) } } diff --git a/packages/util/src/Relay.ts b/packages/util/src/Relay.ts index a6d5d09..95e0ad6 100644 --- a/packages/util/src/Relay.ts +++ b/packages/util/src/Relay.ts @@ -29,6 +29,10 @@ export type RelayProfile = { // Utils related to bare urls export const isRelayUrl = (url: string) => { + if (!url.includes('://')) { + url = 'wss://' + url + } + try { new URL(url) } catch (e) { @@ -59,7 +63,7 @@ export const normalizeRelayUrl = (url: string) => { // Use our library to normalize url = normalizeUrl(url, {stripHash: true, stripAuthentication: false}) - // Strip the protocol since only wss works, lowercase + // Strip the protocol, lowercase url = stripProtocol(url).toLowerCase() // Urls without pathnames are supposed to have a trailing slash