mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
feat(websocket): enhance WebSocket server with request validation and client instantiation
- Added request validation to ensure user authentication before establishing WebSocket connections. - Refactored WebSocket client instantiation to simplify connection management.
This commit is contained in:
@@ -3,6 +3,7 @@ import { applyWSSHandler } from "@trpc/server/adapters/ws";
|
|||||||
import { WebSocketServer } from "ws";
|
import { WebSocketServer } from "ws";
|
||||||
import { appRouter } from "../api/root";
|
import { appRouter } from "../api/root";
|
||||||
import { createTRPCContext } from "../api/trpc";
|
import { createTRPCContext } from "../api/trpc";
|
||||||
|
import { validateRequest } from "@dokploy/server/lib/auth";
|
||||||
|
|
||||||
export const setupDrawerLogsWebSocketServer = (
|
export const setupDrawerLogsWebSocketServer = (
|
||||||
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
|
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
|
||||||
@@ -32,8 +33,13 @@ export const setupDrawerLogsWebSocketServer = (
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Return cleanup function
|
wssTerm.on("connection", async (ws, req) => {
|
||||||
return () => {
|
const _url = new URL(req.url || "", `http://${req.headers.host}`);
|
||||||
wssTerm.close();
|
const { user, session } = await validateRequest(req);
|
||||||
};
|
|
||||||
|
if (!user || !session) {
|
||||||
|
ws.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -27,28 +27,15 @@ const getWsUrl = () => {
|
|||||||
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
|
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
|
||||||
const host = window.location.host;
|
const host = window.location.host;
|
||||||
|
|
||||||
// Use the base URL for all tRPC WebSocket connections
|
|
||||||
return `${protocol}${host}/drawer-logs`;
|
return `${protocol}${host}/drawer-logs`;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Singleton WebSocket client instance
|
const wsClient =
|
||||||
let wsClientInstance: ReturnType<typeof createWSClient> | null = null;
|
typeof window !== "undefined"
|
||||||
|
? createWSClient({
|
||||||
const getWsClient = () => {
|
url: getWsUrl() || "",
|
||||||
if (typeof window === "undefined") return null;
|
})
|
||||||
|
: null;
|
||||||
if (!wsClientInstance) {
|
|
||||||
wsClientInstance = createWSClient({
|
|
||||||
url: getWsUrl() || "",
|
|
||||||
onClose: () => {
|
|
||||||
// Reset the instance when connection closes so it can be recreated
|
|
||||||
wsClientInstance = null;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return wsClientInstance;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** A set of type-safe react-query hooks for your tRPC API. */
|
/** A set of type-safe react-query hooks for your tRPC API. */
|
||||||
export const api = createTRPCNext<AppRouter>({
|
export const api = createTRPCNext<AppRouter>({
|
||||||
@@ -70,7 +57,7 @@ export const api = createTRPCNext<AppRouter>({
|
|||||||
splitLink({
|
splitLink({
|
||||||
condition: (op) => op.type === "subscription",
|
condition: (op) => op.type === "subscription",
|
||||||
true: wsLink({
|
true: wsLink({
|
||||||
client: getWsClient()!,
|
client: wsClient!,
|
||||||
}),
|
}),
|
||||||
false: splitLink({
|
false: splitLink({
|
||||||
condition: (op) => op.input instanceof FormData,
|
condition: (op) => op.input instanceof FormData,
|
||||||
|
|||||||
Reference in New Issue
Block a user