Skip to content
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
33 changes: 33 additions & 0 deletions app/Actions/CheckInternetConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Actions;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Lorisleiva\Actions\Concerns\AsAction;
use Throwable;

class CheckInternetConnection
{
use AsAction;

public function handle(): bool|string
{
try {
$response = Http::retry(3, 100)
->timeout(5)
->get(config('speedtest.checkinternet_url'));

if (! $response->ok()) {
return false;
}

return Str::trim($response->body());
} catch (Throwable $e) {
Log::error('Failed to check internet connection.', [$e->getMessage()]);

return false;
}
}
}
4 changes: 2 additions & 2 deletions app/Jobs/CheckForInternetConnectionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Jobs;

use App\Actions\GetExternalIpAddress;
use App\Actions\CheckInternetConnection;
use App\Enums\ResultStatus;
use App\Events\SpeedtestChecking;
use App\Events\SpeedtestFailed;
Expand Down Expand Up @@ -44,7 +44,7 @@ public function handle(): void

SpeedtestChecking::dispatch($this->result);

if (GetExternalIpAddress::run() !== false) {
if (CheckInternetConnection::run() !== false) {
return;
}

Expand Down
2 changes: 2 additions & 0 deletions config/speedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

'interface' => env('SPEEDTEST_INTERFACE'),

'checkinternet_url' => env('SPEEDTEST_CHECKINTERNET_URL', 'https://icanhazip.com'),

/**
* IP filtering settings.
*/
Expand Down