Starting to resolve the issue on prediction5

This commit is contained in:
Your Name 2026-10-26 15:20:29 +02:00
parent 5469432bd6
commit dacf35f8e2
7 changed files with 74 additions and 54 deletions

View File

@ -115,6 +115,7 @@ INITIAL_SETUP_TOKEN=your-random-setup-token-min-32-chars
BREAK_GLASS_TOKEN= BREAK_GLASS_TOKEN=
# Figées au build Docker (prediqo-web) — mode HTTP VPS : # Figées au build Docker (prediqo-web) — mode HTTP VPS :
# Security/Support/Candle WS prefer same-origin /ws/ via nginx (9080/9443). Never publish :4001.
VITE_API_BASE_URL=http://prediqo.redirectme.net:9080 VITE_API_BASE_URL=http://prediqo.redirectme.net:9080
VITE_WS_ENABLED=true VITE_WS_ENABLED=true
VITE_WS_URL=ws://prediqo.redirectme.net:9080/ws/v1/games/candle-predict VITE_WS_URL=ws://prediqo.redirectme.net:9080/ws/v1/games/candle-predict

View File

@ -174,7 +174,9 @@ Recommandé en **dev local** ; en **production**, activez ClamAV dès que des up
## 3. WebSocket sécurité (rappel) ## 3. WebSocket sécurité (rappel)
Les notifications admin temps réel utilisent `/ws/v1/security` sur le processus `backend-ws`. Nginx route déjà tout le préfixe `/ws/` vers `backend-ws:4001`. Les notifications admin temps réel utilisent `/ws/v1/security` sur le processus `backend-ws`. Nginx route déjà tout le préfixe `/ws/` vers `backend-ws:4001` (interne Docker).
Le frontend résout l’URL en **same-origin** (`ws(s)://<hôte>:9080|9443/ws/v1/security`) — **ne pas** exposer ni bakee `ws://…:4001` en production (timeout navigateur).
Vérifiez dans `.env` : Vérifiez dans `.env` :

View File

@ -1,13 +1,19 @@
# Local dev (npm run dev — Vite sur :5173) # Local dev (npm run dev — Vite sur :5173)
# Security / Support / Candle WS use same-origin in production builds.
# In Vite DEV only, explicit VITE_WS_* may point at backend-ws :4001.
VITE_API_BASE_URL=http://localhost:4000 VITE_API_BASE_URL=http://localhost:4000
VITE_WS_ENABLED=true VITE_WS_ENABLED=true
VITE_WS_URL=ws://localhost:4001/ws/v1/games/candle-predict VITE_WS_URL=ws://localhost:4001/ws/v1/games/candle-predict
# Optional overrides (same host as the page in prod; :4001 only for local DEV):
# VITE_WS_SECURITY_URL=ws://localhost:4001/ws/v1/security
# VITE_WS_SUPPORT_URL=ws://localhost:4001/ws/v1/support
# Observability (optional) # Observability (optional)
# VITE_SENTRY_DSN=https://...@sentry.io/... # VITE_SENTRY_DSN=https://...@sentry.io/...
# VITE_SENTRY_TRACES_SAMPLE_RATE=0.1 # VITE_SENTRY_TRACES_SAMPLE_RATE=0.1
# VPS HTTP (port 9080 — figé au build Docker via .env racine) # VPS HTTP (port 9080 — figé au build Docker via .env racine)
# Do NOT use public :4001 — nginx proxies /ws/ on 9080/9443.
# VITE_API_BASE_URL=http://prediqo.redirectme.net:9080 # VITE_API_BASE_URL=http://prediqo.redirectme.net:9080
# VITE_WS_URL=ws://prediqo.redirectme.net:9080/ws/v1/games/candle-predict # VITE_WS_URL=ws://prediqo.redirectme.net:9080/ws/v1/games/candle-predict

View File

@ -1,4 +1,5 @@
import type { GameState } from '../types'; import type { GameState } from '../types';
import { isViteWsEnabled, resolveSameOriginWsUrl } from '../../../shared/ws/resolveSameOriginWsUrl';
export type WsClientMessage = export type WsClientMessage =
| { type: 'subscribe'; intervalMinutes: number } | { type: 'subscribe'; intervalMinutes: number }
@ -46,38 +47,12 @@ export type WsServerMessage =
| { type: 'error'; code: string; message: string }; | { type: 'error'; code: string; message: string };
export function resolveWsBaseUrl(): string { export function resolveWsBaseUrl(): string {
const explicit = (import.meta.env.VITE_WS_URL as string | undefined)?.replace(/\/$/, ''); return resolveSameOriginWsUrl(
'/ws/v1/games/candle-predict',
if (typeof window !== 'undefined') { import.meta.env.VITE_WS_URL as string | undefined
const pageHost = window.location.host; );
if (explicit) {
try {
const configured = new URL(explicit);
// Use baked URL only when it targets the same host the user is browsing.
if (configured.host === pageHost) return explicit;
} catch {
/* fall through to same-origin */
}
}
// Prefer same-origin /ws/ (nginx on :9080 or :9443) so a stale build URL cannot break live updates.
const u = new URL(window.location.origin);
u.protocol = u.protocol === 'https:' ? 'wss:' : 'ws:';
u.pathname = '/ws/v1/games/candle-predict';
u.search = '';
return u.toString().replace(/\/$/, '');
}
if (explicit) return explicit;
const api = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000';
const url = new URL(api);
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
url.port = import.meta.env.VITE_WS_PORT || '4001';
url.pathname = '/ws/v1/games/candle-predict';
url.search = '';
return url.toString().replace(/\/$/, '');
} }
export function isWsEnabled(): boolean { export function isWsEnabled(): boolean {
return import.meta.env.VITE_WS_ENABLED === 'true'; return isViteWsEnabled();
} }

View File

@ -1,3 +1,5 @@
import { isViteWsEnabled, resolveSameOriginWsUrl } from '../../../shared/ws/resolveSameOriginWsUrl';
export type SecurityWsClientMessage = { type: 'ping' } | { type: 'subscribe_admin' }; export type SecurityWsClientMessage = { type: 'ping' } | { type: 'subscribe_admin' };
export type SecurityWsServerMessage = export type SecurityWsServerMessage =
@ -25,18 +27,12 @@ export type SecurityWsServerMessage =
| { type: 'error'; code: string; message: string }; | { type: 'error'; code: string; message: string };
export function resolveSecurityWsBaseUrl(): string { export function resolveSecurityWsBaseUrl(): string {
const explicit = import.meta.env.VITE_WS_SECURITY_URL as string | undefined; return resolveSameOriginWsUrl(
if (explicit) return explicit.replace(/\/$/, ''); '/ws/v1/security',
import.meta.env.VITE_WS_SECURITY_URL as string | undefined
const api = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000'; );
const url = new URL(api);
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
url.port = import.meta.env.VITE_WS_PORT || '4001';
url.pathname = '/ws/v1/security';
url.search = '';
return url.toString().replace(/\/$/, '');
} }
export function isSecurityWsEnabled(): boolean { export function isSecurityWsEnabled(): boolean {
return import.meta.env.VITE_WS_ENABLED === 'true'; return isViteWsEnabled();
} }

View File

@ -1,4 +1,5 @@
import type { SupportMessage } from '../types'; import type { SupportMessage } from '../types';
import { isViteWsEnabled, resolveSameOriginWsUrl } from '../../../shared/ws/resolveSameOriginWsUrl';
export type SupportWsClientMessage = export type SupportWsClientMessage =
| { type: 'ping' } | { type: 'ping' }
@ -31,18 +32,12 @@ export type SupportWsServerMessage =
| { type: 'error'; code: string; message: string }; | { type: 'error'; code: string; message: string };
export function resolveSupportWsBaseUrl(): string { export function resolveSupportWsBaseUrl(): string {
const explicit = import.meta.env.VITE_WS_SUPPORT_URL as string | undefined; return resolveSameOriginWsUrl(
if (explicit) return explicit.replace(/\/$/, ''); '/ws/v1/support',
import.meta.env.VITE_WS_SUPPORT_URL as string | undefined
const api = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000'; );
const url = new URL(api);
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
url.port = import.meta.env.VITE_WS_PORT || '4001';
url.pathname = '/ws/v1/support';
url.search = '';
return url.toString().replace(/\/$/, '');
} }
export function isSupportWsEnabled(): boolean { export function isSupportWsEnabled(): boolean {
return import.meta.env.VITE_WS_ENABLED === 'true'; return isViteWsEnabled();
} }

View File

@ -0,0 +1,45 @@
/**
* Resolve a WebSocket base URL that prefers same-origin `/ws/...` (nginx :9080 / :9443).
* Avoids baking a public `:4001` host that browsers cannot reach on the VPS.
*/
export function resolveSameOriginWsUrl(pathname: string, explicitUrl?: string): string {
const explicit = explicitUrl?.replace(/\/$/, '') || undefined;
const normalizedPath = pathname.startsWith('/') ? pathname : `/${pathname}`;
if (typeof window !== 'undefined') {
const pageHost = window.location.host;
if (explicit) {
try {
const configured = new URL(explicit);
// Same host as the page (typical VPS nginx :9080 / :9443).
if (configured.host === pageHost) return explicit;
// Vite dev has no /ws/ proxy — keep explicit ws://localhost:4001/...
if (import.meta.env.DEV) return explicit;
} catch {
/* fall through to same-origin */
}
}
// Production / Docker web: same-origin /ws/ via nginx (never public :4001).
const u = new URL(window.location.origin);
u.protocol = u.protocol === 'https:' ? 'wss:' : 'ws:';
u.pathname = normalizedPath;
u.search = '';
return u.toString().replace(/\/$/, '');
}
if (explicit) return explicit;
// Non-browser / SSR fallback for local tooling only.
const api = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000';
const url = new URL(api);
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
url.port = import.meta.env.VITE_WS_PORT || '4001';
url.pathname = normalizedPath;
url.search = '';
return url.toString().replace(/\/$/, '');
}
export function isViteWsEnabled(): boolean {
return import.meta.env.VITE_WS_ENABLED === 'true';
}