-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Skip test when Public IP is in an list (#1714)
* first commit * Commit it * lint * update-all-charts * push_local_git * add-timepicker * add-some-predefined-ranges * remove whiteline * Add_env_for_chart_start * change-env-and_time-ranges * change_average_to_orange * Revert "Add failed and thresholds" * first commit * change_api * update comments * Simplify * Seperate the IP check
- Loading branch information
Showing
6 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use App\Models\Result; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class SpeedtestSkipped | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
*/ | ||
public function __construct( | ||
public Result $result, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use Illuminate\Support\Facades\Log; | ||
|
||
class PublicIpService | ||
{ | ||
/** | ||
* Get the public IP address and its associated details using ipapi.co. | ||
*/ | ||
public function getPublicIp(): array | ||
{ | ||
try { | ||
// Fetch location data from ifconfig.co using curl | ||
$ch = curl_init('https://ifconfig.co/json'); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | ||
$response = curl_exec($ch); | ||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
curl_close($ch); | ||
|
||
// Validate the HTTP response | ||
if ($httpCode !== 200) { | ||
\Log::error("Failed to fetch public IP data from ifconfig.co. HTTP Status Code: $httpCode"); | ||
|
||
return ['ip' => 'unknown', 'isp' => 'unknown']; | ||
} | ||
|
||
// Decode the JSON response | ||
$data = json_decode($response, true); | ||
|
||
// Validate the response format | ||
if (json_last_error() === JSON_ERROR_NONE && isset($data['ip'])) { | ||
return [ | ||
'ip' => $data['ip'], | ||
'isp' => $data['asn_org'] ?? 'unknown', | ||
]; | ||
} | ||
|
||
// Log error if the response is invalid | ||
\Log::error('Invalid response from ifconfig.co: '.$response); | ||
|
||
return ['ip' => 'unknown', 'isp' => 'unknown']; | ||
} catch (\Exception $e) { | ||
\Log::error("Error fetching public IP data from ifconfig.co: {$e->getMessage()}"); | ||
|
||
// Fallback response | ||
return ['ip' => 'unknown', 'isp' => 'unknown']; | ||
} | ||
} | ||
|
||
/** | ||
* Check if the current IP should be skipped. | ||
*/ | ||
public function shouldSkipIp(string $currentIp, array $skipSettings): bool|string | ||
{ | ||
foreach ($skipSettings as $setting) { | ||
// Check for exact IP match | ||
if (filter_var($setting, FILTER_VALIDATE_IP)) { | ||
if ($currentIp === $setting) { | ||
return "Current public IP address ($currentIp) is listed to be skipped for testing."; | ||
} | ||
} | ||
|
||
// Check for subnet match | ||
if (strpos($setting, '/') !== false && $this->ipInSubnet($currentIp, $setting)) { | ||
return "Current public IP address ($currentIp) falls within the excluded subnet ($setting)."; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Check if an IP is in a given subnet. | ||
*/ | ||
protected function ipInSubnet(string $ip, string $subnet): bool | ||
{ | ||
[$subnet, $mask] = explode('/', $subnet) + [1 => '32']; | ||
$subnetDecimal = ip2long($subnet); | ||
$ipDecimal = ip2long($ip); | ||
$maskDecimal = ~((1 << (32 - (int) $mask)) - 1); | ||
|
||
return ($subnetDecimal & $maskDecimal) === ($ipDecimal & $maskDecimal); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters