getUser.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Jobs\VNet;
  3. use App\Models\Node;
  4. use App\Models\User;
  5. use Arr;
  6. use Exception;
  7. use Http;
  8. use Log;
  9. class getUser
  10. {
  11. public function existsinVNet(User $user): array
  12. {
  13. $nodeList = [];
  14. foreach ($user->nodes()->whereType(4)->get() as $node) {
  15. $list = $this->list($node);
  16. if ($list && in_array($user->id, $list, true)) {
  17. $nodeList[] = $node->id;
  18. }
  19. }
  20. return $nodeList;
  21. }
  22. public function list(Node $node, string $mode = 'uid'): false|array
  23. {
  24. $list = $this->send(($node->server ?: $node->ips()[0]).':'.$node->push_port, $node->auth->secret);
  25. if (is_array($list)) {
  26. if ($mode === 'uid') {
  27. return Arr::pluck($list, 'uid');
  28. }
  29. if ($mode === 'port') {
  30. return Arr::pluck($list, 'port');
  31. }
  32. return $list;
  33. }
  34. return false;
  35. }
  36. private function send(string $host, string $secret): false|array
  37. {
  38. try {
  39. $response = Http::baseUrl($host)->timeout(20)->withHeader('secret', $secret)->get('api/user/list');
  40. $message = $response->json();
  41. if ($message && $response->ok()) {
  42. return $message;
  43. }
  44. Log::warning('【用户列表】获取失败(推送地址:'.$host.')');
  45. } catch (Exception $exception) {
  46. Log::alert('【用户列表】获取异常:'.$exception->getMessage());
  47. }
  48. return false;
  49. }
  50. }