This commit is contained in:
Daniel Supernault 2024-09-05 00:40:57 -06:00
parent 21da2b642c
commit 1210bf7502
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -2,32 +2,31 @@
namespace App\Services; namespace App\Services;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
class NodeinfoService class NodeinfoService
{ {
public static function get($domain) public static function get($domain)
{ {
$version = config('pixelfed.version'); $version = config('pixelfed.version');
$appUrl = config('app.url'); $appUrl = config('app.url');
$headers = [ $headers = [
'Accept' => 'application/json', 'Accept' => 'application/json',
'User-Agent' => "(Pixelfed/{$version}; +{$appUrl})", 'User-Agent' => "(Pixelfed/{$version}; +{$appUrl})",
]; ];
$url = 'https://' . $domain; $url = 'https://'.$domain;
$wk = $url . '/.well-known/nodeinfo'; $wk = $url.'/.well-known/nodeinfo';
try { try {
$res = Http::withOptions([ $res = Http::withOptions([
'allow_redirects' => false, 'allow_redirects' => false,
]) ])
->withHeaders($headers) ->withHeaders($headers)
->timeout(5) ->timeout(5)
->get($wk); ->get($wk);
} catch (RequestException $e) { } catch (RequestException $e) {
return false; return false;
} catch (ConnectionException $e) { } catch (ConnectionException $e) {
@ -36,18 +35,18 @@ class NodeinfoService
return false; return false;
} }
if(!$res) { if (! $res) {
return false; return false;
} }
$json = $res->json(); $json = $res->json();
if( !isset($json['links'])) { if (! isset($json['links'])) {
return false; return false;
} }
if(is_array($json['links'])) { if (is_array($json['links'])) {
if(isset($json['links']['href'])) { if (isset($json['links']['href'])) {
$href = $json['links']['href']; $href = $json['links']['href'];
} else { } else {
$href = $json['links'][0]['href']; $href = $json['links'][0]['href'];
@ -59,7 +58,7 @@ class NodeinfoService
$domain = parse_url($url, PHP_URL_HOST); $domain = parse_url($url, PHP_URL_HOST);
$hrefDomain = parse_url($href, PHP_URL_HOST); $hrefDomain = parse_url($href, PHP_URL_HOST);
if($domain !== $hrefDomain) { if ($domain !== $hrefDomain) {
return false; return false;
} }
@ -67,9 +66,9 @@ class NodeinfoService
$res = Http::withOptions([ $res = Http::withOptions([
'allow_redirects' => false, 'allow_redirects' => false,
]) ])
->withHeaders($headers) ->withHeaders($headers)
->timeout(5) ->timeout(5)
->get($href); ->get($href);
} catch (RequestException $e) { } catch (RequestException $e) {
return false; return false;
} catch (ConnectionException $e) { } catch (ConnectionException $e) {
@ -77,6 +76,7 @@ class NodeinfoService
} catch (\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
return $res->json(); return $res->json();
} }
} }