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
20 changes: 15 additions & 5 deletions app/Actions/GetExternalIpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ class GetExternalIpAddress
{
use AsAction;

public function handle(): bool|string
public function handle(?string $url = null): array
{
$url = $url ?? config('speedtest.preflight.get_external_ip_url');

try {
$response = Http::retry(3, 100)
->timeout(5)
->get(url: 'https://icanhazip.com/');
->get(url: $url);
} catch (Throwable $e) {
Log::error('Failed to fetch external IP address.', [$e->getMessage()]);
$message = sprintf('Failed to fetch external IP address from "%s". See the logs for more details.', $url);

Log::error($message, [$e->getMessage()]);

return false;
return [
'ok' => false,
'body' => $message,
];
}

return Str::trim($response->body());
return [
'ok' => $response->ok(),
'body' => Str::of($response->body())->trim()->toString(),
];
}
}
18 changes: 17 additions & 1 deletion app/Jobs/Ookla/SkipSpeedtestJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Actions\GetExternalIpAddress;
use App\Enums\ResultStatus;
use App\Events\SpeedtestFailed;
use App\Events\SpeedtestSkipped;
use App\Helpers\Network;
use App\Models\Result;
Expand Down Expand Up @@ -47,8 +48,23 @@ public function handle(): void

$externalIp = GetExternalIpAddress::run();

if ($externalIp['ok'] === false) {
$this->result->update([
'data->type' => 'log',
'data->level' => 'error',
'data->message' => $externalIp['body'],
'status' => ResultStatus::Failed,
]);

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

$this->batch()->cancel();

return;
}

$shouldSkip = $this->shouldSkip(
externalIp: $externalIp,
externalIp: $externalIp['body'],
);

if ($shouldSkip === false) {
Expand Down
6 changes: 5 additions & 1 deletion config/speedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@

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

'checkinternet_url' => env('SPEEDTEST_CHECKINTERNET_URL', 'https://icanhazip.com'),
'preflight' => [
'get_external_ip_url' => env('SPEEDTEST_CHECKINTERNET_URL') ?? env('SPEEDTEST_GET_EXTERNAL_IP_URL', 'https://icanhazip.com'),
],

'checkinternet_url' => env('SPEEDTEST_CHECKINTERNET_URL'), // ! DEPRECATED, use preflight.get_external_ip_url instead

/**
* IP filtering settings.
Expand Down