namespace Cubist\Net;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\RequestException;
+use Illuminate\Support\Facades\Log;
+
class Util
{
public static function resolve($hostOrCIDR)
return false;
}
-}
\ No newline at end of file
+
+ public static function getFinalUrl($url)
+ {
+ $client = new Client(['allow_redirects' => ['track_redirects' => true]]);
+
+ try {
+ $response = $client->request('GET', $url);
+ $redirectHistory = $response->getHeader('X-Guzzle-Redirect-History');
+
+ // Check if the response body contains a meta-refresh tag
+ $body = (string)$response->getBody();
+ if (preg_match('/<meta\s+http-equiv=["\']refresh["\']\s+content=["\']\d+;\s*url=(["\']?)([^"\'>]*)/i', $body, $matches)) {
+ $metaRefreshUrl = $matches[2];
+ return self::getFinalUrl($metaRefreshUrl);
+ }
+
+ return end($redirectHistory) ?: $url;
+ } catch (RequestException $e) {
+ Log::error("Request failed: " . $e->getMessage());
+ return null;
+ }
+ }
+
+
+}