Skip to content

Commit 03e02b6

Browse files
authored
check for internet using ping (alexjustesen#2556)
Co-authored-by: Alex Justesen <[email protected]>
1 parent 963fc05 commit 03e02b6

File tree

7 files changed

+98
-79
lines changed

7 files changed

+98
-79
lines changed

app/Actions/CheckInternetConnection.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

app/Actions/GetExternalIpAddress.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GetExternalIpAddress
1414

1515
public function handle(?string $url = null): array
1616
{
17-
$url = $url ?? config('speedtest.preflight.get_external_ip_url');
17+
$url = $url ?? config('speedtest.preflight.external_ip_url');
1818

1919
try {
2020
$response = Http::retry(3, 100)

app/Actions/PingHostname.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Actions;
4+
5+
use Lorisleiva\Actions\Concerns\AsAction;
6+
use Spatie\Ping\Ping;
7+
use Spatie\Ping\PingResult;
8+
9+
class PingHostname
10+
{
11+
use AsAction;
12+
13+
public function handle(?string $hostname = null, int $count = 1): PingResult
14+
{
15+
$hostname = $hostname ?? config('speedtest.preflight.internet_check_hostname');
16+
17+
// Remove protocol if present
18+
$hostname = preg_replace('#^https?://#', '', $hostname);
19+
20+
$ping = (new Ping(
21+
hostname: $hostname,
22+
count: $count,
23+
))->run();
24+
25+
return $ping;
26+
}
27+
}

app/Jobs/CheckForInternetConnectionJob.php

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

33
namespace App\Jobs;
44

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

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

47-
if (CheckInternetConnection::run() !== false) {
47+
$ping = PingHostname::run();
48+
49+
if ($ping->isSuccess()) {
4850
return;
4951
}
5052

53+
$message = sprintf('Failed to connected to hostname "%s". Error received "%s".', $ping->getHost(), $ping->error()?->value);
54+
5155
$this->result->update([
5256
'data->type' => 'log',
5357
'data->level' => 'error',
54-
'data->message' => 'Failed to connect to the internet.',
58+
'data->message' => $message,
5559
'status' => ResultStatus::Failed,
5660
]);
5761

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"dragonmantank/cron-expression": "^3.6.0",
2121
"filament/filament": "4.1.0",
2222
"filament/spatie-laravel-settings-plugin": "^4.1",
23-
"geerlingguy/ping": "^1.2.1",
2423
"influxdata/influxdb-client-php": "^3.8",
2524
"laravel-notification-channels/telegram": "^6.0",
2625
"laravel/framework": "^12.41.1",
@@ -36,6 +35,7 @@
3635
"spatie/laravel-query-builder": "^6.3.6",
3736
"spatie/laravel-settings": "^3.6.0",
3837
"spatie/laravel-webhook-server": "^3.8.3",
38+
"spatie/ping": "^1.1.1",
3939
"zircote/swagger-php": "^5.7.6"
4040
},
4141
"require-dev": {

composer.lock

Lines changed: 60 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/speedtest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@
3030
'interface' => env('SPEEDTEST_INTERFACE'),
3131

3232
'preflight' => [
33-
'get_external_ip_url' => env('SPEEDTEST_CHECKINTERNET_URL') ?? env('SPEEDTEST_GET_EXTERNAL_IP_URL', 'https://icanhazip.com'),
33+
'external_ip_url' => env('SPEEDTEST_CHECKINTERNET_URL') ?? env('SPEEDTEST_EXTERNAL_IP_URL', 'https://icanhazip.com'),
34+
'internet_check_hostname' => env('SPEEDTEST_CHECKINTERNET_URL') ?? env('SPEEDTEST_INTERNET_CHECK_HOSTNAME', 'icanhazip.com'),
3435
],
3536

36-
'checkinternet_url' => env('SPEEDTEST_CHECKINTERNET_URL'), // ! DEPRECATED, use preflight.get_external_ip_url instead
37-
3837
/**
3938
* IP filtering settings.
4039
*/

0 commit comments

Comments
 (0)