Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

[fix] Prevent collecting stats if disabled #634

Merged
merged 2 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/API/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.");
}

Expand Down Expand Up @@ -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.');
Expand Down
4 changes: 3 additions & 1 deletion src/API/TriggerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions src/Server/WebSocketHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand All @@ -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);

Expand Down