nuxt-skew-protection 2.0.2 → 2.0.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/module.json +1 -1
- package/dist/runtime/server/plugins/connection-tracking-cloudflare-durable.js +14 -5
- package/dist/runtime/server/plugins/connection-tracking.js +17 -9
- package/dist/runtime/server/routes/__skew/route.post.js +1 -1
- package/dist/runtime/server/routes/__skew/ws.js +8 -3
- package/package.json +1 -1
package/dist/module.json
CHANGED
|
@@ -10,8 +10,11 @@ function getInternal(peer) {
|
|
|
10
10
|
function getCtx(peer) {
|
|
11
11
|
return getInternal(peer)?.durable?.ctx || null;
|
|
12
12
|
}
|
|
13
|
+
function getOpenWebSockets(ctx) {
|
|
14
|
+
return ctx.getWebSockets().filter((ws) => ws.readyState === WebSocket.OPEN);
|
|
15
|
+
}
|
|
13
16
|
function getStats(ctx, forId) {
|
|
14
|
-
const websockets = ctx
|
|
17
|
+
const websockets = getOpenWebSockets(ctx);
|
|
15
18
|
const versions = {};
|
|
16
19
|
const routes = {};
|
|
17
20
|
const connections = [];
|
|
@@ -27,10 +30,10 @@ function getStats(ctx, forId) {
|
|
|
27
30
|
return { total: websockets.length, versions, routes, connections, yourId: forId };
|
|
28
31
|
}
|
|
29
32
|
function broadcastToSubscribers(ctx) {
|
|
30
|
-
const websockets = ctx
|
|
33
|
+
const websockets = getOpenWebSockets(ctx);
|
|
31
34
|
const subscribers = websockets.filter((ws) => {
|
|
32
35
|
const attachment = ws.deserializeAttachment?.() || {};
|
|
33
|
-
return attachment.
|
|
36
|
+
return attachment.stats?._tag === "Authorized";
|
|
34
37
|
});
|
|
35
38
|
if (subscribers.length === 0)
|
|
36
39
|
return;
|
|
@@ -67,8 +70,10 @@ export default defineNitroPlugin((nitroApp) => {
|
|
|
67
70
|
nitroApp.hooks.hook("skew:subscribe-stats", async ({ id, event, peer }) => {
|
|
68
71
|
const ws = getInternal(peer)?.ws;
|
|
69
72
|
const ctx = getCtx(peer);
|
|
70
|
-
if (!ws || !ctx)
|
|
73
|
+
if (!ws || !ctx || ws.readyState !== WebSocket.OPEN)
|
|
71
74
|
return;
|
|
75
|
+
const request = crypto.randomUUID();
|
|
76
|
+
ws.serializeAttachment?.({ ...ws.deserializeAttachment?.() || {}, stats: { _tag: "Pending", request } });
|
|
72
77
|
let authorized = false;
|
|
73
78
|
await nitroApp.hooks.callHook("skew:authorize-stats", {
|
|
74
79
|
event,
|
|
@@ -76,10 +81,14 @@ export default defineNitroPlugin((nitroApp) => {
|
|
|
76
81
|
authorized = true;
|
|
77
82
|
}
|
|
78
83
|
});
|
|
84
|
+
const attachment = ws.deserializeAttachment?.();
|
|
85
|
+
if (ws.readyState !== WebSocket.OPEN || attachment?.stats?._tag !== "Pending" || attachment.stats.request !== request)
|
|
86
|
+
return;
|
|
79
87
|
if (authorized) {
|
|
80
|
-
ws.serializeAttachment?.({ ...
|
|
88
|
+
ws.serializeAttachment?.({ ...attachment, stats: { _tag: "Authorized" } });
|
|
81
89
|
ws.send(JSON.stringify({ type: "stats", ...getStats(ctx, id) }));
|
|
82
90
|
} else {
|
|
91
|
+
ws.serializeAttachment?.({ ...attachment, stats: void 0 });
|
|
83
92
|
ws.send(JSON.stringify({ type: "stats-unauthorized" }));
|
|
84
93
|
}
|
|
85
94
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { defineNitroPlugin } from "#nuxtseo/nitro";
|
|
2
2
|
export default defineNitroPlugin((nitroApp) => {
|
|
3
3
|
const connections = /* @__PURE__ */ new Map();
|
|
4
|
-
const
|
|
4
|
+
const statsSubscriptions = /* @__PURE__ */ new Map();
|
|
5
5
|
const getStats = (forId) => {
|
|
6
6
|
const versions = {};
|
|
7
7
|
const routes = {};
|
|
@@ -14,9 +14,11 @@ export default defineNitroPlugin((nitroApp) => {
|
|
|
14
14
|
return { total: connections.size, versions, routes, connections: connectionList, yourId: forId };
|
|
15
15
|
};
|
|
16
16
|
const broadcastToSubscribers = () => {
|
|
17
|
-
if (
|
|
17
|
+
if (statsSubscriptions.size === 0)
|
|
18
18
|
return;
|
|
19
|
-
for (const id of
|
|
19
|
+
for (const [id, subscription] of statsSubscriptions) {
|
|
20
|
+
if (subscription._tag !== "Authorized")
|
|
21
|
+
continue;
|
|
20
22
|
const conn = connections.get(id);
|
|
21
23
|
conn?.send({ type: "stats", ...getStats(id) });
|
|
22
24
|
}
|
|
@@ -37,10 +39,15 @@ export default defineNitroPlugin((nitroApp) => {
|
|
|
37
39
|
});
|
|
38
40
|
nitroApp.hooks.hook("skew:connection:close", ({ id }) => {
|
|
39
41
|
connections.delete(id);
|
|
40
|
-
|
|
42
|
+
statsSubscriptions.delete(id);
|
|
41
43
|
broadcastToSubscribers();
|
|
42
44
|
});
|
|
43
45
|
nitroApp.hooks.hook("skew:subscribe-stats", async ({ id, event }) => {
|
|
46
|
+
const conn = connections.get(id);
|
|
47
|
+
if (!conn)
|
|
48
|
+
return;
|
|
49
|
+
const pending = { _tag: "Pending" };
|
|
50
|
+
statsSubscriptions.set(id, pending);
|
|
44
51
|
let authorized = false;
|
|
45
52
|
await nitroApp.hooks.callHook("skew:authorize-stats", {
|
|
46
53
|
id,
|
|
@@ -49,13 +56,14 @@ export default defineNitroPlugin((nitroApp) => {
|
|
|
49
56
|
authorized = true;
|
|
50
57
|
}
|
|
51
58
|
});
|
|
59
|
+
if (statsSubscriptions.get(id) !== pending || connections.get(id) !== conn)
|
|
60
|
+
return;
|
|
52
61
|
if (authorized) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
conn?.send({ type: "stats", ...getStats(id) });
|
|
62
|
+
statsSubscriptions.set(id, { _tag: "Authorized" });
|
|
63
|
+
conn.send({ type: "stats", ...getStats(id) });
|
|
56
64
|
} else {
|
|
57
|
-
|
|
58
|
-
conn
|
|
65
|
+
statsSubscriptions.delete(id);
|
|
66
|
+
conn.send({ type: "stats-unauthorized" });
|
|
59
67
|
}
|
|
60
68
|
});
|
|
61
69
|
});
|
|
@@ -2,7 +2,7 @@ import { defineEventHandler, readBody } from "nuxt/server";
|
|
|
2
2
|
import { useNitroApp } from "#nuxtseo/nitro";
|
|
3
3
|
export default defineEventHandler(async (event) => {
|
|
4
4
|
const body = await readBody(event);
|
|
5
|
-
if (!body
|
|
5
|
+
if (!body || typeof body !== "object" || !("connectionId" in body) || typeof body.connectionId !== "string" || !body.connectionId || !("route" in body) || typeof body.route !== "string" || !body.route) {
|
|
6
6
|
return { ok: false };
|
|
7
7
|
}
|
|
8
8
|
await useNitroApp().hooks.callHook("skew:connection:route-update", {
|
|
@@ -31,7 +31,7 @@ export default defineWebSocketHandler({
|
|
|
31
31
|
},
|
|
32
32
|
message(peer, message) {
|
|
33
33
|
const text = message.text();
|
|
34
|
-
if (text.
|
|
34
|
+
if (text === SKEW_MESSAGE_TYPE.PING) {
|
|
35
35
|
peer.send(JSON.stringify({ type: "pong" }));
|
|
36
36
|
return;
|
|
37
37
|
}
|
|
@@ -41,7 +41,11 @@ export default defineWebSocketHandler({
|
|
|
41
41
|
} catch {
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
|
-
if (data
|
|
44
|
+
if (!data || typeof data !== "object" || !("type" in data))
|
|
45
|
+
return;
|
|
46
|
+
if (data.type === SKEW_MESSAGE_TYPE.PING) {
|
|
47
|
+
peer.send(JSON.stringify({ type: "pong" }));
|
|
48
|
+
} else if (data.type === SKEW_MESSAGE_TYPE.ROUTE_UPDATE && "route" in data && typeof data.route === "string" && data.route) {
|
|
45
49
|
useNitroApp().hooks.callHook("skew:connection:route-update", {
|
|
46
50
|
id: peer.id,
|
|
47
51
|
route: data.route,
|
|
@@ -56,6 +60,7 @@ export default defineWebSocketHandler({
|
|
|
56
60
|
}
|
|
57
61
|
},
|
|
58
62
|
close(peer) {
|
|
59
|
-
|
|
63
|
+
peer.close();
|
|
64
|
+
return useNitroApp().hooks.callHook("skew:connection:close", { id: peer.id, peer });
|
|
60
65
|
}
|
|
61
66
|
});
|