addUser.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Jobs\VNet;
  3. use App\Models\User;
  4. use GuzzleHttp\Client;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. class addUser implements ShouldQueue
  11. {
  12. use Dispatchable;
  13. use InteractsWithQueue;
  14. use Queueable;
  15. use SerializesModels;
  16. private $data;
  17. private $nodes;
  18. public function __construct($userIds, $nodes)
  19. {
  20. $this->nodes = $nodes;
  21. $data = [];
  22. foreach (User::findMany($userIds) as $user) {
  23. $data[] = [
  24. 'uid' => $user->id,
  25. 'port' => $user->port,
  26. 'passwd' => $user->passwd,
  27. 'speed_limit' => $user->speed_limit,
  28. 'enable' => $user->enable,
  29. ];
  30. }
  31. $this->data = $data;
  32. }
  33. public function handle(): void
  34. {
  35. foreach ($this->nodes as $node) {
  36. $this->send(($node->server ?: $node->ip).':'.$node->push_port, $node->auth->secret);
  37. }
  38. }
  39. private function send($host, $secret): void
  40. {
  41. $client = new Client([
  42. 'base_uri' => $host,
  43. 'timeout' => 15,
  44. 'headers' => ['secret' => $secret],
  45. ]);
  46. $client->post('api/v2/user/add/list', ['json' => $this->data]);
  47. }
  48. }