GetStreamDump.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Jobs\Hysteria2;
  3. use App\Models\Node;
  4. use Exception;
  5. use Http;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Log;
  12. use Throwable;
  13. class GetStreamDump implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. public function __construct(private readonly Node $node)
  17. {
  18. }
  19. public function handle(): void
  20. {
  21. if ($this->node->is_ddns) {
  22. $this->send($this->node->server.':'.$this->node->push_port, $this->node->auth->secret);
  23. } else { // 多IP支持
  24. foreach ($this->node->ips() as $ip) {
  25. $this->send($ip.':'.$this->node->push_port, $this->node->auth->secret);
  26. }
  27. }
  28. }
  29. private function send(string $host, string $secret): void
  30. {
  31. try {
  32. $request = Http::baseUrl($host)->timeout(15)->withHeader('Authorization', $secret);
  33. $response = $request->get('/dump/streams');
  34. $data = $response->json();
  35. if ($data && isset($data['streams'])) {
  36. // TODO: 这里可以处理流数据,例如记录到日志或存储到数据库
  37. // 目前只是获取数据,可以根据需要进一步处理
  38. Log::info('【流详情】获取成功', ['node_id' => $this->node->id, 'streams_count' => count($data['streams'])]);
  39. }
  40. } catch (Exception $exception) {
  41. Log::alert('【流详情】获取异常:'.$exception->getMessage());
  42. }
  43. }
  44. // 队列失败处理
  45. public function failed(Throwable $exception): void
  46. {
  47. Log::alert('【流详情】获取异常:'.$exception->getMessage());
  48. }
  49. }