reloadNode.php 2.4 KB

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