@candypoets/nipworker 0.91.2 → 0.91.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cache/index.js +6 -6
- package/dist/cache/pkg/cache.d.ts +3 -3
- package/dist/cache/pkg/cache_bg.wasm.d.ts +3 -3
- package/dist/connections/index.js +8 -8
- package/dist/connections/pkg/connections.d.ts +2 -2
- package/dist/connections/pkg/connections_bg.wasm.d.ts +2 -2
- package/dist/proxy/relayProxyServer.d.ts.map +1 -1
- package/dist/relayProxyServer.js +183 -191
- package/package.json +1 -1
package/dist/relayProxyServer.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import { WebSocketServer as
|
|
1
|
+
import * as E from "flatbuffers";
|
|
2
|
+
import { WebSocketServer as h, WebSocket as S } from "ws";
|
|
3
3
|
import { SocksProxyAgent as v } from "socks-proxy-agent";
|
|
4
|
-
import { C as T, W as
|
|
4
|
+
import { C as T, W as k, M, G as A, R as F, N, S as C } from "./worker-message.js";
|
|
5
5
|
function q(t = {}) {
|
|
6
|
-
const
|
|
6
|
+
const n = t.host ?? "127.0.0.1", r = t.port ?? 7777, e = t.path ?? "/", s = t.logger ?? console, o = t.torSocksProxy;
|
|
7
7
|
let f;
|
|
8
8
|
try {
|
|
9
|
-
f = new
|
|
10
|
-
host:
|
|
11
|
-
port:
|
|
9
|
+
f = new h({
|
|
10
|
+
host: n,
|
|
11
|
+
port: r,
|
|
12
12
|
path: e
|
|
13
13
|
});
|
|
14
14
|
} catch (i) {
|
|
15
15
|
throw s.error(`[relay-proxy] failed to create WebSocketServer: ${String(i)}`), i;
|
|
16
16
|
}
|
|
17
|
-
let a =
|
|
17
|
+
let a = r;
|
|
18
18
|
const c = f.address();
|
|
19
19
|
return c && typeof c == "object" && (a = c.port), f.on("error", (i) => {
|
|
20
20
|
s.error(`[relay-proxy] WebSocketServer error: ${String(i)}`);
|
|
@@ -24,24 +24,23 @@ function q(t = {}) {
|
|
|
24
24
|
pendingFrames: /* @__PURE__ */ new Map(),
|
|
25
25
|
dedupBySubId: /* @__PURE__ */ new Map(),
|
|
26
26
|
lastSubIdByRelay: /* @__PURE__ */ new Map(),
|
|
27
|
-
torSocksProxy: o
|
|
28
|
-
lastAuthChallenge: /* @__PURE__ */ new Map()
|
|
27
|
+
torSocksProxy: o
|
|
29
28
|
};
|
|
30
|
-
i.on("message", (y,
|
|
31
|
-
if (
|
|
32
|
-
const
|
|
33
|
-
if (!
|
|
34
|
-
x(u, i,
|
|
29
|
+
i.on("message", (y, d) => {
|
|
30
|
+
if (d) {
|
|
31
|
+
const p = $(y);
|
|
32
|
+
if (!p) return;
|
|
33
|
+
x(u, i, p, s);
|
|
35
34
|
return;
|
|
36
35
|
}
|
|
37
|
-
const l =
|
|
36
|
+
const l = w(y);
|
|
38
37
|
l && b(u, l, s);
|
|
39
38
|
}), i.on("close", () => {
|
|
40
39
|
g(u, s);
|
|
41
40
|
}), i.on("error", () => {
|
|
42
41
|
g(u, s);
|
|
43
42
|
});
|
|
44
|
-
}), s.info(`[relay-proxy] listening on ws://${
|
|
43
|
+
}), s.info(`[relay-proxy] listening on ws://${n}:${a}${e}`), {
|
|
45
44
|
port: a,
|
|
46
45
|
close: () => new Promise((i, u) => {
|
|
47
46
|
f.close((y) => {
|
|
@@ -55,7 +54,7 @@ function q(t = {}) {
|
|
|
55
54
|
};
|
|
56
55
|
}
|
|
57
56
|
function X(t) {
|
|
58
|
-
const
|
|
57
|
+
const n = t.path ?? "/", r = t.logger ?? console, e = t.torSocksProxy, s = new h({
|
|
59
58
|
noServer: true
|
|
60
59
|
}), o = /* @__PURE__ */ new Map();
|
|
61
60
|
return s.on("connection", (a) => {
|
|
@@ -72,22 +71,22 @@ function X(t) {
|
|
|
72
71
|
if (u) {
|
|
73
72
|
const l = $(i);
|
|
74
73
|
if (!l) return;
|
|
75
|
-
x(y, a, l,
|
|
74
|
+
x(y, a, l, r);
|
|
76
75
|
return;
|
|
77
76
|
}
|
|
78
|
-
const
|
|
79
|
-
|
|
77
|
+
const d = w(i);
|
|
78
|
+
d && b(y, d, r);
|
|
80
79
|
}), a.on("close", () => {
|
|
81
80
|
const i = o.get(a);
|
|
82
|
-
i && (g(i,
|
|
81
|
+
i && (g(i, r), o.delete(a));
|
|
83
82
|
}), a.on("error", () => {
|
|
84
83
|
const i = o.get(a);
|
|
85
|
-
i && (g(i,
|
|
84
|
+
i && (g(i, r), o.delete(a));
|
|
86
85
|
});
|
|
87
86
|
}), {
|
|
88
87
|
wss: s,
|
|
89
88
|
close: () => new Promise((a, c) => {
|
|
90
|
-
o.forEach((i) => g(i,
|
|
89
|
+
o.forEach((i) => g(i, r)), o.clear(), s.close((i) => {
|
|
91
90
|
if (i) {
|
|
92
91
|
c(i);
|
|
93
92
|
return;
|
|
@@ -96,16 +95,16 @@ function X(t) {
|
|
|
96
95
|
});
|
|
97
96
|
}),
|
|
98
97
|
handleUpgrade: (a, c, i) => {
|
|
99
|
-
(a.url ?? "").startsWith(
|
|
98
|
+
(a.url ?? "").startsWith(n) && s.handleUpgrade(a, c, i, (y) => {
|
|
100
99
|
s.emit("connection", y, a);
|
|
101
100
|
});
|
|
102
101
|
}
|
|
103
102
|
};
|
|
104
103
|
}
|
|
105
104
|
function Y(t) {
|
|
106
|
-
const { server:
|
|
107
|
-
server:
|
|
108
|
-
path:
|
|
105
|
+
const { server: n, path: r = "/", logger: e = console, torSocksProxy: s } = t, o = new h({
|
|
106
|
+
server: n,
|
|
107
|
+
path: r
|
|
109
108
|
});
|
|
110
109
|
return o.on("connection", (f) => {
|
|
111
110
|
const a = {
|
|
@@ -122,14 +121,14 @@ function Y(t) {
|
|
|
122
121
|
x(a, f, y, e);
|
|
123
122
|
return;
|
|
124
123
|
}
|
|
125
|
-
const u =
|
|
124
|
+
const u = w(c);
|
|
126
125
|
u && b(a, u, e);
|
|
127
126
|
}), f.on("close", () => {
|
|
128
127
|
g(a, e);
|
|
129
128
|
}), f.on("error", () => {
|
|
130
129
|
g(a, e);
|
|
131
130
|
});
|
|
132
|
-
}), e.info(`[relay-proxy] attached to server at path: ${
|
|
131
|
+
}), e.info(`[relay-proxy] attached to server at path: ${r}`), {
|
|
133
132
|
close: () => new Promise((f, a) => {
|
|
134
133
|
o.close((c) => {
|
|
135
134
|
if (c) {
|
|
@@ -141,15 +140,15 @@ function Y(t) {
|
|
|
141
140
|
})
|
|
142
141
|
};
|
|
143
142
|
}
|
|
144
|
-
function Z(t,
|
|
145
|
-
const
|
|
143
|
+
function Z(t, n) {
|
|
144
|
+
const r = n.path ?? "/ws-proxy", e = n.logger ?? console, s = n.torSocksProxy;
|
|
146
145
|
let o = null;
|
|
147
146
|
const f = /* @__PURE__ */ new Map(), a = t.listen.bind(t);
|
|
148
147
|
return t.listen = (...c) => {
|
|
149
148
|
const i = a(...c);
|
|
150
|
-
return o = new
|
|
149
|
+
return o = new h({
|
|
151
150
|
server: i,
|
|
152
|
-
path:
|
|
151
|
+
path: r
|
|
153
152
|
}), o.on("connection", (u) => {
|
|
154
153
|
const y = {
|
|
155
154
|
relaySockets: /* @__PURE__ */ new Map(),
|
|
@@ -158,25 +157,25 @@ function Z(t, r) {
|
|
|
158
157
|
lastSubIdByRelay: /* @__PURE__ */ new Map(),
|
|
159
158
|
torSocksProxy: s
|
|
160
159
|
};
|
|
161
|
-
f.set(u, y), u.on("message", (
|
|
162
|
-
const
|
|
163
|
-
if (!
|
|
160
|
+
f.set(u, y), u.on("message", (d, l) => {
|
|
161
|
+
const p = f.get(u);
|
|
162
|
+
if (!p) return;
|
|
164
163
|
if (l) {
|
|
165
|
-
const B = $(
|
|
164
|
+
const B = $(d);
|
|
166
165
|
if (!B) return;
|
|
167
|
-
x(
|
|
166
|
+
x(p, u, B, e);
|
|
168
167
|
return;
|
|
169
168
|
}
|
|
170
|
-
const I =
|
|
171
|
-
I && b(
|
|
169
|
+
const I = w(d);
|
|
170
|
+
I && b(p, I, e);
|
|
172
171
|
}), u.on("close", () => {
|
|
173
|
-
const
|
|
174
|
-
|
|
172
|
+
const d = f.get(u);
|
|
173
|
+
d && (g(d, e), f.delete(u));
|
|
175
174
|
}), u.on("error", () => {
|
|
176
|
-
const
|
|
177
|
-
|
|
175
|
+
const d = f.get(u);
|
|
176
|
+
d && (g(d, e), f.delete(u));
|
|
178
177
|
});
|
|
179
|
-
}), e.info(`[relay-proxy] attached to Express server at path: ${
|
|
178
|
+
}), e.info(`[relay-proxy] attached to Express server at path: ${r}`), i;
|
|
180
179
|
}, {
|
|
181
180
|
close: () => new Promise((c, i) => {
|
|
182
181
|
if (!o) {
|
|
@@ -193,36 +192,36 @@ function Z(t, r) {
|
|
|
193
192
|
})
|
|
194
193
|
};
|
|
195
194
|
}
|
|
196
|
-
function b(t,
|
|
197
|
-
|
|
195
|
+
function b(t, n, r) {
|
|
196
|
+
r.info(`[relay-proxy] handleClientCommand: ${n.slice(0, 200)}`);
|
|
198
197
|
let e;
|
|
199
198
|
try {
|
|
200
|
-
e = JSON.parse(
|
|
199
|
+
e = JSON.parse(n);
|
|
201
200
|
} catch (s) {
|
|
202
|
-
|
|
201
|
+
r.warn(`[relay-proxy] failed to parse command JSON: ${s}`);
|
|
203
202
|
return;
|
|
204
203
|
}
|
|
205
|
-
if (
|
|
206
|
-
|
|
204
|
+
if (H(e)) {
|
|
205
|
+
r.info(`[relay-proxy] received auth_response for relay: ${e.relay}`);
|
|
207
206
|
const s = JSON.stringify(["AUTH", e.event]);
|
|
208
|
-
|
|
207
|
+
r.info(`[relay-proxy] sending AUTH frame to relay: ${e.relay}`), P(t, e.relay, s, r);
|
|
209
208
|
return;
|
|
210
209
|
}
|
|
211
210
|
if (G(e)) {
|
|
212
211
|
t.dedupBySubId.delete(e.subscription_id);
|
|
213
212
|
for (const [s, o] of t.relaySockets.entries())
|
|
214
|
-
o.readyState ===
|
|
213
|
+
o.readyState === S.OPEN && o.send(JSON.stringify(["CLOSE", e.subscription_id])), t.lastSubIdByRelay.set(s, e.subscription_id);
|
|
215
214
|
}
|
|
216
215
|
}
|
|
217
|
-
function x(t,
|
|
218
|
-
e.info(`[relay-proxy] handleEnvelope: ${
|
|
219
|
-
const s =
|
|
216
|
+
function x(t, n, r, e) {
|
|
217
|
+
e.info(`[relay-proxy] handleEnvelope: ${r.relays.length} relays, ${r.frames.length} frames`), e.info(`[relay-proxy] relays: ${r.relays.join(", ")}`);
|
|
218
|
+
const s = r.frames.map((o) => ({
|
|
220
219
|
frame: o,
|
|
221
220
|
state: L(o)
|
|
222
221
|
}));
|
|
223
222
|
e.info(`[relay-proxy] tracked ${s.length} frames`);
|
|
224
|
-
for (const o of
|
|
225
|
-
e.info(`[relay-proxy] processing relay: ${o}`), W(t,
|
|
223
|
+
for (const o of r.relays) {
|
|
224
|
+
e.info(`[relay-proxy] processing relay: ${o}`), W(t, n, o, e);
|
|
226
225
|
for (const f of s) {
|
|
227
226
|
_(t, o, f.state);
|
|
228
227
|
const a = f.frame;
|
|
@@ -230,202 +229,195 @@ function x(t, r, n, e) {
|
|
|
230
229
|
}
|
|
231
230
|
}
|
|
232
231
|
}
|
|
233
|
-
function W(t,
|
|
234
|
-
const s = t.relaySockets.get(
|
|
235
|
-
if (s && s.readyState !==
|
|
236
|
-
e.info(`[relay-proxy] reusing existing connection to ${
|
|
232
|
+
function W(t, n, r, e) {
|
|
233
|
+
const s = t.relaySockets.get(r);
|
|
234
|
+
if (s && s.readyState !== S.CLOSED) {
|
|
235
|
+
e.info(`[relay-proxy] reusing existing connection to ${r}`);
|
|
237
236
|
return;
|
|
238
237
|
}
|
|
239
|
-
e.info(`[relay-proxy] creating new WebSocket connection to ${
|
|
240
|
-
const o = new URL(
|
|
238
|
+
e.info(`[relay-proxy] creating new WebSocket connection to ${r}`);
|
|
239
|
+
const o = new URL(r), f = `${o.protocol}//${o.host}`, a = o.hostname.endsWith(".onion"), c = {
|
|
241
240
|
headers: {
|
|
242
241
|
Origin: f,
|
|
243
242
|
"User-Agent": "nipworker/0.91.0"
|
|
244
243
|
}
|
|
245
244
|
};
|
|
246
|
-
a && t.torSocksProxy && (e.info(`[relay-proxy] using Tor SOCKS proxy ${t.torSocksProxy} for ${
|
|
247
|
-
const i = new
|
|
248
|
-
t.relaySockets.set(
|
|
245
|
+
a && t.torSocksProxy && (e.info(`[relay-proxy] using Tor SOCKS proxy ${t.torSocksProxy} for ${r}`), c.agent = new v(t.torSocksProxy));
|
|
246
|
+
const i = new S(r, c);
|
|
247
|
+
t.relaySockets.set(r, i), t.pendingFrames.set(r, []);
|
|
249
248
|
const u = Date.now();
|
|
250
249
|
i.on("open", () => {
|
|
251
|
-
e.info(`[relay-proxy] connected to ${
|
|
252
|
-
const y = t.pendingFrames.get(
|
|
250
|
+
e.info(`[relay-proxy] connected to ${r}`);
|
|
251
|
+
const y = t.pendingFrames.get(r);
|
|
253
252
|
if (y) {
|
|
254
|
-
e.info(`[relay-proxy] sending ${y.length} pending frames to ${
|
|
255
|
-
for (const
|
|
256
|
-
i.send(
|
|
257
|
-
t.pendingFrames.set(
|
|
253
|
+
e.info(`[relay-proxy] sending ${y.length} pending frames to ${r}`);
|
|
254
|
+
for (const d of y)
|
|
255
|
+
i.send(d);
|
|
256
|
+
t.pendingFrames.set(r, []);
|
|
258
257
|
}
|
|
259
258
|
}), i.on("message", (y) => {
|
|
260
|
-
const
|
|
261
|
-
if (e.info(`[relay-proxy] received from ${
|
|
262
|
-
e.warn(`[relay-proxy] cannot forward message: raw=${!!
|
|
259
|
+
const d = w(y);
|
|
260
|
+
if (e.info(`[relay-proxy] received from ${r}: ${d == null ? void 0 : d.slice(0, 100)}`), !d || n.readyState !== S.OPEN) {
|
|
261
|
+
e.warn(`[relay-proxy] cannot forward message: raw=${!!d}, clientOpen=${n.readyState === S.OPEN}`);
|
|
263
262
|
return;
|
|
264
263
|
}
|
|
265
|
-
const l = t.lastSubIdByRelay.get(
|
|
266
|
-
if (!
|
|
264
|
+
const l = t.lastSubIdByRelay.get(r), p = J(t, r, d, l, e);
|
|
265
|
+
if (!p) {
|
|
267
266
|
e.warn("[relay-proxy] failed to convert relay frame to worker message");
|
|
268
267
|
return;
|
|
269
268
|
}
|
|
270
|
-
e.info(`[relay-proxy] forwarding ${
|
|
271
|
-
}), i.on("close", (y,
|
|
269
|
+
e.info(`[relay-proxy] forwarding ${p.length} bytes to client`), n.send(p, { binary: true });
|
|
270
|
+
}), i.on("close", (y, d) => {
|
|
272
271
|
var _a, _b;
|
|
273
|
-
if (e.info(`[relay-proxy] connection closed to ${
|
|
274
|
-
const l = ((_b = t.pendingFrames.get(
|
|
275
|
-
if (e.warn(`[relay-proxy] connection to ${
|
|
276
|
-
const
|
|
277
|
-
t.lastSubIdByRelay.get(
|
|
278
|
-
|
|
272
|
+
if (e.info(`[relay-proxy] connection closed to ${r} (code: ${y}, reason: ${d})`), t.pendingFrames.has(r) && ((_a = t.pendingFrames.get(r)) == null ? void 0 : _a.length) > 0) {
|
|
273
|
+
const l = ((_b = t.pendingFrames.get(r)) == null ? void 0 : _b.length) ?? 0;
|
|
274
|
+
if (e.warn(`[relay-proxy] connection to ${r} closed with ${l} pending frames - subscription may fail`), n.readyState === S.OPEN) {
|
|
275
|
+
const p = m(
|
|
276
|
+
t.lastSubIdByRelay.get(r) ?? "",
|
|
277
|
+
r,
|
|
279
278
|
"failed",
|
|
280
|
-
`Connection failed: ${y} ${
|
|
279
|
+
`Connection failed: ${y} ${d}`
|
|
281
280
|
);
|
|
282
|
-
|
|
281
|
+
n.send(p, { binary: true });
|
|
283
282
|
}
|
|
284
283
|
}
|
|
285
|
-
t.pendingFrames.delete(
|
|
284
|
+
t.pendingFrames.delete(r);
|
|
286
285
|
}), i.on("error", (y) => {
|
|
287
|
-
if (!t.pendingFrames.has(
|
|
288
|
-
const
|
|
289
|
-
e.warn(`[relay-proxy] relay socket error for ${
|
|
286
|
+
if (!t.pendingFrames.has(r)) return;
|
|
287
|
+
const d = String(y), l = Date.now() - u;
|
|
288
|
+
e.warn(`[relay-proxy] relay socket error for ${r} after ${l}ms: ${d}`), d.includes("403") && e.warn(`[relay-proxy] 403 Forbidden from ${r} - relay may require NIP-42 auth or block this connection`), t.pendingFrames.delete(r);
|
|
290
289
|
});
|
|
291
290
|
}
|
|
292
|
-
function _(t,
|
|
293
|
-
if (
|
|
294
|
-
if (t.lastSubIdByRelay.set(
|
|
295
|
-
const e =
|
|
291
|
+
function _(t, n, r) {
|
|
292
|
+
if (r) {
|
|
293
|
+
if (t.lastSubIdByRelay.set(n, r.subId), r.type === "REQ") {
|
|
294
|
+
const e = r.subId;
|
|
296
295
|
t.dedupBySubId.has(e) || t.dedupBySubId.set(e, /* @__PURE__ */ new Set());
|
|
297
296
|
return;
|
|
298
297
|
}
|
|
299
|
-
t.dedupBySubId.delete(
|
|
298
|
+
t.dedupBySubId.delete(r.subId);
|
|
300
299
|
}
|
|
301
300
|
}
|
|
302
301
|
function L(t) {
|
|
303
|
-
const
|
|
304
|
-
if (!
|
|
305
|
-
const
|
|
306
|
-
if (
|
|
307
|
-
const e = typeof
|
|
308
|
-
return e ? { type:
|
|
302
|
+
const n = R(t);
|
|
303
|
+
if (!n) return null;
|
|
304
|
+
const r = n[0];
|
|
305
|
+
if (r !== "REQ" && r !== "CLOSE") return null;
|
|
306
|
+
const e = typeof n[1] == "string" ? n[1] : null;
|
|
307
|
+
return e ? { type: r, subId: e } : null;
|
|
309
308
|
}
|
|
310
|
-
function P(t,
|
|
311
|
-
const s = t.relaySockets.get(
|
|
309
|
+
function P(t, n, r, e) {
|
|
310
|
+
const s = t.relaySockets.get(n);
|
|
312
311
|
if (!s) {
|
|
313
|
-
e.warn(`[relay-proxy] no relay socket for ${
|
|
312
|
+
e.warn(`[relay-proxy] no relay socket for ${n}`);
|
|
314
313
|
return;
|
|
315
314
|
}
|
|
316
|
-
if (s.readyState ===
|
|
317
|
-
e.info(`[relay-proxy] sending frame to ${
|
|
315
|
+
if (s.readyState === S.OPEN) {
|
|
316
|
+
e.info(`[relay-proxy] sending frame to ${n} (OPEN): ${r.slice(0, 100)}`), s.send(r);
|
|
318
317
|
return;
|
|
319
318
|
}
|
|
320
|
-
if (s.readyState ===
|
|
321
|
-
e.info(`[relay-proxy] queueing frame for ${
|
|
322
|
-
const o = t.pendingFrames.get(
|
|
323
|
-
o.push(
|
|
319
|
+
if (s.readyState === S.CONNECTING) {
|
|
320
|
+
e.info(`[relay-proxy] queueing frame for ${n} (CONNECTING)`);
|
|
321
|
+
const o = t.pendingFrames.get(n) ?? [];
|
|
322
|
+
o.push(r), t.pendingFrames.set(n, o);
|
|
324
323
|
return;
|
|
325
324
|
}
|
|
326
|
-
e.warn(`[relay-proxy] dropping frame for closed relay socket ${
|
|
325
|
+
e.warn(`[relay-proxy] dropping frame for closed relay socket ${n} (state: ${s.readyState})`);
|
|
327
326
|
}
|
|
328
|
-
function
|
|
329
|
-
const o = R(
|
|
327
|
+
function J(t, n, r, e, s) {
|
|
328
|
+
const o = R(r);
|
|
330
329
|
if (!o || o.length < 1 || typeof o[0] != "string")
|
|
331
|
-
return O(e ?? "",
|
|
330
|
+
return O(e ?? "", n, r);
|
|
332
331
|
const f = o[0];
|
|
333
332
|
if (f === "EVENT") {
|
|
334
|
-
const a = typeof o[1] == "string" ? o[1] : "", c =
|
|
333
|
+
const a = typeof o[1] == "string" ? o[1] : "", c = j(o[2]);
|
|
335
334
|
if (!a || !c)
|
|
336
|
-
return O(a,
|
|
335
|
+
return O(a, n, r);
|
|
337
336
|
const i = t.dedupBySubId.get(a);
|
|
338
|
-
return i && i.has(c.id) ? null : (i && i.add(c.id),
|
|
337
|
+
return i && i.has(c.id) ? null : (i && i.add(c.id), V(a, n, c));
|
|
339
338
|
}
|
|
340
339
|
if (f === "NOTICE") {
|
|
341
340
|
const a = o[1] === void 0 ? null : String(o[1]);
|
|
342
|
-
return
|
|
341
|
+
return m("", n, "NOTICE", a);
|
|
343
342
|
}
|
|
344
343
|
if (f === "AUTH") {
|
|
345
|
-
const a = o[1] === void 0 ? null : String(o[1])
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
return s == null ? void 0 : s.info(`[relay-proxy] dropping duplicate AUTH from ${r} (${y}ms ago)`), null;
|
|
350
|
-
if (y < 1e3)
|
|
351
|
-
return s == null ? void 0 : s.info(`[relay-proxy] rate limiting AUTH from ${r} (${y}ms since last)`), null;
|
|
352
|
-
}
|
|
353
|
-
t.lastAuthChallenge.set(r, { challenge: a, timestamp: c }), s == null ? void 0 : s.info(`[relay-proxy] received AUTH challenge from ${r}: ${a}`);
|
|
354
|
-
const u = S(e ?? "", r, "AUTH", a);
|
|
355
|
-
return s == null ? void 0 : s.info(`[relay-proxy] built AUTH worker message (${u.length} bytes)`), u;
|
|
344
|
+
const a = o[1] === void 0 ? null : String(o[1]);
|
|
345
|
+
s == null ? void 0 : s.info(`[relay-proxy] received AUTH challenge from ${n}: ${a}`);
|
|
346
|
+
const c = m(e ?? "", n, "AUTH", a);
|
|
347
|
+
return s == null ? void 0 : s.info(`[relay-proxy] built AUTH worker message (${c.length} bytes)`), c;
|
|
356
348
|
}
|
|
357
349
|
if (f === "CLOSED") {
|
|
358
350
|
const a = typeof o[1] == "string" ? o[1] : "", c = o[2] === void 0 ? null : String(o[2]);
|
|
359
|
-
return
|
|
351
|
+
return m(a, n, "CLOSED", c);
|
|
360
352
|
}
|
|
361
353
|
if (f === "OK") {
|
|
362
354
|
const a = typeof o[1] == "string" ? o[1] : "", c = o[2] === void 0 ? "false" : String(o[2]), i = o[3] === void 0 ? null : String(o[3]);
|
|
363
|
-
return
|
|
355
|
+
return m(a, n, c, i);
|
|
364
356
|
}
|
|
365
357
|
if (f === "EOSE") {
|
|
366
358
|
const a = typeof o[1] == "string" ? o[1] : "";
|
|
367
|
-
return
|
|
359
|
+
return m(a, n, "EOSE", null);
|
|
368
360
|
}
|
|
369
|
-
return O(e ?? "",
|
|
361
|
+
return O(e ?? "", n, r);
|
|
370
362
|
}
|
|
371
|
-
function
|
|
372
|
-
const e = new
|
|
373
|
-
for (let
|
|
374
|
-
u[
|
|
375
|
-
const y =
|
|
363
|
+
function V(t, n, r) {
|
|
364
|
+
const e = new E.Builder(1024), s = t ? e.createString(t) : 0, o = e.createString(n), f = e.createString(r.id), a = e.createString(r.pubkey), c = e.createString(r.content), i = e.createString(r.sig), u = new Array(r.tags.length);
|
|
365
|
+
for (let p = 0; p < r.tags.length; p++)
|
|
366
|
+
u[p] = D(e, r.tags[p]);
|
|
367
|
+
const y = N.createTagsVector(e, u), d = N.createNostrEvent(
|
|
376
368
|
e,
|
|
377
369
|
f,
|
|
378
370
|
a,
|
|
379
|
-
|
|
371
|
+
r.kind,
|
|
380
372
|
c,
|
|
381
373
|
y,
|
|
382
|
-
|
|
374
|
+
r.created_at,
|
|
383
375
|
i
|
|
384
|
-
), l =
|
|
376
|
+
), l = k.createWorkerMessage(
|
|
385
377
|
e,
|
|
386
378
|
s,
|
|
387
379
|
o,
|
|
388
|
-
k.NostrEvent,
|
|
389
380
|
M.NostrEvent,
|
|
390
|
-
|
|
381
|
+
A.NostrEvent,
|
|
382
|
+
d
|
|
391
383
|
);
|
|
392
384
|
return e.finish(l), e.asUint8Array();
|
|
393
385
|
}
|
|
394
|
-
function
|
|
395
|
-
const s = new
|
|
386
|
+
function m(t, n, r, e) {
|
|
387
|
+
const s = new E.Builder(256), o = t ? s.createString(t) : 0, f = s.createString(n), a = s.createString(r), c = e === null ? 0 : s.createString(e), i = T.createConnectionStatus(
|
|
396
388
|
s,
|
|
397
389
|
f,
|
|
398
390
|
a,
|
|
399
391
|
c
|
|
400
|
-
), u =
|
|
392
|
+
), u = k.createWorkerMessage(
|
|
401
393
|
s,
|
|
402
394
|
o,
|
|
403
395
|
f,
|
|
404
|
-
k.ConnectionStatus,
|
|
405
396
|
M.ConnectionStatus,
|
|
397
|
+
A.ConnectionStatus,
|
|
406
398
|
i
|
|
407
399
|
);
|
|
408
400
|
return s.finish(u), s.asUint8Array();
|
|
409
401
|
}
|
|
410
|
-
function O(t,
|
|
411
|
-
const e = new
|
|
402
|
+
function O(t, n, r) {
|
|
403
|
+
const e = new E.Builder(256), s = t ? e.createString(t) : 0, o = e.createString(n), f = e.createString(r), a = F.createRaw(e, f), c = k.createWorkerMessage(
|
|
412
404
|
e,
|
|
413
405
|
s,
|
|
414
406
|
o,
|
|
415
|
-
k.Raw,
|
|
416
407
|
M.Raw,
|
|
408
|
+
A.Raw,
|
|
417
409
|
a
|
|
418
410
|
);
|
|
419
411
|
return e.finish(c), e.asUint8Array();
|
|
420
412
|
}
|
|
421
|
-
function
|
|
413
|
+
function j(t) {
|
|
422
414
|
if (!t || typeof t != "object") return null;
|
|
423
|
-
const
|
|
424
|
-
if (typeof
|
|
415
|
+
const n = t;
|
|
416
|
+
if (typeof n.id != "string" || typeof n.pubkey != "string" || typeof n.kind != "number" || typeof n.content != "string" || typeof n.created_at != "number" || typeof n.sig != "string" || !Array.isArray(n.tags))
|
|
425
417
|
return null;
|
|
426
|
-
const
|
|
418
|
+
const r = n.tags;
|
|
427
419
|
let e = false;
|
|
428
|
-
for (const o of
|
|
420
|
+
for (const o of r) {
|
|
429
421
|
if (!Array.isArray(o) || o.length === 0) {
|
|
430
422
|
e = true;
|
|
431
423
|
continue;
|
|
@@ -438,10 +430,10 @@ function J(t) {
|
|
|
438
430
|
}
|
|
439
431
|
let s;
|
|
440
432
|
if (!e)
|
|
441
|
-
s =
|
|
433
|
+
s = r;
|
|
442
434
|
else {
|
|
443
435
|
s = [];
|
|
444
|
-
for (const o of
|
|
436
|
+
for (const o of r) {
|
|
445
437
|
if (!Array.isArray(o)) continue;
|
|
446
438
|
const f = [];
|
|
447
439
|
for (const a of o)
|
|
@@ -450,62 +442,62 @@ function J(t) {
|
|
|
450
442
|
}
|
|
451
443
|
}
|
|
452
444
|
return {
|
|
453
|
-
id:
|
|
454
|
-
pubkey:
|
|
455
|
-
kind:
|
|
456
|
-
content:
|
|
457
|
-
created_at:
|
|
458
|
-
sig:
|
|
445
|
+
id: n.id,
|
|
446
|
+
pubkey: n.pubkey,
|
|
447
|
+
kind: n.kind,
|
|
448
|
+
content: n.content,
|
|
449
|
+
created_at: n.created_at,
|
|
450
|
+
sig: n.sig,
|
|
459
451
|
tags: s
|
|
460
452
|
};
|
|
461
453
|
}
|
|
462
|
-
function
|
|
463
|
-
const
|
|
464
|
-
for (let s = 0; s <
|
|
465
|
-
|
|
466
|
-
const e =
|
|
467
|
-
return
|
|
454
|
+
function D(t, n) {
|
|
455
|
+
const r = new Array(n.length);
|
|
456
|
+
for (let s = 0; s < n.length; s++)
|
|
457
|
+
r[s] = t.createString(n[s]);
|
|
458
|
+
const e = C.createItemsVector(t, r);
|
|
459
|
+
return C.createStringVec(t, e);
|
|
468
460
|
}
|
|
469
461
|
function R(t) {
|
|
470
462
|
try {
|
|
471
|
-
const
|
|
472
|
-
return Array.isArray(
|
|
463
|
+
const n = JSON.parse(t);
|
|
464
|
+
return Array.isArray(n) ? n : null;
|
|
473
465
|
} catch {
|
|
474
466
|
return null;
|
|
475
467
|
}
|
|
476
468
|
}
|
|
477
|
-
function $(t,
|
|
478
|
-
const
|
|
479
|
-
if (!
|
|
469
|
+
function $(t, n) {
|
|
470
|
+
const r = w(t);
|
|
471
|
+
if (!r)
|
|
480
472
|
return null;
|
|
481
473
|
try {
|
|
482
|
-
const e = JSON.parse(
|
|
474
|
+
const e = JSON.parse(r);
|
|
483
475
|
if (!Array.isArray(e.relays) || !Array.isArray(e.frames))
|
|
484
|
-
return
|
|
476
|
+
return n == null ? void 0 : n.warn(`[relay-proxy] parseEnvelope: invalid structure (relays: ${Array.isArray(e.relays)}, frames: ${Array.isArray(e.frames)})`), null;
|
|
485
477
|
const s = e.relays.filter((f) => typeof f == "string"), o = e.frames.filter((f) => typeof f == "string");
|
|
486
|
-
return
|
|
478
|
+
return n == null ? void 0 : n.info(`[relay-proxy] parseEnvelope: ${s.length} relays, ${o.length} frames`), s.length === 0 || o.length === 0 ? (n == null ? void 0 : n.warn("[relay-proxy] parseEnvelope: empty relays or frames after filter"), null) : { relays: s, frames: o };
|
|
487
479
|
} catch {
|
|
488
480
|
return null;
|
|
489
481
|
}
|
|
490
482
|
}
|
|
491
|
-
function
|
|
492
|
-
return typeof t == "string" ? t : t instanceof ArrayBuffer ? Buffer.from(t).toString("utf8") : Buffer.isBuffer(t) ? t.toString("utf8") : t instanceof Uint8Array ? Buffer.from(t).toString("utf8") : Array.isArray(t) ? Buffer.concat(t.filter((
|
|
483
|
+
function w(t) {
|
|
484
|
+
return typeof t == "string" ? t : t instanceof ArrayBuffer ? Buffer.from(t).toString("utf8") : Buffer.isBuffer(t) ? t.toString("utf8") : t instanceof Uint8Array ? Buffer.from(t).toString("utf8") : Array.isArray(t) ? Buffer.concat(t.filter((n) => Buffer.isBuffer(n))).toString("utf8") : "";
|
|
493
485
|
}
|
|
494
|
-
function
|
|
486
|
+
function H(t) {
|
|
495
487
|
if (!t || typeof t != "object") return false;
|
|
496
|
-
const
|
|
497
|
-
return
|
|
488
|
+
const n = t;
|
|
489
|
+
return n.type === "auth_response" && typeof n.relay == "string" && n.event !== void 0;
|
|
498
490
|
}
|
|
499
491
|
function G(t) {
|
|
500
492
|
if (!t || typeof t != "object") return false;
|
|
501
|
-
const
|
|
502
|
-
return
|
|
493
|
+
const n = t;
|
|
494
|
+
return n.type === "close_sub" && typeof n.subscription_id == "string";
|
|
503
495
|
}
|
|
504
|
-
function g(t,
|
|
505
|
-
|
|
506
|
-
for (const [
|
|
496
|
+
function g(t, n) {
|
|
497
|
+
n == null ? void 0 : n.info("[relay-proxy] closing session"), t.dedupBySubId.clear(), t.lastSubIdByRelay.clear();
|
|
498
|
+
for (const [r, e] of t.relaySockets.entries())
|
|
507
499
|
try {
|
|
508
|
-
|
|
500
|
+
n == null ? void 0 : n.info(`[relay-proxy] closing socket to ${r}`), e.close();
|
|
509
501
|
} catch {
|
|
510
502
|
}
|
|
511
503
|
t.relaySockets.clear(), t.pendingFrames.clear();
|