reloadNode.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Jobs\VNet;
  3. use Arr;
  4. use Http;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Database\Eloquent\Collection;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Log;
  12. use Throwable;
  13. class reloadNode implements ShouldQueue
  14. {
  15. use Dispatchable;
  16. use InteractsWithQueue;
  17. use Queueable;
  18. use SerializesModels;
  19. private $nodes;
  20. public function __construct($nodes)
  21. {
  22. if (! $nodes instanceof Collection) {
  23. $nodes = collect([$nodes]);
  24. }
  25. $this->nodes = $nodes;
  26. }
  27. public function handle(): bool
  28. {
  29. $allSuccess = true;
  30. foreach ($this->nodes as $node) {
  31. $ret = $this->send(($node->server ?: $node->ip).':'.$node->push_port, $node->auth->secret, [
  32. 'id' => $node->id,
  33. 'port' => (string) $node->port,
  34. 'passwd' => $node->passwd ?: '',
  35. 'method' => $node->method,
  36. 'protocol' => $node->protocol,
  37. 'obfs' => $node->obfs,
  38. 'protocol_param' => $node->protocol_param,
  39. 'obfs_param' => $node->obfs_param ?: '',
  40. 'push_port' => $node->push_port,
  41. 'single' => $node->single,
  42. 'secret' => $node->auth->secret,
  43. 'speed_limit' => $node->getRawOriginal('speed_limit'),
  44. 'is_udp' => $node->is_udp,
  45. 'client_limit' => $node->client_limit,
  46. // 'redirect_url' => (string) sysConfig('redirect_url'),
  47. ]);
  48. if (! $ret) {
  49. $allSuccess = false;
  50. }
  51. }
  52. return $allSuccess;
  53. }
  54. public function send($host, $secret, $data): bool
  55. {
  56. $request = Http::baseUrl($host)->withoutVerifying()->timeout(15)->withHeaders(['secret' => $secret]);
  57. $response = $request->post('api/v2/node/reload', $data);
  58. $message = $response->json();
  59. if ($message && Arr::has($message, ['success', 'content']) && $response->ok()) {
  60. if ($message['success'] === 'false') {
  61. Log::warning('【重载节点】失败:'.$host.' 反馈:'.$message['content']);
  62. return false;
  63. }
  64. Log::info('【重载节点】成功:'.$host.' 反馈:'.$message['content']);
  65. return true;
  66. }
  67. Log::warning('【重载节点】失败:'.$host);
  68. return false;
  69. }
  70. // 队列失败处理
  71. public function failed(Throwable $exception)
  72. {
  73. Log::error('【重载节点】推送异常:'.$exception);
  74. }
  75. }