2021-09-04 02:45:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
2024-09-05 06:40:57 +00:00
|
|
|
use Illuminate\Http\Client\RequestException;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
2021-09-04 02:45:56 +00:00
|
|
|
|
|
|
|
class NodeinfoService
|
|
|
|
{
|
|
|
|
public static function get($domain)
|
|
|
|
{
|
2024-09-05 06:40:57 +00:00
|
|
|
$version = config('pixelfed.version');
|
|
|
|
$appUrl = config('app.url');
|
|
|
|
$headers = [
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
'User-Agent' => "(Pixelfed/{$version}; +{$appUrl})",
|
|
|
|
];
|
2021-09-04 02:45:56 +00:00
|
|
|
|
2024-09-05 06:40:57 +00:00
|
|
|
$url = 'https://'.$domain;
|
|
|
|
$wk = $url.'/.well-known/nodeinfo';
|
2021-09-04 02:45:56 +00:00
|
|
|
|
|
|
|
try {
|
2024-02-07 09:47:34 +00:00
|
|
|
$res = Http::withOptions([
|
|
|
|
'allow_redirects' => false,
|
|
|
|
])
|
2024-09-05 06:40:57 +00:00
|
|
|
->withHeaders($headers)
|
|
|
|
->timeout(5)
|
|
|
|
->get($wk);
|
2021-09-04 02:45:56 +00:00
|
|
|
} catch (RequestException $e) {
|
|
|
|
return false;
|
|
|
|
} catch (ConnectionException $e) {
|
|
|
|
return false;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-09-05 06:40:57 +00:00
|
|
|
if (! $res) {
|
2021-09-04 02:45:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$json = $res->json();
|
|
|
|
|
2024-09-05 06:40:57 +00:00
|
|
|
if (! isset($json['links'])) {
|
2021-09-04 02:45:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-09-05 06:40:57 +00:00
|
|
|
if (is_array($json['links'])) {
|
|
|
|
if (isset($json['links']['href'])) {
|
2021-09-04 02:45:56 +00:00
|
|
|
$href = $json['links']['href'];
|
|
|
|
} else {
|
|
|
|
$href = $json['links'][0]['href'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$domain = parse_url($url, PHP_URL_HOST);
|
|
|
|
$hrefDomain = parse_url($href, PHP_URL_HOST);
|
|
|
|
|
2024-09-05 06:40:57 +00:00
|
|
|
if ($domain !== $hrefDomain) {
|
2024-09-05 06:37:54 +00:00
|
|
|
return false;
|
2021-09-04 02:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-02-07 09:47:34 +00:00
|
|
|
$res = Http::withOptions([
|
|
|
|
'allow_redirects' => false,
|
|
|
|
])
|
2024-09-05 06:40:57 +00:00
|
|
|
->withHeaders($headers)
|
|
|
|
->timeout(5)
|
|
|
|
->get($href);
|
2021-09-04 02:45:56 +00:00
|
|
|
} catch (RequestException $e) {
|
|
|
|
return false;
|
|
|
|
} catch (ConnectionException $e) {
|
|
|
|
return false;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-09-05 06:40:57 +00:00
|
|
|
|
2021-09-04 02:45:56 +00:00
|
|
|
return $res->json();
|
|
|
|
}
|
|
|
|
}
|