editUser.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 editUser 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(User $user, $nodes)
  19. {
  20. $this->nodes = $nodes;
  21. $this->data = [
  22. 'uid' => $user->id,
  23. 'port' => (int) $user->port,
  24. 'passwd' => $user->passwd,
  25. 'speed_limit' => $user->speed_limit,
  26. 'enable' => (int) $user->enable,
  27. ];
  28. }
  29. public function handle(): void
  30. {
  31. foreach ($this->nodes as $node) {
  32. $this->send(($node->server ?: $node->ip).':'.$node->push_port, $node->auth->secret);
  33. }
  34. }
  35. private function send($host, $secret): void
  36. {
  37. $client = new Client([
  38. 'base_uri' => $host,
  39. 'timeout' => 15,
  40. 'headers' => ['secret' => $secret],
  41. ]);
  42. $client->post('api/user/edit', ['json' => $this->data]);
  43. }
  44. }