diff --git a/src/API/Controller.php b/src/API/Controller.php index 74267de537..079637afd9 100644 --- a/src/API/Controller.php +++ b/src/API/Controller.php @@ -51,6 +51,13 @@ abstract class Controller implements HttpServerInterface */ protected $channelManager; + /** + * The app attached with this request. + * + * @var \BeyondCode\LaravelWebSockets\Apps\App|null + */ + protected $app; + /** * Initialize the request. * @@ -176,8 +183,7 @@ protected function handleRequest(ConnectionInterface $connection) $laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest)); - $this - ->ensureValidAppId($laravelRequest->appId) + $this->ensureValidAppId($laravelRequest->get('appId')) ->ensureValidSignature($laravelRequest); // Invoke the controller action @@ -220,7 +226,7 @@ protected function sendAndClose(ConnectionInterface $connection, $response) */ public function ensureValidAppId($appId) { - if (! App::findById($appId)) { + if (! $appId || ! $this->app = App::findById($appId)) { throw new HttpException(401, "Unknown app id `{$appId}` provided."); } @@ -252,9 +258,7 @@ protected function ensureValidSignature(Request $request) $signature = "{$request->getMethod()}\n/{$request->path()}\n".Pusher::array_implode('=', '&', $params); - $app = App::findById($request->get('appId')); - - $authSignature = hash_hmac('sha256', $signature, $app->secret); + $authSignature = hash_hmac('sha256', $signature, $this->app->secret); if ($authSignature !== $request->get('auth_signature')) { throw new HttpException(401, 'Invalid auth signature provided.'); diff --git a/src/API/TriggerEvent.php b/src/API/TriggerEvent.php index 5bb67381a5..7a3d986b87 100644 --- a/src/API/TriggerEvent.php +++ b/src/API/TriggerEvent.php @@ -49,7 +49,9 @@ public function __invoke(Request $request) $request->appId, $request->socket_id, $channelName, (object) $payload ); - StatisticsCollector::apiMessage($request->appId); + if ($this->app->statisticsEnabled) { + StatisticsCollector::apiMessage($request->appId); + } DashboardLogger::log($request->appId, DashboardLogger::TYPE_API_MESSAGE, [ 'event' => $request->name, diff --git a/src/Server/WebSocketHandler.php b/src/Server/WebSocketHandler.php index 8bec3895a8..855532dd3f 100644 --- a/src/Server/WebSocketHandler.php +++ b/src/Server/WebSocketHandler.php @@ -56,7 +56,9 @@ public function onOpen(ConnectionInterface $connection) /** @var \GuzzleHttp\Psr7\Request $request */ $request = $connection->httpRequest; - StatisticsCollector::connection($connection->app->id); + if ($connection->app->statisticsEnabled) { + StatisticsCollector::connection($connection->app->id); + } $this->channelManager->subscribeToApp($connection->app->id); @@ -88,7 +90,9 @@ public function onMessage(ConnectionInterface $connection, MessageInterface $mes $message, $connection, $this->channelManager )->respond(); - StatisticsCollector::webSocketMessage($connection->app->id); + if ($connection->app->statisticsEnabled) { + StatisticsCollector::webSocketMessage($connection->app->id); + } WebSocketMessageReceived::dispatch( $connection->app->id, @@ -109,7 +113,9 @@ public function onClose(ConnectionInterface $connection) ->unsubscribeFromAllChannels($connection) ->then(function (bool $unsubscribed) use ($connection) { if (isset($connection->app)) { - StatisticsCollector::disconnection($connection->app->id); + if ($connection->app->statisticsEnabled) { + StatisticsCollector::disconnection($connection->app->id); + } $this->channelManager->unsubscribeFromApp($connection->app->id);