Skip to content

Commit 68bde7d

Browse files
committed
Improve IP support flow
1 parent 4559328 commit 68bde7d

File tree

3 files changed

+72
-17
lines changed

3 files changed

+72
-17
lines changed

config/ultimate-support.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
*/
1010
'get_local_public_ip' => true,
1111

12+
/**
13+
* Use cache for local client IP address
14+
*
15+
* Cache key: ultimate-support:ip:local_public_ip
16+
* Cache time: 1 day
17+
*
18+
* Default: true
19+
*/
20+
'use_cache_for_local_public_ip' => true,
21+
1222
/**
1323
* Enable/Disable get IP address failure logging
1424
*
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaravelReady\UltimateSupport\Exceptions;
6+
7+
use Exception;
8+
9+
class IpAddressNotFoundException extends Exception
10+
{
11+
public function __construct(Exception $exp)
12+
{
13+
$message = 'IP address not found.' . PHP_EOL . $exp->getMessage();
14+
15+
parent::__construct($message);
16+
}
17+
}

src/Supports/IpSupport.php

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace LaravelReady\UltimateSupport\Supports;
44

55
use Exception;
6-
use Illuminate\Support\Facades\Config;
6+
use Illuminate\Support\Facades\Cache;
77
use Illuminate\Support\Facades\Log;
88
use Illuminate\Support\Facades\Http;
9+
use Illuminate\Support\Facades\Config;
10+
use LaravelReady\UltimateSupport\Exceptions\IpAddressNotFoundException;
911

1012
class IpSupport
1113
{
@@ -18,7 +20,7 @@ class IpSupport
1820
*/
1921
public static function isLocalhost(): bool
2022
{
21-
return in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1', '172.27.0.1']);
23+
return in_array(request()->ip(), ['127.0.0.1', '::1', '172.27.0.1']);
2224
}
2325

2426
/**
@@ -28,19 +30,34 @@ public static function isLocalhost(): bool
2830
*/
2931
public static function getPublicIp(): null | string
3032
{
31-
$ipAddress = null;
33+
$isCacheable = Config::get('ultimate-support.ip.use_cache_for_local_public_ip', true);
34+
$cacheKey = 'ultimate-support:ip:local_public_ip';
35+
36+
if ($isCacheable) {
37+
if (Cache::has($cacheKey)) {
38+
$ipAddress = Cache::get($cacheKey, null);
39+
40+
if ($ipAddress !== null) {
41+
return $ipAddress;
42+
}
43+
}
44+
}
3245

3346
$response = Http::withoutVerifying()->get('https://api.ipify.org/?format=json');
3447

3548
if ($response->ok()) {
3649
$ipAddress = $response->json()['ip'] ?? null;
3750

38-
if (!empty($ipAddress)) {
51+
if ($ipAddress && filter_var($ipAddress, FILTER_VALIDATE_IP)) {
52+
if ($isCacheable) {
53+
Cache::put($cacheKey, $ipAddress, now()->addDay());
54+
}
55+
3956
return $ipAddress;
4057
}
41-
}
4258

43-
return $ipAddress;
59+
return $ipAddress;
60+
}
4461
}
4562

4663
/**
@@ -52,35 +69,46 @@ public static function getPublicIp(): null | string
5269
*
5370
* @return string
5471
*/
55-
public static function getIp(bool $getLocalPublicIp = true): string
72+
public static function getIpAddress(bool $getLocalPublicIp = false): null | array
5673
{
57-
$baseIp = $_SERVER['REMOTE_ADDR'];
74+
$baseIpAddress = request()->ip();
5875

5976
try {
6077
// get localhost public ip
61-
if (($getLocalPublicIp && Config::get('ultimate-support.ip.get_local_public_ip')) && self::isLocalhost()) {
62-
return self::getPublicIp();
78+
if (($getLocalPublicIp || Config::get('ultimate-support.ip.get_local_public_ip')) && self::isLocalhost()) {
79+
return [
80+
'is_local' => true,
81+
'base_ip' => $baseIpAddress,
82+
'ip_address' => self::getPublicIp(),
83+
];
6384
}
6485

86+
$ipAddress = $baseIpAddress;
87+
6588
// check other conditions, cloudflare etc
6689
if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)) {
67-
return $_SERVER['HTTP_CF_CONNECTING_IP'];
90+
$ipAddress = $_SERVER['HTTP_CF_CONNECTING_IP'];
6891
} elseif (isset($_SERVER['HTTP_X_REAL_IP']) && filter_var($_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP)) {
69-
return $_SERVER['HTTP_X_REAL_IP'];
92+
$ipAddress = $_SERVER['HTTP_X_REAL_IP'];
7093
} elseif (isset($_SERVER['HTTP_CLIENT_IP']) && filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
71-
return $_SERVER['HTTP_CLIENT_IP'];
94+
$ipAddress = $_SERVER['HTTP_CLIENT_IP'];
7295
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
73-
return $_SERVER['HTTP_X_FORWARDED_FOR'];
96+
$ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
7497
}
98+
99+
return [
100+
'is_local' => true,
101+
'base_ip' => $baseIpAddress,
102+
'ip_address' => $ipAddress
103+
];
75104
} catch (Exception $exp) {
76105
if (Config::get('ultimate-support.ip.get_local_public_ip')) {
77106
Log::alert('Ultimate Support: getIP error', ['error' => $exp]);
78107
}
79108

80-
// TODO: add custom exception
81-
throw $exp;
109+
throw new IpAddressNotFoundException($exp);
82110
}
83111

84-
return $baseIp;
112+
return $baseIpAddress;
85113
}
86114
}

0 commit comments

Comments
 (0)