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
23 changes: 9 additions & 14 deletions app/Actions/GetExternalIpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,28 @@

namespace App\Actions;

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

class GetExternalIpAddress
{
use AsAction;

public function handle(): bool|string
{
$externalIp = Cache::remember('external_ip', 30, function (): bool|string {
try {
$response = Http::retry(3, 100)
->get('https://icanhazip.com/');
->timeout(5)
->get(url: 'https://icanhazip.com/');
} catch (Throwable $e) {
Log::error('Failed to fetch external IP address.', [$e->getMessage()]);

if ($response->failed()) {
$message = sprintf('Failed to fetch external IP address, %d', $response->status());
return false;
}

Log::warning($message);

return false;
}

return Str::trim($response->body());
});

return $externalIp;
return Str::trim($response->body());
}
}
25 changes: 12 additions & 13 deletions app/Actions/GetOoklaSpeedtestServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace App\Actions;

use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Lorisleiva\Actions\Concerns\AsAction;
use Throwable;

class GetOoklaSpeedtestServers
{
Expand All @@ -20,19 +19,19 @@ public function handle(): array
'limit' => 20,
];

$response = Http::retry(3, 250)
->timeout(5)
->get(url: 'https://www.speedtest.net/api/js/servers', query: $query)
->throw(function (Response $response, RequestException $e) {
Log::error($e);
try {
$response = Http::retry(3, 250)
->timeout(5)
->get(url: 'https://www.speedtest.net/api/js/servers', query: $query);
} catch (Throwable $e) {
Log::error('Unable to retrieve Ookla servers.', [$e->getMessage()]);

return [
'0' => 'There was an issue retrieving Ookla speedtest servers, check the logs for more info.',
];
})
->collect();
return [
'⚠️ Unable to retrieve Ookla servers, check internet connection and see logs.',
];
}

return $response->mapWithKeys(function (array $item, int $key) {
return $response->collect()->mapWithKeys(function (array $item, int $key) {
return [
$item['id'] => $item['sponsor'].' ('.$item['name'].', '.$item['id'].')',
];
Expand Down
1 change: 0 additions & 1 deletion app/Filament/Pages/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ protected function getHeaderActions(): array
Forms\Components\Select::make('server_id')
->label('Select Server')
->helperText('Leave empty to run the speedtest without specifying a server.')
->options(fn (): array => GetOoklaSpeedtestServers::run())
->options(function (): array {
return array_filter([
'Manual servers' => Ookla::getConfigServers(),
Expand Down