Skip to content

Commit

Permalink
refactored speedtest job to use actions and helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjustesen committed Nov 20, 2024
1 parent b90a940 commit 5e386d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 106 deletions.
55 changes: 36 additions & 19 deletions app/Jobs/Speedtests/ExecuteOoklaSpeedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

namespace App\Jobs\Speedtests;

use App\Actions\Helpers\GetExternalIpAddress;
use App\Enums\ResultStatus;
use App\Events\SpeedtestCompleted;
use App\Events\SpeedtestFailed;
use App\Events\SpeedtestSkipped;
use App\Helpers\Network;
use App\Models\Result;
use App\Services\PublicIpService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\URL;
use JJG\Ping;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
Expand All @@ -37,7 +37,6 @@ class ExecuteOoklaSpeedtest implements ShouldBeUnique, ShouldQueue
public function __construct(
public Result $result,
public ?int $serverId = null,
protected PublicIpService $publicIpService = new PublicIpService
) {}

/**
Expand All @@ -49,19 +48,10 @@ public function handle(): void
return;
}

// Fetch public IP data using the PublicIpService
$ipData = $this->publicIpService->getPublicIp();
$currentIp = $ipData['ip'] ?? 'unknown';
$isp = $ipData['isp'] ?? 'unknown'; // Get the ISP value here
$externalIp = GetExternalIpAddress::run();

// Retrieve SPEEDTEST_SKIP_IP Settings
$skipSettings = array_filter(array_map('trim', explode(';', config('speedtest.skip_ip'))));

// Check Each Skip Setting
$skipMessage = $this->publicIpService->shouldSkipIp($currentIp, $skipSettings);
if ($skipMessage) {
// Pass the $isp along with $currentIp and $skipMessage
$this->markAsSkipped($skipMessage, $currentIp, $isp);
if ($skipMessage = $this->shouldSkip($externalIp) !== false) {
$this->markAsSkipped($skipMessage, $externalIp);

return;
}
Expand Down Expand Up @@ -158,17 +148,16 @@ public function handle(): void
/**
* Mark the test as skipped with a specific message.
*/
protected function markAsSkipped(string $message, string $currentIp, string $isp): void
protected function markAsSkipped(string $message, string $externalIp): void
{
$this->result->update([
'data' => [
'type' => 'log',
'level' => 'info',
'level' => 'warning',
'message' => $message,
'interface' => [
'externalIp' => $currentIp,
'externalIp' => $externalIp,
],
'isp' => $isp,
],
'status' => ResultStatus::Skipped,
]);
Expand Down Expand Up @@ -233,6 +222,8 @@ protected function checkForInternetConnection(): bool

/**
* Check if the given URL is a valid ping URL.
*
* TODO: move to Network helper
*/
public function isValidPingUrl(string $url): bool
{
Expand All @@ -246,4 +237,30 @@ public function isValidPingUrl(string $url): bool
|| (filter_var('https://'.$url, FILTER_VALIDATE_URL) && $hasTLD($url))
|| filter_var($url, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 || FILTER_FLAG_IPV6) !== false;
}

/**
* Check if the speedtest should be skipped based on the skip ips list.
*/
public function shouldSkip(string $externalIp): bool|string
{
if (blank(config('speedtest.skip_ips'))) {
return false;
}

$skipIPs = array_map('trim', explode(',', config('speedtest.skip_ips')));

foreach ($skipIPs as $ip) {
// Check for exact IP match
if (filter_var($ip, FILTER_VALIDATE_IP) && $externalIp === $ip) {
return sprintf('"%s" was found in public IP address skip list.', $externalIp);
}

// Check for IP range match
if (strpos($ip, '/') !== false && Network::ipInRange($externalIp, $ip)) {
return sprintf('"%s" was found in public IP address skip list within range "%s".', $externalIp, $ip);
}
}

return false;
}
}
87 changes: 0 additions & 87 deletions app/Services/PublicIpService.php

This file was deleted.

0 comments on commit 5e386d2

Please sign in to comment.