reloadNode.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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(($node->server ?: $node->ip).':'.$node->push_port, $node->auth->secret, [
  26. 'id' => $node->id,
  27. 'port' => (string) $node->port,
  28. 'passwd' => $node->passwd ?: '',
  29. 'method' => $node->method,
  30. 'protocol' => $node->protocol,
  31. 'obfs' => $node->obfs,
  32. 'protocol_param' => $node->protocol_param,
  33. 'obfs_param' => $node->obfs_param ?: '',
  34. 'push_port' => $node->push_port,
  35. 'single' => $node->single,
  36. 'secret' => $node->auth->secret,
  37. // 'is_udp' => $node->is_udp,
  38. // 'speed_limit' => $node->speed_limit,
  39. // 'client_limit' => $node->client_limit,
  40. // 'redirect_url' => (string) sysConfig('redirect_url')
  41. ]);
  42. if (!$ret) {
  43. $allSuccess = false;
  44. }
  45. }
  46. return $allSuccess;
  47. }
  48. public function send($host, $secret, $data): bool
  49. {
  50. $client = new Client([
  51. 'base_uri' => $host,
  52. 'timeout' => 15,
  53. 'headers' => ['secret' => $secret],
  54. ]);
  55. $ret = $client->post('api/v2/node/reload', ['json' => $data]);
  56. if ($ret->getStatusCode() == 200) {
  57. $message = json_decode($ret->getBody(), true);
  58. if (array_key_exists('success', $message) && array_key_exists('content', $message)) {
  59. if ($message['success']) {
  60. return true;
  61. }
  62. Log::error('重载节点失败:'.$host.' 反馈:'.$message['content']);
  63. }
  64. }
  65. Log::error('重载节点失败url: '.$host);
  66. return false;
  67. }
  68. }