editUser.php 1.1 KB

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