addUser.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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(
  37. ($node->server ?: $node->ip) . ':' . $node->push_port,
  38. $node->auth->secret
  39. );
  40. }
  41. }
  42. private function send($host, $secret): void
  43. {
  44. $client = new Client(
  45. [
  46. 'base_uri' => $host,
  47. 'timeout' => 15,
  48. 'headers' => ['secret' => $secret],
  49. ]
  50. );
  51. $client->post('api/v2/user/add/list', ['json' => $this->data]);
  52. }
  53. }