reloadNode.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Jobs\VNet;
  3. use Arr;
  4. use Exception;
  5. use Http;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Database\Eloquent\Collection;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Log;
  13. use Throwable;
  14. class reloadNode implements ShouldQueue
  15. {
  16. use Dispatchable;
  17. use InteractsWithQueue;
  18. use Queueable;
  19. use SerializesModels;
  20. private Collection $nodes;
  21. public function __construct(Collection $nodes)
  22. {
  23. $this->nodes = $nodes;
  24. }
  25. public function handle(): bool
  26. {
  27. foreach ($this->nodes as $node) {
  28. $data = $node->getSSRConfig();
  29. if ($node->is_ddns) {
  30. if (! $this->send($node->server.':'.$node->push_port, $node->auth->secret, $data)) {
  31. $result = false;
  32. }
  33. } else { // 多IP支持
  34. foreach ($node->ips() as $ip) {
  35. if (! $this->send($ip.':'.$node->push_port, $node->auth->secret, $data)) {
  36. $result = false;
  37. }
  38. }
  39. }
  40. }
  41. return $result ?? true;
  42. }
  43. public function send(string $host, string $secret, array $data): bool
  44. {
  45. try {
  46. $response = Http::baseUrl($host)->timeout(15)->withHeaders(['secret' => $secret])->post('api/v2/node/reload', $data);
  47. $message = $response->json();
  48. if ($message && Arr::has($message, ['success', 'content']) && $response->ok()) {
  49. if ($message['success'] === 'false') {
  50. Log::warning("【重载节点】失败:$host 反馈:".$message['content']);
  51. return false;
  52. }
  53. Log::notice("【重载节点】成功:$host 反馈:".$message['content']);
  54. return true;
  55. }
  56. Log::warning("【重载节点】失败:$host");
  57. } catch (Exception $exception) {
  58. Log::alert('【重载节点】推送异常:'.$exception->getMessage());
  59. }
  60. return false;
  61. }
  62. // 队列失败处理
  63. public function failed(Throwable $exception): void
  64. {
  65. Log::alert('【重载节点】推送异常:'.$exception->getMessage());
  66. }
  67. }