reloadNode.php 2.6 KB

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