33namespace LaravelReady \UltimateSupport \Supports ;
44
55use Exception ;
6- use Illuminate \Support \Facades \Config ;
6+ use Illuminate \Support \Facades \Cache ;
77use Illuminate \Support \Facades \Log ;
88use Illuminate \Support \Facades \Http ;
9+ use Illuminate \Support \Facades \Config ;
10+ use LaravelReady \UltimateSupport \Exceptions \IpAddressNotFoundException ;
911
1012class 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