check for internet using ping (#2556)

Co-authored-by: Alex Justesen <1144087+alexjustesen@users.noreply.github.com>
This commit is contained in:
Alex Justesen
2025-12-16 09:24:42 -05:00
committed by GitHub
parent 963fc051ad
commit 03e02b6eb9
7 changed files with 98 additions and 79 deletions
-33
View File
@@ -1,33 +0,0 @@
<?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 connect to the internet.', [$e->getMessage()]);
return false;
}
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ class GetExternalIpAddress
public function handle(?string $url = null): array
{
$url = $url ?? config('speedtest.preflight.get_external_ip_url');
$url = $url ?? config('speedtest.preflight.external_ip_url');
try {
$response = Http::retry(3, 100)
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Actions;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\Ping\Ping;
use Spatie\Ping\PingResult;
class PingHostname
{
use AsAction;
public function handle(?string $hostname = null, int $count = 1): PingResult
{
$hostname = $hostname ?? config('speedtest.preflight.internet_check_hostname');
// Remove protocol if present
$hostname = preg_replace('#^https?://#', '', $hostname);
$ping = (new Ping(
hostname: $hostname,
count: $count,
))->run();
return $ping;
}
}
+7 -3
View File
@@ -2,7 +2,7 @@
namespace App\Jobs;
use App\Actions\CheckInternetConnection;
use App\Actions\PingHostname;
use App\Enums\ResultStatus;
use App\Events\SpeedtestChecking;
use App\Events\SpeedtestFailed;
@@ -44,14 +44,18 @@ class CheckForInternetConnectionJob implements ShouldQueue
SpeedtestChecking::dispatch($this->result);
if (CheckInternetConnection::run() !== false) {
$ping = PingHostname::run();
if ($ping->isSuccess()) {
return;
}
$message = sprintf('Failed to connected to hostname "%s". Error received "%s".', $ping->getHost(), $ping->error()?->value);
$this->result->update([
'data->type' => 'log',
'data->level' => 'error',
'data->message' => 'Failed to connect to the internet.',
'data->message' => $message,
'status' => ResultStatus::Failed,
]);
+1 -1
View File
@@ -20,7 +20,6 @@
"dragonmantank/cron-expression": "^3.6.0",
"filament/filament": "4.1.0",
"filament/spatie-laravel-settings-plugin": "^4.1",
"geerlingguy/ping": "^1.2.1",
"influxdata/influxdb-client-php": "^3.8",
"laravel-notification-channels/telegram": "^6.0",
"laravel/framework": "^12.41.1",
@@ -36,6 +35,7 @@
"spatie/laravel-query-builder": "^6.3.6",
"spatie/laravel-settings": "^3.6.0",
"spatie/laravel-webhook-server": "^3.8.3",
"spatie/ping": "^1.1.1",
"zircote/swagger-php": "^5.7.6"
},
"require-dev": {
Generated
+60 -38
View File
@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "405d221f03e4de1894ce759a9e751448",
"content-hash": "374762e19dbfc99374c14f3f12a4ae3e",
"packages": [
{
"name": "anourvalar/eloquent-serialize",
@@ -1779,43 +1779,6 @@
],
"time": "2025-12-03T09:33:47+00:00"
},
{
"name": "geerlingguy/ping",
"version": "1.2.1",
"source": {
"type": "git",
"url": "https://github.com/geerlingguy/Ping.git",
"reference": "e0206326e23c99e3e8820e24705f8ca517adff93"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/geerlingguy/Ping/zipball/e0206326e23c99e3e8820e24705f8ca517adff93",
"reference": "e0206326e23c99e3e8820e24705f8ca517adff93",
"shasum": ""
},
"type": "library",
"autoload": {
"classmap": [
"JJG/Ping.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jeff Geerling",
"email": "jeff@jeffgeerling.com"
}
],
"description": "A PHP class to ping hosts.",
"support": {
"issues": "https://github.com/geerlingguy/Ping/issues",
"source": "https://github.com/geerlingguy/Ping/tree/1.2.1"
},
"time": "2019-07-29T21:54:12+00:00"
},
{
"name": "graham-campbell/result-type",
"version": "v1.1.3",
@@ -6919,6 +6882,65 @@
],
"time": "2025-02-14T12:55:41+00:00"
},
{
"name": "spatie/ping",
"version": "1.1.1",
"source": {
"type": "git",
"url": "https://github.com/spatie/ping.git",
"reference": "6123a6209148e8919f58121d256f43c75856ab35"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/ping/zipball/6123a6209148e8919f58121d256f43c75856ab35",
"reference": "6123a6209148e8919f58121d256f43c75856ab35",
"shasum": ""
},
"require": {
"php": "^8.4",
"symfony/process": "^7.0"
},
"require-dev": {
"laravel/pint": "^1.0",
"pestphp/pest": "^3.0",
"spatie/pest-expectations": "^1.13",
"spatie/ray": "^1.28"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\Ping\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"role": "Developer"
}
],
"description": "Run an ICMP ping and get structured results",
"homepage": "https://github.com/spatie/ping",
"keywords": [
"ping",
"spatie"
],
"support": {
"issues": "https://github.com/spatie/ping/issues",
"source": "https://github.com/spatie/ping/tree/1.1.1"
},
"funding": [
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2025-08-12T20:58:12+00:00"
},
{
"name": "spatie/shiki-php",
"version": "2.3.2",
+2 -3
View File
@@ -30,11 +30,10 @@ return [
'interface' => env('SPEEDTEST_INTERFACE'),
'preflight' => [
'get_external_ip_url' => env('SPEEDTEST_CHECKINTERNET_URL') ?? env('SPEEDTEST_GET_EXTERNAL_IP_URL', 'https://icanhazip.com'),
'external_ip_url' => env('SPEEDTEST_CHECKINTERNET_URL') ?? env('SPEEDTEST_EXTERNAL_IP_URL', 'https://icanhazip.com'),
'internet_check_hostname' => env('SPEEDTEST_CHECKINTERNET_URL') ?? env('SPEEDTEST_INTERNET_CHECK_HOSTNAME', 'icanhazip.com'),
],
'checkinternet_url' => env('SPEEDTEST_CHECKINTERNET_URL'), // ! DEPRECATED, use preflight.get_external_ip_url instead
/**
* IP filtering settings.
*/