editUser.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  12. private $data;
  13. private $nodes;
  14. public function __construct(User $user, $nodes) {
  15. $this->nodes = $nodes;
  16. $this->data = [
  17. 'uid' => $user->id,
  18. 'port' => (int) $user->port,
  19. 'passwd' => $user->passwd,
  20. 'speed_limit' => $user->speed_limit,
  21. 'enable' => (int) $user->enable
  22. ];
  23. }
  24. public function handle(): void {
  25. foreach($this->nodes as $node){
  26. $this->send(($node->server?: $node->ip).':'.$node->push_port, $node->auth->secret);
  27. }
  28. }
  29. private function send($host, $secret): void {
  30. $client = new Client([
  31. 'base_uri' => $host,
  32. 'timeout' => 15,
  33. 'headers' => ['secret' => $secret]
  34. ]);
  35. $client->post('api/user/edit', ['json' => $this->data]);
  36. }
  37. }