reloadNode.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Jobs\VNet;
  3. use GuzzleHttp\Client;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Log;
  10. class reloadNode implements ShouldQueue
  11. {
  12. use Dispatchable;
  13. use InteractsWithQueue;
  14. use Queueable;
  15. use SerializesModels;
  16. private $nodes;
  17. public function __construct($nodes)
  18. {
  19. $this->nodes = $nodes;
  20. }
  21. public function handle(): bool
  22. {
  23. $allSuccess = true;
  24. foreach ($this->nodes as $node) {
  25. $ret = $this->send(
  26. ($node->server ?: $node->ip) . ':' . $node->push_port,
  27. $node->auth->secret,
  28. [
  29. 'id' => $node->id,
  30. 'port' => (string)$node->port,
  31. 'passwd' => $node->passwd ?: '',
  32. 'method' => $node->method,
  33. 'protocol' => $node->protocol,
  34. 'obfs' => $node->obfs,
  35. 'protocol_param' => $node->protocol_param,
  36. 'obfs_param' => $node->obfs_param ?: '',
  37. 'push_port' => $node->push_port,
  38. 'single' => $node->single,
  39. 'secret' => $node->auth->secret,
  40. // 'is_udp' => $node->is_udp,
  41. // 'speed_limit' => $node->speed_limit,
  42. // 'client_limit' => $node->client_limit,
  43. // 'redirect_url' => (string) sysConfig('redirect_url')
  44. ]
  45. );
  46. if ( ! $ret) {
  47. $allSuccess = false;
  48. }
  49. }
  50. return $allSuccess;
  51. }
  52. public function send($host, $secret, $data): bool
  53. {
  54. $client = new Client(
  55. [
  56. 'base_uri' => $host,
  57. 'timeout' => 15,
  58. 'headers' => ['secret' => $secret],
  59. ]
  60. );
  61. $ret = $client->post('api/v2/node/reload', ['json' => $data]);
  62. if ($ret->getStatusCode() == 200) {
  63. $message = json_decode($ret->getBody(), true);
  64. if (array_key_exists('success', $message) && array_key_exists(
  65. 'content',
  66. $message
  67. )) {
  68. if ($message['success']) {
  69. return true;
  70. }
  71. Log::error('重载节点失败:' . $host . ' 反馈:' . $message['content']);
  72. }
  73. }
  74. Log::error('重载节点失败url: ' . $host);
  75. return false;
  76. }
  77. }