FetchNodeinfo.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use \Zttp\Zttp;
  5. use App\Instance;
  6. use App\InstanceScan;
  7. class FetchNodeinfo extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'instance:fetch {domain}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Fetch Instance Nodeinfo';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $domain = $this->argument('domain');
  38. $url = "https://{$domain}/api/nodeinfo/2.0.json";
  39. try {
  40. $response = Zttp::timeout(60)->get($url);
  41. } catch (\Zttp\ConnectionException $e) {
  42. $instance = Instance::whereDomain($host)->first();
  43. if($instance) {
  44. $instance->approved_at = null;
  45. $instance->save();
  46. $scan = new InstanceScan;
  47. $scan->code = $response->status();
  48. $scan->instance_id = $instance->id;
  49. $scan->domain = $instance->domain;
  50. $scan->user_count = $instance->user_count;
  51. $scan->post_count = $instance->post_count;
  52. $scan->nodeinfo = $body;
  53. $scan->save();
  54. }
  55. return 1;
  56. } catch (\GuzzleHttp\Exception\RequestException $e) {
  57. $instance = Instance::whereDomain($host)->first();
  58. if($instance) {
  59. $instance->approved_at = null;
  60. $instance->save();
  61. $scan = new InstanceScan;
  62. $scan->code = $response->status();
  63. $scan->instance_id = $instance->id;
  64. $scan->domain = $instance->domain;
  65. $scan->user_count = $instance->user_count;
  66. $scan->post_count = $instance->post_count;
  67. $scan->nodeinfo = $body;
  68. $scan->save();
  69. }
  70. return 1;
  71. }
  72. $body = $response->body();
  73. $json = $response->json();
  74. $instance = Instance::whereDomain($domain)->firstOrFail();
  75. if($response->status() != 200 || $json['software']['name'] != 'pixelfed' || $json['openRegistrations'] == false) {
  76. $instance->approved_at = null;
  77. $instance->save();
  78. // todo: remove instance after XX attempts
  79. $this->error('invalid response');
  80. return 1;
  81. }
  82. $instance->nodeinfo = $body;
  83. $instance->user_count = $json['usage']['users']['total'];
  84. $instance->post_count = $json['usage']['localPosts'];
  85. $instance->save();
  86. $scan = new InstanceScan;
  87. $scan->code = $response->status();
  88. $scan->instance_id = $instance->id;
  89. $scan->domain = $instance->domain;
  90. $scan->user_count = $instance->user_count;
  91. $scan->post_count = $instance->post_count;
  92. $scan->nodeinfo = $body;
  93. $scan->save();
  94. return 1;
  95. }
  96. }