Merge pull request #634 from beyondcode/fix/prevent-collecting-if-stats-disable

[fix] Prevent collecting stats if disabled
This commit is contained in:
rennokki 2020-12-08 20:03:17 +02:00 committed by GitHub
commit 01e3b67280
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 10 deletions

View File

@ -51,6 +51,13 @@ abstract class Controller implements HttpServerInterface
*/ */
protected $channelManager; protected $channelManager;
/**
* The app attached with this request.
*
* @var \BeyondCode\LaravelWebSockets\Apps\App|null
*/
protected $app;
/** /**
* Initialize the request. * Initialize the request.
* *
@ -176,8 +183,7 @@ abstract class Controller implements HttpServerInterface
$laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest)); $laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest));
$this $this->ensureValidAppId($laravelRequest->get('appId'))
->ensureValidAppId($laravelRequest->appId)
->ensureValidSignature($laravelRequest); ->ensureValidSignature($laravelRequest);
// Invoke the controller action // Invoke the controller action
@ -220,7 +226,7 @@ abstract class Controller implements HttpServerInterface
*/ */
public function ensureValidAppId($appId) public function ensureValidAppId($appId)
{ {
if (! App::findById($appId)) { if (! $appId || ! $this->app = App::findById($appId)) {
throw new HttpException(401, "Unknown app id `{$appId}` provided."); throw new HttpException(401, "Unknown app id `{$appId}` provided.");
} }
@ -252,9 +258,7 @@ abstract class Controller implements HttpServerInterface
$signature = "{$request->getMethod()}\n/{$request->path()}\n".Pusher::array_implode('=', '&', $params); $signature = "{$request->getMethod()}\n/{$request->path()}\n".Pusher::array_implode('=', '&', $params);
$app = App::findById($request->get('appId')); $authSignature = hash_hmac('sha256', $signature, $this->app->secret);
$authSignature = hash_hmac('sha256', $signature, $app->secret);
if ($authSignature !== $request->get('auth_signature')) { if ($authSignature !== $request->get('auth_signature')) {
throw new HttpException(401, 'Invalid auth signature provided.'); throw new HttpException(401, 'Invalid auth signature provided.');

View File

@ -49,7 +49,9 @@ class TriggerEvent extends Controller
$request->appId, $request->socket_id, $channelName, (object) $payload $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, [ DashboardLogger::log($request->appId, DashboardLogger::TYPE_API_MESSAGE, [
'event' => $request->name, 'event' => $request->name,

View File

@ -56,7 +56,9 @@ class WebSocketHandler implements MessageComponentInterface
/** @var \GuzzleHttp\Psr7\Request $request */ /** @var \GuzzleHttp\Psr7\Request $request */
$request = $connection->httpRequest; $request = $connection->httpRequest;
StatisticsCollector::connection($connection->app->id); if ($connection->app->statisticsEnabled) {
StatisticsCollector::connection($connection->app->id);
}
$this->channelManager->subscribeToApp($connection->app->id); $this->channelManager->subscribeToApp($connection->app->id);
@ -88,7 +90,9 @@ class WebSocketHandler implements MessageComponentInterface
$message, $connection, $this->channelManager $message, $connection, $this->channelManager
)->respond(); )->respond();
StatisticsCollector::webSocketMessage($connection->app->id); if ($connection->app->statisticsEnabled) {
StatisticsCollector::webSocketMessage($connection->app->id);
}
WebSocketMessageReceived::dispatch( WebSocketMessageReceived::dispatch(
$connection->app->id, $connection->app->id,
@ -109,7 +113,9 @@ class WebSocketHandler implements MessageComponentInterface
->unsubscribeFromAllChannels($connection) ->unsubscribeFromAllChannels($connection)
->then(function (bool $unsubscribed) use ($connection) { ->then(function (bool $unsubscribed) use ($connection) {
if (isset($connection->app)) { if (isset($connection->app)) {
StatisticsCollector::disconnection($connection->app->id); if ($connection->app->statisticsEnabled) {
StatisticsCollector::disconnection($connection->app->id);
}
$this->channelManager->unsubscribeFromApp($connection->app->id); $this->channelManager->unsubscribeFromApp($connection->app->id);