reloadNode.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 $nodes;
  21. public function __construct($nodes)
  22. {
  23. if (! $nodes instanceof Collection) {
  24. $nodes = collect([$nodes]);
  25. }
  26. $this->nodes = $nodes;
  27. }
  28. public function handle(): bool
  29. {
  30. foreach ($this->nodes as $node) {
  31. $data = $node->getSSRConfig();
  32. if ($node->is_ddns) {
  33. if (! $this->send($node->server.':'.$node->push_port, $node->auth->secret, $data)) {
  34. $result = false;
  35. }
  36. } else { // 多IP支持
  37. foreach ($node->ips() as $ip) {
  38. if (! $this->send($ip.':'.$node->push_port, $node->auth->secret, $data)) {
  39. $result = false;
  40. }
  41. }
  42. }
  43. }
  44. return $result ?? true;
  45. }
  46. public function send(string $host, string $secret, array $data): bool
  47. {
  48. try {
  49. $response = Http::baseUrl($host)->timeout(15)->withHeaders(['secret' => $secret])->post('api/v2/node/reload', $data);
  50. $message = $response->json();
  51. if ($message && Arr::has($message, ['success', 'content']) && $response->ok()) {
  52. if ($message['success'] === 'false') {
  53. Log::warning("【重载节点】失败:{$host} 反馈:".$message['content']);
  54. return false;
  55. }
  56. Log::notice("【重载节点】成功:{$host} 反馈:".$message['content']);
  57. return true;
  58. }
  59. Log::warning("【重载节点】失败:{$host}");
  60. } catch (Exception $exception) {
  61. Log::alert('【重载节点】推送异常:'.$exception->getMessage());
  62. }
  63. return false;
  64. }
  65. // 队列失败处理
  66. public function failed(Throwable $exception)
  67. {
  68. Log::alert('【重载节点】推送异常:'.$exception->getMessage());
  69. }
  70. }