reloadNode.php 2.4 KB

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