Skip to content

Commit cca2dc1

Browse files
committed
v7.9.36
1 parent da491f4 commit cca2dc1

5 files changed

Lines changed: 36 additions & 34 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nosflare",
3-
"version": "7.9.35",
3+
"version": "7.9.36",
44
"main": "worker.js",
55
"type": "module",
66
"scripts": {

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const relayInfo: RelayInfo = {
2323
contact: "lux@fed.wtf",
2424
supported_nips: [1, 2, 4, 5, 9, 11, 12, 15, 16, 17, 20, 22, 33, 40, 42],
2525
software: "https://github.com/Spl0itable/nosflare",
26-
version: "7.9.35",
26+
version: "7.9.36",
2727
icon: "https://raw.githubusercontent.com/Spl0itable/nosflare/main/images/flare.png",
2828

2929
// Optional fields (uncomment as needed):

src/durable-object.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,9 @@ export class RelayWebSocket implements DurableObject {
604604
host: attachment.host,
605605
// NIP-42: Generate new challenge after hibernation (old one is lost)
606606
challenge: AUTH_REQUIRED ? this.generateAuthChallenge() : undefined,
607-
authenticatedPubkeys: new Set()
607+
authenticatedPubkeys: new Set(),
608+
// Restore payment status from attachment (survives hibernation)
609+
hasPaid: attachment.hasPaid
608610
};
609611
this.sessions.set(attachment.sessionId, session);
610612

@@ -633,7 +635,7 @@ export class RelayWebSocket implements DurableObject {
633635
bookmark: session.bookmark,
634636
host: session.host,
635637
doName: this.doName,
636-
hasPaid: attachment.hasPaid
638+
hasPaid: session.hasPaid
637639
};
638640
ws.serializeAttachment(updatedAttachment);
639641

@@ -787,24 +789,9 @@ export class RelayWebSocket implements DurableObject {
787789
return;
788790
}
789791

790-
// Check if pay to relay is enabled
792+
// Check if pay to relay is enabled (payment status cached on session at AUTH time)
791793
if (PAY_TO_RELAY_ENABLED) {
792-
// Check cache first
793-
let hasPaid = await this.getCachedPaymentStatus(event.pubkey);
794-
795-
if (hasPaid === null) {
796-
// Not in cache, check database
797-
hasPaid = await hasPaidForRelay(event.pubkey, this.env);
798-
// Only cache definitive results, not DB errors (null)
799-
if (hasPaid !== null) {
800-
this.setCachedPaymentStatus(event.pubkey, hasPaid);
801-
}
802-
}
803-
804-
// Block only when we know for certain they haven't paid (false).
805-
// On DB errors (null) allow the event through to avoid dropping
806-
// events from paid users due to transient failures.
807-
if (hasPaid === false) {
794+
if (session.hasPaid === false) {
808795
const protocol = 'https:';
809796
const relayUrl = `${protocol}//${session.host}`;
810797
console.error(`Event denied. Pubkey ${event.pubkey} has not paid for relay access.`);
@@ -1083,6 +1070,15 @@ export class RelayWebSocket implements DurableObject {
10831070
// All checks passed - add pubkey to authenticated list
10841071
session.authenticatedPubkeys.add(authEvent.pubkey);
10851072

1073+
// Check payment status at auth time so we don't hit D1 on every EVENT
1074+
if (PAY_TO_RELAY_ENABLED) {
1075+
const paid = await hasPaidForRelay(authEvent.pubkey, this.env);
1076+
if (paid !== null) {
1077+
session.hasPaid = paid;
1078+
this.setCachedPaymentStatus(authEvent.pubkey, paid);
1079+
}
1080+
}
1081+
10861082
this.sendOK(session.webSocket, authEvent.id, true, '');
10871083

10881084
} catch (error: any) {
@@ -1125,7 +1121,8 @@ export class RelayWebSocket implements DurableObject {
11251121
bookmark: attachment.bookmark,
11261122
host: attachment.host,
11271123
challenge: AUTH_REQUIRED ? this.generateAuthChallenge() : undefined,
1128-
authenticatedPubkeys: new Set()
1124+
authenticatedPubkeys: new Set(),
1125+
hasPaid: attachment.hasPaid
11291126
};
11301127
this.sessions.set(attachment.sessionId, session);
11311128
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ export interface WebSocketSession {
7575
// NIP-42 Authentication
7676
challenge?: string;
7777
authenticatedPubkeys: Set<string>;
78+
// Pay-to-relay: cached per-session, checked at AUTH time
79+
hasPaid?: boolean;
7880
}
7981

8082
export class RateLimiter {

worker.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var relayInfo = {
8282
contact: "lux@fed.wtf",
8383
supported_nips: [1, 2, 4, 5, 9, 11, 12, 15, 16, 17, 20, 22, 33, 40, 42],
8484
software: "https://github.com/Spl0itable/nosflare",
85-
version: "7.9.35",
85+
version: "7.9.36",
8686
icon: "https://raw.githubusercontent.com/Spl0itable/nosflare/main/images/flare.png",
8787
// Optional fields (uncomment as needed):
8888
// banner: "https://example.com/banner.jpg",
@@ -4842,7 +4842,9 @@ var _RelayWebSocket = class _RelayWebSocket {
48424842
host: attachment.host,
48434843
// NIP-42: Generate new challenge after hibernation (old one is lost)
48444844
challenge: AUTH_REQUIRED ? this.generateAuthChallenge() : void 0,
4845-
authenticatedPubkeys: /* @__PURE__ */ new Set()
4845+
authenticatedPubkeys: /* @__PURE__ */ new Set(),
4846+
// Restore payment status from attachment (survives hibernation)
4847+
hasPaid: attachment.hasPaid
48464848
};
48474849
this.sessions.set(attachment.sessionId, session);
48484850
if (AUTH_REQUIRED && session.challenge) {
@@ -4864,7 +4866,7 @@ var _RelayWebSocket = class _RelayWebSocket {
48644866
bookmark: session.bookmark,
48654867
host: session.host,
48664868
doName: this.doName,
4867-
hasPaid: attachment.hasPaid
4869+
hasPaid: session.hasPaid
48684870
};
48694871
ws.serializeAttachment(updatedAttachment);
48704872
} catch (error) {
@@ -4983,14 +4985,7 @@ var _RelayWebSocket = class _RelayWebSocket {
49834985
return;
49844986
}
49854987
if (PAY_TO_RELAY_ENABLED) {
4986-
let hasPaid = await this.getCachedPaymentStatus(event.pubkey);
4987-
if (hasPaid === null) {
4988-
hasPaid = await hasPaidForRelay(event.pubkey, this.env);
4989-
if (hasPaid !== null) {
4990-
this.setCachedPaymentStatus(event.pubkey, hasPaid);
4991-
}
4992-
}
4993-
if (hasPaid === false) {
4988+
if (session.hasPaid === false) {
49944989
const protocol = "https:";
49954990
const relayUrl = `${protocol}//${session.host}`;
49964991
console.error(`Event denied. Pubkey ${event.pubkey} has not paid for relay access.`);
@@ -5185,6 +5180,13 @@ var _RelayWebSocket = class _RelayWebSocket {
51855180
return;
51865181
}
51875182
session.authenticatedPubkeys.add(authEvent.pubkey);
5183+
if (PAY_TO_RELAY_ENABLED) {
5184+
const paid = await hasPaidForRelay(authEvent.pubkey, this.env);
5185+
if (paid !== null) {
5186+
session.hasPaid = paid;
5187+
this.setCachedPaymentStatus(authEvent.pubkey, paid);
5188+
}
5189+
}
51885190
this.sendOK(session.webSocket, authEvent.id, true, "");
51895191
} catch (error) {
51905192
console.error("Error handling AUTH:", error);
@@ -5213,7 +5215,8 @@ var _RelayWebSocket = class _RelayWebSocket {
52135215
bookmark: attachment.bookmark,
52145216
host: attachment.host,
52155217
challenge: AUTH_REQUIRED ? this.generateAuthChallenge() : void 0,
5216-
authenticatedPubkeys: /* @__PURE__ */ new Set()
5218+
authenticatedPubkeys: /* @__PURE__ */ new Set(),
5219+
hasPaid: attachment.hasPaid
52175220
};
52185221
this.sessions.set(attachment.sessionId, session);
52195222
}

0 commit comments

Comments
 (0)