NodeinfoService.php 1.8 KB

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