Skip to content

Commit 077957a

Browse files
authored
[Bug] Separate CheckInternetConnection from GetExternalIpAddress (alexjustesen#2120)
1 parent 071cea4 commit 077957a

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Actions;
4+
5+
use Illuminate\Support\Facades\Http;
6+
use Illuminate\Support\Facades\Log;
7+
use Illuminate\Support\Str;
8+
use Lorisleiva\Actions\Concerns\AsAction;
9+
use Throwable;
10+
11+
class CheckInternetConnection
12+
{
13+
use AsAction;
14+
15+
public function handle(): bool|string
16+
{
17+
try {
18+
$response = Http::retry(3, 100)
19+
->timeout(5)
20+
->get(config('speedtest.checkinternet_url'));
21+
22+
if (! $response->ok()) {
23+
return false;
24+
}
25+
26+
return Str::trim($response->body());
27+
} catch (Throwable $e) {
28+
Log::error('Failed to check internet connection.', [$e->getMessage()]);
29+
30+
return false;
31+
}
32+
}
33+
}

app/Jobs/CheckForInternetConnectionJob.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Jobs;
44

5-
use App\Actions\GetExternalIpAddress;
5+
use App\Actions\CheckInternetConnection;
66
use App\Enums\ResultStatus;
77
use App\Events\SpeedtestChecking;
88
use App\Events\SpeedtestFailed;
@@ -44,7 +44,7 @@ public function handle(): void
4444

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

47-
if (GetExternalIpAddress::run() !== false) {
47+
if (CheckInternetConnection::run() !== false) {
4848
return;
4949
}
5050

config/speedtest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

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

31+
'checkinternet_url' => env('SPEEDTEST_CHECKINTERNET_URL', 'https://icanhazip.com'),
32+
3133
/**
3234
* IP filtering settings.
3335
*/

0 commit comments

Comments
 (0)