diff --git a/.env.example b/.env.example index 9cce79d..4ded324 100644 --- a/.env.example +++ b/.env.example @@ -50,7 +50,9 @@ AWS_USE_PATH_STYLE_ENDPOINT=false # Storefront widget + Nova inbox 1:1 target (users.id) CHAT_SUPPORT_USER_ID= -# Laravel Reverb (`php artisan reverb:start`) +# Laravel Reverb (`php artisan reverb:start` / supervisor). +# PHP broadcasts to this host. Browsers use nginx at wss://mm.com.tm/app. +# Local: REVERB_APP_ID= REVERB_APP_KEY= REVERB_APP_SECRET= @@ -59,6 +61,10 @@ REVERB_PORT=8080 REVERB_SCHEME=http REVERB_SERVER_HOST=0.0.0.0 REVERB_SERVER_PORT=8080 +# Production (keep SERVER_* on 8080; PHP can still use 127.0.0.1:8080): +# REVERB_HOST=127.0.0.1 +# REVERB_PORT=8080 +# REVERB_SCHEME=http VITE_REVERB_APP_KEY="${REVERB_APP_KEY}" VITE_REVERB_HOST="${REVERB_HOST}" diff --git a/app/Http/Controllers/Nova/SupportChatController.php b/app/Http/Controllers/Nova/SupportChatController.php index 5258e43..5b31899 100644 --- a/app/Http/Controllers/Nova/SupportChatController.php +++ b/app/Http/Controllers/Nova/SupportChatController.php @@ -31,12 +31,7 @@ class SupportChatController extends Controller $key ? $this->panelAsset('js/echo.iife.js') : null, $this->panelAsset('js/chat.js'), ])), - 'pusher' => [ - 'key' => $key, - 'host' => config('broadcasting.connections.reverb.options.host'), - 'port' => (int) config('broadcasting.connections.reverb.options.port', 8080), - 'scheme' => config('broadcasting.connections.reverb.options.scheme', 'http'), - ], + 'pusher' => $this->chatPusherConfig($key), ]); } @@ -110,4 +105,36 @@ class SupportChatController extends Controller return (string) file_get_contents($path); } + + /** + * Browser websocket host. Reverb itself stays on 127.0.0.1:8080; + * production browsers connect through nginx at wss://mm.com.tm/app. + * + * @return array{key: mixed, host: string, port: int, scheme: string} + */ + private function chatPusherConfig(mixed $key): array + { + $requestHost = request()->getHost(); + $public = ! in_array($requestHost, ['127.0.0.1', 'localhost', '::1', '0.0.0.0'], true); + + if ($public) { + $https = request()->secure() + || request()->header('X-Forwarded-Proto') === 'https' + || str_contains($requestHost, '.'); + + return [ + 'key' => $key, + 'host' => $requestHost, + 'port' => $https ? 443 : 80, + 'scheme' => $https ? 'https' : 'http', + ]; + } + + return [ + 'key' => $key, + 'host' => (string) config('broadcasting.connections.reverb.options.host'), + 'port' => (int) config('broadcasting.connections.reverb.options.port', 8080), + 'scheme' => (string) config('broadcasting.connections.reverb.options.scheme', 'http'), + ]; + } } diff --git a/nginx/reverb.conf b/nginx/reverb.conf new file mode 100644 index 0000000..794e208 --- /dev/null +++ b/nginx/reverb.conf @@ -0,0 +1,25 @@ +# Laravel Reverb websocket proxy for mm.com.tm +# +# Paste this location into the existing HTTPS server { } for mm.com.tm, +# BEFORE any frontend SPA catch-all. +# +# Reverb (supervisor) stays on 127.0.0.1:8080. +# Browsers connect to wss://mm.com.tm/app/... + +location /app { + proxy_http_version 1.1; + proxy_set_header Host $http_host; + proxy_set_header Scheme $scheme; + proxy_set_header SERVER_PORT $server_port; + proxy_set_header REMOTE_ADDR $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "Upgrade"; + + proxy_connect_timeout 60s; + proxy_send_timeout 60s; + proxy_read_timeout 86400s; + proxy_buffering off; + + proxy_pass http://127.0.0.1:8080; +} diff --git a/public/assets/panel/js/chat.js b/public/assets/panel/js/chat.js index 1f61026..20d925d 100644 --- a/public/assets/panel/js/chat.js +++ b/public/assets/panel/js/chat.js @@ -266,6 +266,28 @@ state.echo.private(`chat.${conversationId}`).listen(".message.sent", upsertIncoming); } + function isLoopbackHost(host) { + return !host || host === "127.0.0.1" || host === "localhost" || host === "0.0.0.0" || host === "::1"; + } + + function echoEndpoint(pusher) { + const pageHost = window.location.hostname; + if (!isLoopbackHost(pageHost)) { + const https = window.location.protocol === "https:"; + return { + host: pageHost, + port: https ? 443 : 80, + forceTLS: https, + }; + } + + return { + host: pusher.host || "127.0.0.1", + port: pusher.port || 8080, + forceTLS: (pusher.scheme || "http") === "https", + }; + } + function bootEcho() { const pusher = config.pusher || {}; const EchoClass = window.Echo; @@ -273,17 +295,16 @@ return; } - const scheme = pusher.scheme || "https"; - const forceTLS = scheme === "https"; + const endpoint = echoEndpoint(pusher); try { state.echo = new EchoClass({ broadcaster: "reverb", key: pusher.key, - wsHost: pusher.host || undefined, - wsPort: pusher.port || undefined, - wssPort: pusher.port || undefined, - forceTLS, + wsHost: endpoint.host, + wsPort: endpoint.port, + wssPort: endpoint.port, + forceTLS: endpoint.forceTLS, enabledTransports: ["ws", "wss"], disableStats: true, cluster: "mt1",