1
0

NodeinfoService.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Http\Client\ConnectionException;
  4. use Illuminate\Http\Client\RequestException;
  5. use Illuminate\Support\Facades\Http;
  6. class NodeinfoService
  7. {
  8. public static function get($domain)
  9. {
  10. $version = config('pixelfed.version');
  11. $appUrl = config('app.url');
  12. $headers = [
  13. 'Accept' => 'application/json',
  14. 'User-Agent' => "(Pixelfed/{$version}; +{$appUrl})",
  15. ];
  16. $url = 'https://'.$domain;
  17. $wk = $url.'/.well-known/nodeinfo';
  18. try {
  19. $res = Http::withOptions([
  20. 'allow_redirects' => false,
  21. ])
  22. ->withHeaders($headers)
  23. ->timeout(5)
  24. ->get($wk);
  25. } catch (RequestException $e) {
  26. return false;
  27. } catch (ConnectionException $e) {
  28. return false;
  29. } catch (\Exception $e) {
  30. return false;
  31. }
  32. if (! $res) {
  33. return false;
  34. }
  35. $json = $res->json();
  36. if (! isset($json['links'])) {
  37. return false;
  38. }
  39. if (is_array($json['links'])) {
  40. if (isset($json['links']['href'])) {
  41. $href = $json['links']['href'];
  42. } else {
  43. $href = $json['links'][0]['href'];
  44. }
  45. } else {
  46. return false;
  47. }
  48. $domain = parse_url($url, PHP_URL_HOST);
  49. $hrefDomain = parse_url($href, PHP_URL_HOST);
  50. if ($domain !== $hrefDomain) {
  51. return false;
  52. }
  53. try {
  54. $res = Http::withOptions([
  55. 'allow_redirects' => false,
  56. ])
  57. ->withHeaders($headers)
  58. ->timeout(5)
  59. ->get($href);
  60. } catch (RequestException $e) {
  61. return false;
  62. } catch (ConnectionException $e) {
  63. return false;
  64. } catch (\Exception $e) {
  65. return false;
  66. }
  67. return $res->json();
  68. }
  69. }