editUser.php 1.4 KB

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